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