Passed
Pull Request — master (#50)
by David
03:57
created

testFindFromSqlOrderByOnInheritedBean()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 0
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/*
4
 Copyright (C) 2006-2014 David Négrier - THE CODING MACHINE
5
6
This program is free software; you can redistribute it and/or modify
7
it under the terms of the GNU General Public License as published by
8
the Free Software Foundation; either version 2 of the License, or
9
(at your option) any later version.
10
11
This program is distributed in the hope that it will be useful,
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
GNU General Public License for more details.
15
16
You should have received a copy of the GNU General Public License
17
along with this program; if not, write to the Free Software
18
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
*/
20
21
namespace TheCodingMachine\TDBM;
22
23
use Doctrine\Common\Cache\ArrayCache;
24
use Doctrine\DBAL\Exception\ForeignKeyConstraintViolationException;
25
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
26
use Mouf\Database\SchemaAnalyzer\SchemaAnalyzer;
27
use Ramsey\Uuid\Uuid;
28
use TheCodingMachine\TDBM\Dao\TestArticleDao;
29
use TheCodingMachine\TDBM\Dao\TestCountryDao;
30
use TheCodingMachine\TDBM\Dao\TestRoleDao;
31
use TheCodingMachine\TDBM\Dao\TestUserDao;
32
use TheCodingMachine\TDBM\Test\Dao\AllNullableDao;
33
use TheCodingMachine\TDBM\Test\Dao\AnimalDao;
34
use TheCodingMachine\TDBM\Test\Dao\Bean\AllNullableBean;
35
use TheCodingMachine\TDBM\Test\Dao\Bean\Article2Bean;
36
use TheCodingMachine\TDBM\Test\Dao\Bean\ArticleBean;
37
use TheCodingMachine\TDBM\Test\Dao\Bean\BoatBean;
38
use TheCodingMachine\TDBM\Test\Dao\Bean\CatBean;
39
use TheCodingMachine\TDBM\Test\Dao\Bean\CategoryBean;
40
use TheCodingMachine\TDBM\Test\Dao\Bean\CountryBean;
41
use TheCodingMachine\TDBM\Test\Dao\Bean\DogBean;
42
use TheCodingMachine\TDBM\Test\Dao\Bean\Generated\UserBaseBean;
43
use TheCodingMachine\TDBM\Test\Dao\Bean\PersonBean;
44
use TheCodingMachine\TDBM\Test\Dao\Bean\RoleBean;
45
use TheCodingMachine\TDBM\Test\Dao\Bean\UserBean;
46
use TheCodingMachine\TDBM\Test\Dao\BoatDao;
47
use TheCodingMachine\TDBM\Test\Dao\CatDao;
48
use TheCodingMachine\TDBM\Test\Dao\CategoryDao;
49
use TheCodingMachine\TDBM\Test\Dao\ContactDao;
50
use TheCodingMachine\TDBM\Test\Dao\CountryDao;
51
use TheCodingMachine\TDBM\Test\Dao\DogDao;
52
use TheCodingMachine\TDBM\Test\Dao\Generated\UserBaseDao;
53
use TheCodingMachine\TDBM\Test\Dao\RefNoPrimKeyDao;
54
use TheCodingMachine\TDBM\Test\Dao\RoleDao;
55
use TheCodingMachine\TDBM\Test\Dao\UserDao;
56
use TheCodingMachine\TDBM\Utils\PathFinder\NoPathFoundException;
57
use TheCodingMachine\TDBM\Utils\TDBMDaoGenerator;
58
use Symfony\Component\Process\Process;
59
60
class TDBMDaoGeneratorTest extends TDBMAbstractServiceTest
61
{
62
    /** @var TDBMDaoGenerator $tdbmDaoGenerator */
63
    protected $tdbmDaoGenerator;
64
65
    private $rootPath;
66
67
    protected function setUp()
68
    {
69
        parent::setUp();
70
        $schemaManager = $this->tdbmService->getConnection()->getSchemaManager();
71
        $schemaAnalyzer = new SchemaAnalyzer($schemaManager);
72
        $tdbmSchemaAnalyzer = new TDBMSchemaAnalyzer($this->tdbmService->getConnection(), new ArrayCache(), $schemaAnalyzer);
73
        $this->tdbmDaoGenerator = new TDBMDaoGenerator($this->getConfiguration(), $tdbmSchemaAnalyzer);
74
        $this->rootPath = __DIR__.'/../';
75
        //$this->tdbmDaoGenerator->setComposerFile($this->rootPath.'composer.json');
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
76
    }
77
78
    public function testDaoGeneration()
79
    {
80
        // Remove all previously generated files.
81
        $this->recursiveDelete($this->rootPath.'src/Test/Dao/');
82
83
        $this->tdbmDaoGenerator->generateAllDaosAndBeans();
84
85
        // Let's require all files to check they are valid PHP!
86
        // Test the daoFactory
87
        require_once $this->rootPath.'src/Test/Dao/Generated/DaoFactory.php';
88
        // Test the others
89
90
        $beanDescriptors = $this->getDummyGeneratorListener()->getBeanDescriptors();
91
92
        foreach ($beanDescriptors as $beanDescriptor) {
93
            $daoName = $beanDescriptor->getDaoClassName();
94
            $daoBaseName = $beanDescriptor->getBaseDaoClassName();
95
            $beanName = $beanDescriptor->getBeanClassName();
96
            $baseBeanName = $beanDescriptor->getBaseBeanClassName();
97
            require_once $this->rootPath.'src/Test/Dao/Bean/Generated/'.$baseBeanName.'.php';
98
            require_once $this->rootPath.'src/Test/Dao/Bean/'.$beanName.'.php';
99
            require_once $this->rootPath.'src/Test/Dao/Generated/'.$daoBaseName.'.php';
100
            require_once $this->rootPath.'src/Test/Dao/'.$daoName.'.php';
101
        }
102
103
        // Check that pivot tables do not generate DAOs or beans.
104
        $this->assertFalse(class_exists('TheCodingMachine\\TDBM\\Test\\Dao\\RolesRightDao'));
105
    }
106
107
    public function testGenerationException()
108
    {
109
        $configuration = new Configuration('UnknownVendor\\Dao', 'UnknownVendor\\Bean', self::getConnection(), $this->getNamingStrategy());
110
111
        $schemaManager = $this->tdbmService->getConnection()->getSchemaManager();
112
        $schemaAnalyzer = new SchemaAnalyzer($schemaManager);
113
        $tdbmSchemaAnalyzer = new TDBMSchemaAnalyzer($this->tdbmService->getConnection(), new ArrayCache(), $schemaAnalyzer);
114
        $tdbmDaoGenerator = new TDBMDaoGenerator($configuration, $tdbmSchemaAnalyzer);
115
        $this->rootPath = __DIR__.'/../../../../';
116
        //$tdbmDaoGenerator->setComposerFile($this->rootPath.'composer.json');
0 ignored issues
show
Unused Code Comprehensibility introduced by
73% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
117
118
        $this->expectException(NoPathFoundException::class);
119
        $tdbmDaoGenerator->generateAllDaosAndBeans();
120
    }
121
122
    /**
123
     * Delete a file or recursively delete a directory.
124
     *
125
     * @param string $str Path to file or directory
126
     * @return bool
127
     */
128
    private function recursiveDelete(string $str) : bool
129
    {
130
        if (is_file($str)) {
131
            return @unlink($str);
132
        } elseif (is_dir($str)) {
133
            $scan = glob(rtrim($str, '/') . '/*');
134
            foreach ($scan as $index => $path) {
135
                $this->recursiveDelete($path);
136
            }
137
138
            return @rmdir($str);
139
        }
140
        return false;
141
    }
142
143
    /**
144
     * @depends testDaoGeneration
145
     */
146
    public function testGetBeanClassName()
147
    {
148
        $this->assertEquals(UserBean::class, $this->tdbmService->getBeanClassName('users'));
149
    }
150
151
    /**
152
     * @depends testDaoGeneration
153
     */
154
    public function testGetBeanClassNameException()
155
    {
156
        $this->expectException(TDBMInvalidArgumentException::class);
157
        $this->tdbmService->getBeanClassName('not_exists');
158
    }
159
160
    /**
161
     * @depends testDaoGeneration
162
     */
163
    public function testGeneratedGetById()
164
    {
165
        $contactDao = new ContactDao($this->tdbmService);
166
        $contactBean = $contactDao->getById(1);
167
        $this->assertEquals(1, $contactBean->getId());
168
        $this->assertInstanceOf('\\DateTimeInterface', $contactBean->getCreatedAt());
169
170
        // FIXME: Question: que faire du paramètre stockage "UTC"????
171
    }
172
173
    /**
174
     * @depends testDaoGeneration
175
     */
176
    public function testGeneratedGetByIdLazyLoaded()
177
    {
178
        $roleDao = new RoleDao($this->tdbmService);
179
        $roleBean = $roleDao->getById(1, true);
180
        $this->assertEquals(1, $roleBean->getId());
181
        $this->assertInstanceOf('\\DateTimeInterface', $roleBean->getCreatedAt());
182
183
        $roleBean2 = $roleDao->getById(1, true);
184
        $this->assertTrue($roleBean === $roleBean2);
185
    }
186
187
    /**
188
     * @depends testDaoGeneration
189
     */
190
    public function testDefaultValueOnNewBean()
191
    {
192
        $roleBean = new RoleBean('my_role');
193
        $this->assertEquals(1, $roleBean->getStatus());
194
    }
195
196
    /**
197
     * @depends testDaoGeneration
198
     */
199 View Code Duplication
    public function testForeignKeyInBean()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
200
    {
201
        $userDao = new UserDao($this->tdbmService);
202
        $userBean = $userDao->getById(1);
203
        $country = $userBean->getCountry();
204
205
        $this->assertEquals('UK', $country->getLabel());
206
207
        $userBean2 = $userDao->getById(1);
208
        $this->assertTrue($userBean === $userBean2);
209
210
        $contactDao = new ContactDao($this->tdbmService);
211
        $contactBean = $contactDao->getById(1);
212
213
        $this->assertTrue($userBean === $contactBean);
214
    }
215
216
    /**
217
     * @depends testDaoGeneration
218
     */
219
    public function testNewBeans()
220
    {
221
        $countryDao = new CountryDao($this->tdbmService);
222
        $userDao = new UserDao($this->tdbmService);
223
        $userBean = new UserBean('John Doe', '[email protected]', $countryDao->getById(2), 'john.doe');
224
        $userBean->setOrder(1); // Let's set a "protected keyword" column.
225
226
        $userDao->save($userBean);
227
228
        $this->assertNull($userBean->getManager());
229
    }
230
231
    /**
232
     * @depends testDaoGeneration
233
     */
234
    public function testDateTimeImmutableGetter()
235
    {
236
        $userDao = new UserDao($this->tdbmService);
237
        $user = $userDao->getById(1);
238
239
        $this->assertInstanceOf('\DateTimeImmutable', $user->getCreatedAt());
240
    }
241
242
    /**
243
     * @depends testDaoGeneration
244
     */
245 View Code Duplication
    public function testAssigningNewBeans()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
246
    {
247
        $userDao = new UserDao($this->tdbmService);
248
        $countryBean = new CountryBean('Mexico');
249
        $userBean = new UserBean('Speedy Gonzalez', '[email protected]', $countryBean, 'speedy.gonzalez');
250
        $this->assertEquals($countryBean, $userBean->getCountry());
251
252
        $userDao->save($userBean);
253
    }
254
255
    /**
256
     * @depends testAssigningNewBeans
257
     */
258
    public function testUpdatingProtectedColumn()
259
    {
260
        $userDao = new UserDao($this->tdbmService);
261
        $userBean = $userDao->findOneByLogin('speedy.gonzalez');
262
        $userBean->setOrder(2);
263
        $userDao->save($userBean);
264
    }
265
266
    /**
267
     * @depends testDaoGeneration
268
     */
269 View Code Duplication
    public function testAssigningExistingRelationship()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
270
    {
271
        $userDao = new UserDao($this->tdbmService);
272
        $user = $userDao->getById(1);
273
        $countryDao = new CountryDao($this->tdbmService);
274
        $country = $countryDao->getById(2);
275
276
        $user->setCountry($country);
277
        $this->assertEquals(TDBMObjectStateEnum::STATE_DIRTY, $user->_getStatus());
278
    }
279
280
    /**
281
     * @depends testDaoGeneration
282
     */
283
    public function testDirectReversedRelationship()
284
    {
285
        $countryDao = new CountryDao($this->tdbmService);
286
        $country = $countryDao->getById(1);
287
        $users = $country->getUsers();
288
289
        $arr = $users->toArray();
290
291
        $this->assertCount(1, $arr);
292
        $this->assertInstanceOf('TheCodingMachine\\TDBM\\Test\\Dao\\Bean\\UserBean', $arr[0]);
293
        $this->assertEquals('jean.dupont', $arr[0]->getLogin());
294
295
        $newUser = new UserBean('Speedy Gonzalez', '[email protected]', $country, 'speedy.gonzalez');
0 ignored issues
show
Unused Code introduced by
$newUser is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
296
        $users = $country->getUsers();
297
298
        $arr = $users->toArray();
299
300
        $this->assertCount(2, $arr);
301
        $this->assertInstanceOf('TheCodingMachine\\TDBM\\Test\\Dao\\Bean\\UserBean', $arr[1]);
302
        $this->assertEquals('speedy.gonzalez', $arr[1]->getLogin());
303
    }
304
305
    /**
306
     * @depends testDaoGeneration
307
     */
308
    public function testDeleteInDirectReversedRelationship()
309
    {
310
        $countryDao = new CountryDao($this->tdbmService);
311
        $country = $countryDao->getById(1);
312
313
        $userDao = new UserDao($this->tdbmService);
314
        $newUser = new UserBean('John Snow', '[email protected]', $country, 'john.snow');
315
        $userDao->save($newUser);
316
317
        $users = $country->getUsers();
318
319
        $arr = $users->toArray();
320
321
        $this->assertCount(2, $arr);
322
323
        $userDao->delete($arr[1]);
324
325
        $users = $country->getUsers();
326
        $arr = $users->toArray();
327
        $this->assertCount(1, $arr);
328
    }
329
330
    /**
331
     * @depends testDaoGeneration
332
     */
333
    public function testJointureGetters()
334
    {
335
        $roleDao = new RoleDao($this->tdbmService);
336
        $role = $roleDao->getById(1);
337
        $users = $role->getUsers();
338
339
        $this->assertCount(2, $users);
340
        $this->assertInstanceOf('TheCodingMachine\\TDBM\\Test\\Dao\\Bean\\UserBean', $users[0]);
341
342
        $rights = $role->getRights();
343
344
        $this->assertCount(2, $rights);
345
        $this->assertInstanceOf('TheCodingMachine\\TDBM\\Test\\Dao\\Bean\\RightBean', $rights[0]);
346
    }
347
348
    /**
349
     * @depends testDaoGeneration
350
     */
351
    public function testNewBeanConstructor()
352
    {
353
        $role = new RoleBean('Newrole');
354
        $this->assertEquals(TDBMObjectStateEnum::STATE_DETACHED, $role->_getStatus());
355
    }
356
357
    /**
358
     * @depends testDaoGeneration
359
     */
360
    public function testJointureAdderOnNewBean()
361
    {
362
        $countryDao = new CountryDao($this->tdbmService);
363
        $country = $countryDao->getById(1);
364
        $user = new UserBean('Speedy Gonzalez', '[email protected]', $country, 'speedy.gonzalez');
365
        $role = new RoleBean('Mouse');
366
        $user->addRole($role);
367
        $roles = $user->getRoles();
368
        $this->assertCount(1, $roles);
369
        $role = $roles[0];
370
        $this->assertInstanceOf('TheCodingMachine\\TDBM\\Test\\Dao\\Bean\\RoleBean', $role);
371
        $users = $role->getUsers();
372
        $this->assertCount(1, $users);
373
        $this->assertEquals($user, $users[0]);
374
375
        $role->removeUser($user);
376
        $this->assertCount(0, $role->getUsers());
377
        $this->assertCount(0, $user->getRoles());
378
    }
379
380
    /**
381
     * @depends testDaoGeneration
382
     */
383 View Code Duplication
    public function testJointureDeleteBeforeGetters()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
384
    {
385
        $roleDao = new RoleDao($this->tdbmService);
386
        $userDao = new UserDao($this->tdbmService);
387
        $role = $roleDao->getById(1);
388
        $user = $userDao->getById(1);
389
390
        // We call removeUser BEFORE calling getUsers
391
        // This should work as expected.
392
        $role->removeUser($user);
393
        $users = $role->getUsers();
394
395
        $this->assertCount(1, $users);
396
    }
397
398
    /**
399
     * @depends testDaoGeneration
400
     */
401
    public function testJointureMultiAdd()
402
    {
403
        $countryDao = new CountryDao($this->tdbmService);
404
        $country = $countryDao->getById(1);
405
        $user = new UserBean('Speedy Gonzalez', '[email protected]', $country, 'speedy.gonzalez');
406
        $role = new RoleBean('Mouse');
407
        $user->addRole($role);
408
        $role->addUser($user);
409
        $user->addRole($role);
410
411
        $this->assertCount(1, $user->getRoles());
412
    }
413
414
    /**
415
     * Step 1: we remove the role 1 from user 1 but save role 1.
416
     * Expected result: no save done.
417
     *
418
     * @depends testDaoGeneration
419
     */
420
    public function testJointureSave1()
421
    {
422
        $roleDao = new RoleDao($this->tdbmService);
423
        $role = $roleDao->getById(1);
424
        $userDao = new UserDao($this->tdbmService);
425
        $user = $userDao->getById(1);
426
427
        $this->assertTrue($user->hasRole($role));
428
        $this->assertTrue($role->hasUser($user));
429
        $user->removeRole($role);
430
        $this->assertFalse($user->hasRole($role));
431
        $this->assertFalse($role->hasUser($user));
432
        $roleDao->save($role);
433
434
        $this->assertEquals(TDBMObjectStateEnum::STATE_DIRTY, $user->_getStatus());
435
        $this->assertEquals(TDBMObjectStateEnum::STATE_LOADED, $role->_getStatus());
436
    }
437
438
    /**
439
     * Step 2: we check that nothing was saved
440
     * Expected result: no save done.
441
     *
442
     * @depends testJointureSave1
443
     */
444
    public function testJointureSave2()
445
    {
446
        $roleDao = new RoleDao($this->tdbmService);
447
        $role = $roleDao->getById(1);
448
        $this->assertCount(2, $role->getUsers());
449
    }
450
451
    /**
452
     * Step 3: we remove the role 1 from user 1 and save user 1.
453
     * Expected result: save done.
454
     *
455
     * @depends testJointureSave2
456
     */
457
    public function testJointureSave3()
458
    {
459
        $roleDao = new RoleDao($this->tdbmService);
460
        $role = $roleDao->getById(1);
461
        $userDao = new UserDao($this->tdbmService);
462
        $user = $userDao->getById(1);
463
464
        $this->assertCount(1, $user->getRoles());
465
        $user->removeRole($role);
466
        $this->assertCount(0, $user->getRoles());
467
        $userDao->save($user);
468
        $this->assertCount(0, $user->getRoles());
469
    }
470
471
    /**
472
     * Step 4: we check that save was done
473
     * Expected result: save done.
474
     *
475
     * @depends testJointureSave3
476
     */
477 View Code Duplication
    public function testJointureSave4()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
478
    {
479
        $roleDao = new RoleDao($this->tdbmService);
480
        $role = $roleDao->getById(1);
481
        $this->assertCount(1, $role->getUsers());
482
        $userDao = new UserDao($this->tdbmService);
483
        $user = $userDao->getById(1);
484
        $this->assertCount(0, $user->getRoles());
485
    }
486
487
    /**
488
     * Step 5: we add the role 1 from user 1 and save user 1.
489
     * Expected result: save done.
490
     *
491
     * @depends testJointureSave4
492
     */
493 View Code Duplication
    public function testJointureSave5()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
494
    {
495
        $roleDao = new RoleDao($this->tdbmService);
496
        $role = $roleDao->getById(1);
497
        $userDao = new UserDao($this->tdbmService);
498
        $user = $userDao->getById(1);
499
500
        $user->addRole($role);
501
        $this->assertCount(1, $user->getRoles());
502
        $userDao->save($user);
503
    }
504
505
    /**
506
     * Step 6: we check that save was done
507
     * Expected result: save done.
508
     *
509
     * @depends testJointureSave5
510
     */
511 View Code Duplication
    public function testJointureSave6()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
512
    {
513
        $roleDao = new RoleDao($this->tdbmService);
514
        $role = $roleDao->getById(1);
515
        $this->assertCount(2, $role->getUsers());
516
        $userDao = new UserDao($this->tdbmService);
517
        $user = $userDao->getById(1);
518
        $this->assertCount(1, $user->getRoles());
519
    }
520
521
    /**
522
     * Step 7: we add a new role to user 1 and save user 1.
523
     * Expected result: save done, including the new role.
524
     *
525
     * @depends testJointureSave6
526
     */
527
    public function testJointureSave7()
528
    {
529
        $role = new RoleBean('my new role');
530
        $userDao = new UserDao($this->tdbmService);
531
        $user = $userDao->getById(1);
532
533
        $user->addRole($role);
534
        $userDao->save($user);
535
536
        $this->assertEquals(TDBMObjectStateEnum::STATE_LOADED, $role->_getStatus());
537
    }
538
539
    /**
540
     * Step 8: we check that save was done
541
     * Expected result: save done.
542
     *
543
     * @depends testJointureSave7
544
     */
545 View Code Duplication
    public function testJointureSave8()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
546
    {
547
        $roleDao = new RoleDao($this->tdbmService);
548
        $userDao = new UserDao($this->tdbmService);
549
        $user = $userDao->getById(1);
550
551
        $roles = $user->getRoles();
552
        foreach ($roles as $role) {
553
            if ($role->getName() === 'my new role') {
554
                $selectedRole = $role;
555
                break;
556
            }
557
        }
558
        $this->assertNotNull($selectedRole);
0 ignored issues
show
Bug introduced by
The variable $selectedRole does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
559
560
        $this->assertCount(2, $user->getRoles());
561
562
        // Expected: relationship removed!
563
        $roleDao->delete($selectedRole);
564
565
        $this->assertCount(1, $user->getRoles());
566
    }
567
568
    /**
569
     * Step 9: Let's test the setXXX method.
570
     *
571
     * @depends testJointureSave8
572
     */
573
    public function testJointureSave9()
574
    {
575
        $roleDao = new RoleDao($this->tdbmService);
576
        $userDao = new UserDao($this->tdbmService);
577
        $user = $userDao->getById(1);
578
579
        // At this point, user 1 is linked to role 1.
580
        // Let's bind it to role 2.
581
        $user->setRoles([$roleDao->getById(2)]);
582
        $userDao->save($user);
583
    }
584
585
    /**
586
     * Step 10: Let's check results of 9.
587
     *
588
     * @depends testJointureSave9
589
     */
590
    public function testJointureSave10()
591
    {
592
        $userDao = new UserDao($this->tdbmService);
593
        $user = $userDao->getById(1);
594
595
        $roles = $user->getRoles();
596
597
        $this->assertCount(1, $roles);
598
        $this->assertEquals(2, $roles[0]->getId());
599
    }
600
601
    /**
602
     * @depends testDaoGeneration
603
     */
604 View Code Duplication
    public function testFindOrderBy()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
605
    {
606
        $userDao = new TestUserDao($this->tdbmService);
607
        $users = $userDao->getUsersByAlphabeticalOrder();
608
609
        $this->assertCount(6, $users);
610
        $this->assertEquals('bill.shakespeare', $users[0]->getLogin());
611
        $this->assertEquals('jean.dupont', $users[1]->getLogin());
612
613
        $users = $userDao->getUsersByCountryOrder();
614
        $this->assertCount(6, $users);
615
        $countryName1 = $users[0]->getCountry()->getLabel();
616
        for ($i = 1; $i < 6; $i++) {
617
            $countryName2 = $users[$i]->getCountry()->getLabel();
618
            $this->assertLessThanOrEqual(0, strcmp($countryName1, $countryName2));
619
            $countryName1 = $countryName2;
620
        }
621
    }
622
623
    /**
624
     * @depends testDaoGeneration
625
     */
626 View Code Duplication
    public function testFindFromSqlOrderBy()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
627
    {
628
        $userDao = new TestUserDao($this->tdbmService);
629
        $users = $userDao->getUsersFromSqlByAlphabeticalOrder();
630
631
        $this->assertCount(6, $users);
632
        $this->assertEquals('bill.shakespeare', $users[0]->getLogin());
633
        $this->assertEquals('jean.dupont', $users[1]->getLogin());
634
635
        $users = $userDao->getUsersFromSqlByCountryOrder();
636
        $this->assertCount(6, $users);
637
        $countryName1 = $users[0]->getCountry()->getLabel();
638
        for ($i = 1; $i < 6; $i++) {
639
            $countryName2 = $users[$i]->getCountry()->getLabel();
640
            $this->assertLessThanOrEqual(0, strcmp($countryName1, $countryName2));
641
            $countryName1 = $countryName2;
642
        }
643
    }
644
645
    /**
646
     * @depends testDaoGeneration
647
     */
648
    public function testFindFromSqlOrderByOnInheritedBean()
649
    {
650
        $articleDao = new TestArticleDao($this->tdbmService);
651
        $articles = $articleDao->getArticlesByUserLogin();
652
653
        foreach ($articles as $article) {
654
            var_dump($article);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($article); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
655
        }
656
        $this->assertCount(0, $articles);
657
    }
658
659
660
    /**
661
     * @depends testDaoGeneration
662
     */
663
    public function testFindFromSqlOrderByJoinRole()
664
    {
665
        $roleDao = new TestRoleDao($this->tdbmService);
666
        $roles = $roleDao->getRolesByRightCanSing('roles.name DESC');
667
668
        $this->assertCount(2, $roles);
669
        $this->assertEquals('Singers', $roles[0]->getName());
670
        $this->assertEquals('Admins', $roles[1]->getName());
671
    }
672
673
    /**
674
     * @depends testDaoGeneration
675
     */
676
    public function testFindFromRawSqlOrderByUserCount()
677
    {
678
        $countryDao = new TestCountryDao($this->tdbmService);
679
        $countries = $countryDao->getCountriesByUserCount();
680
681
        $this->assertCount(4, $countries);
682
        for ($i = 1; $i < count($countries); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
683
            $this->assertLessThanOrEqual($countries[$i - 1]->getUsers()->count(), $countries[$i]->getUsers()->count());
684
        }
685
    }
686
687
    /**
688
     * @depends testDaoGeneration
689
     */
690 View Code Duplication
    public function testFindFilters()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
691
    {
692
        $userDao = new TestUserDao($this->tdbmService);
693
        $users = $userDao->getUsersByLoginStartingWith('bill');
694
695
        $this->assertCount(1, $users);
696
        $this->assertEquals('bill.shakespeare', $users[0]->getLogin());
697
    }
698
699
    /**
700
     * @expectedException \TheCodingMachine\TDBM\TDBMException
701
     * @depends testDaoGeneration
702
     */
703
    public function testFindMode()
704
    {
705
        $userDao = new TestUserDao($this->tdbmService);
706
        $users = $userDao->getUsersByLoginStartingWith('bill', TDBMService::MODE_CURSOR);
707
708
        $users[0];
709
    }
710
711
    /**
712
     * @depends testDaoGeneration
713
     */
714
    public function testFindAll()
715
    {
716
        $userDao = new TestUserDao($this->tdbmService);
717
        $users = $userDao->findAll();
718
719
        $this->assertCount(6, $users);
720
    }
721
722
    /**
723
     * @depends testDaoGeneration
724
     */
725
    public function testFindOne()
726
    {
727
        $userDao = new TestUserDao($this->tdbmService);
728
        $user = $userDao->getUserByLogin('bill.shakespeare');
729
730
        $this->assertEquals('bill.shakespeare', $user->getLogin());
731
    }
732
733
    /**
734
     * @depends testDaoGeneration
735
     */
736
    public function testJsonEncodeBean()
737
    {
738
        $userDao = new TestUserDao($this->tdbmService);
739
        $user = $userDao->getUserByLogin('bill.shakespeare');
740
741
        $jsonEncoded = json_encode($user);
742
        $userDecoded = json_decode($jsonEncoded, true);
743
744
        $this->assertEquals('bill.shakespeare', $userDecoded['login']);
745
746
        // test serialization of dates.
747
        $this->assertTrue(is_string($userDecoded['createdAt']));
748
        $this->assertEquals('2015-10-24', (new \DateTimeImmutable($userDecoded['createdAt']))->format('Y-m-d'));
749
        $this->assertNull($userDecoded['modifiedAt']);
750
751
        // testing many to 1 relationships
752
        $this->assertEquals('UK', $userDecoded['country']['label']);
753
754
        // testing many to many relationships
755
        $this->assertCount(1, $userDecoded['roles']);
756
        $this->assertArrayNotHasKey('users', $userDecoded['roles'][0]);
757
        $this->assertArrayNotHasKey('rights', $userDecoded['roles'][0]);
758
    }
759
760
    /**
761
     * @depends testDaoGeneration
762
     */
763
    public function testNullableForeignKey()
764
    {
765
        $userDao = new TestUserDao($this->tdbmService);
766
        $user = $userDao->getUserByLogin('john.smith');
767
768
        $this->assertNull(null, $user->getManager());
769
770
        $jsonEncoded = json_encode($user);
771
        $userDecoded = json_decode($jsonEncoded, true);
772
773
        $this->assertNull(null, $userDecoded['manager']);
774
    }
775
776
    /**
777
     * Test that setting (and saving) objects' references (foreign keys relations) to null is working.
778
     *
779
     * @depends testDaoGeneration
780
     */
781
    public function testSetToNullForeignKey()
782
    {
783
        $userDao = new TestUserDao($this->tdbmService);
784
        $user = $userDao->getUserByLogin('john.smith');
785
        $manager = $userDao->getUserByLogin('jean.dupont');
786
787
        $user->setManager($manager);
788
        $userDao->save($user);
789
790
        $user->setManager(null);
791
        $userDao->save($user);
792
    }
793
794
    /**
795
     * @depends testDaoGeneration
796
     * @expectedException \Mouf\Database\SchemaAnalyzer\SchemaAnalyzerTableNotFoundException
797
     * @expectedExceptionMessage Could not find table 'contacts'. Did you mean 'contact'?
798
     */
799
    public function testQueryOnWrongTableName()
800
    {
801
        $userDao = new TestUserDao($this->tdbmService);
802
        $users = $userDao->getUsersWrongTableName();
803
        $users->count();
804
    }
805
806
    /**
807
     * @depends testDaoGeneration
808
     */
809
    /*public function testQueryNullForeignKey()
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
810
    {
811
        $userDao = new TestUserDao($this->tdbmService);
812
        $users = $userDao->getUsersByManagerId(null);
813
        $this->assertCount(3, $users);
814
    }*/
815
816
    /**
817
     * @depends testDaoGeneration
818
     */
819
    public function testInnerJsonEncode()
820
    {
821
        $userDao = new TestUserDao($this->tdbmService);
822
        $user = $userDao->getUserByLogin('bill.shakespeare');
823
824
        $jsonEncoded = json_encode(['user' => $user]);
825
        $msgDecoded = json_decode($jsonEncoded, true);
826
827
        $this->assertEquals('bill.shakespeare', $msgDecoded['user']['login']);
828
    }
829
830
    /**
831
     * @depends testDaoGeneration
832
     */
833 View Code Duplication
    public function testArrayJsonEncode()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
834
    {
835
        $userDao = new TestUserDao($this->tdbmService);
836
        $users = $userDao->getUsersByLoginStartingWith('bill');
837
838
        $jsonEncoded = json_encode($users);
839
        $msgDecoded = json_decode($jsonEncoded, true);
840
841
        $this->assertCount(1, $msgDecoded);
842
    }
843
844
    /**
845
     * @depends testDaoGeneration
846
     */
847 View Code Duplication
    public function testCursorJsonEncode()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
848
    {
849
        $userDao = new TestUserDao($this->tdbmService);
850
        $users = $userDao->getUsersByLoginStartingWith('bill', TDBMService::MODE_CURSOR);
851
852
        $jsonEncoded = json_encode($users);
853
        $msgDecoded = json_decode($jsonEncoded, true);
854
855
        $this->assertCount(1, $msgDecoded);
856
    }
857
858
    /**
859
     * @depends testDaoGeneration
860
     */
861 View Code Duplication
    public function testPageJsonEncode()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
862
    {
863
        $userDao = new TestUserDao($this->tdbmService);
864
        $users = $userDao->getUsersByLoginStartingWith('bill');
865
866
        $jsonEncoded = json_encode($users->take(0, 1));
867
        $msgDecoded = json_decode($jsonEncoded, true);
868
869
        $this->assertCount(1, $msgDecoded);
870
    }
871
872
    /**
873
     * @depends testDaoGeneration
874
     */
875 View Code Duplication
    public function testFirst()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
876
    {
877
        $userDao = new TestUserDao($this->tdbmService);
878
        $users = $userDao->getUsersByLoginStartingWith('bill');
879
880
        $bill = $users->first();
881
        $this->assertEquals('bill.shakespeare', $bill->getLogin());
882
    }
883
884
    /**
885
     * @depends testDaoGeneration
886
     */
887
    public function testFirstNull()
888
    {
889
        $userDao = new TestUserDao($this->tdbmService);
890
        $users = $userDao->getUsersByLoginStartingWith('mike');
891
892
        $user = $users->first();
893
        $this->assertNull($user);
894
    }
895
896
    /**
897
     * @depends testDaoGeneration
898
     */
899
    public function testCloneBeanAttachedBean()
900
    {
901
        $userDao = new TestUserDao($this->tdbmService);
902
        $user = $userDao->getUserByLogin('bill.shakespeare');
903
        $this->assertEquals(4, $user->getId());
904
        $user2 = clone $user;
905
        $this->assertNull($user2->getId());
906
        $this->assertEquals('bill.shakespeare', $user2->getLogin());
907
        $this->assertEquals('Bill Shakespeare', $user2->getName());
908
        $this->assertEquals('UK', $user2->getCountry()->getLabel());
909
910
        // MANY 2 MANY must be duplicated
911
        $this->assertEquals('Writers', $user2->getRoles()[0]->getName());
912
913
        // Let's test saving this clone
914
        $user2->setLogin('william.shakespeare');
915
        $userDao->save($user2);
916
917
        $user3 = $userDao->getUserByLogin('william.shakespeare');
918
        $this->assertTrue($user3 === $user2);
919
        $userDao->delete($user3);
920
921
        // Finally, let's test the origin user still exists!
922
        $user4 = $userDao->getUserByLogin('bill.shakespeare');
923
        $this->assertEquals('bill.shakespeare', $user4->getLogin());
924
    }
925
926
    /**
927
     * @depends testDaoGeneration
928
     */
929
    public function testCloneNewBean()
930
    {
931
        $countryDao = new CountryDao($this->tdbmService);
932
        $roleDao = new RoleDao($this->tdbmService);
933
        $role = $roleDao->getById(1);
934
935
        $userBean = new UserBean('John Doe', '[email protected]', $countryDao->getById(2), 'john.doe');
936
        $userBean->addRole($role);
937
938
        $user2 = clone $userBean;
939
940
        $this->assertNull($user2->getId());
941
        $this->assertEquals('john.doe', $user2->getLogin());
942
        $this->assertEquals('John Doe', $user2->getName());
943
        $this->assertEquals('UK', $user2->getCountry()->getLabel());
944
945
        // MANY 2 MANY must be duplicated
946
        $this->assertEquals($role->getName(), $user2->getRoles()[0]->getName());
947
    }
948
949
    /**
950
     * @depends testDaoGeneration
951
     */
952
    public function testCascadeDelete()
953
    {
954
        $userDao = new TestUserDao($this->tdbmService);
955
        $countryDao = new CountryDao($this->tdbmService);
956
957
        $spain = new CountryBean('Spain');
958
        $sanchez = new UserBean('Manuel Sanchez', '[email protected]', $spain, 'manuel.sanchez');
959
960
        $countryDao->save($spain);
961
        $userDao->save($sanchez);
962
963
        $speedy2 = $userDao->getUserByLogin('manuel.sanchez');
964
        $this->assertTrue($sanchez === $speedy2);
965
966
        $exceptionTriggered = false;
967
        try {
968
            $countryDao->delete($spain);
969
        } catch (ForeignKeyConstraintViolationException $e) {
970
            $exceptionTriggered = true;
971
        }
972
        $this->assertTrue($exceptionTriggered);
973
974
        $countryDao->delete($spain, true);
975
976
        // Let's check that speed gonzalez was removed.
977
        $speedy3 = $userDao->getUserByLogin('manuel.sanchez');
978
        $this->assertNull($speedy3);
979
    }
980
981
    /**
982
     * @depends testDaoGeneration
983
     */
984
    public function testDiscardChanges()
985
    {
986
        $contactDao = new ContactDao($this->tdbmService);
987
        $contactBean = $contactDao->getById(1);
988
989
        $oldName = $contactBean->getName();
990
991
        $contactBean->setName('MyNewName');
992
993
        $contactBean->discardChanges();
994
995
        $this->assertEquals($oldName, $contactBean->getName());
996
    }
997
998
    /**
999
     * @expectedException \TheCodingMachine\TDBM\TDBMException
1000
     * @depends testDaoGeneration
1001
     */
1002
    public function testDiscardChangesOnNewBeanFails()
1003
    {
1004
        $person = new PersonBean('John Foo', new \DateTimeImmutable());
1005
        $person->discardChanges();
1006
    }
1007
1008
    /**
1009
     * @expectedException \TheCodingMachine\TDBM\TDBMException
1010
     * @depends testDaoGeneration
1011
     */
1012
    public function testDiscardChangesOnDeletedBeanFails()
1013
    {
1014
        $userDao = new TestUserDao($this->tdbmService);
1015
        $countryDao = new CountryDao($this->tdbmService);
1016
1017
        $sanchez = new UserBean('Manuel Sanchez', '[email protected]', $countryDao->getById(1), 'manuel.sanchez');
1018
1019
        $userDao->save($sanchez);
1020
1021
        $userDao->delete($sanchez);
1022
1023
        // Cannot discard changes on a bean that is already deleted.
1024
        $sanchez->discardChanges();
1025
    }
1026
1027
    /**
1028
     * @depends testDaoGeneration
1029
     */
1030 View Code Duplication
    public function testUniqueIndexBasedSearch()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1031
    {
1032
        $userDao = new UserDao($this->tdbmService);
1033
        $user = $userDao->findOneByLogin('bill.shakespeare');
1034
1035
        $this->assertEquals('bill.shakespeare', $user->getLogin());
1036
        $this->assertEquals('Bill Shakespeare', $user->getName());
1037
    }
1038
1039
    /**
1040
     * @depends testDaoGeneration
1041
     */
1042
    public function testFindOneByRetunsNull()
1043
    {
1044
        // Let's assert that the findOneBy... methods can return null.
1045
        $userDao = new UserDao($this->tdbmService);
1046
        $userBean = $userDao->findOneByLogin('not_exist');
1047
1048
        $this->assertNull($userBean);
1049
    }
1050
1051
    /**
1052
     * @depends testDaoGeneration
1053
     */
1054 View Code Duplication
    public function testMultiColumnsIndexBasedSearch()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1055
    {
1056
        $countryDao = new CountryDao($this->tdbmService);
1057
        $userDao = new UserDao($this->tdbmService);
1058
        $users = $userDao->findByStatusAndCountry('on', $countryDao->getById(1));
1059
1060
        $this->assertEquals('jean.dupont', $users[0]->getLogin());
1061
    }
1062
1063
    /**
1064
     * @depends testDaoGeneration
1065
     */
1066
    public function testCreationInNullableDate()
1067
    {
1068
        $roleDao = new RoleDao($this->tdbmService);
1069
1070
        $role = new RoleBean('newbee');
1071
        $roleDao->save($role);
1072
1073
        $this->assertNull($role->getCreatedAt());
1074
    }
1075
1076
    /**
1077
     * @depends testDaoGeneration
1078
     */
1079
    public function testUpdateInNullableDate()
1080
    {
1081
        $roleDao = new RoleDao($this->tdbmService);
1082
1083
        $role = new RoleBean('newbee');
1084
        $roleDao->save($role);
1085
1086
        $role->setCreatedAt(null);
1087
        $roleDao->save($role);
1088
        $this->assertNull($role->getCreatedAt());
1089
    }
1090
1091
    /**
1092
     * @depends testDaoGeneration
1093
     */
1094
    public function testFindFromSql()
1095
    {
1096
        $roleDao = new TestRoleDao($this->tdbmService);
1097
1098
        $roles = $roleDao->getRolesByRightCanSing();
1099
        $this->assertCount(2, $roles);
1100
        $this->assertInstanceOf(RoleBean::class, $roles[0]);
1101
    }
1102
1103
    /**
1104
     * @depends testDaoGeneration
1105
     */
1106
    public function testFindOneFromSql()
1107
    {
1108
        $roleDao = new TestRoleDao($this->tdbmService);
1109
1110
        $role = $roleDao->getRoleByRightCanSingAndNameSinger();
1111
        $this->assertInstanceOf(RoleBean::class, $role);
1112
    }
1113
1114
    /**
1115
     * @depends testDaoGeneration
1116
     */
1117
    public function testCreateEmptyExtendedBean()
1118
    {
1119
        // This test cases checks issue https://github.com/thecodingmachine/database.tdbm/issues/92
1120
1121
        $dogDao = new DogDao($this->tdbmService);
1122
1123
        // We are not filling no field that is part of dog table.
1124
        $dog = new DogBean('Youki');
1125
        $dog->setOrder(1);
1126
1127
        $dogDao->save($dog);
1128
    }
1129
1130
    /**
1131
     * @depends testCreateEmptyExtendedBean
1132
     */
1133
    public function testFetchEmptyExtendedBean()
1134
    {
1135
        // This test cases checks issue https://github.com/thecodingmachine/database.tdbm/issues/92
1136
1137
        $animalDao = new AnimalDao($this->tdbmService);
1138
1139
        // We are not filling no field that is part of dog table.
1140
        $animalBean = $animalDao->getById(1);
1141
1142
        $this->assertInstanceOf(DogBean::class, $animalBean);
1143
    }
1144
1145
    /**
1146
     * @depends testDaoGeneration
1147
     */
1148
    public function testTwoBranchesHierarchy()
1149
    {
1150
        // This test cases checks issue https://github.com/thecodingmachine/mouf/issues/131
1151
1152
        $catDao = new CatDao($this->tdbmService);
1153
1154
        // We are not filling no field that is part of dog table.
1155
        $cat = new CatBean('Mew');
1156
        $cat->setOrder(2);
1157
1158
        $catDao->save($cat);
1159
    }
1160
1161
    /**
1162
     * @depends testTwoBranchesHierarchy
1163
     */
1164
    public function testFetchTwoBranchesHierarchy()
1165
    {
1166
        // This test cases checks issue https://github.com/thecodingmachine/mouf/issues/131
1167
1168
        $animalDao = new AnimalDao($this->tdbmService);
1169
1170
        $animalBean = $animalDao->getById(2);
1171
1172
        $this->assertInstanceOf(CatBean::class, $animalBean);
1173
        /* @var $animalBean CatBean */
1174
        $animalBean->setCutenessLevel(999);
1175
1176
        $animalDao->save($animalBean);
1177
    }
1178
1179
    /**
1180
     * @depends testDaoGeneration
1181
     */
1182
    public function testExceptionOnGetById()
1183
    {
1184
        $countryDao = new CountryDao($this->tdbmService);
1185
        $this->expectException(\TypeError::class);
1186
        $countryDao->getById(null);
1187
    }
1188
1189
    /**
1190
     * @depends testDaoGeneration
1191
     */
1192
    public function testDisconnectedManyToOne()
1193
    {
1194
        // This test cases checks issue https://github.com/thecodingmachine/database.tdbm/issues/99
1195
1196
        $country = new CountryBean('Spain');
1197
1198
        $user = new UserBean('John Doe', '[email protected]', $country, 'john.doe');
1199
1200
        $this->assertCount(1, $country->getUsers());
1201
        $this->assertSame($user, $country->getUsers()[0]);
1202
    }
1203
1204
    /**
1205
     * @depends testDaoGeneration
1206
     */
1207 View Code Duplication
    public function testOrderByExternalCol()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1208
    {
1209
        // This test cases checks issue https://github.com/thecodingmachine/database.tdbm/issues/106
1210
1211
        $userDao = new TestUserDao($this->tdbmService);
1212
        $users = $userDao->getUsersByCountryName();
1213
1214
        $this->assertEquals('UK', $users[0]->getCountry()->getLabel());
1215
    }
1216
1217
    /**
1218
     * @depends testDaoGeneration
1219
     */
1220
    public function testResultIteratorSort()
1221
    {
1222
        $userDao = new UserDao($this->tdbmService);
1223
        $users = $userDao->findAll()->withOrder('country.label DESC');
1224
1225
        $this->assertEquals('UK', $users[0]->getCountry()->getLabel());
1226
1227
        $users = $users->withOrder('country.label ASC');
1228
        $this->assertEquals('France', $users[0]->getCountry()->getLabel());
1229
    }
1230
1231
    /**
1232
     * @depends testDaoGeneration
1233
     */
1234
    public function testResultIteratorWithParameters()
1235
    {
1236
        $userDao = new TestUserDao($this->tdbmService);
1237
        $users = $userDao->getUsersByLoginStartingWith()->withParameters(['login' => 'bill%']);
1238
        $this->assertEquals('bill.shakespeare', $users[0]->getLogin());
1239
1240
        $users = $users->withParameters(['login' => 'jean%']);
1241
        $this->assertEquals('jean.dupont', $users[0]->getLogin());
1242
    }
1243
1244
    /**
1245
     * @depends testDaoGeneration
1246
     */
1247 View Code Duplication
    public function testOrderByExpression()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1248
    {
1249
        $userDao = new TestUserDao($this->tdbmService);
1250
        $users = $userDao->getUsersByReversedCountryName();
1251
1252
        $this->assertEquals('Jamaica', $users[0]->getCountry()->getLabel());
1253
    }
1254
1255
    /**
1256
     * @depends testDaoGeneration
1257
     */
1258
    public function testOrderByException()
1259
    {
1260
        $userDao = new TestUserDao($this->tdbmService);
1261
        $users = $userDao->getUsersByInvalidOrderBy();
1262
        $this->expectException(TDBMInvalidArgumentException::class);
1263
        $user = $users[0];
0 ignored issues
show
Unused Code introduced by
$user is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
1264
    }
1265
1266
    /**
1267
     * @depends testDaoGeneration
1268
     */
1269
    public function testOrderByProtectedColumn()
1270
    {
1271
        $animalDao = new AnimalDao($this->tdbmService);
1272
        $animals = $animalDao->findAll();
1273
        $animals = $animals->withOrder('`order` ASC');
1274
1275
        $this->assertInstanceOf(DogBean::class, $animals[0]);
1276
        $this->assertInstanceOf(CatBean::class, $animals[1]);
1277
1278
        $animals = $animals->withOrder('`order` DESC');
1279
1280
        $this->assertInstanceOf(CatBean::class, $animals[0]);
1281
        $this->assertInstanceOf(DogBean::class, $animals[1]);
1282
    }
1283
1284
    /**
1285
     * @depends testDaoGeneration
1286
     */
1287
    public function testGetOnAllNullableValues()
1288
    {
1289
        // Tests that a get performed on a column that has only nullable fields succeeds.
1290
        $allNullable = new AllNullableBean();
1291
        $this->assertNull($allNullable->getId());
1292
        $this->assertNull($allNullable->getLabel());
1293
        $this->assertNull($allNullable->getCountry());
1294
    }
1295
1296
    /**
1297
     * @depends testDaoGeneration
1298
     */
1299
    public function testExceptionOnMultipleInheritance()
1300
    {
1301
        $connection = self::getConnection();
1302
        self::insert($connection, 'animal', [
1303
            'id' => 99, 'name' => 'Snoofield',
1304
        ]);
1305
        self::insert($connection, 'dog', [
1306
            'id' => 99, 'race' => 'dog',
1307
        ]);
1308
        self::insert($connection, 'cat', [
1309
            'id' => 99, 'cuteness_level' => 0,
1310
        ]);
1311
1312
        $catched = false;
1313
        try {
1314
            $animalDao = new AnimalDao($this->tdbmService);
1315
            $animalDao->getById(99);
1316
        } catch (TDBMInheritanceException $e) {
1317
            $catched = true;
1318
        }
1319
        $this->assertTrue($catched, 'Exception TDBMInheritanceException was not caught');
1320
1321
        self::delete($connection, 'cat', ['id' => 99]);
1322
        self::delete($connection, 'dog', ['id' => 99]);
1323
        self::delete($connection, 'animal', ['id' => 99]);
1324
    }
1325
1326
    /**
1327
     * @depends testDaoGeneration
1328
     */
1329
    public function testReferenceNotSaved()
1330
    {
1331
        $boatDao = new BoatDao($this->tdbmService);
1332
1333
        $country = new CountryBean('Atlantis');
1334
        $boat = new BoatBean($country, 'Titanic');
1335
1336
        $boatDao->save($boat);
1337
    }
1338
1339
    /**
1340
     * @depends testDaoGeneration
1341
     */
1342
    public function testReferenceDeleted()
1343
    {
1344
        $countryDao = new CountryDao($this->tdbmService);
1345
        $boatDao = new BoatDao($this->tdbmService);
1346
1347
        $country = new CountryBean('Atlantis');
1348
        $countryDao->save($country);
1349
1350
        $boat = new BoatBean($country, 'Titanic');
1351
        $countryDao->delete($country);
1352
1353
        $this->expectException(TDBMMissingReferenceException::class);
1354
        $boatDao->save($boat);
1355
    }
1356
1357
    /**
1358
     * @depends testDaoGeneration
1359
     */
1360 View Code Duplication
    public function testCyclicReferenceWithInheritance()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1361
    {
1362
        $userDao = new UserDao($this->tdbmService);
1363
1364
        $country = new CountryBean('Norrisland');
1365
        $user = new UserBean('Chuck Norris', '[email protected]', $country, 'chuck.norris');
1366
1367
        $user->setManager($user);
1368
1369
        $this->expectException(TDBMCyclicReferenceException::class);
1370
        $userDao->save($user);
1371
    }
1372
1373
    /**
1374
     * @depends testDaoGeneration
1375
     */
1376
    public function testCyclicReference()
1377
    {
1378
        $categoryDao = new CategoryDao($this->tdbmService);
1379
1380
        $category = new CategoryBean('Root');
1381
1382
        $category->setParent($category);
1383
1384
        $this->expectException(TDBMCyclicReferenceException::class);
1385
        $categoryDao->save($category);
1386
    }
1387
1388
    /**
1389
     * @depends testDaoGeneration
1390
     */
1391
    public function testCorrectTypeForPrimaryKeyAfterSave()
1392
    {
1393
        // PosqtgreSQL does not particularly like empty inserts (i.e.: "INSERT INTO all_nullable () VALUES ()" )
1394
        $this->onlyMySql();
1395
1396
        $allNullableDao = new AllNullableDao($this->tdbmService);
1397
        $allNullable = new AllNullableBean();
1398
        $allNullableDao->save($allNullable);
1399
        $id = $allNullable->getId();
1400
1401
        $this->assertTrue(is_int($id));
1402
    }
1403
1404
    /**
1405
     * @depends testDaoGeneration
1406
     */
1407
    public function testPSR2Compliance()
1408
    {
1409
        $process = new Process('vendor/bin/php-cs-fixer fix src/Test/  --dry-run --diff --rules=@PSR2');
1410
        $process->run();
1411
1412
        // executes after the command finishes
1413
        if (!$process->isSuccessful()) {
1414
            echo $process->getOutput();
1415
            $this->fail('Generated code is not PRS2 compliant');
1416
        }
1417
    }
1418
1419
    /**
1420
     * @depends testDaoGeneration
1421
     */
1422
    public function testFindOneByGeneration()
1423
    {
1424
        $reflectionMethod = new \ReflectionMethod(UserBaseDao::class, 'findOneByLogin');
1425
        $parameters = $reflectionMethod->getParameters();
1426
1427
        $this->assertCount(2, $parameters);
1428
        $this->assertSame('login', $parameters[0]->getName());
0 ignored issues
show
Bug introduced by
Consider using $parameters[0]->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
1429
        $this->assertSame('additionalTablesFetch', $parameters[1]->getName());
0 ignored issues
show
Bug introduced by
Consider using $parameters[1]->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
1430
    }
1431
1432
    /**
1433
     * @depends testDaoGeneration
1434
     */
1435 View Code Duplication
    public function testUuid()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1436
    {
1437
        $article = new ArticleBean('content');
1438
        $this->assertSame('content', $article->getContent());
1439
        $this->assertNotEmpty($article->getId());
1440
        $uuid = Uuid::fromString($article->getId());
1441
        $this->assertSame(1, $uuid->getVersion());
1442
    }
1443
1444
    /**
1445
     * @depends testDaoGeneration
1446
     */
1447 View Code Duplication
    public function testUuidv4()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1448
    {
1449
        $article = new Article2Bean('content');
1450
        $this->assertSame('content', $article->getContent());
1451
        $this->assertNotEmpty($article->getId());
1452
        $uuid = Uuid::fromString($article->getId());
1453
        $this->assertSame(4, $uuid->getVersion());
1454
    }
1455
1456
    /**
1457
     * @depends testDaoGeneration
1458
     */
1459
    public function testTypeHintedConstructors()
1460
    {
1461
        $userBaseBeanReflectionConstructor = new \ReflectionMethod(UserBaseBean::class, '__construct');
1462
        $nameParam = $userBaseBeanReflectionConstructor->getParameters()[0];
1463
1464
        $this->assertSame('string', (string) $nameParam->getType());
1465
    }
1466
1467
    /**
1468
     * @depends testDaoGeneration
1469
     */
1470
    public function testSaveTransaction()
1471
    {
1472
        $countryDao = new CountryDao($this->tdbmService);
1473
1474
        $boatDao = new BoatDao($this->tdbmService);
1475
        $boatBean = $boatDao->getById(1);
1476
        $boatBean->setName('Bismark');
1477
1478
        $boatBean->getCountry();
1479
1480
        // Let's insert a row without telling TDBM to trigger an error!
1481
        self::insert($this->getConnection(), 'sailed_countries', [
1482
            'boat_id' => 1,
1483
            'country_id' => 2
1484
        ]);
1485
1486
        $boatBean->addCountry($countryDao->getById(2));
1487
1488
        $this->expectException(UniqueConstraintViolationException::class);
1489
1490
        $boatDao->save($boatBean);
1491
    }
1492
1493
    /**
1494
     * @depends testSaveTransaction
1495
     */
1496
    public function testSaveTransaction2()
1497
    {
1498
        $boatDao = new BoatDao($this->tdbmService);
1499
        $boatBean = $boatDao->getById(1);
1500
1501
        // The name should not have been saved because the transaction of the previous test should have rollbacked.
1502
        $this->assertNotSame('Bismark', $boatBean->getName());
1503
    }
1504
1505
    /**
1506
     * @depends testDaoGeneration
1507
     */
1508
    public function testForeignKeyPointingToNonPrimaryKey()
1509
    {
1510
        $dao = new RefNoPrimKeyDao($this->tdbmService);
1511
        $bean = $dao->getById(1);
1512
1513
        $this->assertSame('foo', $bean->getFrom()->getTo());
1514
    }
1515
}
1516