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 Doctrine\Common\Cache\ArrayCache; |
25
|
|
|
use Doctrine\DBAL\Exception\ForeignKeyConstraintViolationException; |
26
|
|
|
use Doctrine\DBAL\Exception\UniqueConstraintViolationException; |
27
|
|
|
use Mouf\Database\SchemaAnalyzer\SchemaAnalyzer; |
28
|
|
|
use Ramsey\Uuid\Uuid; |
29
|
|
|
use TheCodingMachine\TDBM\Dao\TestArticleDao; |
30
|
|
|
use TheCodingMachine\TDBM\Dao\TestCountryDao; |
31
|
|
|
use TheCodingMachine\TDBM\Dao\TestRoleDao; |
32
|
|
|
use TheCodingMachine\TDBM\Dao\TestUserDao; |
33
|
|
|
use TheCodingMachine\TDBM\Test\Dao\AllNullableDao; |
|
|
|
|
34
|
|
|
use TheCodingMachine\TDBM\Test\Dao\AnimalDao; |
|
|
|
|
35
|
|
|
use TheCodingMachine\TDBM\Test\Dao\Bean\AllNullableBean; |
|
|
|
|
36
|
|
|
use TheCodingMachine\TDBM\Test\Dao\Bean\Article2Bean; |
|
|
|
|
37
|
|
|
use TheCodingMachine\TDBM\Test\Dao\Bean\ArticleBean; |
|
|
|
|
38
|
|
|
use TheCodingMachine\TDBM\Test\Dao\Bean\BoatBean; |
|
|
|
|
39
|
|
|
use TheCodingMachine\TDBM\Test\Dao\Bean\CatBean; |
|
|
|
|
40
|
|
|
use TheCodingMachine\TDBM\Test\Dao\Bean\CategoryBean; |
|
|
|
|
41
|
|
|
use TheCodingMachine\TDBM\Test\Dao\Bean\CountryBean; |
|
|
|
|
42
|
|
|
use TheCodingMachine\TDBM\Test\Dao\Bean\DogBean; |
|
|
|
|
43
|
|
|
use TheCodingMachine\TDBM\Test\Dao\Bean\FileBean; |
|
|
|
|
44
|
|
|
use TheCodingMachine\TDBM\Test\Dao\Bean\Generated\BoatBaseBean; |
|
|
|
|
45
|
|
|
use TheCodingMachine\TDBM\Test\Dao\Bean\Generated\UserBaseBean; |
|
|
|
|
46
|
|
|
use TheCodingMachine\TDBM\Test\Dao\Bean\PersonBean; |
|
|
|
|
47
|
|
|
use TheCodingMachine\TDBM\Test\Dao\Bean\RefNoPrimKeyBean; |
|
|
|
|
48
|
|
|
use TheCodingMachine\TDBM\Test\Dao\Bean\RoleBean; |
|
|
|
|
49
|
|
|
use TheCodingMachine\TDBM\Test\Dao\Bean\StateBean; |
|
|
|
|
50
|
|
|
use TheCodingMachine\TDBM\Test\Dao\Bean\UserBean; |
|
|
|
|
51
|
|
|
use TheCodingMachine\TDBM\Test\Dao\BoatDao; |
|
|
|
|
52
|
|
|
use TheCodingMachine\TDBM\Test\Dao\CatDao; |
|
|
|
|
53
|
|
|
use TheCodingMachine\TDBM\Test\Dao\CategoryDao; |
|
|
|
|
54
|
|
|
use TheCodingMachine\TDBM\Test\Dao\ContactDao; |
|
|
|
|
55
|
|
|
use TheCodingMachine\TDBM\Test\Dao\CountryDao; |
|
|
|
|
56
|
|
|
use TheCodingMachine\TDBM\Test\Dao\DogDao; |
|
|
|
|
57
|
|
|
use TheCodingMachine\TDBM\Test\Dao\FileDao; |
|
|
|
|
58
|
|
|
use TheCodingMachine\TDBM\Test\Dao\Generated\UserBaseDao; |
|
|
|
|
59
|
|
|
use TheCodingMachine\TDBM\Test\Dao\RefNoPrimKeyDao; |
|
|
|
|
60
|
|
|
use TheCodingMachine\TDBM\Test\Dao\RoleDao; |
|
|
|
|
61
|
|
|
use TheCodingMachine\TDBM\Test\Dao\StateDao; |
|
|
|
|
62
|
|
|
use TheCodingMachine\TDBM\Test\Dao\UserDao; |
|
|
|
|
63
|
|
|
use TheCodingMachine\TDBM\Utils\PathFinder\NoPathFoundException; |
64
|
|
|
use TheCodingMachine\TDBM\Utils\TDBMDaoGenerator; |
65
|
|
|
use Symfony\Component\Process\Process; |
66
|
|
|
|
67
|
|
|
class TDBMDaoGeneratorTest extends TDBMAbstractServiceTest |
68
|
|
|
{ |
69
|
|
|
/** @var TDBMDaoGenerator $tdbmDaoGenerator */ |
70
|
|
|
protected $tdbmDaoGenerator; |
71
|
|
|
|
72
|
|
|
private $rootPath; |
73
|
|
|
|
74
|
|
|
protected function setUp() |
75
|
|
|
{ |
76
|
|
|
parent::setUp(); |
77
|
|
|
$schemaManager = $this->tdbmService->getConnection()->getSchemaManager(); |
78
|
|
|
$schemaAnalyzer = new SchemaAnalyzer($schemaManager); |
79
|
|
|
$tdbmSchemaAnalyzer = new TDBMSchemaAnalyzer($this->tdbmService->getConnection(), new ArrayCache(), $schemaAnalyzer); |
80
|
|
|
$this->tdbmDaoGenerator = new TDBMDaoGenerator($this->getConfiguration(), $tdbmSchemaAnalyzer); |
81
|
|
|
$this->rootPath = __DIR__ . '/../'; |
82
|
|
|
//$this->tdbmDaoGenerator->setComposerFile($this->rootPath.'composer.json'); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function testDaoGeneration() |
86
|
|
|
{ |
87
|
|
|
// Remove all previously generated files. |
88
|
|
|
$this->recursiveDelete($this->rootPath . 'src/Test/Dao/'); |
89
|
|
|
|
90
|
|
|
$this->tdbmDaoGenerator->generateAllDaosAndBeans(); |
91
|
|
|
|
92
|
|
|
// Let's require all files to check they are valid PHP! |
93
|
|
|
// Test the daoFactory |
94
|
|
|
require_once $this->rootPath . 'src/Test/Dao/Generated/DaoFactory.php'; |
95
|
|
|
// Test the others |
96
|
|
|
|
97
|
|
|
$beanDescriptors = $this->getDummyGeneratorListener()->getBeanDescriptors(); |
98
|
|
|
|
99
|
|
|
foreach ($beanDescriptors as $beanDescriptor) { |
100
|
|
|
$daoName = $beanDescriptor->getDaoClassName(); |
101
|
|
|
$daoBaseName = $beanDescriptor->getBaseDaoClassName(); |
102
|
|
|
$beanName = $beanDescriptor->getBeanClassName(); |
103
|
|
|
$baseBeanName = $beanDescriptor->getBaseBeanClassName(); |
104
|
|
|
require_once $this->rootPath . 'src/Test/Dao/Bean/Generated/' . $baseBeanName . '.php'; |
105
|
|
|
require_once $this->rootPath . 'src/Test/Dao/Bean/' . $beanName . '.php'; |
106
|
|
|
require_once $this->rootPath . 'src/Test/Dao/Generated/' . $daoBaseName . '.php'; |
107
|
|
|
require_once $this->rootPath . 'src/Test/Dao/' . $daoName . '.php'; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
// Check that pivot tables do not generate DAOs or beans. |
111
|
|
|
$this->assertFalse(class_exists('TheCodingMachine\\TDBM\\Test\\Dao\\RolesRightDao')); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function testGenerationException() |
115
|
|
|
{ |
116
|
|
|
$configuration = new Configuration('UnknownVendor\\Dao', 'UnknownVendor\\Bean', self::getConnection(), $this->getNamingStrategy()); |
117
|
|
|
|
118
|
|
|
$schemaManager = $this->tdbmService->getConnection()->getSchemaManager(); |
119
|
|
|
$schemaAnalyzer = new SchemaAnalyzer($schemaManager); |
120
|
|
|
$tdbmSchemaAnalyzer = new TDBMSchemaAnalyzer($this->tdbmService->getConnection(), new ArrayCache(), $schemaAnalyzer); |
121
|
|
|
$tdbmDaoGenerator = new TDBMDaoGenerator($configuration, $tdbmSchemaAnalyzer); |
122
|
|
|
$this->rootPath = __DIR__ . '/../../../../'; |
123
|
|
|
//$tdbmDaoGenerator->setComposerFile($this->rootPath.'composer.json'); |
124
|
|
|
|
125
|
|
|
$this->expectException(NoPathFoundException::class); |
126
|
|
|
$tdbmDaoGenerator->generateAllDaosAndBeans(); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Delete a file or recursively delete a directory. |
131
|
|
|
* |
132
|
|
|
* @param string $str Path to file or directory |
133
|
|
|
* @return bool |
134
|
|
|
*/ |
135
|
|
|
private function recursiveDelete(string $str): bool |
136
|
|
|
{ |
137
|
|
|
if (is_file($str)) { |
138
|
|
|
return @unlink($str); |
139
|
|
|
} elseif (is_dir($str)) { |
140
|
|
|
$scan = glob(rtrim($str, '/') . '/*'); |
141
|
|
|
foreach ($scan as $index => $path) { |
142
|
|
|
$this->recursiveDelete($path); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
return @rmdir($str); |
146
|
|
|
} |
147
|
|
|
return false; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @depends testDaoGeneration |
152
|
|
|
*/ |
153
|
|
|
public function testGetBeanClassName() |
154
|
|
|
{ |
155
|
|
|
$this->assertEquals(UserBean::class, $this->tdbmService->getBeanClassName('users')); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @depends testDaoGeneration |
160
|
|
|
*/ |
161
|
|
|
public function testGetBeanClassNameException() |
162
|
|
|
{ |
163
|
|
|
$this->expectException(TDBMInvalidArgumentException::class); |
164
|
|
|
$this->tdbmService->getBeanClassName('not_exists'); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @depends testDaoGeneration |
169
|
|
|
*/ |
170
|
|
|
public function testGeneratedGetById() |
171
|
|
|
{ |
172
|
|
|
$contactDao = new ContactDao($this->tdbmService); |
173
|
|
|
$contactBean = $contactDao->getById(1); |
174
|
|
|
$this->assertEquals(1, $contactBean->getId()); |
175
|
|
|
$this->assertInstanceOf('\\DateTimeInterface', $contactBean->getCreatedAt()); |
176
|
|
|
|
177
|
|
|
// FIXME: Question: que faire du paramètre stockage "UTC"???? |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @depends testDaoGeneration |
182
|
|
|
*/ |
183
|
|
|
public function testGeneratedGetByIdLazyLoaded() |
184
|
|
|
{ |
185
|
|
|
$roleDao = new RoleDao($this->tdbmService); |
186
|
|
|
$roleBean = $roleDao->getById(1, true); |
187
|
|
|
$this->assertEquals(1, $roleBean->getId()); |
188
|
|
|
$this->assertInstanceOf('\\DateTimeInterface', $roleBean->getCreatedAt()); |
189
|
|
|
|
190
|
|
|
$roleBean2 = $roleDao->getById(1, true); |
191
|
|
|
$this->assertTrue($roleBean === $roleBean2); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @depends testDaoGeneration |
196
|
|
|
*/ |
197
|
|
|
public function testDefaultValueOnNewBean() |
198
|
|
|
{ |
199
|
|
|
$roleBean = new RoleBean('my_role'); |
200
|
|
|
$this->assertEquals(1, $roleBean->getStatus()); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @depends testDaoGeneration |
205
|
|
|
*/ |
206
|
|
|
public function testForeignKeyInBean() |
207
|
|
|
{ |
208
|
|
|
$userDao = new UserDao($this->tdbmService); |
209
|
|
|
$userBean = $userDao->getById(1); |
210
|
|
|
$country = $userBean->getCountry(); |
211
|
|
|
|
212
|
|
|
$this->assertEquals('UK', $country->getLabel()); |
213
|
|
|
|
214
|
|
|
$userBean2 = $userDao->getById(1); |
215
|
|
|
$this->assertTrue($userBean === $userBean2); |
216
|
|
|
|
217
|
|
|
$contactDao = new ContactDao($this->tdbmService); |
218
|
|
|
$contactBean = $contactDao->getById(1); |
219
|
|
|
|
220
|
|
|
$this->assertTrue($userBean === $contactBean); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* @depends testDaoGeneration |
225
|
|
|
*/ |
226
|
|
|
public function testNewBeans() |
227
|
|
|
{ |
228
|
|
|
$countryDao = new CountryDao($this->tdbmService); |
229
|
|
|
$userDao = new UserDao($this->tdbmService); |
230
|
|
|
$userBean = new UserBean('John Doe', '[email protected]', $countryDao->getById(2), 'john.doe'); |
231
|
|
|
$userBean->setOrder(1); // Let's set a "protected keyword" column. |
232
|
|
|
|
233
|
|
|
$userDao->save($userBean); |
234
|
|
|
|
235
|
|
|
$this->assertNull($userBean->getManager()); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* @depends testDaoGeneration |
240
|
|
|
*/ |
241
|
|
|
public function testDateTimeImmutableGetter() |
242
|
|
|
{ |
243
|
|
|
$userDao = new UserDao($this->tdbmService); |
244
|
|
|
$user = $userDao->getById(1); |
245
|
|
|
|
246
|
|
|
$this->assertInstanceOf('\DateTimeImmutable', $user->getCreatedAt()); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* @depends testDaoGeneration |
251
|
|
|
*/ |
252
|
|
|
public function testAssigningNewBeans() |
253
|
|
|
{ |
254
|
|
|
$userDao = new UserDao($this->tdbmService); |
255
|
|
|
$countryBean = new CountryBean('Mexico'); |
256
|
|
|
$userBean = new UserBean('Speedy Gonzalez', '[email protected]', $countryBean, 'speedy.gonzalez'); |
257
|
|
|
$this->assertEquals($countryBean, $userBean->getCountry()); |
258
|
|
|
|
259
|
|
|
$userDao->save($userBean); |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* @depends testAssigningNewBeans |
264
|
|
|
*/ |
265
|
|
|
public function testUpdatingProtectedColumn() |
266
|
|
|
{ |
267
|
|
|
$userDao = new UserDao($this->tdbmService); |
268
|
|
|
$userBean = $userDao->findOneByLogin('speedy.gonzalez'); |
269
|
|
|
$userBean->setOrder(2); |
270
|
|
|
$userDao->save($userBean); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* @depends testDaoGeneration |
275
|
|
|
*/ |
276
|
|
|
public function testAssigningExistingRelationship() |
277
|
|
|
{ |
278
|
|
|
$userDao = new UserDao($this->tdbmService); |
279
|
|
|
$user = $userDao->getById(1); |
280
|
|
|
$countryDao = new CountryDao($this->tdbmService); |
281
|
|
|
$country = $countryDao->getById(2); |
282
|
|
|
|
283
|
|
|
$user->setCountry($country); |
284
|
|
|
$this->assertEquals(TDBMObjectStateEnum::STATE_DIRTY, $user->_getStatus()); |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* @depends testDaoGeneration |
289
|
|
|
*/ |
290
|
|
|
public function testDirectReversedRelationship() |
291
|
|
|
{ |
292
|
|
|
$countryDao = new CountryDao($this->tdbmService); |
293
|
|
|
$country = $countryDao->getById(1); |
294
|
|
|
$users = $country->getUsers(); |
295
|
|
|
|
296
|
|
|
$arr = $users->toArray(); |
297
|
|
|
|
298
|
|
|
$this->assertCount(1, $arr); |
299
|
|
|
$this->assertInstanceOf('TheCodingMachine\\TDBM\\Test\\Dao\\Bean\\UserBean', $arr[0]); |
300
|
|
|
$this->assertEquals('jean.dupont', $arr[0]->getLogin()); |
301
|
|
|
|
302
|
|
|
$newUser = new UserBean('Speedy Gonzalez', '[email protected]', $country, 'speedy.gonzalez'); |
|
|
|
|
303
|
|
|
$users = $country->getUsers(); |
304
|
|
|
|
305
|
|
|
$arr = $users->toArray(); |
306
|
|
|
|
307
|
|
|
$this->assertCount(2, $arr); |
308
|
|
|
$this->assertInstanceOf('TheCodingMachine\\TDBM\\Test\\Dao\\Bean\\UserBean', $arr[1]); |
309
|
|
|
$this->assertEquals('speedy.gonzalez', $arr[1]->getLogin()); |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* @depends testDaoGeneration |
314
|
|
|
*/ |
315
|
|
|
public function testDeleteInDirectReversedRelationship() |
316
|
|
|
{ |
317
|
|
|
$countryDao = new CountryDao($this->tdbmService); |
318
|
|
|
$country = $countryDao->getById(1); |
319
|
|
|
|
320
|
|
|
$userDao = new UserDao($this->tdbmService); |
321
|
|
|
$newUser = new UserBean('John Snow', '[email protected]', $country, 'john.snow'); |
322
|
|
|
$userDao->save($newUser); |
323
|
|
|
|
324
|
|
|
$users = $country->getUsers(); |
325
|
|
|
|
326
|
|
|
$arr = $users->toArray(); |
327
|
|
|
|
328
|
|
|
$this->assertCount(2, $arr); |
329
|
|
|
|
330
|
|
|
$userDao->delete($arr[1]); |
331
|
|
|
|
332
|
|
|
$users = $country->getUsers(); |
333
|
|
|
$arr = $users->toArray(); |
334
|
|
|
$this->assertCount(1, $arr); |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
/** |
338
|
|
|
* @depends testDaoGeneration |
339
|
|
|
*/ |
340
|
|
|
public function testJointureGetters() |
341
|
|
|
{ |
342
|
|
|
$roleDao = new RoleDao($this->tdbmService); |
343
|
|
|
$role = $roleDao->getById(1); |
344
|
|
|
$users = $role->getUsers(); |
345
|
|
|
|
346
|
|
|
$this->assertCount(2, $users); |
347
|
|
|
$this->assertInstanceOf('TheCodingMachine\\TDBM\\Test\\Dao\\Bean\\UserBean', $users[0]); |
348
|
|
|
|
349
|
|
|
$rights = $role->getRights(); |
350
|
|
|
|
351
|
|
|
$this->assertCount(2, $rights); |
352
|
|
|
$this->assertInstanceOf('TheCodingMachine\\TDBM\\Test\\Dao\\Bean\\RightBean', $rights[0]); |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
/** |
356
|
|
|
* @depends testDaoGeneration |
357
|
|
|
*/ |
358
|
|
|
public function testNestedIterationOnAlterableResultIterator() |
359
|
|
|
{ |
360
|
|
|
$countryDao = new CountryDao($this->tdbmService); |
361
|
|
|
$country = $countryDao->getById(2); |
362
|
|
|
|
363
|
|
|
$count = 0; |
364
|
|
|
// Let's perform 2 nested calls to check that iterators do not melt. |
365
|
|
|
foreach ($country->getUsers() as $user) { |
366
|
|
|
foreach ($country->getUsers() as $user2) { |
367
|
|
|
$count++; |
368
|
|
|
} |
369
|
|
|
} |
370
|
|
|
// There are 3 users linked to country 2. |
371
|
|
|
$this->assertSame(9, $count); |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
/** |
375
|
|
|
* @depends testDaoGeneration |
376
|
|
|
*/ |
377
|
|
|
public function testNewBeanConstructor() |
378
|
|
|
{ |
379
|
|
|
$role = new RoleBean('Newrole'); |
380
|
|
|
$this->assertEquals(TDBMObjectStateEnum::STATE_DETACHED, $role->_getStatus()); |
381
|
|
|
} |
382
|
|
|
|
383
|
|
|
/** |
384
|
|
|
* @depends testDaoGeneration |
385
|
|
|
*/ |
386
|
|
|
public function testJointureAdderOnNewBean() |
387
|
|
|
{ |
388
|
|
|
$countryDao = new CountryDao($this->tdbmService); |
389
|
|
|
$country = $countryDao->getById(1); |
390
|
|
|
$user = new UserBean('Speedy Gonzalez', '[email protected]', $country, 'speedy.gonzalez'); |
391
|
|
|
$role = new RoleBean('Mouse'); |
392
|
|
|
$user->addRole($role); |
393
|
|
|
$roles = $user->getRoles(); |
394
|
|
|
$this->assertCount(1, $roles); |
395
|
|
|
$role = $roles[0]; |
396
|
|
|
$this->assertInstanceOf('TheCodingMachine\\TDBM\\Test\\Dao\\Bean\\RoleBean', $role); |
397
|
|
|
$users = $role->getUsers(); |
398
|
|
|
$this->assertCount(1, $users); |
399
|
|
|
$this->assertEquals($user, $users[0]); |
400
|
|
|
|
401
|
|
|
$role->removeUser($user); |
402
|
|
|
$this->assertCount(0, $role->getUsers()); |
403
|
|
|
$this->assertCount(0, $user->getRoles()); |
404
|
|
|
} |
405
|
|
|
|
406
|
|
|
/** |
407
|
|
|
* @depends testDaoGeneration |
408
|
|
|
*/ |
409
|
|
|
public function testJointureDeleteBeforeGetters() |
410
|
|
|
{ |
411
|
|
|
$roleDao = new RoleDao($this->tdbmService); |
412
|
|
|
$userDao = new UserDao($this->tdbmService); |
413
|
|
|
$role = $roleDao->getById(1); |
414
|
|
|
$user = $userDao->getById(1); |
415
|
|
|
|
416
|
|
|
// We call removeUser BEFORE calling getUsers |
417
|
|
|
// This should work as expected. |
418
|
|
|
$role->removeUser($user); |
419
|
|
|
$users = $role->getUsers(); |
420
|
|
|
|
421
|
|
|
$this->assertCount(1, $users); |
422
|
|
|
} |
423
|
|
|
|
424
|
|
|
/** |
425
|
|
|
* @depends testDaoGeneration |
426
|
|
|
*/ |
427
|
|
|
public function testJointureMultiAdd() |
428
|
|
|
{ |
429
|
|
|
$countryDao = new CountryDao($this->tdbmService); |
430
|
|
|
$country = $countryDao->getById(1); |
431
|
|
|
$user = new UserBean('Speedy Gonzalez', '[email protected]', $country, 'speedy.gonzalez'); |
432
|
|
|
$role = new RoleBean('Mouse'); |
433
|
|
|
$user->addRole($role); |
434
|
|
|
$role->addUser($user); |
435
|
|
|
$user->addRole($role); |
436
|
|
|
|
437
|
|
|
$this->assertCount(1, $user->getRoles()); |
438
|
|
|
} |
439
|
|
|
|
440
|
|
|
/** |
441
|
|
|
* Step 1: we remove the role 1 from user 1 but save role 1. |
442
|
|
|
* Expected result: no save done. |
443
|
|
|
* |
444
|
|
|
* @depends testDaoGeneration |
445
|
|
|
*/ |
446
|
|
|
public function testJointureSave1() |
447
|
|
|
{ |
448
|
|
|
$roleDao = new RoleDao($this->tdbmService); |
449
|
|
|
$role = $roleDao->getById(1); |
450
|
|
|
$userDao = new UserDao($this->tdbmService); |
451
|
|
|
$user = $userDao->getById(1); |
452
|
|
|
|
453
|
|
|
$this->assertTrue($user->hasRole($role)); |
454
|
|
|
$this->assertTrue($role->hasUser($user)); |
455
|
|
|
$user->removeRole($role); |
456
|
|
|
$this->assertFalse($user->hasRole($role)); |
457
|
|
|
$this->assertFalse($role->hasUser($user)); |
458
|
|
|
$roleDao->save($role); |
459
|
|
|
|
460
|
|
|
$this->assertEquals(TDBMObjectStateEnum::STATE_DIRTY, $user->_getStatus()); |
461
|
|
|
$this->assertEquals(TDBMObjectStateEnum::STATE_LOADED, $role->_getStatus()); |
462
|
|
|
} |
463
|
|
|
|
464
|
|
|
/** |
465
|
|
|
* Step 2: we check that nothing was saved |
466
|
|
|
* Expected result: no save done. |
467
|
|
|
* |
468
|
|
|
* @depends testJointureSave1 |
469
|
|
|
*/ |
470
|
|
|
public function testJointureSave2() |
471
|
|
|
{ |
472
|
|
|
$roleDao = new RoleDao($this->tdbmService); |
473
|
|
|
$role = $roleDao->getById(1); |
474
|
|
|
$this->assertCount(2, $role->getUsers()); |
475
|
|
|
} |
476
|
|
|
|
477
|
|
|
/** |
478
|
|
|
* Step 3: we remove the role 1 from user 1 and save user 1. |
479
|
|
|
* Expected result: save done. |
480
|
|
|
* |
481
|
|
|
* @depends testJointureSave2 |
482
|
|
|
*/ |
483
|
|
|
public function testJointureSave3() |
484
|
|
|
{ |
485
|
|
|
$roleDao = new RoleDao($this->tdbmService); |
486
|
|
|
$role = $roleDao->getById(1); |
487
|
|
|
$userDao = new UserDao($this->tdbmService); |
488
|
|
|
$user = $userDao->getById(1); |
489
|
|
|
|
490
|
|
|
$this->assertCount(1, $user->getRoles()); |
491
|
|
|
$user->removeRole($role); |
492
|
|
|
$this->assertCount(0, $user->getRoles()); |
493
|
|
|
$userDao->save($user); |
494
|
|
|
$this->assertCount(0, $user->getRoles()); |
495
|
|
|
} |
496
|
|
|
|
497
|
|
|
/** |
498
|
|
|
* Step 4: we check that save was done |
499
|
|
|
* Expected result: save done. |
500
|
|
|
* |
501
|
|
|
* @depends testJointureSave3 |
502
|
|
|
*/ |
503
|
|
|
public function testJointureSave4() |
504
|
|
|
{ |
505
|
|
|
$roleDao = new RoleDao($this->tdbmService); |
506
|
|
|
$role = $roleDao->getById(1); |
507
|
|
|
$this->assertCount(1, $role->getUsers()); |
508
|
|
|
$userDao = new UserDao($this->tdbmService); |
509
|
|
|
$user = $userDao->getById(1); |
510
|
|
|
$this->assertCount(0, $user->getRoles()); |
511
|
|
|
} |
512
|
|
|
|
513
|
|
|
/** |
514
|
|
|
* Step 5: we add the role 1 from user 1 and save user 1. |
515
|
|
|
* Expected result: save done. |
516
|
|
|
* |
517
|
|
|
* @depends testJointureSave4 |
518
|
|
|
*/ |
519
|
|
|
public function testJointureSave5() |
520
|
|
|
{ |
521
|
|
|
$roleDao = new RoleDao($this->tdbmService); |
522
|
|
|
$role = $roleDao->getById(1); |
523
|
|
|
$userDao = new UserDao($this->tdbmService); |
524
|
|
|
$user = $userDao->getById(1); |
525
|
|
|
|
526
|
|
|
$user->addRole($role); |
527
|
|
|
$this->assertCount(1, $user->getRoles()); |
528
|
|
|
$userDao->save($user); |
529
|
|
|
} |
530
|
|
|
|
531
|
|
|
/** |
532
|
|
|
* Step 6: we check that save was done |
533
|
|
|
* Expected result: save done. |
534
|
|
|
* |
535
|
|
|
* @depends testJointureSave5 |
536
|
|
|
*/ |
537
|
|
|
public function testJointureSave6() |
538
|
|
|
{ |
539
|
|
|
$roleDao = new RoleDao($this->tdbmService); |
540
|
|
|
$role = $roleDao->getById(1); |
541
|
|
|
$this->assertCount(2, $role->getUsers()); |
542
|
|
|
$userDao = new UserDao($this->tdbmService); |
543
|
|
|
$user = $userDao->getById(1); |
544
|
|
|
$this->assertCount(1, $user->getRoles()); |
545
|
|
|
} |
546
|
|
|
|
547
|
|
|
/** |
548
|
|
|
* Step 7: we add a new role to user 1 and save user 1. |
549
|
|
|
* Expected result: save done, including the new role. |
550
|
|
|
* |
551
|
|
|
* @depends testJointureSave6 |
552
|
|
|
*/ |
553
|
|
|
public function testJointureSave7() |
554
|
|
|
{ |
555
|
|
|
$role = new RoleBean('my new role'); |
556
|
|
|
$userDao = new UserDao($this->tdbmService); |
557
|
|
|
$user = $userDao->getById(1); |
558
|
|
|
|
559
|
|
|
$user->addRole($role); |
560
|
|
|
$userDao->save($user); |
561
|
|
|
|
562
|
|
|
$this->assertEquals(TDBMObjectStateEnum::STATE_LOADED, $role->_getStatus()); |
563
|
|
|
} |
564
|
|
|
|
565
|
|
|
/** |
566
|
|
|
* Step 8: we check that save was done |
567
|
|
|
* Expected result: save done. |
568
|
|
|
* |
569
|
|
|
* @depends testJointureSave7 |
570
|
|
|
*/ |
571
|
|
|
public function testJointureSave8() |
572
|
|
|
{ |
573
|
|
|
$roleDao = new RoleDao($this->tdbmService); |
574
|
|
|
$userDao = new UserDao($this->tdbmService); |
575
|
|
|
$user = $userDao->getById(1); |
576
|
|
|
|
577
|
|
|
$roles = $user->getRoles(); |
578
|
|
|
foreach ($roles as $role) { |
579
|
|
|
if ($role->getName() === 'my new role') { |
580
|
|
|
$selectedRole = $role; |
581
|
|
|
break; |
582
|
|
|
} |
583
|
|
|
} |
584
|
|
|
$this->assertNotNull($selectedRole); |
|
|
|
|
585
|
|
|
|
586
|
|
|
$this->assertCount(2, $user->getRoles()); |
587
|
|
|
|
588
|
|
|
// Expected: relationship removed! |
589
|
|
|
$roleDao->delete($selectedRole); |
590
|
|
|
|
591
|
|
|
$this->assertCount(1, $user->getRoles()); |
592
|
|
|
} |
593
|
|
|
|
594
|
|
|
/** |
595
|
|
|
* Step 9: Let's test the setXXX method. |
596
|
|
|
* |
597
|
|
|
* @depends testJointureSave8 |
598
|
|
|
*/ |
599
|
|
|
public function testJointureSave9() |
600
|
|
|
{ |
601
|
|
|
$roleDao = new RoleDao($this->tdbmService); |
602
|
|
|
$userDao = new UserDao($this->tdbmService); |
603
|
|
|
$user = $userDao->getById(1); |
604
|
|
|
|
605
|
|
|
// At this point, user 1 is linked to role 1. |
606
|
|
|
// Let's bind it to role 2. |
607
|
|
|
$user->setRoles([$roleDao->getById(2)]); |
608
|
|
|
$userDao->save($user); |
609
|
|
|
} |
610
|
|
|
|
611
|
|
|
/** |
612
|
|
|
* Step 10: Let's check results of 9. |
613
|
|
|
* |
614
|
|
|
* @depends testJointureSave9 |
615
|
|
|
*/ |
616
|
|
|
public function testJointureSave10() |
617
|
|
|
{ |
618
|
|
|
$userDao = new UserDao($this->tdbmService); |
619
|
|
|
$user = $userDao->getById(1); |
620
|
|
|
|
621
|
|
|
$roles = $user->getRoles(); |
622
|
|
|
|
623
|
|
|
$this->assertCount(1, $roles); |
624
|
|
|
$this->assertEquals(2, $roles[0]->getId()); |
625
|
|
|
} |
626
|
|
|
|
627
|
|
|
/** |
628
|
|
|
* @depends testDaoGeneration |
629
|
|
|
*/ |
630
|
|
|
public function testFindOrderBy() |
631
|
|
|
{ |
632
|
|
|
$userDao = new TestUserDao($this->tdbmService); |
633
|
|
|
$users = $userDao->getUsersByAlphabeticalOrder(); |
634
|
|
|
|
635
|
|
|
$this->assertCount(6, $users); |
636
|
|
|
$this->assertEquals('bill.shakespeare', $users[0]->getLogin()); |
637
|
|
|
$this->assertEquals('jean.dupont', $users[1]->getLogin()); |
638
|
|
|
|
639
|
|
|
$users = $userDao->getUsersByCountryOrder(); |
640
|
|
|
$this->assertCount(6, $users); |
641
|
|
|
$countryName1 = $users[0]->getCountry()->getLabel(); |
642
|
|
|
for ($i = 1; $i < 6; $i++) { |
643
|
|
|
$countryName2 = $users[$i]->getCountry()->getLabel(); |
644
|
|
|
$this->assertLessThanOrEqual(0, strcmp($countryName1, $countryName2)); |
645
|
|
|
$countryName1 = $countryName2; |
646
|
|
|
} |
647
|
|
|
} |
648
|
|
|
|
649
|
|
|
/** |
650
|
|
|
* @depends testDaoGeneration |
651
|
|
|
*/ |
652
|
|
|
public function testFindFromSqlOrderBy() |
653
|
|
|
{ |
654
|
|
|
$userDao = new TestUserDao($this->tdbmService); |
655
|
|
|
$users = $userDao->getUsersFromSqlByAlphabeticalOrder(); |
656
|
|
|
|
657
|
|
|
$this->assertCount(6, $users); |
658
|
|
|
$this->assertEquals('bill.shakespeare', $users[0]->getLogin()); |
659
|
|
|
$this->assertEquals('jean.dupont', $users[1]->getLogin()); |
660
|
|
|
|
661
|
|
|
$users = $userDao->getUsersFromSqlByCountryOrder(); |
662
|
|
|
$this->assertCount(6, $users); |
663
|
|
|
$countryName1 = $users[0]->getCountry()->getLabel(); |
664
|
|
|
for ($i = 1; $i < 6; $i++) { |
665
|
|
|
$countryName2 = $users[$i]->getCountry()->getLabel(); |
666
|
|
|
$this->assertLessThanOrEqual(0, strcmp($countryName1, $countryName2)); |
667
|
|
|
$countryName1 = $countryName2; |
668
|
|
|
} |
669
|
|
|
} |
670
|
|
|
|
671
|
|
|
/** |
672
|
|
|
* @depends testDaoGeneration |
673
|
|
|
*/ |
674
|
|
|
public function testFindFromSqlOrderByOnInheritedBean() |
675
|
|
|
{ |
676
|
|
|
$articleDao = new TestArticleDao($this->tdbmService); |
677
|
|
|
$articles = $articleDao->getArticlesByUserLogin(); |
678
|
|
|
|
679
|
|
|
foreach ($articles as $article) { |
680
|
|
|
var_dump($article); |
|
|
|
|
681
|
|
|
} |
682
|
|
|
$this->assertCount(0, $articles); |
683
|
|
|
} |
684
|
|
|
|
685
|
|
|
|
686
|
|
|
/** |
687
|
|
|
* @depends testDaoGeneration |
688
|
|
|
*/ |
689
|
|
|
public function testFindFromSqlOrderByJoinRole() |
690
|
|
|
{ |
691
|
|
|
$roleDao = new TestRoleDao($this->tdbmService); |
692
|
|
|
$roles = $roleDao->getRolesByRightCanSing('roles.name DESC'); |
693
|
|
|
|
694
|
|
|
$this->assertCount(2, $roles); |
695
|
|
|
$this->assertEquals('Singers', $roles[0]->getName()); |
696
|
|
|
$this->assertEquals('Admins', $roles[1]->getName()); |
697
|
|
|
} |
698
|
|
|
|
699
|
|
|
/** |
700
|
|
|
* @depends testDaoGeneration |
701
|
|
|
*/ |
702
|
|
|
public function testFindFromRawSqlOrderByUserCount() |
703
|
|
|
{ |
704
|
|
|
$countryDao = new TestCountryDao($this->tdbmService); |
705
|
|
|
$countries = $countryDao->getCountriesByUserCount(); |
706
|
|
|
|
707
|
|
|
$this->assertCount(4, $countries); |
708
|
|
|
for ($i = 1; $i < count($countries); $i++) { |
|
|
|
|
709
|
|
|
$this->assertLessThanOrEqual($countries[$i - 1]->getUsers()->count(), $countries[$i]->getUsers()->count()); |
710
|
|
|
} |
711
|
|
|
} |
712
|
|
|
|
713
|
|
|
/** |
714
|
|
|
* @depends testDaoGeneration |
715
|
|
|
*/ |
716
|
|
|
public function testFindFromRawSqlWithUnion() |
717
|
|
|
{ |
718
|
|
|
$countryDao = new TestCountryDao($this->tdbmService); |
719
|
|
|
$countries = $countryDao->getCountriesUsingUnion(); |
720
|
|
|
|
721
|
|
|
$this->assertCount(2, $countries); |
722
|
|
|
$this->assertEquals(1, $countries[0]->getId()); |
723
|
|
|
} |
724
|
|
|
|
725
|
|
|
/** |
726
|
|
|
* @depends testDaoGeneration |
727
|
|
|
*/ |
728
|
|
|
public function testFindFromRawSqlWithSimpleQuery() |
729
|
|
|
{ |
730
|
|
|
$countryDao = new TestCountryDao($this->tdbmService); |
731
|
|
|
$countries = $countryDao->getCountriesUsingSimpleQuery(); |
732
|
|
|
|
733
|
|
|
$this->assertCount(1, $countries); |
734
|
|
|
$this->assertEquals(1, $countries[0]->getId()); |
735
|
|
|
} |
736
|
|
|
|
737
|
|
|
/** |
738
|
|
|
* @depends testDaoGeneration |
739
|
|
|
*/ |
740
|
|
|
public function testFindFromRawSqlWithDistinctQuery() |
741
|
|
|
{ |
742
|
|
|
$countryDao = new TestCountryDao($this->tdbmService); |
743
|
|
|
$countries = $countryDao->getCountriesUsingDistinctQuery(); |
744
|
|
|
|
745
|
|
|
$this->assertCount(1, $countries); |
746
|
|
|
$this->assertEquals(2, $countries[0]->getId()); |
747
|
|
|
} |
748
|
|
|
|
749
|
|
|
/** |
750
|
|
|
* @depends testDaoGeneration |
751
|
|
|
*/ |
752
|
|
|
public function testFindFilters() |
753
|
|
|
{ |
754
|
|
|
$userDao = new TestUserDao($this->tdbmService); |
755
|
|
|
$users = $userDao->getUsersByLoginStartingWith('bill'); |
756
|
|
|
|
757
|
|
|
$this->assertCount(1, $users); |
758
|
|
|
$this->assertEquals('bill.shakespeare', $users[0]->getLogin()); |
759
|
|
|
} |
760
|
|
|
|
761
|
|
|
/** |
762
|
|
|
* @expectedException \TheCodingMachine\TDBM\TDBMException |
763
|
|
|
* @depends testDaoGeneration |
764
|
|
|
*/ |
765
|
|
|
public function testFindMode() |
766
|
|
|
{ |
767
|
|
|
$userDao = new TestUserDao($this->tdbmService); |
768
|
|
|
$users = $userDao->getUsersByLoginStartingWith('bill', TDBMService::MODE_CURSOR); |
769
|
|
|
|
770
|
|
|
$users[0]; |
771
|
|
|
} |
772
|
|
|
|
773
|
|
|
/** |
774
|
|
|
* @depends testDaoGeneration |
775
|
|
|
*/ |
776
|
|
|
public function testFindAll() |
777
|
|
|
{ |
778
|
|
|
$userDao = new TestUserDao($this->tdbmService); |
779
|
|
|
$users = $userDao->findAll(); |
780
|
|
|
|
781
|
|
|
$this->assertCount(6, $users); |
782
|
|
|
} |
783
|
|
|
|
784
|
|
|
/** |
785
|
|
|
* @depends testDaoGeneration |
786
|
|
|
*/ |
787
|
|
|
public function testFindOne() |
788
|
|
|
{ |
789
|
|
|
$userDao = new TestUserDao($this->tdbmService); |
790
|
|
|
$user = $userDao->getUserByLogin('bill.shakespeare'); |
791
|
|
|
|
792
|
|
|
$this->assertEquals('bill.shakespeare', $user->getLogin()); |
793
|
|
|
} |
794
|
|
|
|
795
|
|
|
/** |
796
|
|
|
* @depends testDaoGeneration |
797
|
|
|
*/ |
798
|
|
|
public function testJsonEncodeBean() |
799
|
|
|
{ |
800
|
|
|
$userDao = new TestUserDao($this->tdbmService); |
801
|
|
|
$user = $userDao->getUserByLogin('bill.shakespeare'); |
802
|
|
|
|
803
|
|
|
$jsonEncoded = json_encode($user); |
804
|
|
|
$userDecoded = json_decode($jsonEncoded, true); |
805
|
|
|
|
806
|
|
|
$this->assertEquals('bill.shakespeare', $userDecoded['login']); |
807
|
|
|
|
808
|
|
|
// test serialization of dates. |
809
|
|
|
$this->assertTrue(is_string($userDecoded['createdAt'])); |
810
|
|
|
$this->assertEquals('2015-10-24', (new \DateTimeImmutable($userDecoded['createdAt']))->format('Y-m-d')); |
811
|
|
|
$this->assertNull($userDecoded['modifiedAt']); |
812
|
|
|
|
813
|
|
|
// testing many to 1 relationships |
814
|
|
|
$this->assertEquals('UK', $userDecoded['country']['label']); |
815
|
|
|
|
816
|
|
|
// testing many to many relationships |
817
|
|
|
$this->assertCount(1, $userDecoded['roles']); |
818
|
|
|
$this->assertArrayNotHasKey('users', $userDecoded['roles'][0]); |
819
|
|
|
$this->assertArrayNotHasKey('rights', $userDecoded['roles'][0]); |
820
|
|
|
} |
821
|
|
|
|
822
|
|
|
/** |
823
|
|
|
* @depends testDaoGeneration |
824
|
|
|
*/ |
825
|
|
|
public function testNullableForeignKey() |
826
|
|
|
{ |
827
|
|
|
$userDao = new TestUserDao($this->tdbmService); |
828
|
|
|
$user = $userDao->getUserByLogin('john.smith'); |
829
|
|
|
|
830
|
|
|
$this->assertNull(null, $user->getManager()); |
831
|
|
|
|
832
|
|
|
$jsonEncoded = json_encode($user); |
833
|
|
|
$userDecoded = json_decode($jsonEncoded, true); |
834
|
|
|
|
835
|
|
|
$this->assertNull(null, $userDecoded['manager']); |
836
|
|
|
} |
837
|
|
|
|
838
|
|
|
/** |
839
|
|
|
* Test that setting (and saving) objects' references (foreign keys relations) to null is working. |
840
|
|
|
* |
841
|
|
|
* @depends testDaoGeneration |
842
|
|
|
*/ |
843
|
|
|
public function testSetToNullForeignKey() |
844
|
|
|
{ |
845
|
|
|
$userDao = new TestUserDao($this->tdbmService); |
846
|
|
|
$user = $userDao->getUserByLogin('john.smith'); |
847
|
|
|
$manager = $userDao->getUserByLogin('jean.dupont'); |
848
|
|
|
|
849
|
|
|
$user->setManager($manager); |
850
|
|
|
$userDao->save($user); |
851
|
|
|
|
852
|
|
|
$user->setManager(null); |
853
|
|
|
$userDao->save($user); |
854
|
|
|
} |
855
|
|
|
|
856
|
|
|
/** |
857
|
|
|
* @depends testDaoGeneration |
858
|
|
|
* @expectedException \Mouf\Database\SchemaAnalyzer\SchemaAnalyzerTableNotFoundException |
859
|
|
|
* @expectedExceptionMessage Could not find table 'contacts'. Did you mean 'contact'? |
860
|
|
|
*/ |
861
|
|
|
public function testQueryOnWrongTableName() |
862
|
|
|
{ |
863
|
|
|
$userDao = new TestUserDao($this->tdbmService); |
864
|
|
|
$users = $userDao->getUsersWrongTableName(); |
865
|
|
|
$users->count(); |
866
|
|
|
} |
867
|
|
|
|
868
|
|
|
/** |
869
|
|
|
* @depends testDaoGeneration |
870
|
|
|
*/ |
871
|
|
|
/*public function testQueryNullForeignKey() |
872
|
|
|
{ |
873
|
|
|
$userDao = new TestUserDao($this->tdbmService); |
874
|
|
|
$users = $userDao->getUsersByManagerId(null); |
875
|
|
|
$this->assertCount(3, $users); |
876
|
|
|
}*/ |
877
|
|
|
|
878
|
|
|
/** |
879
|
|
|
* @depends testDaoGeneration |
880
|
|
|
*/ |
881
|
|
|
public function testInnerJsonEncode() |
882
|
|
|
{ |
883
|
|
|
$userDao = new TestUserDao($this->tdbmService); |
884
|
|
|
$user = $userDao->getUserByLogin('bill.shakespeare'); |
885
|
|
|
|
886
|
|
|
$jsonEncoded = json_encode(['user' => $user]); |
887
|
|
|
$msgDecoded = json_decode($jsonEncoded, true); |
888
|
|
|
|
889
|
|
|
$this->assertEquals('bill.shakespeare', $msgDecoded['user']['login']); |
890
|
|
|
} |
891
|
|
|
|
892
|
|
|
/** |
893
|
|
|
* @depends testDaoGeneration |
894
|
|
|
*/ |
895
|
|
|
public function testArrayJsonEncode() |
896
|
|
|
{ |
897
|
|
|
$userDao = new TestUserDao($this->tdbmService); |
898
|
|
|
$users = $userDao->getUsersByLoginStartingWith('bill'); |
899
|
|
|
|
900
|
|
|
$jsonEncoded = json_encode($users); |
901
|
|
|
$msgDecoded = json_decode($jsonEncoded, true); |
902
|
|
|
|
903
|
|
|
$this->assertCount(1, $msgDecoded); |
904
|
|
|
} |
905
|
|
|
|
906
|
|
|
/** |
907
|
|
|
* @depends testDaoGeneration |
908
|
|
|
*/ |
909
|
|
|
public function testCursorJsonEncode() |
910
|
|
|
{ |
911
|
|
|
$userDao = new TestUserDao($this->tdbmService); |
912
|
|
|
$users = $userDao->getUsersByLoginStartingWith('bill', TDBMService::MODE_CURSOR); |
913
|
|
|
|
914
|
|
|
$jsonEncoded = json_encode($users); |
915
|
|
|
$msgDecoded = json_decode($jsonEncoded, true); |
916
|
|
|
|
917
|
|
|
$this->assertCount(1, $msgDecoded); |
918
|
|
|
} |
919
|
|
|
|
920
|
|
|
/** |
921
|
|
|
* @depends testDaoGeneration |
922
|
|
|
*/ |
923
|
|
|
public function testPageJsonEncode() |
924
|
|
|
{ |
925
|
|
|
$userDao = new TestUserDao($this->tdbmService); |
926
|
|
|
$users = $userDao->getUsersByLoginStartingWith('bill'); |
927
|
|
|
|
928
|
|
|
$jsonEncoded = json_encode($users->take(0, 1)); |
929
|
|
|
$msgDecoded = json_decode($jsonEncoded, true); |
930
|
|
|
|
931
|
|
|
$this->assertCount(1, $msgDecoded); |
932
|
|
|
} |
933
|
|
|
|
934
|
|
|
/** |
935
|
|
|
* @depends testDaoGeneration |
936
|
|
|
*/ |
937
|
|
|
public function testFirst() |
938
|
|
|
{ |
939
|
|
|
$userDao = new TestUserDao($this->tdbmService); |
940
|
|
|
$users = $userDao->getUsersByLoginStartingWith('bill'); |
941
|
|
|
|
942
|
|
|
$bill = $users->first(); |
943
|
|
|
$this->assertEquals('bill.shakespeare', $bill->getLogin()); |
944
|
|
|
} |
945
|
|
|
|
946
|
|
|
/** |
947
|
|
|
* @depends testDaoGeneration |
948
|
|
|
*/ |
949
|
|
|
public function testFirstNull() |
950
|
|
|
{ |
951
|
|
|
$userDao = new TestUserDao($this->tdbmService); |
952
|
|
|
$users = $userDao->getUsersByLoginStartingWith('mike'); |
953
|
|
|
|
954
|
|
|
$user = $users->first(); |
955
|
|
|
$this->assertNull($user); |
956
|
|
|
} |
957
|
|
|
|
958
|
|
|
/** |
959
|
|
|
* @depends testDaoGeneration |
960
|
|
|
*/ |
961
|
|
|
public function testCloneBeanAttachedBean() |
962
|
|
|
{ |
963
|
|
|
$userDao = new TestUserDao($this->tdbmService); |
964
|
|
|
$user = $userDao->getUserByLogin('bill.shakespeare'); |
965
|
|
|
$this->assertEquals(4, $user->getId()); |
966
|
|
|
$user2 = clone $user; |
967
|
|
|
$this->assertNull($user2->getId()); |
968
|
|
|
$this->assertEquals('bill.shakespeare', $user2->getLogin()); |
969
|
|
|
$this->assertEquals('Bill Shakespeare', $user2->getName()); |
970
|
|
|
$this->assertEquals('UK', $user2->getCountry()->getLabel()); |
971
|
|
|
|
972
|
|
|
// MANY 2 MANY must be duplicated |
973
|
|
|
$this->assertEquals('Writers', $user2->getRoles()[0]->getName()); |
974
|
|
|
|
975
|
|
|
// Let's test saving this clone |
976
|
|
|
$user2->setLogin('william.shakespeare'); |
977
|
|
|
$userDao->save($user2); |
978
|
|
|
|
979
|
|
|
$user3 = $userDao->getUserByLogin('william.shakespeare'); |
980
|
|
|
$this->assertTrue($user3 === $user2); |
981
|
|
|
$userDao->delete($user3); |
982
|
|
|
|
983
|
|
|
// Finally, let's test the origin user still exists! |
984
|
|
|
$user4 = $userDao->getUserByLogin('bill.shakespeare'); |
985
|
|
|
$this->assertEquals('bill.shakespeare', $user4->getLogin()); |
986
|
|
|
} |
987
|
|
|
|
988
|
|
|
/** |
989
|
|
|
* @depends testDaoGeneration |
990
|
|
|
*/ |
991
|
|
|
public function testCloneNewBean() |
992
|
|
|
{ |
993
|
|
|
$countryDao = new CountryDao($this->tdbmService); |
994
|
|
|
$roleDao = new RoleDao($this->tdbmService); |
995
|
|
|
$role = $roleDao->getById(1); |
996
|
|
|
|
997
|
|
|
$userBean = new UserBean('John Doe', '[email protected]', $countryDao->getById(2), 'john.doe'); |
998
|
|
|
$userBean->addRole($role); |
999
|
|
|
|
1000
|
|
|
$user2 = clone $userBean; |
1001
|
|
|
|
1002
|
|
|
$this->assertNull($user2->getId()); |
1003
|
|
|
$this->assertEquals('john.doe', $user2->getLogin()); |
1004
|
|
|
$this->assertEquals('John Doe', $user2->getName()); |
1005
|
|
|
$this->assertEquals('UK', $user2->getCountry()->getLabel()); |
1006
|
|
|
|
1007
|
|
|
// MANY 2 MANY must be duplicated |
1008
|
|
|
$this->assertEquals($role->getName(), $user2->getRoles()[0]->getName()); |
1009
|
|
|
} |
1010
|
|
|
|
1011
|
|
|
/** |
1012
|
|
|
* @depends testDaoGeneration |
1013
|
|
|
*/ |
1014
|
|
|
public function testCascadeDelete() |
1015
|
|
|
{ |
1016
|
|
|
$userDao = new TestUserDao($this->tdbmService); |
1017
|
|
|
$countryDao = new CountryDao($this->tdbmService); |
1018
|
|
|
|
1019
|
|
|
$spain = new CountryBean('Spain'); |
1020
|
|
|
$sanchez = new UserBean('Manuel Sanchez', '[email protected]', $spain, 'manuel.sanchez'); |
1021
|
|
|
|
1022
|
|
|
$countryDao->save($spain); |
1023
|
|
|
$userDao->save($sanchez); |
1024
|
|
|
|
1025
|
|
|
$speedy2 = $userDao->getUserByLogin('manuel.sanchez'); |
1026
|
|
|
$this->assertTrue($sanchez === $speedy2); |
1027
|
|
|
|
1028
|
|
|
$exceptionTriggered = false; |
1029
|
|
|
try { |
1030
|
|
|
$countryDao->delete($spain); |
1031
|
|
|
} catch (ForeignKeyConstraintViolationException $e) { |
1032
|
|
|
$exceptionTriggered = true; |
1033
|
|
|
} |
1034
|
|
|
$this->assertTrue($exceptionTriggered); |
1035
|
|
|
|
1036
|
|
|
$countryDao->delete($spain, true); |
1037
|
|
|
|
1038
|
|
|
// Let's check that speed gonzalez was removed. |
1039
|
|
|
$speedy3 = $userDao->getUserByLogin('manuel.sanchez'); |
1040
|
|
|
$this->assertNull($speedy3); |
1041
|
|
|
} |
1042
|
|
|
|
1043
|
|
|
/** |
1044
|
|
|
* @depends testDaoGeneration |
1045
|
|
|
*/ |
1046
|
|
|
public function testDiscardChanges() |
1047
|
|
|
{ |
1048
|
|
|
$contactDao = new ContactDao($this->tdbmService); |
1049
|
|
|
$contactBean = $contactDao->getById(1); |
1050
|
|
|
|
1051
|
|
|
$oldName = $contactBean->getName(); |
1052
|
|
|
|
1053
|
|
|
$contactBean->setName('MyNewName'); |
1054
|
|
|
|
1055
|
|
|
$contactBean->discardChanges(); |
1056
|
|
|
|
1057
|
|
|
$this->assertEquals($oldName, $contactBean->getName()); |
1058
|
|
|
} |
1059
|
|
|
|
1060
|
|
|
/** |
1061
|
|
|
* @expectedException \TheCodingMachine\TDBM\TDBMException |
1062
|
|
|
* @depends testDaoGeneration |
1063
|
|
|
*/ |
1064
|
|
|
public function testDiscardChangesOnNewBeanFails() |
1065
|
|
|
{ |
1066
|
|
|
$person = new PersonBean('John Foo', new \DateTimeImmutable()); |
1067
|
|
|
$person->discardChanges(); |
1068
|
|
|
} |
1069
|
|
|
|
1070
|
|
|
/** |
1071
|
|
|
* @expectedException \TheCodingMachine\TDBM\TDBMException |
1072
|
|
|
* @depends testDaoGeneration |
1073
|
|
|
*/ |
1074
|
|
|
public function testDiscardChangesOnDeletedBeanFails() |
1075
|
|
|
{ |
1076
|
|
|
$userDao = new TestUserDao($this->tdbmService); |
1077
|
|
|
$countryDao = new CountryDao($this->tdbmService); |
1078
|
|
|
|
1079
|
|
|
$sanchez = new UserBean('Manuel Sanchez', '[email protected]', $countryDao->getById(1), 'manuel.sanchez'); |
1080
|
|
|
|
1081
|
|
|
$userDao->save($sanchez); |
1082
|
|
|
|
1083
|
|
|
$userDao->delete($sanchez); |
1084
|
|
|
|
1085
|
|
|
// Cannot discard changes on a bean that is already deleted. |
1086
|
|
|
$sanchez->discardChanges(); |
1087
|
|
|
} |
1088
|
|
|
|
1089
|
|
|
/** |
1090
|
|
|
* @depends testDaoGeneration |
1091
|
|
|
*/ |
1092
|
|
|
public function testUniqueIndexBasedSearch() |
1093
|
|
|
{ |
1094
|
|
|
$userDao = new UserDao($this->tdbmService); |
1095
|
|
|
$user = $userDao->findOneByLogin('bill.shakespeare'); |
1096
|
|
|
|
1097
|
|
|
$this->assertEquals('bill.shakespeare', $user->getLogin()); |
1098
|
|
|
$this->assertEquals('Bill Shakespeare', $user->getName()); |
1099
|
|
|
} |
1100
|
|
|
|
1101
|
|
|
/** |
1102
|
|
|
* @depends testDaoGeneration |
1103
|
|
|
*/ |
1104
|
|
|
public function testFindOneByRetunsNull() |
1105
|
|
|
{ |
1106
|
|
|
// Let's assert that the findOneBy... methods can return null. |
1107
|
|
|
$userDao = new UserDao($this->tdbmService); |
1108
|
|
|
$userBean = $userDao->findOneByLogin('not_exist'); |
1109
|
|
|
|
1110
|
|
|
$this->assertNull($userBean); |
1111
|
|
|
} |
1112
|
|
|
|
1113
|
|
|
/** |
1114
|
|
|
* @depends testDaoGeneration |
1115
|
|
|
*/ |
1116
|
|
|
public function testMultiColumnsIndexBasedSearch() |
1117
|
|
|
{ |
1118
|
|
|
$countryDao = new CountryDao($this->tdbmService); |
1119
|
|
|
$userDao = new UserDao($this->tdbmService); |
1120
|
|
|
$users = $userDao->findByStatusAndCountry('on', $countryDao->getById(1)); |
1121
|
|
|
|
1122
|
|
|
$this->assertEquals('jean.dupont', $users[0]->getLogin()); |
1123
|
|
|
} |
1124
|
|
|
|
1125
|
|
|
/** |
1126
|
|
|
* @depends testDaoGeneration |
1127
|
|
|
*/ |
1128
|
|
|
public function testPartialMultiColumnsIndexBasedSearch() |
1129
|
|
|
{ |
1130
|
|
|
$userDao = new UserDao($this->tdbmService); |
1131
|
|
|
$users = $userDao->findByStatusAndCountry('on'); |
1132
|
|
|
|
1133
|
|
|
$this->assertCount(2, $users); |
1134
|
|
|
} |
1135
|
|
|
|
1136
|
|
|
/** |
1137
|
|
|
* @depends testDaoGeneration |
1138
|
|
|
*/ |
1139
|
|
|
public function testCreationInNullableDate() |
1140
|
|
|
{ |
1141
|
|
|
$roleDao = new RoleDao($this->tdbmService); |
1142
|
|
|
|
1143
|
|
|
$role = new RoleBean('newbee'); |
1144
|
|
|
$roleDao->save($role); |
1145
|
|
|
|
1146
|
|
|
$this->assertNull($role->getCreatedAt()); |
1147
|
|
|
} |
1148
|
|
|
|
1149
|
|
|
/** |
1150
|
|
|
* @depends testDaoGeneration |
1151
|
|
|
*/ |
1152
|
|
|
public function testUpdateInNullableDate() |
1153
|
|
|
{ |
1154
|
|
|
$roleDao = new RoleDao($this->tdbmService); |
1155
|
|
|
|
1156
|
|
|
$role = new RoleBean('newbee'); |
1157
|
|
|
$roleDao->save($role); |
1158
|
|
|
|
1159
|
|
|
$role->setCreatedAt(null); |
1160
|
|
|
$roleDao->save($role); |
1161
|
|
|
$this->assertNull($role->getCreatedAt()); |
1162
|
|
|
} |
1163
|
|
|
|
1164
|
|
|
/** |
1165
|
|
|
* @depends testDaoGeneration |
1166
|
|
|
*/ |
1167
|
|
|
public function testFindFromSql() |
1168
|
|
|
{ |
1169
|
|
|
$roleDao = new TestRoleDao($this->tdbmService); |
1170
|
|
|
|
1171
|
|
|
$roles = $roleDao->getRolesByRightCanSing(); |
1172
|
|
|
$this->assertCount(2, $roles); |
1173
|
|
|
$this->assertInstanceOf(RoleBean::class, $roles[0]); |
1174
|
|
|
} |
1175
|
|
|
|
1176
|
|
|
/** |
1177
|
|
|
* @depends testDaoGeneration |
1178
|
|
|
*/ |
1179
|
|
|
public function testFindOneFromSql() |
1180
|
|
|
{ |
1181
|
|
|
$roleDao = new TestRoleDao($this->tdbmService); |
1182
|
|
|
|
1183
|
|
|
$role = $roleDao->getRoleByRightCanSingAndNameSinger(); |
1184
|
|
|
$this->assertInstanceOf(RoleBean::class, $role); |
1185
|
|
|
} |
1186
|
|
|
|
1187
|
|
|
/** |
1188
|
|
|
* @depends testDaoGeneration |
1189
|
|
|
*/ |
1190
|
|
|
public function testCreateEmptyExtendedBean() |
1191
|
|
|
{ |
1192
|
|
|
// This test cases checks issue https://github.com/thecodingmachine/database.tdbm/issues/92 |
1193
|
|
|
|
1194
|
|
|
$dogDao = new DogDao($this->tdbmService); |
1195
|
|
|
|
1196
|
|
|
// We are not filling no field that is part of dog table. |
1197
|
|
|
$dog = new DogBean('Youki'); |
1198
|
|
|
$dog->setOrder(1); |
1199
|
|
|
|
1200
|
|
|
$dogDao->save($dog); |
1201
|
|
|
} |
1202
|
|
|
|
1203
|
|
|
/** |
1204
|
|
|
* @depends testCreateEmptyExtendedBean |
1205
|
|
|
*/ |
1206
|
|
|
public function testFetchEmptyExtendedBean() |
1207
|
|
|
{ |
1208
|
|
|
// This test cases checks issue https://github.com/thecodingmachine/database.tdbm/issues/92 |
1209
|
|
|
|
1210
|
|
|
$animalDao = new AnimalDao($this->tdbmService); |
1211
|
|
|
|
1212
|
|
|
// We are not filling no field that is part of dog table. |
1213
|
|
|
$animalBean = $animalDao->getById(1); |
1214
|
|
|
|
1215
|
|
|
$this->assertInstanceOf(DogBean::class, $animalBean); |
1216
|
|
|
} |
1217
|
|
|
|
1218
|
|
|
/** |
1219
|
|
|
* @depends testDaoGeneration |
1220
|
|
|
*/ |
1221
|
|
|
public function testTwoBranchesHierarchy() |
1222
|
|
|
{ |
1223
|
|
|
// This test cases checks issue https://github.com/thecodingmachine/mouf/issues/131 |
1224
|
|
|
|
1225
|
|
|
$catDao = new CatDao($this->tdbmService); |
1226
|
|
|
|
1227
|
|
|
// We are not filling no field that is part of dog table. |
1228
|
|
|
$cat = new CatBean('Mew'); |
1229
|
|
|
$cat->setOrder(2); |
1230
|
|
|
|
1231
|
|
|
$catDao->save($cat); |
1232
|
|
|
} |
1233
|
|
|
|
1234
|
|
|
/** |
1235
|
|
|
* @depends testTwoBranchesHierarchy |
1236
|
|
|
*/ |
1237
|
|
|
public function testFetchTwoBranchesHierarchy() |
1238
|
|
|
{ |
1239
|
|
|
// This test cases checks issue https://github.com/thecodingmachine/mouf/issues/131 |
1240
|
|
|
|
1241
|
|
|
$animalDao = new AnimalDao($this->tdbmService); |
1242
|
|
|
|
1243
|
|
|
$animalBean = $animalDao->getById(2); |
1244
|
|
|
|
1245
|
|
|
$this->assertInstanceOf(CatBean::class, $animalBean); |
1246
|
|
|
/* @var $animalBean CatBean */ |
1247
|
|
|
$animalBean->setCutenessLevel(999); |
1248
|
|
|
|
1249
|
|
|
$animalDao->save($animalBean); |
1250
|
|
|
} |
1251
|
|
|
|
1252
|
|
|
/** |
1253
|
|
|
* @depends testDaoGeneration |
1254
|
|
|
*/ |
1255
|
|
|
public function testExceptionOnGetById() |
1256
|
|
|
{ |
1257
|
|
|
$countryDao = new CountryDao($this->tdbmService); |
1258
|
|
|
$this->expectException(\TypeError::class); |
1259
|
|
|
$countryDao->getById(null); |
1260
|
|
|
} |
1261
|
|
|
|
1262
|
|
|
/** |
1263
|
|
|
* @depends testDaoGeneration |
1264
|
|
|
*/ |
1265
|
|
|
public function testDisconnectedManyToOne() |
1266
|
|
|
{ |
1267
|
|
|
// This test cases checks issue https://github.com/thecodingmachine/database.tdbm/issues/99 |
1268
|
|
|
|
1269
|
|
|
$country = new CountryBean('Spain'); |
1270
|
|
|
|
1271
|
|
|
$user = new UserBean('John Doe', '[email protected]', $country, 'john.doe'); |
1272
|
|
|
|
1273
|
|
|
$this->assertCount(1, $country->getUsers()); |
1274
|
|
|
$this->assertSame($user, $country->getUsers()[0]); |
1275
|
|
|
} |
1276
|
|
|
|
1277
|
|
|
/** |
1278
|
|
|
* @depends testDaoGeneration |
1279
|
|
|
*/ |
1280
|
|
|
public function testOrderByExternalCol() |
1281
|
|
|
{ |
1282
|
|
|
// This test cases checks issue https://github.com/thecodingmachine/database.tdbm/issues/106 |
1283
|
|
|
|
1284
|
|
|
$userDao = new TestUserDao($this->tdbmService); |
1285
|
|
|
$users = $userDao->getUsersByCountryName(); |
1286
|
|
|
|
1287
|
|
|
$this->assertEquals('UK', $users[0]->getCountry()->getLabel()); |
1288
|
|
|
} |
1289
|
|
|
|
1290
|
|
|
/** |
1291
|
|
|
* @depends testDaoGeneration |
1292
|
|
|
*/ |
1293
|
|
|
public function testResultIteratorSort() |
1294
|
|
|
{ |
1295
|
|
|
$userDao = new UserDao($this->tdbmService); |
1296
|
|
|
$users = $userDao->findAll()->withOrder('country.label DESC'); |
1297
|
|
|
|
1298
|
|
|
$this->assertEquals('UK', $users[0]->getCountry()->getLabel()); |
1299
|
|
|
|
1300
|
|
|
$users = $users->withOrder('country.label ASC'); |
1301
|
|
|
$this->assertEquals('France', $users[0]->getCountry()->getLabel()); |
1302
|
|
|
} |
1303
|
|
|
|
1304
|
|
|
/** |
1305
|
|
|
* @depends testDaoGeneration |
1306
|
|
|
*/ |
1307
|
|
|
public function testResultIteratorWithParameters() |
1308
|
|
|
{ |
1309
|
|
|
$userDao = new TestUserDao($this->tdbmService); |
1310
|
|
|
$users = $userDao->getUsersByLoginStartingWith()->withParameters(['login' => 'bill%']); |
1311
|
|
|
$this->assertEquals('bill.shakespeare', $users[0]->getLogin()); |
|
|
|
|
1312
|
|
|
|
1313
|
|
|
$users = $users->withParameters(['login' => 'jean%']); |
1314
|
|
|
$this->assertEquals('jean.dupont', $users[0]->getLogin()); |
1315
|
|
|
} |
1316
|
|
|
|
1317
|
|
|
/** |
1318
|
|
|
* @depends testDaoGeneration |
1319
|
|
|
*/ |
1320
|
|
|
public function testOrderByExpression() |
1321
|
|
|
{ |
1322
|
|
|
$userDao = new TestUserDao($this->tdbmService); |
1323
|
|
|
$users = $userDao->getUsersByReversedCountryName(); |
1324
|
|
|
|
1325
|
|
|
$this->assertEquals('Jamaica', $users[0]->getCountry()->getLabel()); |
1326
|
|
|
} |
1327
|
|
|
|
1328
|
|
|
/** |
1329
|
|
|
* @depends testDaoGeneration |
1330
|
|
|
*/ |
1331
|
|
|
public function testOrderByException() |
1332
|
|
|
{ |
1333
|
|
|
$userDao = new TestUserDao($this->tdbmService); |
1334
|
|
|
$users = $userDao->getUsersByInvalidOrderBy(); |
1335
|
|
|
$this->expectException(TDBMInvalidArgumentException::class); |
1336
|
|
|
$user = $users[0]; |
|
|
|
|
1337
|
|
|
} |
1338
|
|
|
|
1339
|
|
|
/** |
1340
|
|
|
* @depends testDaoGeneration |
1341
|
|
|
*/ |
1342
|
|
|
public function testOrderByProtectedColumn() |
1343
|
|
|
{ |
1344
|
|
|
$animalDao = new AnimalDao($this->tdbmService); |
1345
|
|
|
$animals = $animalDao->findAll(); |
1346
|
|
|
$animals = $animals->withOrder('`order` ASC'); |
1347
|
|
|
|
1348
|
|
|
$this->assertInstanceOf(DogBean::class, $animals[0]); |
1349
|
|
|
$this->assertInstanceOf(CatBean::class, $animals[1]); |
1350
|
|
|
|
1351
|
|
|
$animals = $animals->withOrder('`order` DESC'); |
1352
|
|
|
|
1353
|
|
|
$this->assertInstanceOf(CatBean::class, $animals[0]); |
1354
|
|
|
$this->assertInstanceOf(DogBean::class, $animals[1]); |
1355
|
|
|
} |
1356
|
|
|
|
1357
|
|
|
/** |
1358
|
|
|
* @depends testDaoGeneration |
1359
|
|
|
*/ |
1360
|
|
|
public function testGetOnAllNullableValues() |
1361
|
|
|
{ |
1362
|
|
|
// Tests that a get performed on a column that has only nullable fields succeeds. |
1363
|
|
|
$allNullable = new AllNullableBean(); |
1364
|
|
|
$this->assertNull($allNullable->getId()); |
1365
|
|
|
$this->assertNull($allNullable->getLabel()); |
1366
|
|
|
$this->assertNull($allNullable->getCountry()); |
1367
|
|
|
} |
1368
|
|
|
|
1369
|
|
|
/** |
1370
|
|
|
* @depends testDaoGeneration |
1371
|
|
|
*/ |
1372
|
|
|
public function testExceptionOnMultipleInheritance() |
1373
|
|
|
{ |
1374
|
|
|
$connection = self::getConnection(); |
1375
|
|
|
self::insert($connection, 'animal', [ |
1376
|
|
|
'id' => 99, 'name' => 'Snoofield', |
1377
|
|
|
]); |
1378
|
|
|
self::insert($connection, 'dog', [ |
1379
|
|
|
'id' => 99, 'race' => 'dog', |
1380
|
|
|
]); |
1381
|
|
|
self::insert($connection, 'cat', [ |
1382
|
|
|
'id' => 99, 'cuteness_level' => 0, |
1383
|
|
|
]); |
1384
|
|
|
|
1385
|
|
|
$catched = false; |
1386
|
|
|
try { |
1387
|
|
|
$animalDao = new AnimalDao($this->tdbmService); |
1388
|
|
|
$animalDao->getById(99); |
1389
|
|
|
} catch (TDBMInheritanceException $e) { |
1390
|
|
|
$catched = true; |
1391
|
|
|
} |
1392
|
|
|
$this->assertTrue($catched, 'Exception TDBMInheritanceException was not caught'); |
1393
|
|
|
|
1394
|
|
|
self::delete($connection, 'cat', ['id' => 99]); |
1395
|
|
|
self::delete($connection, 'dog', ['id' => 99]); |
1396
|
|
|
self::delete($connection, 'animal', ['id' => 99]); |
1397
|
|
|
} |
1398
|
|
|
|
1399
|
|
|
/** |
1400
|
|
|
* @depends testDaoGeneration |
1401
|
|
|
*/ |
1402
|
|
|
public function testReferenceNotSaved() |
1403
|
|
|
{ |
1404
|
|
|
$boatDao = new BoatDao($this->tdbmService); |
1405
|
|
|
|
1406
|
|
|
$country = new CountryBean('Atlantis'); |
1407
|
|
|
$boat = new BoatBean($country, 'Titanic'); |
1408
|
|
|
|
1409
|
|
|
$boatDao->save($boat); |
1410
|
|
|
} |
1411
|
|
|
|
1412
|
|
|
/** |
1413
|
|
|
* @depends testReferenceNotSaved |
1414
|
|
|
*/ |
1415
|
|
|
public function testUniqueIndexOnForeignKeyThenScalar() |
1416
|
|
|
{ |
1417
|
|
|
$boatDao = new BoatDao($this->tdbmService); |
1418
|
|
|
$countryDao = new CountryDao($this->tdbmService); |
1419
|
|
|
|
1420
|
|
|
$countryBean = $countryDao->findOneByLabel('Atlantis'); |
1421
|
|
|
$boatBean = $boatDao->findOneByAnchorageCountryAndName($countryBean, 'Titanic'); |
1422
|
|
|
|
1423
|
|
|
$this->assertNotNull($boatBean); |
1424
|
|
|
} |
1425
|
|
|
|
1426
|
|
|
/** |
1427
|
|
|
* @depends testDaoGeneration |
1428
|
|
|
*/ |
1429
|
|
|
public function testReferenceDeleted() |
1430
|
|
|
{ |
1431
|
|
|
$countryDao = new CountryDao($this->tdbmService); |
1432
|
|
|
$boatDao = new BoatDao($this->tdbmService); |
1433
|
|
|
|
1434
|
|
|
$country = new CountryBean('Bikini Bottom'); |
1435
|
|
|
$countryDao->save($country); |
1436
|
|
|
|
1437
|
|
|
$boat = new BoatBean($country, 'Squirrel boat'); |
1438
|
|
|
$countryDao->delete($country); |
1439
|
|
|
|
1440
|
|
|
$this->expectException(TDBMMissingReferenceException::class); |
1441
|
|
|
$boatDao->save($boat); |
1442
|
|
|
} |
1443
|
|
|
|
1444
|
|
|
/** |
1445
|
|
|
* @depends testDaoGeneration |
1446
|
|
|
*/ |
1447
|
|
|
public function testCyclicReferenceWithInheritance() |
1448
|
|
|
{ |
1449
|
|
|
$userDao = new UserDao($this->tdbmService); |
1450
|
|
|
|
1451
|
|
|
$country = new CountryBean('Norrisland'); |
1452
|
|
|
$user = new UserBean('Chuck Norris', '[email protected]', $country, 'chuck.norris'); |
1453
|
|
|
|
1454
|
|
|
$user->setManager($user); |
1455
|
|
|
|
1456
|
|
|
$this->expectException(TDBMCyclicReferenceException::class); |
1457
|
|
|
$userDao->save($user); |
1458
|
|
|
} |
1459
|
|
|
|
1460
|
|
|
/** |
1461
|
|
|
* @depends testDaoGeneration |
1462
|
|
|
*/ |
1463
|
|
|
public function testCyclicReference() |
1464
|
|
|
{ |
1465
|
|
|
$categoryDao = new CategoryDao($this->tdbmService); |
1466
|
|
|
|
1467
|
|
|
$category = new CategoryBean('Root'); |
1468
|
|
|
|
1469
|
|
|
$category->setParent($category); |
1470
|
|
|
|
1471
|
|
|
$this->expectException(TDBMCyclicReferenceException::class); |
1472
|
|
|
$categoryDao->save($category); |
1473
|
|
|
} |
1474
|
|
|
|
1475
|
|
|
/** |
1476
|
|
|
* @depends testDaoGeneration |
1477
|
|
|
*/ |
1478
|
|
|
public function testCorrectTypeForPrimaryKeyAfterSave() |
1479
|
|
|
{ |
1480
|
|
|
// PosqtgreSQL does not particularly like empty inserts (i.e.: "INSERT INTO all_nullable () VALUES ()" ) |
1481
|
|
|
$this->onlyMySql(); |
1482
|
|
|
|
1483
|
|
|
$allNullableDao = new AllNullableDao($this->tdbmService); |
1484
|
|
|
$allNullable = new AllNullableBean(); |
1485
|
|
|
$allNullableDao->save($allNullable); |
1486
|
|
|
$id = $allNullable->getId(); |
1487
|
|
|
|
1488
|
|
|
$this->assertTrue(is_int($id)); |
1489
|
|
|
} |
1490
|
|
|
|
1491
|
|
|
/** |
1492
|
|
|
* @depends testDaoGeneration |
1493
|
|
|
*/ |
1494
|
|
|
public function testPSR2Compliance() |
1495
|
|
|
{ |
1496
|
|
|
$process = new Process('vendor/bin/php-cs-fixer fix src/Test/ --dry-run --diff --rules=@PSR2'); |
1497
|
|
|
$process->run(); |
1498
|
|
|
|
1499
|
|
|
// executes after the command finishes |
1500
|
|
|
if (!$process->isSuccessful()) { |
1501
|
|
|
echo $process->getOutput(); |
1502
|
|
|
$this->fail('Generated code is not PSR-2 compliant'); |
1503
|
|
|
} |
1504
|
|
|
} |
1505
|
|
|
|
1506
|
|
|
/** |
1507
|
|
|
* @depends testDaoGeneration |
1508
|
|
|
*/ |
1509
|
|
|
public function testFindOneByGeneration() |
1510
|
|
|
{ |
1511
|
|
|
$reflectionMethod = new \ReflectionMethod(UserBaseDao::class, 'findOneByLogin'); |
1512
|
|
|
$parameters = $reflectionMethod->getParameters(); |
1513
|
|
|
|
1514
|
|
|
$this->assertCount(2, $parameters); |
1515
|
|
|
$this->assertSame('login', $parameters[0]->getName()); |
1516
|
|
|
$this->assertSame('additionalTablesFetch', $parameters[1]->getName()); |
1517
|
|
|
} |
1518
|
|
|
|
1519
|
|
|
/** |
1520
|
|
|
* @depends testDaoGeneration |
1521
|
|
|
*/ |
1522
|
|
|
public function testUuid() |
1523
|
|
|
{ |
1524
|
|
|
$article = new ArticleBean('content'); |
1525
|
|
|
$this->assertSame('content', $article->getContent()); |
1526
|
|
|
$this->assertNotEmpty($article->getId()); |
1527
|
|
|
$uuid = Uuid::fromString($article->getId()); |
1528
|
|
|
$this->assertSame(1, $uuid->getVersion()); |
1529
|
|
|
} |
1530
|
|
|
|
1531
|
|
|
/** |
1532
|
|
|
* @depends testDaoGeneration |
1533
|
|
|
*/ |
1534
|
|
|
public function testUuidv4() |
1535
|
|
|
{ |
1536
|
|
|
$article = new Article2Bean('content'); |
1537
|
|
|
$this->assertSame('content', $article->getContent()); |
1538
|
|
|
$this->assertNotEmpty($article->getId()); |
1539
|
|
|
$uuid = Uuid::fromString($article->getId()); |
1540
|
|
|
$this->assertSame(4, $uuid->getVersion()); |
1541
|
|
|
} |
1542
|
|
|
|
1543
|
|
|
/** |
1544
|
|
|
* @depends testDaoGeneration |
1545
|
|
|
*/ |
1546
|
|
|
public function testTypeHintedConstructors() |
1547
|
|
|
{ |
1548
|
|
|
$userBaseBeanReflectionConstructor = new \ReflectionMethod(UserBaseBean::class, '__construct'); |
1549
|
|
|
$nameParam = $userBaseBeanReflectionConstructor->getParameters()[0]; |
1550
|
|
|
|
1551
|
|
|
$this->assertSame('string', (string)$nameParam->getType()); |
1552
|
|
|
} |
1553
|
|
|
|
1554
|
|
|
/** |
1555
|
|
|
* @depends testDaoGeneration |
1556
|
|
|
*/ |
1557
|
|
|
public function testSaveTransaction() |
1558
|
|
|
{ |
1559
|
|
|
$countryDao = new CountryDao($this->tdbmService); |
1560
|
|
|
|
1561
|
|
|
$boatDao = new BoatDao($this->tdbmService); |
1562
|
|
|
$boatBean = $boatDao->getById(1); |
1563
|
|
|
$boatBean->setName('Bismark'); |
1564
|
|
|
|
1565
|
|
|
$boatBean->getCountry(); |
1566
|
|
|
|
1567
|
|
|
// Let's insert a row without telling TDBM to trigger an error! |
1568
|
|
|
self::insert($this->getConnection(), 'sailed_countries', [ |
1569
|
|
|
'boat_id' => 1, |
1570
|
|
|
'country_id' => 2 |
1571
|
|
|
]); |
1572
|
|
|
|
1573
|
|
|
$boatBean->addCountry($countryDao->getById(2)); |
1574
|
|
|
|
1575
|
|
|
$this->expectException(UniqueConstraintViolationException::class); |
1576
|
|
|
|
1577
|
|
|
$boatDao->save($boatBean); |
1578
|
|
|
} |
1579
|
|
|
|
1580
|
|
|
/** |
1581
|
|
|
* @depends testSaveTransaction |
1582
|
|
|
*/ |
1583
|
|
|
public function testSaveTransaction2() |
1584
|
|
|
{ |
1585
|
|
|
$boatDao = new BoatDao($this->tdbmService); |
1586
|
|
|
$boatBean = $boatDao->getById(1); |
1587
|
|
|
|
1588
|
|
|
// The name should not have been saved because the transaction of the previous test should have rollbacked. |
1589
|
|
|
$this->assertNotSame('Bismark', $boatBean->getName()); |
1590
|
|
|
} |
1591
|
|
|
|
1592
|
|
|
/** |
1593
|
|
|
* @depends testDaoGeneration |
1594
|
|
|
*/ |
1595
|
|
|
public function testForeignKeyPointingToNonPrimaryKey() |
1596
|
|
|
{ |
1597
|
|
|
$dao = new RefNoPrimKeyDao($this->tdbmService); |
1598
|
|
|
$bean = $dao->getById(1); |
1599
|
|
|
|
1600
|
|
|
$this->assertSame('foo', $bean->getFrom()->getTo()); |
1601
|
|
|
|
1602
|
|
|
$newBean = new RefNoPrimKeyBean($bean, 'baz'); |
1603
|
|
|
$dao->save($newBean); |
1604
|
|
|
$this->assertSame('foo', $newBean->getFrom()->getTo()); |
1605
|
|
|
} |
1606
|
|
|
|
1607
|
|
|
/** |
1608
|
|
|
* @depends testDaoGeneration |
1609
|
|
|
*/ |
1610
|
|
|
public function testCloningUuidBean() |
1611
|
|
|
{ |
1612
|
|
|
$article = new ArticleBean('content'); |
1613
|
|
|
$this->assertNotEmpty($article->getId()); |
1614
|
|
|
$article2 = clone $article; |
1615
|
|
|
$this->assertNotEmpty($article2->getId()); |
1616
|
|
|
$this->assertNotSame($article->getId(), $article2->getId()); |
1617
|
|
|
} |
1618
|
|
|
|
1619
|
|
|
/** |
1620
|
|
|
* @depends testDaoGeneration |
1621
|
|
|
*/ |
1622
|
|
|
public function testRecursiveSave() |
1623
|
|
|
{ |
1624
|
|
|
$categoryDao = new CategoryDao($this->tdbmService); |
1625
|
|
|
|
1626
|
|
|
$root1 = new CategoryBean('Root1'); |
1627
|
|
|
$categoryDao->save($root1); |
1628
|
|
|
// Root 2 is not saved yet. |
1629
|
|
|
$root2 = new CategoryBean('Root2'); |
1630
|
|
|
$intermediate = new CategoryBean('Intermediate'); |
1631
|
|
|
$categoryDao->save($intermediate); |
1632
|
|
|
|
1633
|
|
|
// Let's switch the parent to a bean in detached state. |
1634
|
|
|
$intermediate->setParent($root2); |
1635
|
|
|
|
1636
|
|
|
// Now, let's save a new category that references the leaf category. |
1637
|
|
|
$leaf = new CategoryBean('Leaf'); |
1638
|
|
|
$leaf->setParent($intermediate); |
1639
|
|
|
$categoryDao->save($leaf); |
1640
|
|
|
} |
1641
|
|
|
|
1642
|
|
|
/** |
1643
|
|
|
* @depends testDaoGeneration |
1644
|
|
|
*/ |
1645
|
|
|
public function testBlob() |
1646
|
|
|
{ |
1647
|
|
|
$fp = fopen(__FILE__, 'r'); |
1648
|
|
|
$file = new FileBean($fp); |
1649
|
|
|
|
1650
|
|
|
$fileDao = new FileDao($this->tdbmService); |
1651
|
|
|
|
1652
|
|
|
$fileDao->save($file); |
1653
|
|
|
|
1654
|
|
|
$loadedFile = $fileDao->getById($file->getId()); |
1655
|
|
|
|
1656
|
|
|
$resource = $loadedFile->getFile(); |
1657
|
|
|
$result = fseek($resource, 0); |
1658
|
|
|
$this->assertSame(0, $result); |
1659
|
|
|
$this->assertInternalType('resource', $resource); |
1660
|
|
|
$firstLine = fgets($resource); |
1661
|
|
|
$this->assertSame("<?php\n", $firstLine); |
1662
|
|
|
} |
1663
|
|
|
|
1664
|
|
|
/** |
1665
|
|
|
* @depends testBlob |
1666
|
|
|
*/ |
1667
|
|
|
public function testReadBlob() |
1668
|
|
|
{ |
1669
|
|
|
$fileDao = new FileDao($this->tdbmService); |
1670
|
|
|
$loadedFile = $fileDao->getById(1); |
1671
|
|
|
|
1672
|
|
|
$resource = $loadedFile->getFile(); |
1673
|
|
|
$this->assertInternalType('resource', $resource); |
1674
|
|
|
$firstLine = fgets($resource); |
1675
|
|
|
$this->assertSame("<?php\n", $firstLine); |
1676
|
|
|
|
1677
|
|
|
stream_get_contents($resource); |
1678
|
|
|
|
1679
|
|
|
$loadedFile->setId($loadedFile->getId()); |
1680
|
|
|
|
1681
|
|
|
$fileDao->save($loadedFile); |
1682
|
|
|
} |
1683
|
|
|
|
1684
|
|
|
/** |
1685
|
|
|
* @depends testReadBlob |
1686
|
|
|
*/ |
1687
|
|
|
public function testReadAndSaveBlob() |
1688
|
|
|
{ |
1689
|
|
|
$fileDao = new FileDao($this->tdbmService); |
1690
|
|
|
$loadedFile = $fileDao->getById(1); |
1691
|
|
|
|
1692
|
|
|
$resource = $loadedFile->getFile(); |
1693
|
|
|
|
1694
|
|
|
$firstLine = fgets($resource); |
1695
|
|
|
$this->assertSame("<?php\n", $firstLine); |
1696
|
|
|
} |
1697
|
|
|
|
1698
|
|
|
/** |
1699
|
|
|
* @depends testDaoGeneration |
1700
|
|
|
*/ |
1701
|
|
|
public function testBlobResourceException() |
1702
|
|
|
{ |
1703
|
|
|
$this->expectException(TDBMInvalidArgumentException::class); |
1704
|
|
|
$this->expectExceptionMessage('Invalid argument passed to \'TheCodingMachine\\TDBM\\Test\\Dao\\Bean\\Generated\\FileBaseBean::setFile\'. Expecting a resource. Got a string.'); |
1705
|
|
|
new FileBean('foobar'); |
1706
|
|
|
} |
1707
|
|
|
|
1708
|
|
|
/** |
1709
|
|
|
* @depends testDaoGeneration |
1710
|
|
|
*/ |
1711
|
|
|
public function testFilterBag() |
1712
|
|
|
{ |
1713
|
|
|
$userDao = new TestUserDao($this->tdbmService); |
1714
|
|
|
$countryDao = new CountryDao($this->tdbmService); |
1715
|
|
|
|
1716
|
|
|
$country = $countryDao->getById(2); |
1717
|
|
|
|
1718
|
|
|
// Let's test filter bags by bean and filter bag with many values. |
1719
|
|
|
$users = $userDao->getUsersByComplexFilterBag($country, ['John Doe', 'Jane Doe']); |
1720
|
|
|
|
1721
|
|
|
$this->assertCount(1, $users); |
1722
|
|
|
$this->assertSame('John Doe', $users[0]->getName()); |
1723
|
|
|
} |
1724
|
|
|
|
1725
|
|
|
/** |
1726
|
|
|
* @depends testDaoGeneration |
1727
|
|
|
*/ |
1728
|
|
|
public function testDecimalIsMappedToString() |
1729
|
|
|
{ |
1730
|
|
|
$reflectionClass = new \ReflectionClass(BoatBaseBean::class); |
1731
|
|
|
$this->assertSame('string', (string) $reflectionClass->getMethod('getLength')->getReturnType()); |
1732
|
|
|
} |
1733
|
|
|
|
1734
|
|
|
/** |
1735
|
|
|
* @depends testDaoGeneration |
1736
|
|
|
*/ |
1737
|
|
|
public function testNoGetByIdOnMultiPrimaryKeys() |
1738
|
|
|
{ |
1739
|
|
|
$reflectionClass = new \ReflectionClass(StateDao::class); |
1740
|
|
|
$this->assertFalse($reflectionClass->hasMethod('getById')); |
1741
|
|
|
} |
1742
|
|
|
|
1743
|
|
|
/** |
1744
|
|
|
* @depends testDaoGeneration |
1745
|
|
|
*/ |
1746
|
|
|
public function testInsertMultiPrimaryKeysBean() |
1747
|
|
|
{ |
1748
|
|
|
$countryDao = new CountryDao($this->tdbmService); |
1749
|
|
|
|
1750
|
|
|
$country = $countryDao->getById(1); |
1751
|
|
|
|
1752
|
|
|
$stateDao = new StateDao($this->tdbmService); |
1753
|
|
|
$state = new StateBean($country, 'IDF', 'Ile de France'); |
1754
|
|
|
$stateDao->save($state); |
1755
|
|
|
|
1756
|
|
|
$this->assertSame($state, $stateDao->findAll()[0]); |
1757
|
|
|
} |
1758
|
|
|
|
1759
|
|
|
/** |
1760
|
|
|
* @depends testInsertMultiPrimaryKeysBean |
1761
|
|
|
*/ |
1762
|
|
|
public function testDeleteMultiPrimaryKeysBean() |
1763
|
|
|
{ |
1764
|
|
|
$stateDao = new StateDao($this->tdbmService); |
1765
|
|
|
|
1766
|
|
|
$state = $stateDao->findAll()[0]; |
1767
|
|
|
$stateDao->delete($state); |
1768
|
|
|
$this->assertCount(0, $stateDao->findAll()); |
1769
|
|
|
|
1770
|
|
|
} |
1771
|
|
|
} |
1772
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths