Passed
Push — master ( 1e3bf6...07a9b1 )
by Marco
15:46
created

ConnectionMock::query()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Doctrine\Tests\Mocks;
4
5
use Doctrine\DBAL\Connection;
6
use Doctrine\DBAL\Driver\Statement;
7
8
/**
9
 * Mock class for Connection.
10
 */
11
class ConnectionMock extends Connection
12
{
13
    /**
14
     * @var mixed
15
     */
16
    private $_fetchOneResult;
17
18
    /**
19
     * @var \Exception|null
20
     */
21
    private $_fetchOneException;
22
23
    /**
24
     * @var Statement|null
25
     */
26
    private $_queryResult;
27
28
    /**
29
     * @var DatabasePlatformMock
30
     */
31
    private $_platformMock;
32
33
    /**
34
     * @var int
35
     */
36
    private $_lastInsertId = 0;
37
38
    /**
39
     * @var array
40
     */
41
    private $_inserts = [];
42
43
    /**
44
     * @var array
45
     */
46
    private $_executeUpdates = [];
47
48
    /**
49
     * @param array                              $params
50
     * @param \Doctrine\DBAL\Driver              $driver
51
     * @param \Doctrine\DBAL\Configuration|null  $config
52
     * @param \Doctrine\Common\EventManager|null $eventManager
53
     */
54
    public function __construct(array $params, $driver, $config = null, $eventManager = null)
55
    {
56
        $this->_platformMock = new DatabasePlatformMock();
57
58
        parent::__construct($params, $driver, $config, $eventManager);
59
60
        // Override possible assignment of platform to database platform mock
61
        $this->_platform = $this->_platformMock;
0 ignored issues
show
Bug introduced by
The property _platform does not seem to exist. Did you mean _platformMock?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    public function getDatabasePlatform()
68
    {
69
        return $this->_platformMock;
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public function insert($tableName, array $data, array $types = [])
76
    {
77
        $this->_inserts[$tableName][] = $data;
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83
    public function executeUpdate($query, array $params = [], array $types = [])
84
    {
85
        $this->_executeUpdates[] = ['query' => $query, 'params' => $params, 'types' => $types];
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91
    public function lastInsertId($seqName = null)
92
    {
93
        return $this->_lastInsertId;
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99
    public function fetchColumn($statement, array $params = [], $colnum = 0, array $types = [])
100
    {
101
        if (null !== $this->_fetchOneException) {
102
            throw $this->_fetchOneException;
103
        }
104
105
        return $this->_fetchOneResult;
106
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111
    public function query() : Statement
112
    {
113
        return $this->_queryResult;
114
    }
115
116
    /**
117
     * {@inheritdoc}
118
     */
119
    public function quote($input, $type = null)
120
    {
121
        if (is_string($input)) {
122
            return "'" . $input . "'";
123
        }
124
        return $input;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $input; (object|integer|double|null|array|boolean) is incompatible with the return type of the parent method Doctrine\DBAL\Connection::quote of type string.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
125
    }
126
127
    /* Mock API */
128
129
    /**
130
     * @param mixed $fetchOneResult
131
     *
132
     * @return void
133
     */
134
    public function setFetchOneResult($fetchOneResult)
135
    {
136
        $this->_fetchOneResult = $fetchOneResult;
137
    }
138
139
    /**
140
     * @param \Exception|null $exception
141
     *
142
     * @return void
143
     */
144
    public function setFetchOneException(\Exception $exception = null)
145
    {
146
        $this->_fetchOneException = $exception;
147
    }
148
149
    /**
150
     * @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform
151
     *
152
     * @return void
153
     */
154
    public function setDatabasePlatform($platform)
155
    {
156
        $this->_platformMock = $platform;
0 ignored issues
show
Documentation Bug introduced by
$platform is of type object<Doctrine\DBAL\Platforms\AbstractPlatform>, but the property $_platformMock was declared to be of type object<Doctrine\Tests\Mocks\DatabasePlatformMock>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
157
    }
158
159
    /**
160
     * @param int $id
161
     *
162
     * @return void
163
     */
164
    public function setLastInsertId($id)
165
    {
166
        $this->_lastInsertId = $id;
167
    }
168
169
    /**
170
     * @param Statement $result
171
     */
172
    public function setQueryResult(Statement $result)
173
    {
174
        $this->_queryResult = $result;
175
    }
176
177
    /**
178
     * @return array
179
     */
180
    public function getInserts()
181
    {
182
        return $this->_inserts;
183
    }
184
185
    /**
186
     * @return array
187
     */
188
    public function getExecuteUpdates()
189
    {
190
        return $this->_executeUpdates;
191
    }
192
193
    /**
194
     * @return void
195
     */
196
    public function reset()
197
    {
198
        $this->_inserts = [];
199
        $this->_lastInsertId = 0;
200
    }
201
}
202