1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
/* |
5
|
|
|
Copyright (C) 2006-2014 David Négrier - THE CODING MACHINE |
6
|
|
|
|
7
|
|
|
This program is free software; you can redistribute it and/or modify |
8
|
|
|
it under the terms of the GNU General Public License as published by |
9
|
|
|
the Free Software Foundation; either version 2 of the License, or |
10
|
|
|
(at your option) any later version. |
11
|
|
|
|
12
|
|
|
This program is distributed in the hope that it will be useful, |
13
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
14
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15
|
|
|
GNU General Public License for more details. |
16
|
|
|
|
17
|
|
|
You should have received a copy of the GNU General Public License |
18
|
|
|
along with this program; if not, write to the Free Software |
19
|
|
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
namespace TheCodingMachine\TDBM; |
23
|
|
|
|
24
|
|
|
use Psr\Log\LogLevel; |
25
|
|
|
use Wa72\SimpleLogger\ArrayLogger; |
26
|
|
|
|
27
|
|
|
class TDBMServiceTest extends TDBMAbstractServiceTest |
28
|
|
|
{ |
29
|
|
|
public function testGetLinkBetweenInheritedTables() |
30
|
|
|
{ |
31
|
|
|
$this->assertEquals(['users', 'contact', 'person'], $this->tdbmService->_getLinkBetweenInheritedTables(['contact', 'users'])); |
32
|
|
|
$this->assertEquals(['users', 'contact', 'person'], $this->tdbmService->_getLinkBetweenInheritedTables(['users', 'contact'])); |
33
|
|
|
$this->assertEquals(['contact', 'person'], $this->tdbmService->_getLinkBetweenInheritedTables(['person', 'contact'])); |
34
|
|
|
$this->assertEquals(['users', 'contact', 'person'], $this->tdbmService->_getLinkBetweenInheritedTables(['users'])); |
35
|
|
|
$this->assertEquals(['person'], $this->tdbmService->_getLinkBetweenInheritedTables(['person'])); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function testGetRelatedTablesByInheritance() |
39
|
|
|
{ |
40
|
|
|
$contactRelatedTables = $this->tdbmService->_getRelatedTablesByInheritance('contact'); |
41
|
|
|
$this->assertCount(3, $contactRelatedTables); |
42
|
|
|
$this->assertContains('users', $contactRelatedTables); |
43
|
|
|
$this->assertContains('contact', $contactRelatedTables); |
44
|
|
|
$this->assertContains('person', $contactRelatedTables); |
45
|
|
|
$this->assertEquals(['person', 'contact', 'users'], $this->tdbmService->_getRelatedTablesByInheritance('users')); |
46
|
|
|
$this->assertEquals(['person', 'contact', 'users'], $this->tdbmService->_getRelatedTablesByInheritance('person')); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @expectedException \TheCodingMachine\TDBM\TDBMException |
51
|
|
|
* |
52
|
|
|
* @throws TDBMException |
53
|
|
|
*/ |
54
|
|
|
public function testGetPrimaryKeysFromIndexedPrimaryKeysException() |
55
|
|
|
{ |
56
|
|
|
$this->tdbmService->_getPrimaryKeysFromIndexedPrimaryKeys('users', [5, 4]); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @expectedException \TheCodingMachine\TDBM\TDBMException |
61
|
|
|
* |
62
|
|
|
* @throws TDBMException |
63
|
|
|
*/ |
64
|
|
|
public function testGetLinkBetweenInheritedTablesExceptions() |
65
|
|
|
{ |
66
|
|
|
$this->tdbmService->_getLinkBetweenInheritedTables(['contact', 'country']); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function testHashPrimaryKey() |
70
|
|
|
{ |
71
|
|
|
$reflection = new \ReflectionClass(get_class($this->tdbmService)); |
72
|
|
|
$method = $reflection->getMethod('getObjectHash'); |
73
|
|
|
$method->setAccessible(true); |
74
|
|
|
|
75
|
|
|
$result = $method->invokeArgs($this->tdbmService, [ |
76
|
|
|
['id' => 42], |
77
|
|
|
]); |
78
|
|
|
$this->assertEquals(42, $result); |
79
|
|
|
|
80
|
|
|
// Check that multiple primary keys are insensitive to column order |
81
|
|
|
$result1 = $method->invokeArgs($this->tdbmService, [ |
82
|
|
|
['id1' => 42, 'id2' => 24], |
83
|
|
|
]); |
84
|
|
|
$result2 = $method->invokeArgs($this->tdbmService, [ |
85
|
|
|
['id2' => 24, 'id1' => 42], |
86
|
|
|
]); |
87
|
|
|
$this->assertEquals($result1, $result2); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function testInsertAndUpdateAndDelete() |
91
|
|
|
{ |
92
|
|
|
$object = new TDBMObject('users'); |
93
|
|
|
$object->setProperty('login', 'john.doe'); |
94
|
|
|
$object->setProperty('country_id', 3); |
95
|
|
|
$object->setProperty('name', 'John Doe', 'person'); |
96
|
|
|
$object->setProperty('email', '[email protected]', 'contact'); |
97
|
|
|
|
98
|
|
|
$this->tdbmService->save($object); |
99
|
|
|
|
100
|
|
|
$this->assertNotEmpty($object->getProperty('id', 'person')); |
101
|
|
|
$this->assertNotEmpty($object->getProperty('id', 'users')); |
102
|
|
|
$this->assertEquals($object->getProperty('id', 'person'), $object->getProperty('id', 'users')); |
103
|
|
|
|
104
|
|
|
$object->setProperty('country_id', 2, 'users'); |
105
|
|
|
|
106
|
|
|
$this->tdbmService->save($object); |
107
|
|
|
|
108
|
|
|
$this->tdbmService->delete($object); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function testInsertMultipleDataAtOnceInInheritance() |
112
|
|
|
{ |
113
|
|
|
$object = new TDBMObject(); |
114
|
|
|
$object->setProperty('login', 'jane.doe', 'users'); |
115
|
|
|
$object->setProperty('name', 'Jane Doe', 'person'); |
116
|
|
|
$object->setProperty('country_id', 1, 'users'); |
117
|
|
|
$object->setProperty('email', '[email protected]', 'contact'); |
118
|
|
|
|
119
|
|
|
$this->tdbmService->save($object); |
120
|
|
|
|
121
|
|
|
$this->assertNotEmpty($object->getProperty('id', 'person')); |
122
|
|
|
$this->assertNotEmpty($object->getProperty('id', 'users')); |
123
|
|
|
$this->assertEquals($object->getProperty('id', 'person'), $object->getProperty('id', 'users')); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function testCompleteSave() |
127
|
|
|
{ |
128
|
|
|
$beans = $this->tdbmService->findObjects('users', 'users.login = :login', ['login' => 'jane.doe'], null, [], null, TDBMObject::class); |
129
|
|
|
$jane = $beans[0]; |
130
|
|
|
$jane->setProperty('country_id', 2, 'users'); |
131
|
|
|
|
132
|
|
|
$this->tdbmService->completeSave(); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function testCompleteSave2() |
136
|
|
|
{ |
137
|
|
|
$beans = $this->tdbmService->findObjects('users', 'users.login = :login', ['login' => 'jane.doe'], null, [], null, TDBMObject::class); |
138
|
|
|
$jane = $beans[0]; |
139
|
|
|
|
140
|
|
|
$this->assertEquals(2, $jane->getProperty('country_id', 'users')); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function testUpdatePrimaryKey() |
144
|
|
|
{ |
145
|
|
|
$object = new TDBMObject('rights'); |
146
|
|
|
$object->setProperty('label', 'CAN_EDIT_BOUK'); |
147
|
|
|
|
148
|
|
|
$this->tdbmService->save($object); |
149
|
|
|
|
150
|
|
|
$object->setProperty('label', 'CAN_EDIT_BOOK'); |
151
|
|
|
|
152
|
|
|
$this->tdbmService->save($object); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @expectedException \TheCodingMachine\TDBM\TDBMInvalidOperationException |
157
|
|
|
* |
158
|
|
|
* @throws TDBMInvalidOperationException |
159
|
|
|
*/ |
160
|
|
|
public function testCannotDeleteDetachedObjects() |
161
|
|
|
{ |
162
|
|
|
$object = new TDBMObject('rights'); |
163
|
|
|
$object->setProperty('label', 'CAN_DELETE'); |
164
|
|
|
|
165
|
|
|
$this->tdbmService->delete($object); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
public function testDeleteNewObject() |
169
|
|
|
{ |
170
|
|
|
$object = new TDBMObject('rights'); |
171
|
|
|
$object->setProperty('label', 'CAN_DELETE'); |
172
|
|
|
|
173
|
|
|
$this->tdbmService->attach($object); |
174
|
|
|
|
175
|
|
|
$this->tdbmService->delete($object); |
176
|
|
|
|
177
|
|
|
$exceptionRaised = false; |
178
|
|
|
try { |
179
|
|
|
$this->tdbmService->save($object); |
180
|
|
|
} catch (TDBMInvalidOperationException $e) { |
181
|
|
|
$exceptionRaised = true; |
182
|
|
|
} |
183
|
|
|
$this->assertTrue($exceptionRaised); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
public function testDeleteLoadedObject() |
187
|
|
|
{ |
188
|
|
|
$object = new TDBMObject('rights'); |
189
|
|
|
$object->setProperty('label', 'CAN_DELETE'); |
190
|
|
|
|
191
|
|
|
$this->tdbmService->save($object); |
192
|
|
|
|
193
|
|
|
$object->setProperty('label', 'CAN_DELETE2'); |
194
|
|
|
|
195
|
|
|
$this->tdbmService->delete($object); |
196
|
|
|
|
197
|
|
|
// Try to delete a deleted object (this should do nothing) |
198
|
|
|
$this->tdbmService->delete($object); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
public function testFindObjects() |
202
|
|
|
{ |
203
|
|
|
/*$magicQuery = new MagicQuery($this->tdbmService->getConnection()); |
|
|
|
|
204
|
|
|
$result = $magicQuery->parse("SELECT DISTINCT users.id, users.login FROM users"); |
205
|
|
|
var_dump($result);*/ |
206
|
|
|
|
207
|
|
|
$beans = $this->tdbmService->findObjects('contact', null, [], 'contact.id ASC', [], null, TDBMObject::class); |
208
|
|
|
$beans2 = $this->tdbmService->findObjects('contact', 'contact.id = :id', ['id' => 1], null, [], null, TDBMObject::class); |
209
|
|
|
|
210
|
|
|
foreach ($beans as $bean) { |
211
|
|
|
$bean1 = $bean; |
212
|
|
|
break; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
foreach ($beans2 as $bean) { |
216
|
|
|
$bean2 = $bean; |
217
|
|
|
break; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
$this->assertTrue($bean1 === $bean2); |
221
|
|
|
$this->assertEquals(5, $beans->count()); |
222
|
|
|
$this->assertEquals(1, $beans2->count()); |
223
|
|
|
|
224
|
|
|
//$this->assertTrue($beans[0] === $beans2[0]); |
|
|
|
|
225
|
|
|
//var_dump($beans); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
public function testRawSqlFilterCountriesByUserCount() |
229
|
|
|
{ |
230
|
|
|
$this->onlyMySql(); |
231
|
|
|
|
232
|
|
|
$sql = <<<SQL |
233
|
|
|
SELECT country.*, GROUP_CONCAT(users.id) AS ids |
234
|
|
|
FROM country |
235
|
|
|
JOIN users ON country.id= users.country_id |
236
|
|
|
GROUP BY country.id |
237
|
|
|
HAVING COUNT(users.id) > 1; |
238
|
|
|
SQL; |
239
|
|
|
/** @var Test\Dao\Bean\CountryBean[]|\Porpaginas\Result $beans */ |
240
|
|
|
$beans = $this->tdbmService->findObjectsFromRawSql('country', $sql, [], null, Test\Dao\Bean\CountryBean::class); |
241
|
|
|
|
242
|
|
|
$count = 0; |
243
|
|
|
foreach ($beans as $country) { |
244
|
|
|
$this->assertGreaterThan(1, count($country->getUsers())); |
245
|
|
|
$count++; |
246
|
|
|
} |
247
|
|
|
$this->assertEquals($count, $beans->count()); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
public function testRawSqlOrderCountriesByUserCount() |
251
|
|
|
{ |
252
|
|
|
$this->onlyMySql(); |
253
|
|
|
|
254
|
|
|
$sql = <<<SQL |
255
|
|
|
SELECT country.*, GROUP_CONCAT(users.id) AS ids |
256
|
|
|
FROM country |
257
|
|
|
JOIN users ON country.id= users.country_id |
258
|
|
|
GROUP BY country.id |
259
|
|
|
ORDER BY COUNT(users.id); |
260
|
|
|
SQL; |
261
|
|
|
|
262
|
|
|
/** @var Test\Dao\Bean\CountryBean[]|\Porpaginas\Result $beans */ |
263
|
|
|
$beans = $this->tdbmService->findObjectsFromRawSql('country', $sql, [], null, Test\Dao\Bean\CountryBean::class); |
264
|
|
|
|
265
|
|
|
$count = 0; |
266
|
|
|
foreach ($beans as $country) { |
267
|
|
|
$count++; |
268
|
|
|
} |
269
|
|
|
$this->assertEquals($count, $beans->count()); |
270
|
|
|
|
271
|
|
|
for ($i = 1; $i < count($beans); $i++) { |
|
|
|
|
272
|
|
|
$this->assertLessThanOrEqual(count($beans[$i]->getUsers()), count($beans[$i - 1]->getUsers())); |
273
|
|
|
} |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
public function testRawSqlOrderUsersByCustomRoleOrder() |
277
|
|
|
{ |
278
|
|
|
$this->onlyMySql(); |
279
|
|
|
|
280
|
|
|
$sql = <<<SQL |
281
|
|
|
SELECT `person`.*, `contact`.*, `users`.* |
282
|
|
|
FROM `contact` |
283
|
|
|
JOIN `users` ON `users`.`id` = `contact`.`id` |
284
|
|
|
JOIN `person` ON `person`.`id` = `users`.`id` |
285
|
|
|
JOIN `users_roles` ON users.id = users_roles.user_id |
286
|
|
|
JOIN `roles` ON roles.id = users_roles.role_id |
287
|
|
|
GROUP BY users.id |
288
|
|
|
ORDER BY MAX(IF(roles.name = 'Admins', 3, IF(roles.name = 'Writers', 2, IF(roles.name = 'Singers', 1, 0)))) DESC |
289
|
|
|
SQL; |
290
|
|
|
|
291
|
|
|
/** @var Test\Dao\Bean\UserBean[]|\Porpaginas\Result $beans */ |
292
|
|
|
$beans = $this->tdbmService->findObjectsFromRawSql('contact', $sql, [], null, Test\Dao\Bean\UserBean::class); |
293
|
|
|
|
294
|
|
|
function getCustomOrder(Test\Dao\Bean\UserBean $contact) |
295
|
|
|
{ |
296
|
|
|
$max = 0; |
297
|
|
|
foreach ($contact->getRoles() as $role) { |
298
|
|
|
$max = max($max, [ |
299
|
|
|
'Admins' => 3, |
300
|
|
|
'Writers' => 2, |
301
|
|
|
'Singers' => 1, |
302
|
|
|
][$role->getName()]); |
303
|
|
|
} |
304
|
|
|
return $max; |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
$this->assertCount(4, $beans); |
308
|
|
|
|
309
|
|
|
for ($i = 1; $i < count($beans); $i++) { |
|
|
|
|
310
|
|
|
$this->assertGreaterThanOrEqual(getCustomOrder($beans[$i]), getCustomOrder($beans[$i - 1]), 'Beans order does not comply with expected result.'); |
311
|
|
|
} |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
public function testArrayAccess() |
315
|
|
|
{ |
316
|
|
|
$beans = $this->tdbmService->findObjects('contact', null, [], 'contact.id ASC', [], null, TDBMObject::class); |
317
|
|
|
|
318
|
|
|
$this->assertTrue(isset($beans[0])); |
319
|
|
|
$this->assertFalse(isset($beans[42])); |
320
|
|
|
$this->assertEquals(1, $beans[0]->getProperty('id', 'person')); |
321
|
|
|
|
322
|
|
|
$result1 = []; |
323
|
|
|
foreach ($beans as $bean) { |
324
|
|
|
$result1[] = $bean; |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
$result2 = []; |
328
|
|
|
foreach ($beans as $bean) { |
329
|
|
|
$result2[] = $bean; |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
$this->assertEquals($result1, $result2); |
333
|
|
|
$this->assertTrue($result1[0] === $result2[0]); |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
/** |
337
|
|
|
* @expectedException \TheCodingMachine\TDBM\TDBMInvalidOffsetException |
338
|
|
|
* |
339
|
|
|
* @throws TDBMInvalidOffsetException |
340
|
|
|
*/ |
341
|
|
|
public function testArrayAccessException() |
342
|
|
|
{ |
343
|
|
|
$beans = $this->tdbmService->findObjects('contact', null, [], 'contact.id ASC', [], null, TDBMObject::class); |
344
|
|
|
|
345
|
|
|
$beans[-1]; |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
/** |
349
|
|
|
* @expectedException \TheCodingMachine\TDBM\TDBMInvalidOffsetException |
350
|
|
|
* |
351
|
|
|
* @throws TDBMInvalidOffsetException |
352
|
|
|
*/ |
353
|
|
|
public function testArrayAccessException2() |
354
|
|
|
{ |
355
|
|
|
$beans = $this->tdbmService->findObjects('contact', null, [], 'contact.id ASC', [], null, TDBMObject::class); |
356
|
|
|
|
357
|
|
|
$beans['foo']; |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
/** |
361
|
|
|
* @expectedException \TheCodingMachine\TDBM\TDBMException |
362
|
|
|
* |
363
|
|
|
* @throws TDBMException |
364
|
|
|
*/ |
365
|
|
|
public function testBeanGetException() |
366
|
|
|
{ |
367
|
|
|
$beans = $this->tdbmService->findObjects('contact', null, [], 'contact.id ASC', [], null, TDBMObject::class); |
368
|
|
|
$bean = $beans[0]; |
369
|
|
|
|
370
|
|
|
// we don't specify the table on inheritance table => exception. |
371
|
|
|
$bean->getProperty('id'); |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
/** |
375
|
|
|
* @expectedException \TheCodingMachine\TDBM\TDBMException |
376
|
|
|
* |
377
|
|
|
* @throws TDBMException |
378
|
|
|
*/ |
379
|
|
|
public function testBeanSetException() |
380
|
|
|
{ |
381
|
|
|
$beans = $this->tdbmService->findObjects('contact', null, [], 'contact.id ASC', [], null, TDBMObject::class); |
382
|
|
|
$bean = $beans[0]; |
383
|
|
|
|
384
|
|
|
// we don't specify the table on inheritance table => exception. |
385
|
|
|
$bean->setProperty('name', 'foo'); |
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
public function testTake() |
389
|
|
|
{ |
390
|
|
|
$beans = $this->tdbmService->findObjects('contact', null, [], 'contact.id ASC', [], null, TDBMObject::class); |
391
|
|
|
|
392
|
|
|
$page = $beans->take(0, 2); |
393
|
|
|
|
394
|
|
|
$this->assertEquals(2, $page->count()); |
395
|
|
|
|
396
|
|
|
$results = []; |
397
|
|
|
foreach ($page as $result) { |
398
|
|
|
$results[] = $result; |
399
|
|
|
} |
400
|
|
|
$this->assertCount(2, $results); |
401
|
|
|
|
402
|
|
|
$this->assertEquals(5, $page->totalCount()); |
403
|
|
|
|
404
|
|
|
$page = $beans->take(1, 1); |
405
|
|
|
|
406
|
|
|
$this->assertEquals(1, $page->count()); |
407
|
|
|
|
408
|
|
|
$resultArray = $page->toArray(); |
409
|
|
|
$this->assertCount(1, $resultArray); |
410
|
|
|
$this->assertTrue($resultArray[0] === $page[0]); |
411
|
|
|
// Test page isset |
412
|
|
|
$this->assertTrue(isset($page[0])); |
413
|
|
|
} |
414
|
|
|
|
415
|
|
|
public function testTakeInCursorMode() |
416
|
|
|
{ |
417
|
|
|
$beans = $this->tdbmService->findObjects('contact', null, [], 'contact.id ASC', [], TDBMService::MODE_CURSOR, TDBMObject::class); |
418
|
|
|
|
419
|
|
|
$page = $beans->take(0, 2); |
420
|
|
|
|
421
|
|
|
$this->assertEquals(2, $page->count()); |
422
|
|
|
$this->assertEquals(0, $page->getCurrentOffset()); |
423
|
|
|
$this->assertEquals(2, $page->getCurrentLimit()); |
424
|
|
|
$this->assertEquals(1, $page->getCurrentPage()); |
425
|
|
|
|
426
|
|
|
$results = []; |
427
|
|
|
foreach ($page as $result) { |
428
|
|
|
$results[] = $result; |
429
|
|
|
} |
430
|
|
|
$this->assertCount(2, $results); |
431
|
|
|
|
432
|
|
|
$this->assertEquals(5, $page->totalCount()); |
433
|
|
|
|
434
|
|
|
$page = $beans->take(1, 1); |
435
|
|
|
$this->assertEquals(1, $page->getCurrentOffset()); |
436
|
|
|
$this->assertEquals(1, $page->getCurrentLimit()); |
437
|
|
|
$this->assertEquals(2, $page->getCurrentPage()); |
438
|
|
|
|
439
|
|
|
$this->assertEquals(1, $page->count()); |
440
|
|
|
} |
441
|
|
|
|
442
|
|
|
public function testMap() |
443
|
|
|
{ |
444
|
|
|
$beans = $this->tdbmService->findObjects('person', null, [], 'person.id ASC', [], null, TDBMObject::class); |
445
|
|
|
|
446
|
|
|
$results = $beans->map(function ($item) { |
447
|
|
|
return $item->getProperty('id', 'person'); |
448
|
|
|
})->toArray(); |
449
|
|
|
|
450
|
|
|
$this->assertEquals([1, 2, 3, 4, 6], $results); |
451
|
|
|
|
452
|
|
|
// Same test with page |
453
|
|
|
$page = $beans->take(0, 2); |
454
|
|
|
|
455
|
|
|
$results = $page->map(function ($item) { |
456
|
|
|
return $item->getProperty('id', 'person'); |
457
|
|
|
})->toArray(); |
458
|
|
|
|
459
|
|
|
$this->assertEquals([1, 2], $results); |
460
|
|
|
} |
461
|
|
|
|
462
|
|
|
/** |
463
|
|
|
* @expectedException \TheCodingMachine\TDBM\TDBMException |
464
|
|
|
* |
465
|
|
|
* @throws TDBMException |
466
|
|
|
*/ |
467
|
|
|
public function testUnsetException() |
468
|
|
|
{ |
469
|
|
|
$beans = $this->tdbmService->findObjects('contact', null, [], 'contact.id ASC', [], null, TDBMObject::class); |
470
|
|
|
|
471
|
|
|
unset($beans[0]); |
472
|
|
|
} |
473
|
|
|
|
474
|
|
|
/** |
475
|
|
|
* @expectedException \TheCodingMachine\TDBM\TDBMException |
476
|
|
|
* |
477
|
|
|
* @throws TDBMException |
478
|
|
|
*/ |
479
|
|
|
public function testSetException() |
480
|
|
|
{ |
481
|
|
|
$beans = $this->tdbmService->findObjects('contact', null, [], 'contact.id ASC', [], null, TDBMObject::class); |
482
|
|
|
|
483
|
|
|
$beans[0] = 'foo'; |
484
|
|
|
} |
485
|
|
|
|
486
|
|
|
/** |
487
|
|
|
* @expectedException \TheCodingMachine\TDBM\TDBMException |
488
|
|
|
* |
489
|
|
|
* @throws TDBMException |
490
|
|
|
*/ |
491
|
|
|
public function testPageUnsetException() |
492
|
|
|
{ |
493
|
|
|
$beans = $this->tdbmService->findObjects('contact', null, [], 'contact.id ASC', [], null, TDBMObject::class); |
494
|
|
|
$page = $beans->take(0, 1); |
495
|
|
|
unset($page[0]); |
496
|
|
|
} |
497
|
|
|
|
498
|
|
|
/** |
499
|
|
|
* @expectedException \TheCodingMachine\TDBM\TDBMException |
500
|
|
|
* |
501
|
|
|
* @throws TDBMException |
502
|
|
|
*/ |
503
|
|
|
public function testPageSetException() |
504
|
|
|
{ |
505
|
|
|
$beans = $this->tdbmService->findObjects('contact', null, [], 'contact.id ASC', [], null, TDBMObject::class); |
506
|
|
|
$page = $beans->take(0, 1); |
507
|
|
|
$page[0] = 'foo'; |
508
|
|
|
} |
509
|
|
|
|
510
|
|
|
public function testToArray() |
511
|
|
|
{ |
512
|
|
|
$beans = $this->tdbmService->findObjects('contact', 'contact.id = :id', ['id' => 1], null, [], null, TDBMObject::class); |
513
|
|
|
|
514
|
|
|
$beanArray = $beans->toArray(); |
515
|
|
|
|
516
|
|
|
$this->assertCount(1, $beanArray); |
517
|
|
|
$this->assertEquals(1, $beanArray[0]->getProperty('id', 'contact')); |
518
|
|
|
} |
519
|
|
|
|
520
|
|
|
public function testCursorMode() |
521
|
|
|
{ |
522
|
|
|
$beans = $this->tdbmService->findObjects('contact', 'contact.id = :id', ['id' => 1], null, [], TDBMService::MODE_CURSOR, TDBMObject::class); |
523
|
|
|
|
524
|
|
|
$this->assertInstanceOf('\\TheCodingMachine\\TDBM\\ResultIterator', $beans); |
525
|
|
|
|
526
|
|
|
$result = []; |
527
|
|
|
foreach ($beans as $bean) { |
528
|
|
|
$result[] = $bean; |
529
|
|
|
} |
530
|
|
|
|
531
|
|
|
$this->assertCount(1, $result); |
532
|
|
|
|
533
|
|
|
// In cursor mode, access by array causes an exception. |
534
|
|
|
$exceptionTriggered = false; |
535
|
|
|
try { |
536
|
|
|
$beans[0]; |
537
|
|
|
} catch (TDBMInvalidOperationException $e) { |
538
|
|
|
$exceptionTriggered = true; |
539
|
|
|
} |
540
|
|
|
$this->assertTrue($exceptionTriggered); |
541
|
|
|
|
542
|
|
|
$exceptionTriggered = false; |
543
|
|
|
try { |
544
|
|
|
isset($beans[0]); |
545
|
|
|
} catch (TDBMInvalidOperationException $e) { |
546
|
|
|
$exceptionTriggered = true; |
547
|
|
|
} |
548
|
|
|
$this->assertTrue($exceptionTriggered); |
549
|
|
|
} |
550
|
|
|
|
551
|
|
|
public function testSetFetchMode() |
552
|
|
|
{ |
553
|
|
|
$this->tdbmService->setFetchMode(TDBMService::MODE_CURSOR); |
554
|
|
|
$beans = $this->tdbmService->findObjects('contact', 'contact.id = :id', ['id' => 1], null, [], null, TDBMObject::class); |
555
|
|
|
|
556
|
|
|
$this->assertInstanceOf('\\TheCodingMachine\\TDBM\\ResultIterator', $beans); |
557
|
|
|
|
558
|
|
|
// In cursor mode, access by array causes an exception. |
559
|
|
|
$exceptionTriggered = false; |
560
|
|
|
try { |
561
|
|
|
$beans[0]; |
562
|
|
|
} catch (TDBMInvalidOperationException $e) { |
563
|
|
|
$exceptionTriggered = true; |
564
|
|
|
} |
565
|
|
|
$this->assertTrue($exceptionTriggered); |
566
|
|
|
} |
567
|
|
|
|
568
|
|
|
/** |
569
|
|
|
* @expectedException \TheCodingMachine\TDBM\TDBMException |
570
|
|
|
* |
571
|
|
|
* @throws TDBMException |
572
|
|
|
*/ |
573
|
|
|
public function testInvalidSetFetchMode() |
574
|
|
|
{ |
575
|
|
|
$this->tdbmService->setFetchMode('foo'); |
576
|
|
|
} |
577
|
|
|
|
578
|
|
|
/** |
579
|
|
|
* @expectedException \TheCodingMachine\TDBM\TDBMException |
580
|
|
|
* |
581
|
|
|
* @throws TDBMException |
582
|
|
|
*/ |
583
|
|
|
public function testCursorModeException() |
584
|
|
|
{ |
585
|
|
|
$beans = $this->tdbmService->findObjects('contact', 'contact.id = :id', ['id' => 1], null, [], 99); |
586
|
|
|
} |
587
|
|
|
|
588
|
|
|
/** |
589
|
|
|
* @expectedException \TheCodingMachine\TDBM\TDBMException |
590
|
|
|
* |
591
|
|
|
* @throws TDBMException |
592
|
|
|
*/ |
593
|
|
|
public function testTableNameException() |
594
|
|
|
{ |
595
|
|
|
$beans = $this->tdbmService->findObjects('foo bar'); |
596
|
|
|
} |
597
|
|
|
|
598
|
|
|
public function testLinkedTableFetch() |
599
|
|
|
{ |
600
|
|
|
$beans = $this->tdbmService->findObjects('contact', 'contact.id = :id', ['id' => 1], null, ['country'], null, TDBMObject::class); |
601
|
|
|
} |
602
|
|
|
|
603
|
|
|
public function testFindObject() |
604
|
|
|
{ |
605
|
|
|
$bean = $this->tdbmService->findObject('contact', 'contact.id = :id', ['id' => -42], [], TDBMObject::class); |
606
|
|
|
$this->assertNull($bean); |
607
|
|
|
} |
608
|
|
|
|
609
|
|
|
/** |
610
|
|
|
* @expectedException \TheCodingMachine\TDBM\NoBeanFoundException |
611
|
|
|
* |
612
|
|
|
* @throws NoBeanFoundException |
613
|
|
|
*/ |
614
|
|
|
public function testFindObjectOrFail() |
615
|
|
|
{ |
616
|
|
|
$bean = $this->tdbmService->findObjectOrFail('contact', 'contact.id = :id', ['id' => -42], [], TDBMObject::class); |
617
|
|
|
} |
618
|
|
|
|
619
|
|
|
/** |
620
|
|
|
* @throws NoBeanFoundException |
621
|
|
|
*/ |
622
|
|
|
public function testFindObjectByPkException() |
623
|
|
|
{ |
624
|
|
|
$this->expectException(NoBeanFoundException::class); |
625
|
|
|
$this->expectExceptionMessage("No result found for query on table 'contact' for 'id' = -42"); |
626
|
|
|
$bean = $this->tdbmService->findObjectByPk('contact', ['id' => -42], [], false, TDBMObject::class); |
627
|
|
|
} |
628
|
|
|
|
629
|
|
|
/** |
630
|
|
|
* @expectedException \TheCodingMachine\TDBM\DuplicateRowException |
631
|
|
|
* |
632
|
|
|
* @throws DuplicateRowException |
633
|
|
|
*/ |
634
|
|
|
public function testFindObjectDuplicateRow() |
635
|
|
|
{ |
636
|
|
|
$bean = $this->tdbmService->findObject('contact'); |
637
|
|
|
} |
638
|
|
|
|
639
|
|
|
public function testFindObjectsByBean() |
640
|
|
|
{ |
641
|
|
|
$countryBean = $this->tdbmService->findObject('country', 'id = :id', ['id' => 1], [], TDBMObject::class); |
642
|
|
|
|
643
|
|
|
$users = $this->tdbmService->findObjects('users', $countryBean, [], null, [], null, TDBMObject::class); |
644
|
|
|
$this->assertCount(1, $users); |
645
|
|
|
$this->assertEquals('jean.dupont', $users[0]->getProperty('login', 'users')); |
646
|
|
|
} |
647
|
|
|
|
648
|
|
|
/** |
649
|
|
|
* @expectedException \TheCodingMachine\TDBM\TDBMException |
650
|
|
|
* |
651
|
|
|
* @throws TDBMException |
652
|
|
|
* @throws TDBMInvalidOperationException |
653
|
|
|
*/ |
654
|
|
|
public function testBeanWithoutStatus() |
655
|
|
|
{ |
656
|
|
|
$reflectionClass = new \ReflectionClass(TDBMObject::class); |
657
|
|
|
$object = $reflectionClass->newInstanceWithoutConstructor(); |
658
|
|
|
$object->_getStatus(); |
659
|
|
|
} |
660
|
|
|
|
661
|
|
|
public function testFindObjectsFromSql() |
662
|
|
|
{ |
663
|
|
|
$roles = $this->tdbmService->findObjectsFromSql( |
664
|
|
|
'roles', |
665
|
|
|
'roles JOIN roles_rights ON roles.id = roles_rights.role_id JOIN rights ON rights.label = roles_rights.right_label', |
666
|
|
|
'rights.label = :right', |
667
|
|
|
array('right' => 'CAN_SING'), |
668
|
|
|
'roles.name DESC' |
669
|
|
|
); |
670
|
|
|
$this->assertCount(2, $roles); |
671
|
|
|
$this->assertInstanceOf(AbstractTDBMObject::class, $roles[0]); |
672
|
|
|
} |
673
|
|
|
|
674
|
|
|
/** |
675
|
|
|
* @expectedException \TheCodingMachine\TDBM\TDBMException |
676
|
|
|
* |
677
|
|
|
* @throws TDBMException |
678
|
|
|
*/ |
679
|
|
|
public function testFindObjectsFromSqlBadTableName() |
680
|
|
|
{ |
681
|
|
|
$this->tdbmService->findObjectsFromSql( |
682
|
|
|
'#{azerty', |
683
|
|
|
'roles JOIN roles_rights ON roles.id = roles_rights.role_id JOIN rights ON rights.label = roles_rights.right_label', |
684
|
|
|
'rights.label = :right', |
685
|
|
|
array('right' => 'CAN_SING'), |
686
|
|
|
'name DESC' |
687
|
|
|
); |
688
|
|
|
} |
689
|
|
|
|
690
|
|
|
/** |
691
|
|
|
* @expectedException \TheCodingMachine\TDBM\TDBMException |
692
|
|
|
* |
693
|
|
|
* @throws TDBMException |
694
|
|
|
*/ |
695
|
|
|
public function testFindObjectsFromSqlGroupBy() |
696
|
|
|
{ |
697
|
|
|
$roles = $this->tdbmService->findObjectsFromSql( |
698
|
|
|
'roles', |
699
|
|
|
'roles JOIN roles_rights ON roles.id = roles_rights.role_id JOIN rights ON rights.label = roles_rights.right_label', |
700
|
|
|
'rights.label = :right GROUP BY roles.name', |
701
|
|
|
array('right' => 'CAN_SING'), |
702
|
|
|
'name DESC' |
703
|
|
|
); |
704
|
|
|
$role = $roles[0]; |
705
|
|
|
} |
706
|
|
|
|
707
|
|
|
public function testFindObjectFromSql() |
708
|
|
|
{ |
709
|
|
|
$role = $this->tdbmService->findObjectFromSql( |
710
|
|
|
'roles', |
711
|
|
|
'roles JOIN roles_rights ON roles.id = roles_rights.role_id JOIN rights ON rights.label = roles_rights.right_label', |
712
|
|
|
'rights.label = :right AND name = :name', |
713
|
|
|
array('right' => 'CAN_SING', 'name' => 'Singers') |
714
|
|
|
); |
715
|
|
|
$this->assertInstanceOf(AbstractTDBMObject::class, $role); |
716
|
|
|
} |
717
|
|
|
|
718
|
|
|
/** |
719
|
|
|
* @expectedException \TheCodingMachine\TDBM\DuplicateRowException |
720
|
|
|
* |
721
|
|
|
* @throws DuplicateRowException |
722
|
|
|
*/ |
723
|
|
|
public function testFindObjectFromSqlException() |
724
|
|
|
{ |
725
|
|
|
$this->tdbmService->findObjectFromSql( |
726
|
|
|
'roles', |
727
|
|
|
'roles JOIN roles_rights ON roles.id = roles_rights.role_id JOIN rights ON rights.label = roles_rights.right_label', |
728
|
|
|
'rights.label = :right', |
729
|
|
|
array('right' => 'CAN_SING') |
730
|
|
|
); |
731
|
|
|
} |
732
|
|
|
|
733
|
|
|
public function testFindObjectsFromSqlHierarchyDown() |
734
|
|
|
{ |
735
|
|
|
$users = $this->tdbmService->findObjectsFromSql( |
736
|
|
|
'person', |
737
|
|
|
'person', |
738
|
|
|
'name LIKE :name OR name LIKE :name2', |
739
|
|
|
array('name' => 'Robert Marley', 'name2' => 'Bill Shakespeare'), |
740
|
|
|
null, |
741
|
|
|
null, |
742
|
|
|
TDBMObject::class |
743
|
|
|
); |
744
|
|
|
$this->assertCount(2, $users); |
745
|
|
|
$this->assertSame('robert.marley', $users[0]->getProperty('login', 'users')); |
746
|
|
|
} |
747
|
|
|
|
748
|
|
|
public function testFindObjectsFromSqlHierarchyUp() |
749
|
|
|
{ |
750
|
|
|
$users = $this->tdbmService->findObjectsFromSql( |
751
|
|
|
'users', |
752
|
|
|
'users', |
753
|
|
|
'login LIKE :login OR login LIKE :login2', |
754
|
|
|
array('login' => 'robert.marley', 'login2' => 'bill.shakespeare'), |
755
|
|
|
null, |
756
|
|
|
null, |
757
|
|
|
TDBMObject::class |
758
|
|
|
); |
759
|
|
|
$this->assertCount(2, $users); |
760
|
|
|
$this->assertSame('Robert Marley', $users[0]->getProperty('name', 'person')); |
761
|
|
|
} |
762
|
|
|
|
763
|
|
|
public function testLogger() |
764
|
|
|
{ |
765
|
|
|
$arrayLogger = new ArrayLogger(); |
766
|
|
|
$tdbmService = new TDBMService(new Configuration('TheCodingMachine\\TDBM\\Test\\Dao\\Bean', 'TheCodingMachine\\TDBM\\Test\\Dao', self::getConnection(), $this->getNamingStrategy(), null, null, $arrayLogger)); |
767
|
|
|
|
768
|
|
|
$tdbmService->setLogLevel(LogLevel::DEBUG); |
769
|
|
|
$beans = $tdbmService->findObjects('contact', null, [], 'contact.id ASC', [], null, TDBMObject::class); |
770
|
|
|
$beans->first(); |
771
|
|
|
|
772
|
|
|
$this->assertNotEmpty($arrayLogger->get()); |
773
|
|
|
} |
774
|
|
|
|
775
|
|
|
public function testFindObjectsCountWithOneToManyLink() |
776
|
|
|
{ |
777
|
|
|
$countries = $this->tdbmService->findObjects('country', "users.status = 'on' OR users.status = 'off'"); |
778
|
|
|
|
779
|
|
|
$this->assertEquals(3, $countries->count()); |
780
|
|
|
} |
781
|
|
|
|
782
|
|
|
public function testFindObjectsFromSqlCountWithOneToManyLink() |
783
|
|
|
{ |
784
|
|
|
$countries = $this->tdbmService->findObjectsFromSql('country', 'country LEFT JOIN users ON country.id = users.country_id', "users.status = 'on' OR users.status = 'off'"); |
785
|
|
|
|
786
|
|
|
$this->assertEquals(3, $countries->count()); |
787
|
|
|
} |
788
|
|
|
} |
789
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.