Passed
Pull Request — 5.1 (#190)
by
unknown
09:45 queued 23s
created

TDBMDaoGeneratorTest::testJointureSave6()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
declare(strict_types=1);
3
4
/*
5
 Copyright (C) 2006-2018 David Négrier - THE CODING MACHINE
6
7
This program is free software; you can redistribute it and/or modify
8
it under the terms of the GNU General Public License as published by
9
the Free Software Foundation; either version 2 of the License, or
10
(at your option) any later version.
11
12
This program is distributed in the hope that it will be useful,
13
but WITHOUT ANY WARRANTY; without even the implied warranty of
14
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
GNU General Public License for more details.
16
17
You should have received a copy of the GNU General Public License
18
along with this program; if not, write to the Free Software
19
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20
*/
21
22
namespace TheCodingMachine\TDBM;
23
24
use Doctrine\Common\Cache\ArrayCache;
25
use Doctrine\DBAL\Exception\ForeignKeyConstraintViolationException;
26
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
27
use Doctrine\DBAL\Platforms\MySQL57Platform;
28
use Doctrine\DBAL\Platforms\MySqlPlatform;
29
use Mouf\Database\SchemaAnalyzer\SchemaAnalyzer;
30
use Ramsey\Uuid\Uuid;
31
use ReflectionClass;
32
use ReflectionMethod;
33
use ReflectionNamedType;
34
use TheCodingMachine\TDBM\Dao\TestAlbumDao;
35
use TheCodingMachine\TDBM\Dao\TestArticleDao;
36
use TheCodingMachine\TDBM\Dao\TestCountryDao;
37
use TheCodingMachine\TDBM\Dao\TestPersonDao;
38
use TheCodingMachine\TDBM\Dao\TestRoleDao;
39
use TheCodingMachine\TDBM\Dao\TestUserDao;
40
use TheCodingMachine\TDBM\Fixtures\Interfaces\TestUserDaoInterface;
41
use TheCodingMachine\TDBM\Fixtures\Interfaces\TestUserInterface;
42
use TheCodingMachine\TDBM\Test\Dao\AlbumDao;
0 ignored issues
show
Bug introduced by
The type TheCodingMachine\TDBM\Test\Dao\AlbumDao was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
43
use TheCodingMachine\TDBM\Test\Dao\AllNullableDao;
0 ignored issues
show
Bug introduced by
The type TheCodingMachine\TDBM\Test\Dao\AllNullableDao was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
44
use TheCodingMachine\TDBM\Test\Dao\AnimalDao;
0 ignored issues
show
Bug introduced by
The type TheCodingMachine\TDBM\Test\Dao\AnimalDao was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
45
use TheCodingMachine\TDBM\Test\Dao\ArtistDao;
0 ignored issues
show
Bug introduced by
The type TheCodingMachine\TDBM\Test\Dao\ArtistDao was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
46
use TheCodingMachine\TDBM\Test\Dao\BaseObjectDao;
0 ignored issues
show
Bug introduced by
The type TheCodingMachine\TDBM\Test\Dao\BaseObjectDao was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
47
use TheCodingMachine\TDBM\Test\Dao\Bean\AccountBean;
0 ignored issues
show
Bug introduced by
The type TheCodingMachine\TDBM\Test\Dao\Bean\AccountBean was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
48
use TheCodingMachine\TDBM\Test\Dao\Bean\AlbumBean;
0 ignored issues
show
Bug introduced by
The type TheCodingMachine\TDBM\Test\Dao\Bean\AlbumBean was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
49
use TheCodingMachine\TDBM\Test\Dao\Bean\AllNullableBean;
0 ignored issues
show
Bug introduced by
The type TheCodingMachine\TDBM\Te...ao\Bean\AllNullableBean was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
50
use TheCodingMachine\TDBM\Test\Dao\Bean\AnimalBean;
0 ignored issues
show
Bug introduced by
The type TheCodingMachine\TDBM\Test\Dao\Bean\AnimalBean was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
51
use TheCodingMachine\TDBM\Test\Dao\Bean\Article2Bean;
0 ignored issues
show
Bug introduced by
The type TheCodingMachine\TDBM\Test\Dao\Bean\Article2Bean was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
52
use TheCodingMachine\TDBM\Test\Dao\Bean\ArticleBean;
0 ignored issues
show
Bug introduced by
The type TheCodingMachine\TDBM\Test\Dao\Bean\ArticleBean was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
53
use TheCodingMachine\TDBM\Test\Dao\Bean\ArtistBean;
0 ignored issues
show
Bug introduced by
The type TheCodingMachine\TDBM\Test\Dao\Bean\ArtistBean was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
54
use TheCodingMachine\TDBM\Test\Dao\Bean\BaseObjectBean;
0 ignored issues
show
Bug introduced by
The type TheCodingMachine\TDBM\Test\Dao\Bean\BaseObjectBean was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
55
use TheCodingMachine\TDBM\Test\Dao\Bean\BoatBean;
0 ignored issues
show
Bug introduced by
The type TheCodingMachine\TDBM\Test\Dao\Bean\BoatBean was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
56
use TheCodingMachine\TDBM\Test\Dao\Bean\CatBean;
0 ignored issues
show
Bug introduced by
The type TheCodingMachine\TDBM\Test\Dao\Bean\CatBean was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
57
use TheCodingMachine\TDBM\Test\Dao\Bean\CategoryBean;
0 ignored issues
show
Bug introduced by
The type TheCodingMachine\TDBM\Test\Dao\Bean\CategoryBean was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
58
use TheCodingMachine\TDBM\Test\Dao\Bean\CountryBean;
0 ignored issues
show
Bug introduced by
The type TheCodingMachine\TDBM\Test\Dao\Bean\CountryBean was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
59
use TheCodingMachine\TDBM\Test\Dao\Bean\DogBean;
0 ignored issues
show
Bug introduced by
The type TheCodingMachine\TDBM\Test\Dao\Bean\DogBean was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
60
use TheCodingMachine\TDBM\Test\Dao\Bean\FileBean;
0 ignored issues
show
Bug introduced by
The type TheCodingMachine\TDBM\Test\Dao\Bean\FileBean was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
61
use TheCodingMachine\TDBM\Test\Dao\Bean\Generated\ArticleBaseBean;
0 ignored issues
show
Bug introduced by
The type TheCodingMachine\TDBM\Te...nerated\ArticleBaseBean was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
62
use TheCodingMachine\TDBM\Test\Dao\Bean\Generated\BoatBaseBean;
0 ignored issues
show
Bug introduced by
The type TheCodingMachine\TDBM\Te...\Generated\BoatBaseBean was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
63
use TheCodingMachine\TDBM\Test\Dao\Bean\Generated\FileBaseBean;
0 ignored issues
show
Bug introduced by
The type TheCodingMachine\TDBM\Te...\Generated\FileBaseBean was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
64
use TheCodingMachine\TDBM\Test\Dao\Bean\Generated\UserBaseBean;
0 ignored issues
show
Bug introduced by
The type TheCodingMachine\TDBM\Te...\Generated\UserBaseBean was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
65
use TheCodingMachine\TDBM\Test\Dao\Bean\InheritanceAgencyBean;
0 ignored issues
show
Bug introduced by
The type TheCodingMachine\TDBM\Te...n\InheritanceAgencyBean was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
66
use TheCodingMachine\TDBM\Test\Dao\Bean\InheritanceSocietyBean;
0 ignored issues
show
Bug introduced by
The type TheCodingMachine\TDBM\Te...\InheritanceSocietyBean was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
67
use TheCodingMachine\TDBM\Test\Dao\Bean\InheritedObjectBean;
0 ignored issues
show
Bug introduced by
The type TheCodingMachine\TDBM\Te...ean\InheritedObjectBean was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
68
use TheCodingMachine\TDBM\Test\Dao\Bean\NodeBean;
0 ignored issues
show
Bug introduced by
The type TheCodingMachine\TDBM\Test\Dao\Bean\NodeBean was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
69
use TheCodingMachine\TDBM\Test\Dao\Bean\PersonBean;
0 ignored issues
show
Bug introduced by
The type TheCodingMachine\TDBM\Test\Dao\Bean\PersonBean was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
70
use TheCodingMachine\TDBM\Test\Dao\Bean\RefNoPrimKeyBean;
0 ignored issues
show
Bug introduced by
The type TheCodingMachine\TDBM\Te...o\Bean\RefNoPrimKeyBean was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
71
use TheCodingMachine\TDBM\Test\Dao\Bean\RoleBean;
0 ignored issues
show
Bug introduced by
The type TheCodingMachine\TDBM\Test\Dao\Bean\RoleBean was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
72
use TheCodingMachine\TDBM\Test\Dao\Bean\StateBean;
0 ignored issues
show
Bug introduced by
The type TheCodingMachine\TDBM\Test\Dao\Bean\StateBean was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
73
use TheCodingMachine\TDBM\Test\Dao\Bean\UserBean;
0 ignored issues
show
Bug introduced by
The type TheCodingMachine\TDBM\Test\Dao\Bean\UserBean was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
74
use TheCodingMachine\TDBM\Test\Dao\BoatDao;
0 ignored issues
show
Bug introduced by
The type TheCodingMachine\TDBM\Test\Dao\BoatDao was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
75
use TheCodingMachine\TDBM\Test\Dao\CatDao;
0 ignored issues
show
Bug introduced by
The type TheCodingMachine\TDBM\Test\Dao\CatDao was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
76
use TheCodingMachine\TDBM\Test\Dao\CategoryDao;
0 ignored issues
show
Bug introduced by
The type TheCodingMachine\TDBM\Test\Dao\CategoryDao was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
77
use TheCodingMachine\TDBM\Test\Dao\CompositeFkSourceDao;
0 ignored issues
show
Bug introduced by
The type TheCodingMachine\TDBM\Te...ao\CompositeFkSourceDao was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
78
use TheCodingMachine\TDBM\Test\Dao\ContactDao;
0 ignored issues
show
Bug introduced by
The type TheCodingMachine\TDBM\Test\Dao\ContactDao was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
79
use TheCodingMachine\TDBM\Test\Dao\CountryDao;
0 ignored issues
show
Bug introduced by
The type TheCodingMachine\TDBM\Test\Dao\CountryDao was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
80
use TheCodingMachine\TDBM\Test\Dao\DogDao;
0 ignored issues
show
Bug introduced by
The type TheCodingMachine\TDBM\Test\Dao\DogDao was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
81
use TheCodingMachine\TDBM\Test\Dao\FileDao;
0 ignored issues
show
Bug introduced by
The type TheCodingMachine\TDBM\Test\Dao\FileDao was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
82
use TheCodingMachine\TDBM\Test\Dao\Generated\UserBaseDao;
0 ignored issues
show
Bug introduced by
The type TheCodingMachine\TDBM\Te...o\Generated\UserBaseDao was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
83
use TheCodingMachine\TDBM\Test\Dao\InheritanceAgencyDao;
0 ignored issues
show
Bug introduced by
The type TheCodingMachine\TDBM\Te...ao\InheritanceAgencyDao was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
84
use TheCodingMachine\TDBM\Test\Dao\InheritanceSocietyDao;
0 ignored issues
show
Bug introduced by
The type TheCodingMachine\TDBM\Te...o\InheritanceSocietyDao was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
85
use TheCodingMachine\TDBM\Test\Dao\InheritedObjectDao;
0 ignored issues
show
Bug introduced by
The type TheCodingMachine\TDBM\Test\Dao\InheritedObjectDao was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
86
use TheCodingMachine\TDBM\Test\Dao\NodeDao;
0 ignored issues
show
Bug introduced by
The type TheCodingMachine\TDBM\Test\Dao\NodeDao was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
87
use TheCodingMachine\TDBM\Test\Dao\PersonDao;
0 ignored issues
show
Bug introduced by
The type TheCodingMachine\TDBM\Test\Dao\PersonDao was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
88
use TheCodingMachine\TDBM\Test\Dao\RefNoPrimKeyDao;
0 ignored issues
show
Bug introduced by
The type TheCodingMachine\TDBM\Test\Dao\RefNoPrimKeyDao was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
89
use TheCodingMachine\TDBM\Test\Dao\RoleDao;
0 ignored issues
show
Bug introduced by
The type TheCodingMachine\TDBM\Test\Dao\RoleDao was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
90
use TheCodingMachine\TDBM\Test\Dao\StateDao;
0 ignored issues
show
Bug introduced by
The type TheCodingMachine\TDBM\Test\Dao\StateDao was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
91
use TheCodingMachine\TDBM\Test\Dao\UserDao;
0 ignored issues
show
Bug introduced by
The type TheCodingMachine\TDBM\Test\Dao\UserDao was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
92
use TheCodingMachine\TDBM\Utils\PathFinder\NoPathFoundException;
93
use TheCodingMachine\TDBM\Utils\PathFinder\PathFinder;
94
use TheCodingMachine\TDBM\Utils\TDBMDaoGenerator;
95
use Symfony\Component\Process\Process;
96
use function gettype;
97
98
class TDBMDaoGeneratorTest extends TDBMAbstractServiceTest
99
{
100
    /** @var TDBMDaoGenerator $tdbmDaoGenerator */
101
    protected $tdbmDaoGenerator;
102
103
    private $rootPath;
104
105
    protected function setUp(): void
106
    {
107
        parent::setUp();
108
        $schemaManager = $this->tdbmService->getConnection()->getSchemaManager();
109
        $schemaAnalyzer = new SchemaAnalyzer($schemaManager);
110
        $tdbmSchemaAnalyzer = new TDBMSchemaAnalyzer($this->tdbmService->getConnection(), new ArrayCache(), $schemaAnalyzer);
111
        $this->tdbmDaoGenerator = new TDBMDaoGenerator($this->getConfiguration(), $tdbmSchemaAnalyzer);
112
        $this->rootPath = __DIR__ . '/../';
113
        //$this->tdbmDaoGenerator->setComposerFile($this->rootPath.'composer.json');
114
    }
115
116
    public function testDaoGeneration(): void
117
    {
118
        // Remove all previously generated files.
119
        $this->recursiveDelete($this->rootPath . 'src/Test/Dao/');
120
        mkdir($this->rootPath . 'src/Test/Dao/Generated', 0755, true);
121
        // Let's generate a dummy file to see it is indeed removed.
122
        $dummyFile = $this->rootPath . 'src/Test/Dao/Generated/foobar.php';
123
        touch($dummyFile);
124
        $this->assertFileExists($dummyFile);
125
126
        $this->tdbmDaoGenerator->generateAllDaosAndBeans();
127
128
        $this->assertFileNotExists($dummyFile);
129
130
        // Let's require all files to check they are valid PHP!
131
        // Test the daoFactory
132
        require_once $this->rootPath . 'src/Test/Dao/Generated/DaoFactory.php';
133
        // Test the others
134
135
        $beanDescriptors = $this->getDummyGeneratorListener()->getBeanDescriptors();
136
137
        foreach ($beanDescriptors as $beanDescriptor) {
138
            $daoName = $beanDescriptor->getDaoClassName();
139
            $daoBaseName = $beanDescriptor->getBaseDaoClassName();
140
            $beanName = $beanDescriptor->getBeanClassName();
141
            $baseBeanName = $beanDescriptor->getBaseBeanClassName();
142
            require_once $this->rootPath . 'src/Test/Dao/Bean/Generated/' . $baseBeanName . '.php';
143
            require_once $this->rootPath . 'src/Test/Dao/Bean/' . $beanName . '.php';
144
            require_once $this->rootPath . 'src/Test/Dao/Generated/' . $daoBaseName . '.php';
145
            require_once $this->rootPath . 'src/Test/Dao/' . $daoName . '.php';
146
        }
147
148
        // Check that pivot tables do not generate DAOs or beans.
149
        $this->assertFalse(class_exists('TheCodingMachine\\TDBM\\Test\\Dao\\RolesRightDao'));
150
    }
151
152
    public function testGenerationException(): void
153
    {
154
        $configuration = new Configuration('UnknownVendor\\Dao', 'UnknownVendor\\Bean', self::getConnection(), $this->getNamingStrategy());
155
156
        $schemaManager = $this->tdbmService->getConnection()->getSchemaManager();
157
        $schemaAnalyzer = new SchemaAnalyzer($schemaManager);
158
        $tdbmSchemaAnalyzer = new TDBMSchemaAnalyzer($this->tdbmService->getConnection(), new ArrayCache(), $schemaAnalyzer);
159
        $tdbmDaoGenerator = new TDBMDaoGenerator($configuration, $tdbmSchemaAnalyzer);
160
        $this->rootPath = __DIR__ . '/../../../../';
161
        //$tdbmDaoGenerator->setComposerFile($this->rootPath.'composer.json');
162
163
        $this->expectException(NoPathFoundException::class);
164
        $tdbmDaoGenerator->generateAllDaosAndBeans();
165
    }
166
167
    /**
168
     * Delete a file or recursively delete a directory.
169
     *
170
     * @param string $str Path to file or directory
171
     * @return bool
172
     */
173
    private function recursiveDelete(string $str): bool
174
    {
175
        if (is_file($str)) {
176
            return @unlink($str);
177
        } elseif (is_dir($str)) {
178
            $scan = glob(rtrim($str, '/') . '/*');
179
            foreach ($scan as $index => $path) {
180
                $this->recursiveDelete($path);
181
            }
182
183
            return @rmdir($str);
184
        }
185
        return false;
186
    }
187
188
    /**
189
     * @depends testDaoGeneration
190
     */
191
    public function testGetBeanClassName(): void
192
    {
193
        $this->assertEquals(UserBean::class, $this->tdbmService->getBeanClassName('users'));
194
195
        // Let's create another TDBMService to test the cache.
196
        $configuration = new Configuration('TheCodingMachine\\TDBM\\Test\\Dao\\Bean', 'TheCodingMachine\\TDBM\\Test\\Dao', self::getConnection(), $this->getNamingStrategy(), $this->getCache(), null, null, [$this->getDummyGeneratorListener()]);
197
        $configuration->setPathFinder(new PathFinder(null, dirname(__DIR__, 4)));
198
        $newTdbmService = new TDBMService($configuration);
199
        $this->assertEquals(UserBean::class, $newTdbmService->getBeanClassName('users'));
200
    }
201
202
    /**
203
     * @depends testDaoGeneration
204
     */
205
    public function testGeneratedGetById(): void
206
    {
207
        $contactDao = new ContactDao($this->tdbmService);
208
        $contactBean = $contactDao->getById(1);
209
        $this->assertEquals(1, $contactBean->getId());
210
        $this->assertInstanceOf('\\DateTimeInterface', $contactBean->getCreatedAt());
211
212
        // FIXME: Question: que faire du paramètre stockage "UTC"????
213
    }
214
215
    /**
216
     * @depends testDaoGeneration
217
     */
218
    public function testGeneratedGetByIdLazyLoaded(): void
219
    {
220
        $roleDao = new RoleDao($this->tdbmService);
221
        $roleBean = $roleDao->getById(1, true);
222
        $this->assertEquals(1, $roleBean->getId());
223
        $this->assertInstanceOf('\\DateTimeInterface', $roleBean->getCreatedAt());
224
225
        $roleBean2 = $roleDao->getById(1, true);
226
        $this->assertTrue($roleBean === $roleBean2);
227
    }
228
229
    /**
230
     * @depends testDaoGeneration
231
     */
232
    public function testDefaultValueOnNewBean(): void
233
    {
234
        $roleBean = new RoleBean('my_role');
235
        $this->assertEquals(1, $roleBean->getStatus());
236
    }
237
238
    /**
239
     * @depends testDaoGeneration
240
     */
241
    public function testForeignKeyInBean(): void
242
    {
243
        $userDao = new UserDao($this->tdbmService);
244
        $userBean = $userDao->getById(1);
245
        $country = $userBean->getCountry();
246
247
        $this->assertEquals('UK', $country->getLabel());
248
249
        $userBean2 = $userDao->getById(1);
250
        $this->assertTrue($userBean === $userBean2);
251
252
        $contactDao = new ContactDao($this->tdbmService);
253
        $contactBean = $contactDao->getById(1);
254
255
        $this->assertTrue($userBean === $contactBean);
256
    }
257
258
    /**
259
     * @depends testDaoGeneration
260
     */
261
    public function testNewBeans(): void
262
    {
263
        $countryDao = new CountryDao($this->tdbmService);
264
        $userDao = new UserDao($this->tdbmService);
265
        $userBean = new UserBean('John Doe', '[email protected]', $countryDao->getById(2), 'john.doe');
266
        $userBean->setOrder(1); // Let's set a "protected keyword" column.
267
268
        $userDao->save($userBean);
269
270
        $this->assertNull($userBean->getManager());
271
    }
272
273
    /**
274
     * @depends testDaoGeneration
275
     */
276
    public function testDateTimeImmutableGetter(): void
277
    {
278
        $userDao = new UserDao($this->tdbmService);
279
        $user = $userDao->getById(1);
280
281
        $this->assertInstanceOf('\DateTimeImmutable', $user->getCreatedAt());
282
    }
283
284
    /**
285
     * @depends testDaoGeneration
286
     */
287
    public function testAssigningNewBeans(): void
288
    {
289
        $userDao = new UserDao($this->tdbmService);
290
        $countryBean = new CountryBean('Mexico');
291
        $userBean = new UserBean('Speedy Gonzalez', '[email protected]', $countryBean, 'speedy.gonzalez');
292
        $this->assertEquals($countryBean, $userBean->getCountry());
293
294
        $userDao->save($userBean);
295
    }
296
297
    /**
298
     * @depends testAssigningNewBeans
299
     */
300
    public function testUpdatingProtectedColumn(): void
301
    {
302
        $userDao = new UserDao($this->tdbmService);
303
        $userBean = $userDao->findOneByLogin('speedy.gonzalez');
304
        $userBean->setOrder(2);
305
        $userDao->save($userBean);
306
        $this->assertSame(2, $userBean->getOrder());
307
    }
308
309
    /**
310
     * @depends testDaoGeneration
311
     */
312
    public function testAssigningExistingRelationship(): void
313
    {
314
        $userDao = new UserDao($this->tdbmService);
315
        $user = $userDao->getById(1);
316
        $countryDao = new CountryDao($this->tdbmService);
317
        $country = $countryDao->getById(2);
318
319
        $user->setCountry($country);
320
        $this->assertEquals(TDBMObjectStateEnum::STATE_DIRTY, $user->_getStatus());
321
    }
322
323
    /**
324
     * @depends testDaoGeneration
325
     */
326
    public function testDirectReversedRelationship(): void
327
    {
328
        $countryDao = new CountryDao($this->tdbmService);
329
        $country = $countryDao->getById(1);
330
        $users = $country->getUsers();
331
332
        $arr = $users->toArray();
333
334
        $this->assertCount(1, $arr);
335
        $this->assertInstanceOf('TheCodingMachine\\TDBM\\Test\\Dao\\Bean\\UserBean', $arr[0]);
336
        $this->assertEquals('jean.dupont', $arr[0]->getLogin());
337
338
        $newUser = new UserBean('Speedy Gonzalez', '[email protected]', $country, 'speedy.gonzalez');
0 ignored issues
show
Unused Code introduced by
The assignment to $newUser is dead and can be removed.
Loading history...
339
        $users = $country->getUsers();
340
341
        $arr = $users->toArray();
342
343
        $this->assertCount(2, $arr);
344
        $this->assertInstanceOf('TheCodingMachine\\TDBM\\Test\\Dao\\Bean\\UserBean', $arr[1]);
345
        $this->assertEquals('speedy.gonzalez', $arr[1]->getLogin());
346
    }
347
348
    /**
349
     * @depends testDaoGeneration
350
     */
351
    public function testDeleteInDirectReversedRelationship(): void
352
    {
353
        $countryDao = new CountryDao($this->tdbmService);
354
        $country = $countryDao->getById(1);
355
356
        $userDao = new UserDao($this->tdbmService);
357
        $newUser = new UserBean('John Snow', '[email protected]', $country, 'john.snow');
358
        $userDao->save($newUser);
359
360
        $users = $country->getUsers();
361
362
        $arr = $users->toArray();
363
364
        $this->assertCount(2, $arr);
365
366
        $userDao->delete($arr[1]);
367
368
        $users = $country->getUsers();
369
        $arr = $users->toArray();
370
        $this->assertCount(1, $arr);
371
    }
372
373
    /**
374
     * @depends testDaoGeneration
375
     */
376
    public function testJointureGetters(): void
377
    {
378
        $roleDao = new RoleDao($this->tdbmService);
379
        $role = $roleDao->getById(1);
380
        $users = $role->getUsers();
381
382
        $this->assertCount(2, $users);
383
        $this->assertInstanceOf('TheCodingMachine\\TDBM\\Test\\Dao\\Bean\\UserBean', $users[0]);
384
385
        $rights = $role->getRights();
386
387
        $this->assertCount(2, $rights);
388
        $this->assertInstanceOf('TheCodingMachine\\TDBM\\Test\\Dao\\Bean\\RightBean', $rights[0]);
389
    }
390
391
    /**
392
     * @depends testDaoGeneration
393
     */
394
    public function testNestedIterationOnAlterableResultIterator(): void
395
    {
396
        $countryDao = new CountryDao($this->tdbmService);
397
        $country = $countryDao->getById(2);
398
399
        $count = 0;
400
        // Let's perform 2 nested calls to check that iterators do not melt.
401
        foreach ($country->getUsers() as $user) {
402
            foreach ($country->getUsers() as $user2) {
403
                $count++;
404
            }
405
        }
406
        // There are 3 users linked to country 2.
407
        $this->assertSame(9, $count);
408
    }
409
410
    /**
411
     * @depends testDaoGeneration
412
     */
413
    public function testNewBeanConstructor(): void
414
    {
415
        $role = new RoleBean('Newrole');
416
        $this->assertEquals(TDBMObjectStateEnum::STATE_DETACHED, $role->_getStatus());
417
    }
418
419
    /**
420
     * @depends testDaoGeneration
421
     */
422
    public function testJointureAdderOnNewBean(): void
423
    {
424
        $countryDao = new CountryDao($this->tdbmService);
425
        $country = $countryDao->getById(1);
426
        $user = new UserBean('Speedy Gonzalez', '[email protected]', $country, 'speedy.gonzalez');
427
        $role = new RoleBean('Mouse');
428
        $user->addRole($role);
429
        $roles = $user->getRoles();
430
        $this->assertCount(1, $roles);
431
        $role = $roles[0];
432
        $this->assertInstanceOf('TheCodingMachine\\TDBM\\Test\\Dao\\Bean\\RoleBean', $role);
433
        $users = $role->getUsers();
434
        $this->assertCount(1, $users);
435
        $this->assertEquals($user, $users[0]);
436
437
        $role->removeUser($user);
438
        $this->assertCount(0, $role->getUsers());
439
        $this->assertCount(0, $user->getRoles());
440
    }
441
442
    /**
443
     * @depends testDaoGeneration
444
     */
445
    public function testJointureDeleteBeforeGetters(): void
446
    {
447
        $roleDao = new RoleDao($this->tdbmService);
448
        $userDao = new UserDao($this->tdbmService);
449
        $role = $roleDao->getById(1);
450
        $user = $userDao->getById(1);
451
452
        // We call removeUser BEFORE calling getUsers
453
        // This should work as expected.
454
        $role->removeUser($user);
455
        $users = $role->getUsers();
456
457
        $this->assertCount(1, $users);
458
    }
459
460
    /**
461
     * @depends testDaoGeneration
462
     */
463
    public function testJointureMultiAdd(): void
464
    {
465
        $countryDao = new CountryDao($this->tdbmService);
466
        $country = $countryDao->getById(1);
467
        $user = new UserBean('Speedy Gonzalez', '[email protected]', $country, 'speedy.gonzalez');
468
        $role = new RoleBean('Mouse');
469
        $user->addRole($role);
470
        $role->addUser($user);
471
        $user->addRole($role);
472
473
        $this->assertCount(1, $user->getRoles());
474
    }
475
476
    /**
477
     * Step 1: we remove the role 1 from user 1 but save role 1.
478
     * Expected result: no save done.
479
     *
480
     * @depends testDaoGeneration
481
     */
482
    public function testJointureSave1(): void
483
    {
484
        $roleDao = new RoleDao($this->tdbmService);
485
        $role = $roleDao->getById(1);
486
        $userDao = new UserDao($this->tdbmService);
487
        $user = $userDao->getById(1);
488
489
        $this->assertTrue($user->hasRole($role));
490
        $this->assertTrue($role->hasUser($user));
491
        $user->removeRole($role);
492
        $this->assertFalse($user->hasRole($role));
493
        $this->assertFalse($role->hasUser($user));
494
        $roleDao->save($role);
495
496
        $this->assertEquals(TDBMObjectStateEnum::STATE_DIRTY, $user->_getStatus());
497
        $this->assertEquals(TDBMObjectStateEnum::STATE_LOADED, $role->_getStatus());
498
    }
499
500
    /**
501
     * Step 2: we check that nothing was saved
502
     * Expected result: no save done.
503
     *
504
     * @depends testJointureSave1
505
     */
506
    public function testJointureSave2(): void
507
    {
508
        $roleDao = new RoleDao($this->tdbmService);
509
        $role = $roleDao->getById(1);
510
        $this->assertCount(2, $role->getUsers());
511
    }
512
513
    /**
514
     * Step 3: we remove the role 1 from user 1 and save user 1.
515
     * Expected result: save done.
516
     *
517
     * @depends testJointureSave2
518
     */
519
    public function testJointureSave3(): void
520
    {
521
        $roleDao = new RoleDao($this->tdbmService);
522
        $role = $roleDao->getById(1);
523
        $userDao = new UserDao($this->tdbmService);
524
        $user = $userDao->getById(1);
525
526
        $this->assertCount(1, $user->getRoles());
527
        $user->removeRole($role);
528
        $this->assertCount(0, $user->getRoles());
529
        $userDao->save($user);
530
        $this->assertCount(0, $user->getRoles());
531
    }
532
533
    /**
534
     * Step 4: we check that save was done
535
     * Expected result: save done.
536
     *
537
     * @depends testJointureSave3
538
     */
539
    public function testJointureSave4(): void
540
    {
541
        $roleDao = new RoleDao($this->tdbmService);
542
        $role = $roleDao->getById(1);
543
        $this->assertCount(1, $role->getUsers());
544
        $userDao = new UserDao($this->tdbmService);
545
        $user = $userDao->getById(1);
546
        $this->assertCount(0, $user->getRoles());
547
    }
548
549
    /**
550
     * Step 5: we add the role 1 from user 1 and save user 1.
551
     * Expected result: save done.
552
     *
553
     * @depends testJointureSave4
554
     */
555
    public function testJointureSave5(): void
556
    {
557
        $roleDao = new RoleDao($this->tdbmService);
558
        $role = $roleDao->getById(1);
559
        $userDao = new UserDao($this->tdbmService);
560
        $user = $userDao->getById(1);
561
562
        $user->addRole($role);
563
        $this->assertCount(1, $user->getRoles());
564
        $userDao->save($user);
565
    }
566
567
    /**
568
     * Step 6: we check that save was done
569
     * Expected result: save done.
570
     *
571
     * @depends testJointureSave5
572
     */
573
    public function testJointureSave6(): void
574
    {
575
        $roleDao = new RoleDao($this->tdbmService);
576
        $role = $roleDao->getById(1);
577
        $this->assertCount(2, $role->getUsers());
578
        $userDao = new UserDao($this->tdbmService);
579
        $user = $userDao->getById(1);
580
        $this->assertCount(1, $user->getRoles());
581
    }
582
583
    /**
584
     * Step 7: we add a new role to user 1 and save user 1.
585
     * Expected result: save done, including the new role.
586
     *
587
     * @depends testJointureSave6
588
     */
589
    public function testJointureSave7(): void
590
    {
591
        $role = new RoleBean('my new role');
592
        $userDao = new UserDao($this->tdbmService);
593
        $user = $userDao->getById(1);
594
595
        $user->addRole($role);
596
        $userDao->save($user);
597
598
        $this->assertEquals(TDBMObjectStateEnum::STATE_LOADED, $role->_getStatus());
599
    }
600
601
    /**
602
     * Step 8: we check that save was done
603
     * Expected result: save done.
604
     *
605
     * @depends testJointureSave7
606
     */
607
    public function testJointureSave8(): void
608
    {
609
        $roleDao = new RoleDao($this->tdbmService);
610
        $userDao = new UserDao($this->tdbmService);
611
        $user = $userDao->getById(1);
612
613
        $roles = $user->getRoles();
614
        foreach ($roles as $role) {
615
            if ($role->getName() === 'my new role') {
616
                $selectedRole = $role;
617
                break;
618
            }
619
        }
620
        $this->assertNotNull($selectedRole);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $selectedRole does not seem to be defined for all execution paths leading up to this point.
Loading history...
621
622
        $this->assertCount(2, $user->getRoles());
623
624
        // Expected: relationship removed!
625
        $roleDao->delete($selectedRole);
626
627
        $this->assertCount(1, $user->getRoles());
628
    }
629
630
    /**
631
     * Step 9: Let's test the setXXX method.
632
     *
633
     * @depends testJointureSave8
634
     */
635
    public function testJointureSave9(): void
636
    {
637
        $roleDao = new RoleDao($this->tdbmService);
638
        $userDao = new UserDao($this->tdbmService);
639
        $user = $userDao->getById(1);
640
641
        // At this point, user 1 is linked to role 1.
642
        // Let's bind it to role 2.
643
        $user->setRoles([$roleDao->getById(2)]);
644
        $userDao->save($user);
645
        $this->assertTrue($user->hasRole($roleDao->getById(2)));
646
    }
647
648
    /**
649
     * Step 10: Let's check results of 9.
650
     *
651
     * @depends testJointureSave9
652
     */
653
    public function testJointureSave10(): void
654
    {
655
        $userDao = new UserDao($this->tdbmService);
656
        $user = $userDao->getById(1);
657
658
        $roles = $user->getRoles();
659
660
        $this->assertCount(1, $roles);
661
        $this->assertEquals(2, $roles[0]->getId());
662
    }
663
664
    /**
665
     * Test jointure in a parent table in an inheritance relationship
666
     *
667
     * @depends testDaoGeneration
668
     */
669
    public function testJointureInParentTable(): void
670
    {
671
        $userDao = new UserDao($this->tdbmService);
672
        $user = $userDao->getById(1);
673
674
        $boats = $user->getBoats();
675
676
        $this->assertCount(1, $boats);
677
        $this->assertEquals(1, $boats[0]->getId());
678
    }
679
680
    /**
681
     * @depends testDaoGeneration
682
     */
683
    public function testFindOrderBy(): void
684
    {
685
        $userDao = new TestUserDao($this->tdbmService);
686
        $users = $userDao->getUsersByAlphabeticalOrder();
687
688
        $this->assertCount(6, $users);
689
        $this->assertEquals('bill.shakespeare', $users[0]->getLogin());
690
        $this->assertEquals('jean.dupont', $users[1]->getLogin());
691
692
        $users = $userDao->getUsersByCountryOrder();
693
        $this->assertCount(6, $users);
694
        $countryName1 = $users[0]->getCountry()->getLabel();
695
        for ($i = 1; $i < 6; $i++) {
696
            $countryName2 = $users[$i]->getCountry()->getLabel();
697
            $this->assertLessThanOrEqual(0, strcmp($countryName1, $countryName2));
698
            $countryName1 = $countryName2;
699
        }
700
    }
701
702
    /**
703
     * @depends testDaoGeneration
704
     */
705
    public function testFindFromSqlOrderBy(): void
706
    {
707
        $userDao = new TestUserDao($this->tdbmService);
708
        $users = $userDao->getUsersFromSqlByAlphabeticalOrder();
709
710
        $this->assertCount(6, $users);
711
        $this->assertEquals('bill.shakespeare', $users[0]->getLogin());
712
        $this->assertEquals('jean.dupont', $users[1]->getLogin());
713
714
        $users = $userDao->getUsersFromSqlByCountryOrder();
715
        $this->assertCount(6, $users);
716
        $countryName1 = $users[0]->getCountry()->getLabel();
717
        for ($i = 1; $i < 6; $i++) {
718
            $countryName2 = $users[$i]->getCountry()->getLabel();
719
            $this->assertLessThanOrEqual(0, strcmp($countryName1, $countryName2));
720
            $countryName1 = $countryName2;
721
        }
722
    }
723
724
    /**
725
     * @depends testDaoGeneration
726
     */
727
    public function testFindFromSqlOrderByOnInheritedBean(): void
728
    {
729
        $articleDao = new TestArticleDao($this->tdbmService);
730
        $articles = $articleDao->getArticlesByUserLogin();
731
732
        foreach ($articles as $article) {
733
            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?
Loading history...
734
        }
735
        $this->assertCount(0, $articles);
736
    }
737
738
739
    /**
740
     * @depends testDaoGeneration
741
     */
742
    public function testFindFromSqlOrderByJoinRole(): void
743
    {
744
        $roleDao = new TestRoleDao($this->tdbmService);
745
        $roles = $roleDao->getRolesByRightCanSing('roles.name DESC');
746
747
        $this->assertCount(2, $roles);
748
        $this->assertEquals('Singers', $roles[0]->getName());
749
        $this->assertEquals('Admins', $roles[1]->getName());
750
    }
751
752
    /**
753
     * @depends testDaoGeneration
754
     */
755
    public function testFindFromRawSqlOrderByUserCount(): void
756
    {
757
        $countryDao = new TestCountryDao($this->tdbmService);
758
        $countries = $countryDao->getCountriesByUserCount();
759
760
        $nbCountries = 4;
761
        $this->assertCount($nbCountries, $countries);
762
        for ($i = 1; $i < $nbCountries; $i++) {
763
            $this->assertLessThanOrEqual($countries[$i - 1]->getUsers()->count(), $countries[$i]->getUsers()->count());
764
        }
765
    }
766
767
    /**
768
     * @depends testDaoGeneration
769
     */
770
    public function testFindFromRawSqlWithUnion(): void
771
    {
772
        $countryDao = new TestCountryDao($this->tdbmService);
773
        $countries = $countryDao->getCountriesUsingUnion();
774
775
        $this->assertCount(2, $countries);
776
        $this->assertEquals(1, $countries[0]->getId());
777
    }
778
779
    /**
780
     * @depends testDaoGeneration
781
     */
782
    public function testFindFromRawSqlWithSimpleQuery(): void
783
    {
784
        $countryDao = new TestCountryDao($this->tdbmService);
785
        $countries = $countryDao->getCountriesUsingSimpleQuery();
786
787
        $this->assertCount(1, $countries);
788
        $this->assertEquals(1, $countries[0]->getId());
789
    }
790
791
    /**
792
     * @depends testDaoGeneration
793
     */
794
    public function testFindFromRawSqlWithDistinctQuery(): void
795
    {
796
        $countryDao = new TestCountryDao($this->tdbmService);
797
        $countries = $countryDao->getCountriesUsingDistinctQuery();
798
799
        $this->assertCount(1, $countries);
800
        $this->assertEquals(2, $countries[0]->getId());
801
    }
802
803
    /**
804
     * @depends testDaoGeneration
805
     */
806
    public function testFindFilters(): void
807
    {
808
        $userDao = new TestUserDao($this->tdbmService);
809
        $users = $userDao->getUsersByLoginStartingWith('bill');
810
811
        $this->assertCount(1, $users);
812
        $this->assertEquals('bill.shakespeare', $users[0]->getLogin());
813
    }
814
815
    /**
816
     * @depends testDaoGeneration
817
     */
818
    public function testFindMode(): void
819
    {
820
        $userDao = new TestUserDao($this->tdbmService);
821
        $users = $userDao->getUsersByLoginStartingWith('bill', TDBMService::MODE_CURSOR);
822
823
        $this->expectException('TheCodingMachine\TDBM\TDBMException');
824
        $users[0];
825
    }
826
827
    /**
828
     * @depends testDaoGeneration
829
     */
830
    public function testFindAll(): void
831
    {
832
        $userDao = new TestUserDao($this->tdbmService);
833
        $users = $userDao->findAll();
834
835
        $this->assertCount(6, $users);
836
    }
837
838
    /**
839
     * @depends testDaoGeneration
840
     */
841
    public function testFindOne(): void
842
    {
843
        $userDao = new TestUserDao($this->tdbmService);
844
        $user = $userDao->getUserByLogin('bill.shakespeare');
845
846
        $this->assertEquals('bill.shakespeare', $user->getLogin());
847
    }
848
849
    /**
850
     * @depends testDaoGeneration
851
     */
852
    public function testJsonEncodeBean(): void
853
    {
854
        $userDao = new TestUserDao($this->tdbmService);
855
        $user = $userDao->getUserByLogin('bill.shakespeare');
856
857
        $jsonEncoded = json_encode($user);
858
        $userDecoded = json_decode($jsonEncoded, true);
859
860
        $this->assertEquals('bill.shakespeare', $userDecoded['login']);
861
862
        // test serialization of dates.
863
        $this->assertTrue(is_string($userDecoded['createdAt']));
864
        $this->assertEquals('2015-10-24', (new \DateTimeImmutable($userDecoded['createdAt']))->format('Y-m-d'));
865
        $this->assertNull($userDecoded['modifiedAt']);
866
867
        // testing many to 1 relationships
868
        $this->assertEquals('UK', $userDecoded['country']['label']);
869
870
        // testing many to many relationships
871
        $this->assertCount(1, $userDecoded['roles']);
872
        $this->assertArrayNotHasKey('users', $userDecoded['roles'][0]);
873
        $this->assertArrayNotHasKey('rights', $userDecoded['roles'][0]);
874
    }
875
876
    /**
877
     * @depends testDaoGeneration
878
     */
879
    public function testNullableForeignKey(): void
880
    {
881
        $userDao = new TestUserDao($this->tdbmService);
882
        $user = $userDao->getUserByLogin('john.smith');
883
884
        $this->assertNull($user->getManager());
885
886
        $jsonEncoded = json_encode($user);
887
        $userDecoded = json_decode($jsonEncoded, true);
888
889
        $this->assertNull($userDecoded['manager']);
890
    }
891
892
    /**
893
     * Test that setting (and saving) objects' references (foreign keys relations) to null is working.
894
     *
895
     * @depends testDaoGeneration
896
     */
897
    public function testSetToNullForeignKey(): void
898
    {
899
        $userDao = new TestUserDao($this->tdbmService);
900
        $user = $userDao->getUserByLogin('john.smith');
901
        $manager = $userDao->getUserByLogin('jean.dupont');
902
903
        $user->setManager($manager);
904
        $userDao->save($user);
905
906
        $user->setManager(null);
907
        $userDao->save($user);
908
        $this->assertNull($user->getManager());
909
    }
910
911
    /**
912
     * @depends testDaoGeneration
913
     */
914
    public function testQueryOnWrongTableName(): void
915
    {
916
        $userDao = new TestUserDao($this->tdbmService);
917
        $users = $userDao->getUsersWrongTableName();
918
        $this->expectException('Mouf\Database\SchemaAnalyzer\SchemaAnalyzerTableNotFoundException');
919
        $this->expectExceptionMessage('Could not find table \'contacts\'. Did you mean \'contact\'?');
920
        $users->count();
921
    }
922
923
    /**
924
     * @depends testDaoGeneration
925
     */
926
    /*public function testQueryNullForeignKey(): void
927
    {
928
        $userDao = new TestUserDao($this->tdbmService);
929
        $users = $userDao->getUsersByManagerId(null);
930
        $this->assertCount(3, $users);
931
    }*/
932
933
    /**
934
     * @depends testDaoGeneration
935
     */
936
    public function testInnerJsonEncode(): void
937
    {
938
        $userDao = new TestUserDao($this->tdbmService);
939
        $user = $userDao->getUserByLogin('bill.shakespeare');
940
941
        $jsonEncoded = json_encode(['user' => $user]);
942
        $msgDecoded = json_decode($jsonEncoded, true);
943
944
        $this->assertEquals('bill.shakespeare', $msgDecoded['user']['login']);
945
    }
946
947
    /**
948
     * @depends testDaoGeneration
949
     */
950
    public function testArrayJsonEncode(): void
951
    {
952
        $userDao = new TestUserDao($this->tdbmService);
953
        $users = $userDao->getUsersByLoginStartingWith('bill');
954
955
        $jsonEncoded = json_encode($users);
956
        $msgDecoded = json_decode($jsonEncoded, true);
957
958
        $this->assertCount(1, $msgDecoded);
959
    }
960
961
    /**
962
     * @depends testDaoGeneration
963
     */
964
    public function testCursorJsonEncode(): void
965
    {
966
        $userDao = new TestUserDao($this->tdbmService);
967
        $users = $userDao->getUsersByLoginStartingWith('bill', TDBMService::MODE_CURSOR);
968
969
        $jsonEncoded = json_encode($users);
970
        $msgDecoded = json_decode($jsonEncoded, true);
971
972
        $this->assertCount(1, $msgDecoded);
973
    }
974
975
    /**
976
     * @depends testDaoGeneration
977
     */
978
    public function testPageJsonEncode(): void
979
    {
980
        $userDao = new TestUserDao($this->tdbmService);
981
        $users = $userDao->getUsersByLoginStartingWith('bill');
982
983
        $jsonEncoded = json_encode($users->take(0, 1));
984
        $msgDecoded = json_decode($jsonEncoded, true);
985
986
        $this->assertCount(1, $msgDecoded);
987
    }
988
989
    /**
990
     * @depends testDaoGeneration
991
     */
992
    public function testInnerResultIteratorCountAfterFetch(): void
993
    {
994
        $userDao = new TestUserDao($this->tdbmService);
995
        $users = $userDao->getUsersByLoginStartingWith('j')->take(0, 4);
996
        $users->toArray(); // We force to fetch
997
        $this->assertEquals(3, $users->count());
998
    }
999
1000
    /**
1001
     * @depends testDaoGeneration
1002
     */
1003
    public function testFirst(): void
1004
    {
1005
        $userDao = new TestUserDao($this->tdbmService);
1006
        $users = $userDao->getUsersByLoginStartingWith('bill');
1007
1008
        $bill = $users->first();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $bill is correct as $users->first() targeting TheCodingMachine\TDBM\ResultIterator::first() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
1009
        $this->assertEquals('bill.shakespeare', $bill->getLogin());
1010
    }
1011
1012
    /**
1013
     * @depends testDaoGeneration
1014
     */
1015
    public function testFirstNull(): void
1016
    {
1017
        $userDao = new TestUserDao($this->tdbmService);
1018
        $users = $userDao->getUsersByLoginStartingWith('mike');
1019
1020
        $user = $users->first();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $user is correct as $users->first() targeting TheCodingMachine\TDBM\ResultIterator::first() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
1021
        $this->assertNull($user);
1022
    }
1023
1024
    /**
1025
     * @depends testDaoGeneration
1026
     */
1027
    public function testCloneBeanAttachedBean(): void
1028
    {
1029
        $userDao = new TestUserDao($this->tdbmService);
1030
        $user = $userDao->getUserByLogin('bill.shakespeare');
1031
        $this->assertEquals(4, $user->getId());
1032
        $user2 = clone $user;
1033
        $this->assertNull($user2->getId());
1034
        $this->assertEquals('bill.shakespeare', $user2->getLogin());
1035
        $this->assertEquals('Bill Shakespeare', $user2->getName());
1036
        $this->assertEquals('UK', $user2->getCountry()->getLabel());
1037
1038
        // MANY 2 MANY must be duplicated
1039
        $this->assertEquals('Writers', $user2->getRoles()[0]->getName());
1040
1041
        // Let's test saving this clone
1042
        $user2->setLogin('william.shakespeare');
1043
        $userDao->save($user2);
1044
1045
        $user3 = $userDao->getUserByLogin('william.shakespeare');
1046
        $this->assertTrue($user3 === $user2);
1047
        $userDao->delete($user3);
1048
1049
        // Finally, let's test the origin user still exists!
1050
        $user4 = $userDao->getUserByLogin('bill.shakespeare');
1051
        $this->assertEquals('bill.shakespeare', $user4->getLogin());
1052
    }
1053
1054
    /**
1055
     * @depends testDaoGeneration
1056
     */
1057
    public function testCloneNewBean(): void
1058
    {
1059
        $countryDao = new CountryDao($this->tdbmService);
1060
        $roleDao = new RoleDao($this->tdbmService);
1061
        $role = $roleDao->getById(1);
1062
1063
        $userBean = new UserBean('John Doe', '[email protected]', $countryDao->getById(2), 'john.doe');
1064
        $userBean->addRole($role);
1065
1066
        $user2 = clone $userBean;
1067
1068
        $this->assertNull($user2->getId());
1069
        $this->assertEquals('john.doe', $user2->getLogin());
1070
        $this->assertEquals('John Doe', $user2->getName());
1071
        $this->assertEquals('UK', $user2->getCountry()->getLabel());
1072
1073
        // MANY 2 MANY must be duplicated
1074
        $this->assertEquals($role->getName(), $user2->getRoles()[0]->getName());
1075
    }
1076
1077
    /**
1078
     * @depends testDaoGeneration
1079
     */
1080
    public function testCascadeDelete(): void
1081
    {
1082
        $userDao = new TestUserDao($this->tdbmService);
1083
        $countryDao = new CountryDao($this->tdbmService);
1084
1085
        $spain = new CountryBean('Spain');
1086
        $sanchez = new UserBean('Manuel Sanchez', '[email protected]', $spain, 'manuel.sanchez');
1087
1088
        $countryDao->save($spain);
1089
        $userDao->save($sanchez);
1090
1091
        $speedy2 = $userDao->getUserByLogin('manuel.sanchez');
1092
        $this->assertTrue($sanchez === $speedy2);
1093
1094
        $exceptionTriggered = false;
1095
        try {
1096
            $countryDao->delete($spain);
1097
        } catch (ForeignKeyConstraintViolationException $e) {
1098
            $exceptionTriggered = true;
1099
        }
1100
        $this->assertTrue($exceptionTriggered);
1101
1102
        $countryDao->delete($spain, true);
1103
1104
        // Let's check that speed gonzalez was removed.
1105
        $speedy3 = $userDao->getUserByLogin('manuel.sanchez');
1106
        $this->assertNull($speedy3);
1107
    }
1108
1109
    /**
1110
     * @depends testDaoGeneration
1111
     */
1112
    public function testDiscardChanges(): void
1113
    {
1114
        $contactDao = new ContactDao($this->tdbmService);
1115
        $contactBean = $contactDao->getById(1);
1116
1117
        $oldName = $contactBean->getName();
1118
1119
        $contactBean->setName('MyNewName');
1120
1121
        $contactBean->discardChanges();
1122
1123
        $this->assertEquals($oldName, $contactBean->getName());
1124
    }
1125
1126
    /**
1127
     * @depends testDaoGeneration
1128
     */
1129
    public function testDiscardChangesOnNewBeanFails(): void
1130
    {
1131
        $person = new PersonBean('John Foo', new \DateTimeImmutable());
1132
        $this->expectException('TheCodingMachine\TDBM\TDBMException');
1133
        $person->discardChanges();
1134
    }
1135
1136
    /**
1137
     * @depends testDaoGeneration
1138
     */
1139
    public function testDiscardChangesOnDeletedBeanFails(): void
1140
    {
1141
        $userDao = new TestUserDao($this->tdbmService);
1142
        $countryDao = new CountryDao($this->tdbmService);
1143
1144
        $sanchez = new UserBean('Manuel Sanchez', '[email protected]', $countryDao->getById(1), 'manuel.sanchez');
1145
1146
        $userDao->save($sanchez);
1147
1148
        $userDao->delete($sanchez);
1149
1150
        $this->expectException('TheCodingMachine\TDBM\TDBMException');
1151
        // Cannot discard changes on a bean that is already deleted.
1152
        $sanchez->discardChanges();
1153
    }
1154
1155
    /**
1156
     * @depends testDaoGeneration
1157
     */
1158
    public function testUniqueIndexBasedSearch(): void
1159
    {
1160
        $userDao = new UserDao($this->tdbmService);
1161
        $user = $userDao->findOneByLogin('bill.shakespeare');
1162
1163
        $this->assertEquals('bill.shakespeare', $user->getLogin());
1164
        $this->assertEquals('Bill Shakespeare', $user->getName());
1165
    }
1166
1167
    /**
1168
     * @depends testDaoGeneration
1169
     */
1170
    public function testFindOneByRetunsNull(): void
1171
    {
1172
        // Let's assert that the findOneBy... methods can return null.
1173
        $userDao = new UserDao($this->tdbmService);
1174
        $userBean = $userDao->findOneByLogin('not_exist');
1175
1176
        $this->assertNull($userBean);
1177
    }
1178
1179
    /**
1180
     * @depends testDaoGeneration
1181
     */
1182
    public function testMultiColumnsIndexBasedSearch(): void
1183
    {
1184
        $countryDao = new CountryDao($this->tdbmService);
1185
        $userDao = new UserDao($this->tdbmService);
1186
        $users = $userDao->findByStatusAndCountry('on', $countryDao->getById(1));
1187
1188
        $this->assertEquals('jean.dupont', $users[0]->getLogin());
1189
    }
1190
1191
    /**
1192
     * @depends testDaoGeneration
1193
     */
1194
    public function testPartialMultiColumnsIndexBasedSearch(): void
1195
    {
1196
        $userDao = new UserDao($this->tdbmService);
1197
        $users = $userDao->findByStatusAndCountry('on');
1198
1199
        $this->assertCount(2, $users);
1200
    }
1201
1202
    /**
1203
     * @depends testDaoGeneration
1204
     */
1205
    public function testCreationInNullableDate(): void
1206
    {
1207
        $roleDao = new RoleDao($this->tdbmService);
1208
1209
        $role = new RoleBean('newbee');
1210
        $roleDao->save($role);
1211
1212
        $this->assertNull($role->getCreatedAt());
1213
    }
1214
1215
    /**
1216
     * @depends testDaoGeneration
1217
     */
1218
    public function testUpdateInNullableDate(): void
1219
    {
1220
        $roleDao = new RoleDao($this->tdbmService);
1221
1222
        $role = new RoleBean('newbee');
1223
        $roleDao->save($role);
1224
1225
        $role->setCreatedAt(null);
1226
        $roleDao->save($role);
1227
        $this->assertNull($role->getCreatedAt());
1228
    }
1229
1230
    /**
1231
     * @depends testDaoGeneration
1232
     */
1233
    public function testFindFromSql(): void
1234
    {
1235
        $roleDao = new TestRoleDao($this->tdbmService);
1236
1237
        $roles = $roleDao->getRolesByRightCanSing();
1238
        $this->assertCount(2, $roles);
1239
        $this->assertInstanceOf(RoleBean::class, $roles[0]);
1240
    }
1241
1242
    /**
1243
     * @depends testDaoGeneration
1244
     */
1245
    public function testFindOneFromSql(): void
1246
    {
1247
        $roleDao = new TestRoleDao($this->tdbmService);
1248
1249
        $role = $roleDao->getRoleByRightCanSingAndNameSinger();
1250
        $this->assertInstanceOf(RoleBean::class, $role);
1251
    }
1252
1253
    /**
1254
     * @depends testDaoGeneration
1255
     */
1256
    public function testCreateEmptyExtendedBean(): void
1257
    {
1258
        // This test cases checks issue https://github.com/thecodingmachine/database.tdbm/issues/92
1259
1260
        $dogDao = new DogDao($this->tdbmService);
1261
1262
        // We are not filling no field that is part of dog table.
1263
        $dog = new DogBean('Youki');
1264
        $dog->setOrder(1);
1265
1266
        $dogDao->save($dog);
1267
        $this->assertNull($dog->getRace());
1268
    }
1269
1270
    /**
1271
     * @depends testCreateEmptyExtendedBean
1272
     */
1273
    public function testFetchEmptyExtendedBean(): void
1274
    {
1275
        // This test cases checks issue https://github.com/thecodingmachine/database.tdbm/issues/92
1276
1277
        $animalDao = new AnimalDao($this->tdbmService);
1278
1279
        // We are not filling no field that is part of dog table.
1280
        $animalBean = $animalDao->getById(1);
1281
1282
        $this->assertInstanceOf(DogBean::class, $animalBean);
1283
    }
1284
1285
    /**
1286
     * @depends testDaoGeneration
1287
     */
1288
    public function testTwoBranchesHierarchy(): void
1289
    {
1290
        // This test cases checks issue https://github.com/thecodingmachine/mouf/issues/131
1291
1292
        $catDao = new CatDao($this->tdbmService);
1293
1294
        // We are not filling no field that is part of dog table.
1295
        $cat = new CatBean('Mew');
1296
        $cat->setOrder(2);
1297
1298
        $catDao->save($cat);
1299
        $this->assertNotNull($cat->getId());
1300
    }
1301
1302
    /**
1303
     * @depends testTwoBranchesHierarchy
1304
     */
1305
    public function testFetchTwoBranchesHierarchy(): void
1306
    {
1307
        // This test cases checks issue https://github.com/thecodingmachine/mouf/issues/131
1308
1309
        $animalDao = new AnimalDao($this->tdbmService);
1310
1311
        $animalBean = $animalDao->getById(2);
1312
1313
        $this->assertInstanceOf(CatBean::class, $animalBean);
1314
        /* @var $animalBean CatBean */
1315
        $animalBean->setCutenessLevel(999);
1316
        $animalBean->setUppercaseColumn('foobar');
1317
1318
        $animalDao->save($animalBean);
1319
    }
1320
1321
    /**
1322
     * @depends testDaoGeneration
1323
     */
1324
    public function testExceptionOnGetById(): void
1325
    {
1326
        $countryDao = new CountryDao($this->tdbmService);
1327
        $this->expectException(\TypeError::class);
1328
        $countryDao->getById(null);
1329
    }
1330
1331
    /**
1332
     * @depends testDaoGeneration
1333
     */
1334
    public function testDisconnectedManyToOne(): void
1335
    {
1336
        // This test cases checks issue https://github.com/thecodingmachine/database.tdbm/issues/99
1337
1338
        $country = new CountryBean('Spain');
1339
1340
        $user = new UserBean('John Doe', '[email protected]', $country, 'john.doe');
1341
1342
        $this->assertCount(1, $country->getUsers());
1343
        $this->assertSame($user, $country->getUsers()[0]);
1344
    }
1345
1346
    /**
1347
     * @depends testDaoGeneration
1348
     */
1349
    public function testOrderByExternalCol(): void
1350
    {
1351
        // This test cases checks issue https://github.com/thecodingmachine/database.tdbm/issues/106
1352
1353
        $userDao = new TestUserDao($this->tdbmService);
1354
        $users = $userDao->getUsersByCountryName();
1355
1356
        $this->assertEquals('UK', $users[0]->getCountry()->getLabel());
1357
    }
1358
1359
    /**
1360
     * @depends testDaoGeneration
1361
     */
1362
    public function testResultIteratorSort(): void
1363
    {
1364
        $userDao = new UserDao($this->tdbmService);
1365
        $users = $userDao->findAll()->withOrder('country.label DESC');
1366
1367
        $this->assertEquals('UK', $users[0]->getCountry()->getLabel());
1368
1369
        $users = $users->withOrder('country.label ASC');
1370
        $this->assertEquals('France', $users[0]->getCountry()->getLabel());
1371
    }
1372
1373
    /**
1374
     * @depends testDaoGeneration
1375
     */
1376
    public function testResultIteratorWithParameters(): void
1377
    {
1378
        $userDao = new TestUserDao($this->tdbmService);
1379
        $users = $userDao->getUsersByLoginStartingWith()->withParameters(['login' => 'bill%']);
1380
        $this->assertEquals('bill.shakespeare', $users[0]->getLogin());
1381
1382
        $users = $users->withParameters(['login' => 'jean%']);
1383
        $this->assertEquals('jean.dupont', $users[0]->getLogin());
1384
    }
1385
1386
    /**
1387
     * @depends testDaoGeneration
1388
     */
1389
    public function testOrderByExpression(): void
1390
    {
1391
        $userDao = new TestUserDao($this->tdbmService);
1392
        $users = $userDao->getUsersByReversedCountryName();
1393
1394
        $this->assertEquals('Jamaica', $users[0]->getCountry()->getLabel());
1395
    }
1396
1397
    /**
1398
     * @depends testDaoGeneration
1399
     */
1400
    public function testOrderByException(): void
1401
    {
1402
        $userDao = new TestUserDao($this->tdbmService);
1403
        $users = $userDao->getUsersByInvalidOrderBy();
1404
        $this->expectException(TDBMInvalidArgumentException::class);
1405
        $user = $users[0];
0 ignored issues
show
Unused Code introduced by
The assignment to $user is dead and can be removed.
Loading history...
1406
    }
1407
1408
    /**
1409
     * @depends testDaoGeneration
1410
     */
1411
    public function testOrderByProtectedColumn(): void
1412
    {
1413
        $animalDao = new AnimalDao($this->tdbmService);
1414
        $animals = $animalDao->findAll();
1415
        $animals = $animals->withOrder('`order` ASC');
1416
1417
        $this->assertInstanceOf(DogBean::class, $animals[0]);
1418
        $this->assertInstanceOf(CatBean::class, $animals[1]);
1419
1420
        $animals = $animals->withOrder('`order` DESC');
1421
1422
        $this->assertInstanceOf(CatBean::class, $animals[0]);
1423
        $this->assertInstanceOf(DogBean::class, $animals[1]);
1424
    }
1425
1426
    /**
1427
     * @depends testDaoGeneration
1428
     */
1429
    public function testGetOnAllNullableValues(): void
1430
    {
1431
        // Tests that a get performed on a column that has only nullable fields succeeds.
1432
        $allNullable = new AllNullableBean();
1433
        $this->assertNull($allNullable->getId());
1434
        $this->assertNull($allNullable->getLabel());
1435
        $this->assertNull($allNullable->getCountry());
1436
    }
1437
1438
    /**
1439
     * @depends testDaoGeneration
1440
     */
1441
    public function testExceptionOnMultipleInheritance(): void
1442
    {
1443
        $connection = self::getConnection();
1444
        self::insert($connection, 'animal', [
1445
            'id' => 99, 'name' => 'Snoofield',
1446
        ]);
1447
        self::insert($connection, 'dog', [
1448
            'id' => 99, 'race' => 'dog',
1449
        ]);
1450
        self::insert($connection, 'cat', [
1451
            'id' => 99, 'cuteness_level' => 0,
1452
        ]);
1453
1454
        $catched = false;
1455
        try {
1456
            $animalDao = new AnimalDao($this->tdbmService);
1457
            $animalDao->getById(99);
1458
        } catch (TDBMInheritanceException $e) {
1459
            $catched = true;
1460
        }
1461
        $this->assertTrue($catched, 'Exception TDBMInheritanceException was not caught');
1462
1463
        self::delete($connection, 'cat', ['id' => 99]);
1464
        self::delete($connection, 'dog', ['id' => 99]);
1465
        self::delete($connection, 'animal', ['id' => 99]);
1466
    }
1467
1468
    /**
1469
     * @depends testDaoGeneration
1470
     */
1471
    public function testReferenceNotSaved(): void
1472
    {
1473
        $boatDao = new BoatDao($this->tdbmService);
1474
1475
        $country = new CountryBean('Atlantis');
1476
        $boat = new BoatBean($country, 'Titanic');
1477
1478
        $boatDao->save($boat);
1479
        $this->assertNotNull($country->getId());
1480
    }
1481
1482
    /**
1483
     * @depends testReferenceNotSaved
1484
     */
1485
    public function testUniqueIndexOnForeignKeyThenScalar(): void
1486
    {
1487
        $boatDao = new BoatDao($this->tdbmService);
1488
        $countryDao = new CountryDao($this->tdbmService);
1489
1490
        $countryBean = $countryDao->findOneByLabel('Atlantis');
1491
        $boatBean = $boatDao->findOneByAnchorageCountryAndName($countryBean, 'Titanic');
1492
1493
        $this->assertNotNull($boatBean);
1494
    }
1495
1496
    /**
1497
     * @depends testDaoGeneration
1498
     */
1499
    public function testReferenceDeleted(): void
1500
    {
1501
        $countryDao = new CountryDao($this->tdbmService);
1502
        $boatDao = new BoatDao($this->tdbmService);
1503
1504
        $country = new CountryBean('Bikini Bottom');
1505
        $countryDao->save($country);
1506
1507
        $boat = new BoatBean($country, 'Squirrel boat');
1508
        $countryDao->delete($country);
1509
1510
        $this->expectException(TDBMMissingReferenceException::class);
1511
        $boatDao->save($boat);
1512
    }
1513
1514
    /**
1515
     * @depends testDaoGeneration
1516
     */
1517
    public function testCyclicReferenceWithInheritance(): void
1518
    {
1519
        $userDao = new UserDao($this->tdbmService);
1520
1521
        $country = new CountryBean('Norrisland');
1522
        $user = new UserBean('Chuck Norris', '[email protected]', $country, 'chuck.norris');
1523
1524
        $user->setManager($user);
1525
1526
        $this->expectException(TDBMCyclicReferenceException::class);
1527
        $userDao->save($user);
1528
    }
1529
1530
    /**
1531
     * @depends testDaoGeneration
1532
     */
1533
    public function testCyclicReference(): void
1534
    {
1535
        $categoryDao = new CategoryDao($this->tdbmService);
1536
1537
        $category = new CategoryBean('Root');
1538
1539
        $category->setParent($category);
1540
1541
        $this->expectException(TDBMCyclicReferenceException::class);
1542
        $categoryDao->save($category);
1543
    }
1544
1545
    /**
1546
     * @depends testDaoGeneration
1547
     */
1548
    public function testCorrectTypeForPrimaryKeyAfterSave(): void
1549
    {
1550
        // PosqtgreSQL does not particularly like empty inserts (i.e.: "INSERT INTO all_nullable () VALUES ()" )
1551
        $this->onlyMySql();
1552
1553
        $allNullableDao = new AllNullableDao($this->tdbmService);
1554
        $allNullable = new AllNullableBean();
1555
        $allNullableDao->save($allNullable);
1556
        $id = $allNullable->getId();
1557
1558
        $this->assertTrue(is_int($id));
1559
    }
1560
1561
    /**
1562
     * @depends testDaoGeneration
1563
     */
1564
    public function testPSR2Compliance(): void
1565
    {
1566
        $process = new Process(['vendor/bin/php-cs-fixer', 'fix', 'src/Test/', '--dry-run', '--diff', '--rules=@PSR2']);
1567
        $process->run();
1568
1569
        // executes after the command finishes
1570
        if (!$process->isSuccessful()) {
1571
            echo $process->getOutput();
1572
            $this->fail('Generated code is not PSR-2 compliant');
1573
        }
1574
        $this->assertTrue($process->isSuccessful());
1575
    }
1576
1577
    /**
1578
     * @depends testDaoGeneration
1579
     */
1580
    public function testFindOneByGeneration(): void
1581
    {
1582
        $reflectionMethod = new \ReflectionMethod(UserBaseDao::class, 'findOneByLogin');
1583
        $parameters = $reflectionMethod->getParameters();
1584
1585
        $this->assertCount(2, $parameters);
1586
        $this->assertSame('login', $parameters[0]->getName());
1587
        $this->assertSame('additionalTablesFetch', $parameters[1]->getName());
1588
    }
1589
1590
    /**
1591
     * @depends testDaoGeneration
1592
     */
1593
    public function testUuid(): void
1594
    {
1595
        $article = new ArticleBean('content');
1596
        $this->assertSame('content', $article->getContent());
1597
        $this->assertNotEmpty($article->getId());
1598
        $uuid = Uuid::fromString($article->getId());
1599
        $this->assertSame(1, $uuid->getVersion());
1600
    }
1601
1602
    /**
1603
     * @depends testDaoGeneration
1604
     */
1605
    public function testUuidv4(): void
1606
    {
1607
        $article = new Article2Bean('content');
1608
        $this->assertSame('content', $article->getContent());
1609
        $this->assertNotEmpty($article->getId());
1610
        $uuid = Uuid::fromString($article->getId());
1611
        $this->assertSame(4, $uuid->getVersion());
1612
    }
1613
1614
    /**
1615
     * @depends testDaoGeneration
1616
     */
1617
    public function testTypeHintedConstructors(): void
1618
    {
1619
        $userBaseBeanReflectionConstructor = new \ReflectionMethod(UserBaseBean::class, '__construct');
1620
        /** @var ReflectionNamedType $nameParam */
1621
        $nameParam = $userBaseBeanReflectionConstructor->getParameters()[0];
1622
1623
        $this->assertSame('string', $nameParam->getType()->getName());
0 ignored issues
show
Bug introduced by
The method getType() does not exist on ReflectionNamedType. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

1623
        $this->assertSame('string', $nameParam->/** @scrutinizer ignore-call */ getType()->getName());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1624
    }
1625
1626
    /**
1627
     * @depends testDaoGeneration
1628
     */
1629
    public function testSaveTransaction(): void
1630
    {
1631
        $countryDao = new CountryDao($this->tdbmService);
1632
1633
        $boatDao = new BoatDao($this->tdbmService);
1634
        $boatBean = $boatDao->getById(1);
1635
        $boatBean->setName('Bismark');
1636
1637
        $boatBean->getCountry();
1638
1639
        // Let's insert a row without telling TDBM to trigger an error!
1640
        self::insert($this->getConnection(), 'sailed_countries', [
1641
            'boat_id' => 1,
1642
            'country_id' => 2
1643
        ]);
1644
1645
        $boatBean->addCountry($countryDao->getById(2));
1646
1647
        $this->expectException(UniqueConstraintViolationException::class);
1648
1649
        $boatDao->save($boatBean);
1650
    }
1651
1652
    /**
1653
     * @depends testSaveTransaction
1654
     */
1655
    public function testSaveTransaction2(): void
1656
    {
1657
        $boatDao = new BoatDao($this->tdbmService);
1658
        $boatBean = $boatDao->getById(1);
1659
1660
        // The name should not have been saved because the transaction of the previous test should have rollbacked.
1661
        $this->assertNotSame('Bismark', $boatBean->getName());
1662
    }
1663
1664
    /**
1665
     * @depends testDaoGeneration
1666
     */
1667
    public function testForeignKeyPointingToNonPrimaryKey(): void
1668
    {
1669
        $dao = new RefNoPrimKeyDao($this->tdbmService);
1670
        $bean = $dao->getById(1);
1671
1672
        $this->assertSame('foo', $bean->getFrom()->getTo());
1673
1674
        $newBean = new RefNoPrimKeyBean($bean, 'baz');
1675
        $dao->save($newBean);
1676
        $this->assertSame('foo', $newBean->getFrom()->getTo());
1677
1678
        $resultSet = $bean->getRefNoPrimKey();
1679
        $this->assertCount(2, $resultSet);
1680
    }
1681
1682
    /**
1683
     * @depends testDaoGeneration
1684
     */
1685
    public function testCloningUuidBean(): void
1686
    {
1687
        $article = new ArticleBean('content');
1688
        $this->assertNotEmpty($article->getId());
1689
        $article2 = clone $article;
1690
        $this->assertNotEmpty($article2->getId());
1691
        $this->assertNotSame($article->getId(), $article2->getId());
1692
    }
1693
1694
    /**
1695
     * @depends testDaoGeneration
1696
     */
1697
    public function testRecursiveSave(): void
1698
    {
1699
        $categoryDao = new CategoryDao($this->tdbmService);
1700
1701
        $root1 = new CategoryBean('Root1');
1702
        $categoryDao->save($root1);
1703
        // Root 2 is not saved yet.
1704
        $root2 = new CategoryBean('Root2');
1705
        $intermediate = new CategoryBean('Intermediate');
1706
        $categoryDao->save($intermediate);
1707
1708
        // Let's switch the parent to a bean in detached state.
1709
        $intermediate->setParent($root2);
1710
1711
        // Now, let's save a new category that references the leaf category.
1712
        $leaf = new CategoryBean('Leaf');
1713
        $leaf->setParent($intermediate);
1714
        $categoryDao->save($leaf);
1715
        $this->assertNull($root2->getId());
1716
    }
1717
1718
    /**
1719
     * @depends testDaoGeneration
1720
     */
1721
    public function testBlob(): void
1722
    {
1723
        $fp = fopen(__FILE__, 'r');
1724
        $file = new FileBean($fp);
1725
1726
        $fileDao = new FileDao($this->tdbmService);
1727
1728
        $fileDao->save($file);
1729
1730
        $loadedFile = $fileDao->getById($file->getId());
1731
1732
        $resource = $loadedFile->getFile();
1733
        $result = fseek($resource, 0);
1734
        $this->assertSame(0, $result);
1735
        $this->assertSame('resource', gettype($resource));
1736
        $firstLine = fgets($resource);
1737
        $this->assertSame("<?php\n", $firstLine);
1738
    }
1739
1740
    /**
1741
     * @depends testBlob
1742
     */
1743
    public function testReadBlob(): void
1744
    {
1745
        $fileDao = new FileDao($this->tdbmService);
1746
        $loadedFile = $fileDao->getById(1);
1747
1748
        $resource = $loadedFile->getFile();
1749
        $this->assertSame('resource', gettype($resource));
1750
        $firstLine = fgets($resource);
1751
        $this->assertSame("<?php\n", $firstLine);
1752
1753
        stream_get_contents($resource);
1754
1755
        $loadedFile->setId($loadedFile->getId());
1756
1757
        $fileDao->save($loadedFile);
1758
    }
1759
1760
    /**
1761
     * @depends testReadBlob
1762
     */
1763
    public function testReadAndSaveBlob(): void
1764
    {
1765
        $fileDao = new FileDao($this->tdbmService);
1766
        $loadedFile = $fileDao->getById(1);
1767
1768
        $resource = $loadedFile->getFile();
1769
1770
        $firstLine = fgets($resource);
1771
        $this->assertSame("<?php\n", $firstLine);
1772
    }
1773
1774
    /**
1775
     * @depends testReadBlob
1776
     */
1777
    public function testProtectedGetterSetter(): void
1778
    {
1779
        $md5Getter = new ReflectionMethod(FileBaseBean::class, 'getMd5');
1780
        $md5Setter = new ReflectionMethod(FileBaseBean::class, 'setMd5');
1781
1782
        $this->assertTrue($md5Getter->isProtected());
1783
        $this->assertTrue($md5Setter->isProtected());
1784
1785
        $md5Getter2 = new ReflectionMethod(FileBaseBean::class, 'getArticle');
1786
        $md5Setter2 = new ReflectionMethod(FileBaseBean::class, 'setArticle');
1787
1788
        $this->assertTrue($md5Getter2->isProtected());
1789
        $this->assertTrue($md5Setter2->isProtected());
1790
1791
        $oneToManyGetter = new ReflectionMethod(ArticleBaseBean::class, 'getFiles');
1792
        $this->assertTrue($oneToManyGetter->isProtected());
1793
1794
        $fileDao = new FileDao($this->tdbmService);
1795
        $loadedFile = $fileDao->getById(1);
1796
1797
        // The md5 and article columns are not JSON serialized
1798
        $this->assertSame([
1799
            'id' => 1,
1800
        ], $loadedFile->jsonSerialize());
1801
    }
1802
1803
    /**
1804
     * @depends testDaoGeneration
1805
     */
1806
    public function testBlobResourceException(): void
1807
    {
1808
        $this->expectException(TDBMInvalidArgumentException::class);
1809
        $this->expectExceptionMessage('Invalid argument passed to \'TheCodingMachine\\TDBM\\Test\\Dao\\Bean\\Generated\\FileBaseBean::setFile\'. Expecting a resource. Got a string.');
1810
        new FileBean('foobar');
1811
    }
1812
1813
    /**
1814
     * @depends testDaoGeneration
1815
     */
1816
    public function testFilterBag(): void
1817
    {
1818
        $userDao = new TestUserDao($this->tdbmService);
1819
        $countryDao = new CountryDao($this->tdbmService);
1820
1821
        $country = $countryDao->getById(2);
1822
1823
        // Let's test filter bags by bean and filter bag with many values.
1824
        $users = $userDao->getUsersByComplexFilterBag($country, ['John Doe', 'Jane Doe']);
1825
1826
        $this->assertCount(1, $users);
1827
        $this->assertSame('John Doe', $users[0]->getName());
1828
    }
1829
1830
    /**
1831
     * @depends testDaoGeneration
1832
     */
1833
    public function testDecimalIsMappedToString(): void
1834
    {
1835
        $reflectionClass = new \ReflectionClass(BoatBaseBean::class);
1836
        $this->assertSame('string', $reflectionClass->getMethod('getLength')->getReturnType()->getName());
1837
    }
1838
1839
    /**
1840
     * @depends testDaoGeneration
1841
     */
1842
    public function testInsertMultiPrimaryKeysBean(): void
1843
    {
1844
        $countryDao = new CountryDao($this->tdbmService);
1845
1846
        $country = $countryDao->getById(1);
1847
1848
        $stateDao = new StateDao($this->tdbmService);
1849
        $state = new StateBean($country, 'IDF', 'Ile de France');
1850
        $stateDao->save($state);
1851
1852
        $this->assertSame($state, $stateDao->findAll()[0]);
1853
    }
1854
1855
    /**
1856
     * @depends testInsertMultiPrimaryKeysBean
1857
     */
1858
    public function testDeleteMultiPrimaryKeysBean(): void
1859
    {
1860
        $stateDao = new StateDao($this->tdbmService);
1861
1862
        $state = $stateDao->findAll()[0];
1863
        $stateDao->delete($state);
1864
        $this->assertCount(0, $stateDao->findAll());
1865
    }
1866
1867
    /**
1868
     * @depends testDaoGeneration
1869
     */
1870
    public function testCompositePrimaryKeyGetter(): void
1871
    {
1872
        $stateDao = new StateDao($this->tdbmService);
1873
        $country = new CountryBean('USA');
1874
        $stateBean = new StateBean($country, 'CA', 'California');
1875
        $stateDao->save($stateBean);
1876
        $this->assertSame($stateBean, $stateDao->getById($country->getId(), 'CA'));
1877
    }
1878
1879
    /**
1880
     * @depends testDaoGeneration
1881
     */
1882
    public function testSortOnInheritedTable(): void
1883
    {
1884
        $animalDao = new AnimalDao($this->tdbmService);
1885
1886
        // Let's insert an animal that is nothing.
1887
        $animal = new AnimalBean('Mickey');
1888
        $animalDao->save($animal);
1889
1890
        $animals = $animalDao->findAll()->withOrder('dog.race ASC');
1891
1892
        $animalsArr = $animals->toArray();
1893
        $this->assertCount(3, $animalsArr);
1894
    }
1895
1896
    /**
1897
     * @depends testDaoGeneration
1898
     */
1899
    public function testJsonKey(): void
1900
    {
1901
        $node = new NodeBean('foo.html');
1902
        $json = $node->jsonSerialize();
1903
        self::assertTrue(isset($json['basename']));
1904
        self::assertEquals('foo.html', $json['basename']);
1905
    }
1906
1907
    /**
1908
     * @depends testDaoGeneration
1909
     */
1910
    public function testJsonIgnore(): void
1911
    {
1912
        $nodeDao = new NodeDao($this->tdbmService);
1913
        $index = $nodeDao->getById(6);
1914
        $json = $index->jsonSerialize();
1915
        // Ignored scalar 'id'
1916
        self::assertTrue(!isset($json['id']));
1917
        // Ignored object 'root'
1918
        self::assertTrue(!isset($json['root']));
1919
        self::assertTrue(isset($json['guests']));
1920
        self::assertTrue(!empty($json['guests']));
1921
        $account = $index->getAccounts()[0];
1922
        $json = $account->jsonSerialize();
1923
        // Ignored array 'nodes' (from nodes_users table)
1924
        self::assertTrue(!isset($json['nodes']));
1925
    }
1926
1927
    /**
1928
     * @depends testDaoGeneration
1929
     */
1930
    public function testJsonInclude(): void
1931
    {
1932
        $nodeDao = new NodeDao($this->tdbmService);
1933
        $index = $nodeDao->getById(6);
1934
        $json = $index->jsonSerialize();
1935
        // Whole chain of parents should be serialized
1936
        self::assertTrue(isset($json['parent']));
1937
        self::assertTrue(isset($json['parent']['parent']));
1938
        self::assertTrue(isset($json['parent']['parent']['parent']));
1939
        self::assertEquals('/', $json['parent']['parent']['parent']['basename']);
1940
    }
1941
1942
    /**
1943
     * @depends testDaoGeneration
1944
     */
1945
    public function testJsonRecursive(): void
1946
    {
1947
        $nodeDao = new NodeDao($this->tdbmService);
1948
        $index = $nodeDao->getById(8);
1949
        $json = $index->jsonSerialize();
1950
        // Original chain of aliases is recursively serialized, ...
1951
        self::assertTrue(isset($json['alias']));
1952
        self::assertTrue(isset($json['alias']['alias']));
1953
        self::assertEquals('index.html', $json['alias']['alias']['basename']);
1954
        // ... each alias even serializes its parents, ...
1955
        self::assertTrue(isset($json['alias']['alias']['parent']['parent']));
1956
        // ... however, parents aliases chains have just their foreign key (id), as parents are serialized with $stopRecursion=true
1957
        self::assertEquals(3, $json['alias']['alias']['parent']['parent']['alias']['id']);
1958
        self::assertCount(1, $json['alias']['alias']['parent']['parent']['alias']);
1959
    }
1960
1961
    /**
1962
     * @depends testDaoGeneration
1963
     */
1964
    public function testJsonFormat(): void
1965
    {
1966
        $nodeDao = new NodeDao($this->tdbmService);
1967
        $index = $nodeDao->getById(6);
1968
        $json = $index->jsonSerialize();
1969
        self::assertTrue(isset($json['size']));
1970
        self::assertEquals('512 o', $json['size']);
1971
        self::assertEquals('42.50g', $json['weight']);
1972
        self::assertEquals($index->getCreatedAt()->format('Y-m-d'), $json['createdAt']);
1973
        self::assertEquals($index->getOwner()->getName(), $json['owner']);
1974
        self::assertEquals($index->getAccounts()[1]->getName(), $json['guests'][1]);
1975
        self::assertTrue(isset($json['entries']));
1976
        self::assertEquals('Hello, World', $json['entries'][1]);
1977
        $www = $index->getParent();
1978
        $json = $www->jsonSerialize();
1979
        self::assertEquals('0 o', $json['size']);
1980
        self::assertNull($json['weight']);
1981
        self::assertNull($json['owner']);
1982
    }
1983
1984
    /**
1985
     * @depends testDaoGeneration
1986
     */
1987
    public function testJsonCollection(): void
1988
    {
1989
        $artists = new ArtistDao($this->tdbmService);
1990
        $pinkFloyd = $artists->getById(1);
1991
        $animals =  $pinkFloyd->getAlbums()[0];
1992
        $json = $pinkFloyd->jsonSerialize();
1993
        // Collection name properly handled ('discography' instead of default 'albums')
1994
        self::assertTrue(isset($json['discography']));
1995
        self::assertEquals($animals->getTitle(), $json['discography'][0]['title']);
1996
        // Make sure top object have just its primary key
1997
        self::assertEquals(1, $json['discography'][0]['artist']['id']);
1998
        self::assertCount(1, $json['discography'][0]['artist']);
1999
        $json = $animals->jsonSerialize();
2000
        // Nevertheless, artist should be serialized in album as top object...
2001
        self::assertTrue(isset($json['artist']));
2002
        // ... as should be tracks...
2003
        self::assertTrue(isset($json['tracks'][0]));
2004
        self::assertEquals('Pigs on the Wing 1', $json['tracks'][0]['title']);
2005
        // ... and, ultimately, list of featuring artists, since feat is included
2006
        self::assertTrue(isset($json['tracks'][0]['feat'][0]));
2007
        self::assertEquals('Roger Waters', $json['tracks'][0]['feat'][0]['name']);
2008
    }
2009
2010
    public function testFloydHasNoParent(): void
2011
    {
2012
        $artists = new ArtistDao($this->tdbmService);
2013
        $pinkFloyd = $artists->getById(1);
2014
        $parents = $pinkFloyd->getParents();
2015
2016
        $this->assertEquals([], $parents);
2017
    }
2018
2019
    public function testFloydHasOneChild(): void
2020
    {
2021
        $artists = new ArtistDao($this->tdbmService);
2022
        $pinkFloyd = $artists->getById(1);
2023
        $children = $pinkFloyd->getChildrenByArtistsRelations();
2024
2025
        $this->assertEquals(1, count($children));
2026
        $this->assertEquals(2, $children[0]->getId());
2027
    }
2028
2029
    /**
2030
     * @depends testDaoGeneration
2031
     */
2032
    public function testAddInterfaceAnnotation(): void
2033
    {
2034
        if (!$this->tdbmService->getConnection()->getDatabasePlatform() instanceof MySqlPlatform) {
2035
            // See https://github.com/doctrine/dbal/pull/3512
2036
            $this->markTestSkipped('Only MySQL supports table level comments');
2037
        }
2038
2039
        $refClass = new ReflectionClass(UserBaseBean::class);
2040
        $this->assertTrue($refClass->implementsInterface(TestUserInterface::class));
2041
    }
2042
2043
    /**
2044
     * @depends testDaoGeneration
2045
     */
2046
    public function testAddInterfaceOnDaoAnnotation(): void
2047
    {
2048
        if (!$this->tdbmService->getConnection()->getDatabasePlatform() instanceof MySqlPlatform) {
2049
            // See https://github.com/doctrine/dbal/pull/3512
2050
            $this->markTestSkipped('Only MySQL supports table level comments');
2051
        }
2052
2053
        $refClass = new ReflectionClass(UserBaseDao::class);
2054
        $this->assertTrue($refClass->implementsInterface(TestUserDaoInterface::class));
2055
    }
2056
2057
    /**
2058
     * @depends testDaoGeneration
2059
     */
2060
    public function testTrait(): void
2061
    {
2062
        if (!$this->tdbmService->getConnection()->getDatabasePlatform() instanceof MySqlPlatform) {
2063
            // See https://github.com/doctrine/dbal/pull/3512
2064
            $this->markTestSkipped('Only MySQL supports table level comments');
2065
        }
2066
2067
        $userDao = new UserDao($this->tdbmService);
2068
        $userBean = $userDao->getById(1);
2069
2070
        $this->assertSame('TestOtherUserTrait', $userBean->method1());
2071
        $this->assertSame('TestUserTrait', $userBean->method1renamed());
2072
2073
        $refClass = new ReflectionClass(UserBaseDao::class);
2074
        $this->assertTrue($refClass->hasMethod('findNothing'));
2075
    }
2076
2077
    /**
2078
     * @depends testDaoGeneration
2079
     */
2080
    public function testNonInstantiableAbstractDaosAndBeans(): void
2081
    {
2082
        $refClass = new ReflectionClass(UserBaseDao::class);
2083
        $this->assertFalse($refClass->isInstantiable());
2084
2085
        $refClass = new ReflectionClass(UserBaseBean::class);
2086
        $this->assertFalse($refClass->isInstantiable());
2087
    }
2088
2089
    /**
2090
     * @depends testDaoGeneration
2091
     */
2092
    public function testCanNullifyBlob(): void
2093
    {
2094
        $article = new ArticleBean('content');
2095
        $fp = fopen(__FILE__, 'r');
2096
        $article->setAttachment($fp);
2097
        $article->setAttachment(null);
2098
        $this->assertNull($article->getAttachment(null));
2099
        fclose($fp);
0 ignored issues
show
Bug introduced by
It seems like $fp can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

2099
        fclose(/** @scrutinizer ignore-type */ $fp);
Loading history...
2100
    }
2101
2102
    /**
2103
     * @depends testDaoGeneration
2104
     */
2105
    public function testOptionnalParametersCanBeNullInFindOneBy()
2106
    {
2107
        $albumDao = new AlbumDao($this->tdbmService);
2108
        $artist = new ArtistBean('Marcel');
2109
2110
        $albumDao->findOneByArtistAndNode($artist, null);
2111
        $this->assertEquals(1, 1);
2112
    }
2113
2114
    /**
2115
     * @depends testDaoGeneration
2116
     */
2117
    public function testRequiredParametersCannotBeNullInFindOneBy()
2118
    {
2119
        $albumDao = new AlbumDao($this->tdbmService);
2120
        $artist = new ArtistBean('Marcel');
2121
        $account = new AccountBean('Jamie');
2122
2123
        $albumDao->findOneByArtistAndAccount($artist, $account);
2124
2125
        $this->expectException('TypeError');
2126
        $albumDao->findOneByArtistAndAccount($artist, null);
2127
    }
2128
2129
    /**
2130
     * @depends testDaoGeneration
2131
     */
2132
    public function testLazyLoad(): void
2133
    {
2134
        $roleDao = new RoleDao($this->tdbmService);
2135
        $roleBean = $roleDao->getById(1, true);
2136
2137
        $this->assertSame(TDBMObjectStateEnum::STATE_NOT_LOADED, $roleBean->_getDbRows()['roles']->_getStatus());
2138
        $roleBean->getId();
2139
        $this->assertSame(TDBMObjectStateEnum::STATE_NOT_LOADED, $roleBean->_getDbRows()['roles']->_getStatus());
2140
    }
2141
2142
    /**
2143
     * @depends testDaoGeneration
2144
     */
2145
    public function testOneToOneInverseRelationGetter(): void
2146
    {
2147
        $objectBaseDao = new BaseObjectDao($this->tdbmService);
2148
        $objectInheritedDao = new InheritedObjectDao($this->tdbmService);
2149
        $objectBase = new BaseObjectBean('label');
2150
        $objectBaseDao->save($objectBase);
2151
        $this->assertNull($objectBase->getInheritedObject());
2152
        $objectInherited = new InheritedObjectBean($objectBase);
2153
        $objectInheritedDao->save($objectInherited);
2154
        $this->assertSame($objectInherited, $objectBase->getInheritedObject());
2155
        $this->assertEquals(1, $objectBase->jsonSerialize()['inheritedObject']['id']);
2156
    }
2157
2158
    public function testLazyStopRecursion(): void
2159
    {
2160
        $albumDao = new AlbumDao($this->tdbmService);
2161
        $albumBean = $albumDao->getById(1);
2162
        $json = $albumBean->jsonSerialize(true);
2163
        $this->assertArrayHasKey('id', $json['artist']);
2164
        $this->assertArrayNotHasKey('name', $json['artist']);
2165
    }
2166
2167
    public function testLazyStopRecursionOnCompositeForeignKey(): void
2168
    {
2169
        $compositeFkSourceDao = new CompositeFkSourceDao($this->tdbmService);
2170
        $compositeFkSourceBean = $compositeFkSourceDao->getById(1);
2171
        $json = $compositeFkSourceBean->jsonSerialize(true);
2172
        $this->assertEquals(1, $json['compositeFkTarget']['1']['id']);
2173
        $this->assertEquals(1, $json['compositeFkTarget']['id2']);
2174
    }
2175
2176
    public function testMethodNameConflictsBetweenRegularAndAutoPivotProperties(): void
2177
    {
2178
        $artist = new ArtistBean('Super');
2179
        $artist->getChildren(); // regular property
2180
        $artist->getChildrenByArtistId(); // one-to-may relationship
2181
        $artist->getChildrenByArtistsRelations(); // auto-pivot relationship
2182
        $this->assertEquals(1, 1);
2183
    }
2184
2185
    public function testFindByDateTime(): void
2186
    {
2187
        $personDao = new PersonDao($this->tdbmService);
2188
        $personDao->findByModifiedAt(new \DateTimeImmutable())->count();
2189
        $this->assertTrue(true);
2190
    }
2191
2192
    /**
2193
     * Bug: find from sql use a `COUNT(DISTINCT *)` which fails because of null values.
2194
     */
2195
    public function testFindFromRawSqlCount(): void
2196
    {
2197
        $dao = new TestAlbumDao($this->tdbmService);
2198
        $albums = $dao->findAllFromRawSql();
2199
2200
        $firstAlbum = $albums->first();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $firstAlbum is correct as $albums->first() targeting TheCodingMachine\TDBM\ResultIterator::first() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
2201
        assert($firstAlbum instanceof AlbumBean);
2202
        $this->assertNull($firstAlbum->getNode()); // This null ensure reproducibility of the bug
2203
        $expectedCount = $dao->findAllFromRawSqlWithCount()->count();
2204
        $this->assertEquals($expectedCount, $albums->count());
2205
    }
2206
2207
    public function testFindFromRawSQLOnInheritance(): void
2208
    {
2209
        $dao = new TestPersonDao($this->tdbmService);
2210
        $objects = $dao->testFindFromRawSQLOnInherited();
2211
2212
        $this->assertNotNull($objects->first());
0 ignored issues
show
Bug introduced by
Are you sure the usage of $objects->first() targeting TheCodingMachine\TDBM\ResultIterator::first() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
2213
        $this->assertEquals(6, $objects->count());
2214
    }
2215
2216
    public function testInheritanceFkWithDifferentPkName(): void
2217
    {
2218
        $inheritanceSocietyDao = new InheritanceSocietyDao($this->tdbmService);
2219
        $inheritanceAgencyDao = new InheritanceAgencyDao($this->tdbmService);
2220
        $society = new InheritanceSocietyBean('test');
2221
        $inheritanceSocietyDao->save($society);
2222
        $this->assertNotNull($society->getId());
2223
        $agency = new InheritanceAgencyBean($society);
2224
        $inheritanceAgencyDao->save($agency);
2225
    }
2226
}
2227