|
1
|
|
|
<?php |
|
2
|
|
|
namespace Ajir\RabbitMqSqlBundle\Tests\Provider; |
|
3
|
|
|
|
|
4
|
|
|
use Doctrine\DBAL\Logging\EchoSQLLogger; |
|
5
|
|
|
use Ajir\RabbitMqSqlBundle\Provider\SqlProvider; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* |
|
9
|
|
|
* @author Florian Ajir <[email protected]> |
|
10
|
|
|
*/ |
|
11
|
|
|
class SqlProviderTest extends \PHPUnit_Framework_TestCase |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* |
|
15
|
|
|
*/ |
|
16
|
|
|
public function testInstanciateWithDebugLogger() |
|
17
|
|
|
{ |
|
18
|
|
|
$configMock = $this->getConfigurationMock(); |
|
19
|
|
|
$configMock->expects($this->once()) |
|
|
|
|
|
|
20
|
|
|
->method('setSQLLogger') |
|
21
|
|
|
->with(new EchoSQLLogger()) |
|
22
|
|
|
; |
|
23
|
|
|
$conn = $this->getConnectionMock(); |
|
24
|
|
|
$conn->expects($this->once()) |
|
|
|
|
|
|
25
|
|
|
->method('getConfiguration') |
|
26
|
|
|
->with() |
|
27
|
|
|
->will($this->returnValue($configMock)); |
|
28
|
|
|
new SqlProvider($conn, 'test'); |
|
|
|
|
|
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* |
|
33
|
|
|
*/ |
|
34
|
|
|
public function testDelete() |
|
35
|
|
|
{ |
|
36
|
|
|
$table = 'test'; |
|
37
|
|
|
$conditions = array('id' => 1); |
|
38
|
|
|
$returned = 1; |
|
39
|
|
|
$conn = $this->getConnectionMock(); |
|
40
|
|
|
$conn->expects($this->once()) |
|
|
|
|
|
|
41
|
|
|
->method('delete') |
|
42
|
|
|
->with($table, $conditions) |
|
43
|
|
|
->will($this->returnValue($returned)); |
|
44
|
|
|
$sqlProvider = new SqlProvider($conn, 'prod'); |
|
|
|
|
|
|
45
|
|
|
$result = $sqlProvider->delete($table, $conditions); |
|
46
|
|
|
$this->assertEquals($returned, $result); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|\Doctrine\DBAL\Connection |
|
51
|
|
|
*/ |
|
52
|
|
|
private function getConnectionMock() |
|
53
|
|
|
{ |
|
54
|
|
|
$conn = $this->getMockBuilder('\Doctrine\DBAL\Connection') |
|
55
|
|
|
->disableOriginalConstructor() |
|
56
|
|
|
->getMock(); |
|
57
|
|
|
|
|
58
|
|
|
return $conn; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|\Doctrine\DBAL\Configuration |
|
63
|
|
|
*/ |
|
64
|
|
|
private function getConfigurationMock() |
|
65
|
|
|
{ |
|
66
|
|
|
$config = $this->getMockBuilder('\Doctrine\DBAL\Configuration') |
|
67
|
|
|
->disableOriginalConstructor() |
|
68
|
|
|
->getMock(); |
|
69
|
|
|
|
|
70
|
|
|
return $config; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* |
|
75
|
|
|
*/ |
|
76
|
|
|
public function testExists() |
|
77
|
|
|
{ |
|
78
|
|
|
$exists = '1'; |
|
79
|
|
|
$table = 'test'; |
|
80
|
|
|
$identifier = 'id'; |
|
81
|
|
|
$value = 1; |
|
82
|
|
|
$conn = $this->getConnectionMock(); |
|
83
|
|
|
$sth = $this->getStatementMock(); |
|
84
|
|
|
$sth->expects($this->once()) |
|
|
|
|
|
|
85
|
|
|
->method('fetchColumn') |
|
86
|
|
|
->will($this->returnValue($exists)); |
|
87
|
|
|
$conn->expects($this->once()) |
|
|
|
|
|
|
88
|
|
|
->method('executeQuery') |
|
89
|
|
|
->with("SELECT count(*) FROM `{$table}` WHERE `{$identifier}` = '{$value}'") |
|
90
|
|
|
->will($this->returnValue($sth)); |
|
91
|
|
|
$sqlProvider = new SqlProvider($conn, 'prod'); |
|
|
|
|
|
|
92
|
|
|
$result = $sqlProvider->exists($table, $identifier, $value); |
|
93
|
|
|
$this->assertEquals($exists, $result); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|\Doctrine\DBAL\Statement |
|
98
|
|
|
*/ |
|
99
|
|
|
private function getStatementMock() |
|
100
|
|
|
{ |
|
101
|
|
|
$sth = $this->getMockBuilder('\Doctrine\DBAL\Statement') |
|
102
|
|
|
->disableOriginalConstructor() |
|
103
|
|
|
->getMock(); |
|
104
|
|
|
|
|
105
|
|
|
return $sth; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* |
|
110
|
|
|
*/ |
|
111
|
|
|
public function testUpdate() |
|
112
|
|
|
{ |
|
113
|
|
|
$data = array( |
|
114
|
|
|
'id' => '1', |
|
115
|
|
|
'name' => 'test' |
|
116
|
|
|
); |
|
117
|
|
|
$table = 'test'; |
|
118
|
|
|
$identifier = array('id' => 1); |
|
119
|
|
|
$conn = $this->getConnectionMock(); |
|
120
|
|
|
$conn->expects($this->exactly(count($data))) |
|
|
|
|
|
|
121
|
|
|
->method('quoteIdentifier') |
|
122
|
|
|
->with($this->anything()) |
|
123
|
|
|
->will( |
|
124
|
|
|
$this->returnCallback( |
|
125
|
|
|
'Ajir\RabbitMqSqlBundle\Tests\Provider\SqlProviderTest::quoteIdentifierCallback' |
|
126
|
|
|
) |
|
127
|
|
|
); |
|
128
|
|
|
$conn->expects($this->once()) |
|
129
|
|
|
->method('lastInsertId') |
|
130
|
|
|
->will($this->returnValue($data['id'])); |
|
131
|
|
|
$sqlProvider = new SqlProvider($conn, 'prod'); |
|
|
|
|
|
|
132
|
|
|
$result = $sqlProvider->update($table, $data, $identifier); |
|
133
|
|
|
$this->assertEquals($data['id'], $result); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
public static function quoteIdentifierCallback() |
|
137
|
|
|
{ |
|
138
|
|
|
$args = func_get_args(); |
|
139
|
|
|
|
|
140
|
|
|
return "`{$args[0]}`"; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* |
|
145
|
|
|
*/ |
|
146
|
|
|
public function testInsert() |
|
147
|
|
|
{ |
|
148
|
|
|
$lastInsertId = '1'; |
|
149
|
|
|
$data = array( |
|
150
|
|
|
'name' => 'test' |
|
151
|
|
|
); |
|
152
|
|
|
$table = 'test'; |
|
153
|
|
|
$conn = $this->getConnectionMock(); |
|
154
|
|
|
$conn->expects($this->exactly(count($data))) |
|
|
|
|
|
|
155
|
|
|
->method('quoteIdentifier') |
|
156
|
|
|
->with($this->anything()) |
|
157
|
|
|
->will( |
|
158
|
|
|
$this->returnCallback( |
|
159
|
|
|
'Ajir\RabbitMqSqlBundle\Tests\Provider\SqlProviderTest::quoteIdentifierCallback' |
|
160
|
|
|
) |
|
161
|
|
|
); |
|
162
|
|
|
$conn->expects($this->once()) |
|
163
|
|
|
->method('lastInsertId') |
|
164
|
|
|
->will($this->returnValue($lastInsertId)); |
|
165
|
|
|
$sqlProvider = new SqlProvider($conn, 'prod'); |
|
|
|
|
|
|
166
|
|
|
$result = $sqlProvider->insert($table, $data); |
|
167
|
|
|
$this->assertEquals($lastInsertId, $result); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* |
|
172
|
|
|
*/ |
|
173
|
|
|
public function testInsertOrUpdateWithoutIdentifier() |
|
174
|
|
|
{ |
|
175
|
|
|
$data = array( |
|
176
|
|
|
'id' => '1', |
|
177
|
|
|
'name' => 'test' |
|
178
|
|
|
); |
|
179
|
|
|
$table = 'test'; |
|
180
|
|
|
$conn = $this->getConnectionMock(); |
|
181
|
|
|
$conn->expects($this->exactly(count($data))) |
|
|
|
|
|
|
182
|
|
|
->method('quoteIdentifier') |
|
183
|
|
|
->with($this->anything()) |
|
184
|
|
|
->will( |
|
185
|
|
|
$this->returnCallback( |
|
186
|
|
|
'Ajir\RabbitMqSqlBundle\Tests\Provider\SqlProviderTest::quoteIdentifierCallback' |
|
187
|
|
|
) |
|
188
|
|
|
); |
|
189
|
|
|
$conn->expects($this->once()) |
|
190
|
|
|
->method('lastInsertId') |
|
191
|
|
|
->will($this->returnValue($data['id'])); |
|
192
|
|
|
$sqlProvider = new SqlProvider($conn, 'prod'); |
|
|
|
|
|
|
193
|
|
|
$result = $sqlProvider->insertOrUpdateIfExists($table, $data); |
|
194
|
|
|
$this->assertEquals($data['id'], $result); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* |
|
199
|
|
|
*/ |
|
200
|
|
|
public function testInsertOrUpdateWithIdentifier() |
|
201
|
|
|
{ |
|
202
|
|
|
$exists = '1'; |
|
203
|
|
|
$data = array( |
|
204
|
|
|
'id' => '1', |
|
205
|
|
|
'name' => 'test' |
|
206
|
|
|
); |
|
207
|
|
|
$table = 'test'; |
|
208
|
|
|
$identifier = array('id' => 1); |
|
209
|
|
|
$conn = $this->getConnectionMock(); |
|
210
|
|
|
$sth = $this->getStatementMock(); |
|
211
|
|
|
$sth->expects($this->once()) |
|
|
|
|
|
|
212
|
|
|
->method('fetchColumn') |
|
213
|
|
|
->will($this->returnValue($exists)); |
|
214
|
|
|
$conn->expects($this->once()) |
|
|
|
|
|
|
215
|
|
|
->method('executeQuery') |
|
216
|
|
|
->with("SELECT count(*) FROM `{$table}` WHERE `id` = '1'") |
|
217
|
|
|
->will($this->returnValue($sth)); |
|
218
|
|
|
$conn->expects($this->exactly(count($data))) |
|
219
|
|
|
->method('quoteIdentifier') |
|
220
|
|
|
->with($this->anything()) |
|
221
|
|
|
->will( |
|
222
|
|
|
$this->returnCallback( |
|
223
|
|
|
'Ajir\RabbitMqSqlBundle\Tests\Provider\SqlProviderTest::quoteIdentifierCallback' |
|
224
|
|
|
) |
|
225
|
|
|
); |
|
226
|
|
|
$conn->expects($this->once()) |
|
227
|
|
|
->method('lastInsertId') |
|
228
|
|
|
->will($this->returnValue($data['id'])); |
|
229
|
|
|
$sqlProvider = new SqlProvider($conn, 'prod'); |
|
|
|
|
|
|
230
|
|
|
$result = $sqlProvider->insertOrUpdateIfExists($table, $data, $identifier); |
|
231
|
|
|
$this->assertEquals($data['id'], $result); |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
/** |
|
235
|
|
|
* |
|
236
|
|
|
*/ |
|
237
|
|
|
public function testGetColumnValueWhere() |
|
238
|
|
|
{ |
|
239
|
|
|
$column = 'name'; |
|
240
|
|
|
$where = 'id'; |
|
241
|
|
|
$value = '1'; |
|
242
|
|
|
$table = 'test'; |
|
243
|
|
|
$fetch = 'test'; |
|
244
|
|
|
$conn = $this->getConnectionMock(); |
|
245
|
|
|
$sth = $this->getStatementMock(); |
|
246
|
|
|
$sth->expects($this->once()) |
|
|
|
|
|
|
247
|
|
|
->method('fetchColumn') |
|
248
|
|
|
->will($this->returnValue($fetch)); |
|
249
|
|
|
$conn->expects($this->once()) |
|
|
|
|
|
|
250
|
|
|
->method('executeQuery') |
|
251
|
|
|
->with("SELECT `{$column}` FROM `{$table}` WHERE `{$where}` = '{$value}'") |
|
252
|
|
|
->will($this->returnValue($sth)); |
|
253
|
|
|
$sqlProvider = new SqlProvider($conn, 'prod'); |
|
|
|
|
|
|
254
|
|
|
$result = $sqlProvider->getColumnValueWhere($table, $column, $where, $value); |
|
255
|
|
|
$this->assertEquals($fetch, $result); |
|
256
|
|
|
} |
|
257
|
|
|
} |
|
258
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: