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