1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace Doctrine\Tests\Mocks; |
||
6 | |||
7 | use Doctrine\Common\EventManager; |
||
8 | use Doctrine\DBAL\Configuration; |
||
9 | use Doctrine\DBAL\Connection; |
||
10 | use Doctrine\DBAL\Driver; |
||
11 | use Doctrine\DBAL\Driver\Statement; |
||
12 | use Doctrine\DBAL\Platforms\AbstractPlatform; |
||
13 | use function is_string; |
||
14 | |||
15 | /** |
||
16 | * Mock class for Connection. |
||
17 | */ |
||
18 | class ConnectionMock extends Connection |
||
19 | { |
||
20 | /** @var mixed */ |
||
21 | private $fetchOneResult; |
||
22 | |||
23 | /** @var \Exception|null */ |
||
24 | private $fetchOneException; |
||
25 | |||
26 | /** @var Statement|null */ |
||
27 | private $queryResult; |
||
28 | |||
29 | /** @var DatabasePlatformMock */ |
||
30 | private $platformMock; |
||
31 | |||
32 | /** @var int */ |
||
33 | private $lastInsertId = 0; |
||
34 | |||
35 | /** @var array */ |
||
36 | private $inserts = []; |
||
37 | |||
38 | /** @var array */ |
||
39 | private $executeUpdates = []; |
||
40 | |||
41 | /** |
||
42 | * @param array $params |
||
43 | * @param Driver $driver |
||
44 | * @param Configuration|null $config |
||
45 | * @param EventManager|null $eventManager |
||
46 | */ |
||
47 | public function __construct(array $params, $driver, $config = null, $eventManager = null) |
||
48 | { |
||
49 | $this->platformMock = new DatabasePlatformMock(); |
||
50 | |||
51 | parent::__construct($params, $driver, $config, $eventManager); |
||
52 | |||
53 | // Override possible assignment of platform to database platform mock |
||
54 | $this->platform = $this->platformMock; |
||
55 | } |
||
56 | |||
57 | /** |
||
58 | * {@inheritdoc} |
||
59 | */ |
||
60 | public function getDatabasePlatform() |
||
61 | { |
||
62 | return $this->platformMock; |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * {@inheritdoc} |
||
67 | */ |
||
68 | public function insert($tableName, array $data, array $types = []) |
||
69 | { |
||
70 | $this->inserts[$tableName][] = $data; |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * {@inheritdoc} |
||
75 | */ |
||
76 | public function executeUpdate($query, array $params = [], array $types = []) |
||
77 | { |
||
78 | $this->executeUpdates[] = ['query' => $query, 'params' => $params, 'types' => $types]; |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * {@inheritdoc} |
||
83 | */ |
||
84 | public function lastInsertId($seqName = null) |
||
85 | { |
||
86 | return $this->lastInsertId; |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * {@inheritdoc} |
||
91 | */ |
||
92 | public function fetchColumn($statement, array $params = [], $colnum = 0, array $types = []) |
||
93 | { |
||
94 | if ($this->fetchOneException !== null) { |
||
95 | throw $this->fetchOneException; |
||
96 | } |
||
97 | |||
98 | return $this->fetchOneResult; |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * {@inheritdoc} |
||
103 | */ |
||
104 | public function query() : Statement |
||
105 | { |
||
106 | return $this->queryResult; |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
107 | } |
||
108 | |||
109 | /** |
||
110 | * {@inheritdoc} |
||
111 | */ |
||
112 | public function quote($input, $type = null) |
||
113 | { |
||
114 | if (is_string($input)) { |
||
115 | return "'" . $input . "'"; |
||
116 | } |
||
117 | return $input; |
||
118 | } |
||
119 | |||
120 | /* Mock API */ |
||
121 | |||
122 | /** |
||
123 | * @param mixed $fetchOneResult |
||
124 | * |
||
125 | * @return void |
||
126 | */ |
||
127 | public function setFetchOneResult($fetchOneResult) |
||
128 | { |
||
129 | $this->fetchOneResult = $fetchOneResult; |
||
130 | } |
||
131 | |||
132 | /** |
||
133 | * @return void |
||
134 | */ |
||
135 | public function setFetchOneException(?\Exception $exception = null) |
||
136 | { |
||
137 | $this->fetchOneException = $exception; |
||
138 | } |
||
139 | |||
140 | /** |
||
141 | * @param AbstractPlatform $platform |
||
142 | * |
||
143 | * @return void |
||
144 | */ |
||
145 | public function setDatabasePlatform($platform) |
||
146 | { |
||
147 | $this->platformMock = $platform; |
||
148 | } |
||
149 | |||
150 | /** |
||
151 | * @param int $id |
||
152 | * |
||
153 | * @return void |
||
154 | */ |
||
155 | public function setLastInsertId($id) |
||
156 | { |
||
157 | $this->lastInsertId = $id; |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * @return void |
||
162 | */ |
||
163 | public function setQueryResult(Statement $result) |
||
164 | { |
||
165 | $this->queryResult = $result; |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * @return array |
||
170 | */ |
||
171 | public function getInserts() |
||
172 | { |
||
173 | return $this->inserts; |
||
174 | } |
||
175 | |||
176 | /** |
||
177 | * @return array |
||
178 | */ |
||
179 | public function getExecuteUpdates() |
||
180 | { |
||
181 | return $this->executeUpdates; |
||
182 | } |
||
183 | |||
184 | /** |
||
185 | * @return void |
||
186 | */ |
||
187 | public function reset() |
||
188 | { |
||
189 | $this->inserts = []; |
||
190 | $this->lastInsertId = 0; |
||
191 | } |
||
192 | } |
||
193 |