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