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