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