Passed
Pull Request — master (#146)
by David
03:00
created

TDBMDaoGeneratorTest::testTrait()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 15
rs 10
c 0
b 0
f 0
cc 2
nc 2
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 TheCodingMachine\TDBM\Dao\TestArticleDao;
34
use TheCodingMachine\TDBM\Dao\TestCountryDao;
35
use TheCodingMachine\TDBM\Dao\TestRoleDao;
36
use TheCodingMachine\TDBM\Dao\TestUserDao;
37
use TheCodingMachine\TDBM\Fixtures\Interfaces\TestUserDaoInterface;
38
use TheCodingMachine\TDBM\Fixtures\Interfaces\TestUserInterface;
39
use TheCodingMachine\TDBM\Test\Dao\AlbumDao;
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...
40
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...
41
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...
42
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...
43
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...
44
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...
45
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...
46
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...
47
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...
48
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...
49
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...
50
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...
51
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...
52
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...
53
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...
54
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...
55
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...
56
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...
57
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...
58
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...
59
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...
60
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...
61
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...
62
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...
63
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...
64
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...
65
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...
66
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...
67
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...
68
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...
69
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...
70
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...
71
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...
72
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...
73
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...
74
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...
75
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...
76
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...
77
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...
78
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...
79
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...
80
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...
81
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...
82
use TheCodingMachine\TDBM\Utils\PathFinder\NoPathFoundException;
83
use TheCodingMachine\TDBM\Utils\PathFinder\PathFinder;
84
use TheCodingMachine\TDBM\Utils\TDBMDaoGenerator;
85
use Symfony\Component\Process\Process;
86
87
class TDBMDaoGeneratorTest extends TDBMAbstractServiceTest
88
{
89
    /** @var TDBMDaoGenerator $tdbmDaoGenerator */
90
    protected $tdbmDaoGenerator;
91
92
    private $rootPath;
93
94
    protected function setUp(): void
95
    {
96
        parent::setUp();
97
        $schemaManager = $this->tdbmService->getConnection()->getSchemaManager();
98
        $schemaAnalyzer = new SchemaAnalyzer($schemaManager);
99
        $tdbmSchemaAnalyzer = new TDBMSchemaAnalyzer($this->tdbmService->getConnection(), new ArrayCache(), $schemaAnalyzer);
100
        $this->tdbmDaoGenerator = new TDBMDaoGenerator($this->getConfiguration(), $tdbmSchemaAnalyzer);
101
        $this->rootPath = __DIR__ . '/../';
102
        //$this->tdbmDaoGenerator->setComposerFile($this->rootPath.'composer.json');
103
    }
104
105
    public function testDaoGeneration(): void
106
    {
107
        // Remove all previously generated files.
108
        $this->recursiveDelete($this->rootPath . 'src/Test/Dao/');
109
        mkdir($this->rootPath . 'src/Test/Dao/Generated', 0755, true);
110
        // Let's generate a dummy file to see it is indeed removed.
111
        $dummyFile = $this->rootPath . 'src/Test/Dao/Generated/foobar.php';
112
        touch($dummyFile);
113
        $this->assertFileExists($dummyFile);
114
115
        $this->tdbmDaoGenerator->generateAllDaosAndBeans();
116
117
        $this->assertFileNotExists($dummyFile);
118
119
        // Let's require all files to check they are valid PHP!
120
        // Test the daoFactory
121
        require_once $this->rootPath . 'src/Test/Dao/Generated/DaoFactory.php';
122
        // Test the others
123
124
        $beanDescriptors = $this->getDummyGeneratorListener()->getBeanDescriptors();
125
126
        foreach ($beanDescriptors as $beanDescriptor) {
127
            $daoName = $beanDescriptor->getDaoClassName();
128
            $daoBaseName = $beanDescriptor->getBaseDaoClassName();
129
            $beanName = $beanDescriptor->getBeanClassName();
130
            $baseBeanName = $beanDescriptor->getBaseBeanClassName();
131
            require_once $this->rootPath . 'src/Test/Dao/Bean/Generated/' . $baseBeanName . '.php';
132
            require_once $this->rootPath . 'src/Test/Dao/Bean/' . $beanName . '.php';
133
            require_once $this->rootPath . 'src/Test/Dao/Generated/' . $daoBaseName . '.php';
134
            require_once $this->rootPath . 'src/Test/Dao/' . $daoName . '.php';
135
        }
136
137
        // Check that pivot tables do not generate DAOs or beans.
138
        $this->assertFalse(class_exists('TheCodingMachine\\TDBM\\Test\\Dao\\RolesRightDao'));
139
    }
140
141
    public function testGenerationException(): void
142
    {
143
        $configuration = new Configuration('UnknownVendor\\Dao', 'UnknownVendor\\Bean', self::getConnection(), $this->getNamingStrategy());
144
145
        $schemaManager = $this->tdbmService->getConnection()->getSchemaManager();
146
        $schemaAnalyzer = new SchemaAnalyzer($schemaManager);
147
        $tdbmSchemaAnalyzer = new TDBMSchemaAnalyzer($this->tdbmService->getConnection(), new ArrayCache(), $schemaAnalyzer);
148
        $tdbmDaoGenerator = new TDBMDaoGenerator($configuration, $tdbmSchemaAnalyzer);
149
        $this->rootPath = __DIR__ . '/../../../../';
150
        //$tdbmDaoGenerator->setComposerFile($this->rootPath.'composer.json');
151
152
        $this->expectException(NoPathFoundException::class);
153
        $tdbmDaoGenerator->generateAllDaosAndBeans();
154
    }
155
156
    /**
157
     * Delete a file or recursively delete a directory.
158
     *
159
     * @param string $str Path to file or directory
160
     * @return bool
161
     */
162
    private function recursiveDelete(string $str): bool
163
    {
164
        if (is_file($str)) {
165
            return @unlink($str);
166
        } elseif (is_dir($str)) {
167
            $scan = glob(rtrim($str, '/') . '/*');
168
            foreach ($scan as $index => $path) {
169
                $this->recursiveDelete($path);
170
            }
171
172
            return @rmdir($str);
173
        }
174
        return false;
175
    }
176
177
    /**
178
     * @depends testDaoGeneration
179
     */
180
    public function testGetBeanClassName(): void
181
    {
182
        $this->assertEquals(UserBean::class, $this->tdbmService->getBeanClassName('users'));
183
184
        // Let's create another TDBMService to test the cache.
185
        $configuration = new Configuration('TheCodingMachine\\TDBM\\Test\\Dao\\Bean', 'TheCodingMachine\\TDBM\\Test\\Dao', self::getConnection(), $this->getNamingStrategy(), $this->getCache(), null, null, [$this->getDummyGeneratorListener()]);
186
        $configuration->setPathFinder(new PathFinder(null, dirname(__DIR__, 4)));
187
        $newTdbmService = new TDBMService($configuration);
188
        $this->assertEquals(UserBean::class, $newTdbmService->getBeanClassName('users'));
189
    }
190
191
    /**
192
     * @depends testDaoGeneration
193
     */
194
    public function testGeneratedGetById(): void
195
    {
196
        $contactDao = new ContactDao($this->tdbmService);
197
        $contactBean = $contactDao->getById(1);
198
        $this->assertEquals(1, $contactBean->getId());
199
        $this->assertInstanceOf('\\DateTimeInterface', $contactBean->getCreatedAt());
200
201
        // FIXME: Question: que faire du paramètre stockage "UTC"????
202
    }
203
204
    /**
205
     * @depends testDaoGeneration
206
     */
207
    public function testGeneratedGetByIdLazyLoaded(): void
208
    {
209
        $roleDao = new RoleDao($this->tdbmService);
210
        $roleBean = $roleDao->getById(1, true);
211
        $this->assertEquals(1, $roleBean->getId());
212
        $this->assertInstanceOf('\\DateTimeInterface', $roleBean->getCreatedAt());
213
214
        $roleBean2 = $roleDao->getById(1, true);
215
        $this->assertTrue($roleBean === $roleBean2);
216
    }
217
218
    /**
219
     * @depends testDaoGeneration
220
     */
221
    public function testDefaultValueOnNewBean(): void
222
    {
223
        $roleBean = new RoleBean('my_role');
224
        $this->assertEquals(1, $roleBean->getStatus());
225
    }
226
227
    /**
228
     * @depends testDaoGeneration
229
     */
230
    public function testForeignKeyInBean(): void
231
    {
232
        $userDao = new UserDao($this->tdbmService);
233
        $userBean = $userDao->getById(1);
234
        $country = $userBean->getCountry();
235
236
        $this->assertEquals('UK', $country->getLabel());
237
238
        $userBean2 = $userDao->getById(1);
239
        $this->assertTrue($userBean === $userBean2);
240
241
        $contactDao = new ContactDao($this->tdbmService);
242
        $contactBean = $contactDao->getById(1);
243
244
        $this->assertTrue($userBean === $contactBean);
245
    }
246
247
    /**
248
     * @depends testDaoGeneration
249
     */
250
    public function testNewBeans(): void
251
    {
252
        $countryDao = new CountryDao($this->tdbmService);
253
        $userDao = new UserDao($this->tdbmService);
254
        $userBean = new UserBean('John Doe', '[email protected]', $countryDao->getById(2), 'john.doe');
255
        $userBean->setOrder(1); // Let's set a "protected keyword" column.
256
257
        $userDao->save($userBean);
258
259
        $this->assertNull($userBean->getManager());
260
    }
261
262
    /**
263
     * @depends testDaoGeneration
264
     */
265
    public function testDateTimeImmutableGetter(): void
266
    {
267
        $userDao = new UserDao($this->tdbmService);
268
        $user = $userDao->getById(1);
269
270
        $this->assertInstanceOf('\DateTimeImmutable', $user->getCreatedAt());
271
    }
272
273
    /**
274
     * @depends testDaoGeneration
275
     */
276
    public function testAssigningNewBeans(): void
277
    {
278
        $userDao = new UserDao($this->tdbmService);
279
        $countryBean = new CountryBean('Mexico');
280
        $userBean = new UserBean('Speedy Gonzalez', '[email protected]', $countryBean, 'speedy.gonzalez');
281
        $this->assertEquals($countryBean, $userBean->getCountry());
282
283
        $userDao->save($userBean);
284
    }
285
286
    /**
287
     * @depends testAssigningNewBeans
288
     */
289
    public function testUpdatingProtectedColumn(): void
290
    {
291
        $userDao = new UserDao($this->tdbmService);
292
        $userBean = $userDao->findOneByLogin('speedy.gonzalez');
293
        $userBean->setOrder(2);
294
        $userDao->save($userBean);
295
        $this->assertSame(2, $userBean->getOrder());
296
    }
297
298
    /**
299
     * @depends testDaoGeneration
300
     */
301
    public function testAssigningExistingRelationship(): void
302
    {
303
        $userDao = new UserDao($this->tdbmService);
304
        $user = $userDao->getById(1);
305
        $countryDao = new CountryDao($this->tdbmService);
306
        $country = $countryDao->getById(2);
307
308
        $user->setCountry($country);
309
        $this->assertEquals(TDBMObjectStateEnum::STATE_DIRTY, $user->_getStatus());
310
    }
311
312
    /**
313
     * @depends testDaoGeneration
314
     */
315
    public function testDirectReversedRelationship(): void
316
    {
317
        $countryDao = new CountryDao($this->tdbmService);
318
        $country = $countryDao->getById(1);
319
        $users = $country->getUsers();
320
321
        $arr = $users->toArray();
322
323
        $this->assertCount(1, $arr);
324
        $this->assertInstanceOf('TheCodingMachine\\TDBM\\Test\\Dao\\Bean\\UserBean', $arr[0]);
325
        $this->assertEquals('jean.dupont', $arr[0]->getLogin());
326
327
        $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...
328
        $users = $country->getUsers();
329
330
        $arr = $users->toArray();
331
332
        $this->assertCount(2, $arr);
333
        $this->assertInstanceOf('TheCodingMachine\\TDBM\\Test\\Dao\\Bean\\UserBean', $arr[1]);
334
        $this->assertEquals('speedy.gonzalez', $arr[1]->getLogin());
335
    }
336
337
    /**
338
     * @depends testDaoGeneration
339
     */
340
    public function testDeleteInDirectReversedRelationship(): void
341
    {
342
        $countryDao = new CountryDao($this->tdbmService);
343
        $country = $countryDao->getById(1);
344
345
        $userDao = new UserDao($this->tdbmService);
346
        $newUser = new UserBean('John Snow', '[email protected]', $country, 'john.snow');
347
        $userDao->save($newUser);
348
349
        $users = $country->getUsers();
350
351
        $arr = $users->toArray();
352
353
        $this->assertCount(2, $arr);
354
355
        $userDao->delete($arr[1]);
356
357
        $users = $country->getUsers();
358
        $arr = $users->toArray();
359
        $this->assertCount(1, $arr);
360
    }
361
362
    /**
363
     * @depends testDaoGeneration
364
     */
365
    public function testJointureGetters(): void
366
    {
367
        $roleDao = new RoleDao($this->tdbmService);
368
        $role = $roleDao->getById(1);
369
        $users = $role->getUsers();
370
371
        $this->assertCount(2, $users);
372
        $this->assertInstanceOf('TheCodingMachine\\TDBM\\Test\\Dao\\Bean\\UserBean', $users[0]);
373
374
        $rights = $role->getRights();
375
376
        $this->assertCount(2, $rights);
377
        $this->assertInstanceOf('TheCodingMachine\\TDBM\\Test\\Dao\\Bean\\RightBean', $rights[0]);
378
    }
379
380
    /**
381
     * @depends testDaoGeneration
382
     */
383
    public function testNestedIterationOnAlterableResultIterator(): void
384
    {
385
        $countryDao = new CountryDao($this->tdbmService);
386
        $country = $countryDao->getById(2);
387
388
        $count = 0;
389
        // Let's perform 2 nested calls to check that iterators do not melt.
390
        foreach ($country->getUsers() as $user) {
391
            foreach ($country->getUsers() as $user2) {
392
                $count++;
393
            }
394
        }
395
        // There are 3 users linked to country 2.
396
        $this->assertSame(9, $count);
397
    }
398
399
    /**
400
     * @depends testDaoGeneration
401
     */
402
    public function testNewBeanConstructor(): void
403
    {
404
        $role = new RoleBean('Newrole');
405
        $this->assertEquals(TDBMObjectStateEnum::STATE_DETACHED, $role->_getStatus());
406
    }
407
408
    /**
409
     * @depends testDaoGeneration
410
     */
411
    public function testJointureAdderOnNewBean(): void
412
    {
413
        $countryDao = new CountryDao($this->tdbmService);
414
        $country = $countryDao->getById(1);
415
        $user = new UserBean('Speedy Gonzalez', '[email protected]', $country, 'speedy.gonzalez');
416
        $role = new RoleBean('Mouse');
417
        $user->addRole($role);
418
        $roles = $user->getRoles();
419
        $this->assertCount(1, $roles);
420
        $role = $roles[0];
421
        $this->assertInstanceOf('TheCodingMachine\\TDBM\\Test\\Dao\\Bean\\RoleBean', $role);
422
        $users = $role->getUsers();
423
        $this->assertCount(1, $users);
424
        $this->assertEquals($user, $users[0]);
425
426
        $role->removeUser($user);
427
        $this->assertCount(0, $role->getUsers());
428
        $this->assertCount(0, $user->getRoles());
429
    }
430
431
    /**
432
     * @depends testDaoGeneration
433
     */
434
    public function testJointureDeleteBeforeGetters(): void
435
    {
436
        $roleDao = new RoleDao($this->tdbmService);
437
        $userDao = new UserDao($this->tdbmService);
438
        $role = $roleDao->getById(1);
439
        $user = $userDao->getById(1);
440
441
        // We call removeUser BEFORE calling getUsers
442
        // This should work as expected.
443
        $role->removeUser($user);
444
        $users = $role->getUsers();
445
446
        $this->assertCount(1, $users);
447
    }
448
449
    /**
450
     * @depends testDaoGeneration
451
     */
452
    public function testJointureMultiAdd(): void
453
    {
454
        $countryDao = new CountryDao($this->tdbmService);
455
        $country = $countryDao->getById(1);
456
        $user = new UserBean('Speedy Gonzalez', '[email protected]', $country, 'speedy.gonzalez');
457
        $role = new RoleBean('Mouse');
458
        $user->addRole($role);
459
        $role->addUser($user);
460
        $user->addRole($role);
461
462
        $this->assertCount(1, $user->getRoles());
463
    }
464
465
    /**
466
     * Step 1: we remove the role 1 from user 1 but save role 1.
467
     * Expected result: no save done.
468
     *
469
     * @depends testDaoGeneration
470
     */
471
    public function testJointureSave1(): void
472
    {
473
        $roleDao = new RoleDao($this->tdbmService);
474
        $role = $roleDao->getById(1);
475
        $userDao = new UserDao($this->tdbmService);
476
        $user = $userDao->getById(1);
477
478
        $this->assertTrue($user->hasRole($role));
479
        $this->assertTrue($role->hasUser($user));
480
        $user->removeRole($role);
481
        $this->assertFalse($user->hasRole($role));
482
        $this->assertFalse($role->hasUser($user));
483
        $roleDao->save($role);
484
485
        $this->assertEquals(TDBMObjectStateEnum::STATE_DIRTY, $user->_getStatus());
486
        $this->assertEquals(TDBMObjectStateEnum::STATE_LOADED, $role->_getStatus());
487
    }
488
489
    /**
490
     * Step 2: we check that nothing was saved
491
     * Expected result: no save done.
492
     *
493
     * @depends testJointureSave1
494
     */
495
    public function testJointureSave2(): void
496
    {
497
        $roleDao = new RoleDao($this->tdbmService);
498
        $role = $roleDao->getById(1);
499
        $this->assertCount(2, $role->getUsers());
500
    }
501
502
    /**
503
     * Step 3: we remove the role 1 from user 1 and save user 1.
504
     * Expected result: save done.
505
     *
506
     * @depends testJointureSave2
507
     */
508
    public function testJointureSave3(): void
509
    {
510
        $roleDao = new RoleDao($this->tdbmService);
511
        $role = $roleDao->getById(1);
512
        $userDao = new UserDao($this->tdbmService);
513
        $user = $userDao->getById(1);
514
515
        $this->assertCount(1, $user->getRoles());
516
        $user->removeRole($role);
517
        $this->assertCount(0, $user->getRoles());
518
        $userDao->save($user);
519
        $this->assertCount(0, $user->getRoles());
520
    }
521
522
    /**
523
     * Step 4: we check that save was done
524
     * Expected result: save done.
525
     *
526
     * @depends testJointureSave3
527
     */
528
    public function testJointureSave4(): void
529
    {
530
        $roleDao = new RoleDao($this->tdbmService);
531
        $role = $roleDao->getById(1);
532
        $this->assertCount(1, $role->getUsers());
533
        $userDao = new UserDao($this->tdbmService);
534
        $user = $userDao->getById(1);
535
        $this->assertCount(0, $user->getRoles());
536
    }
537
538
    /**
539
     * Step 5: we add the role 1 from user 1 and save user 1.
540
     * Expected result: save done.
541
     *
542
     * @depends testJointureSave4
543
     */
544
    public function testJointureSave5(): void
545
    {
546
        $roleDao = new RoleDao($this->tdbmService);
547
        $role = $roleDao->getById(1);
548
        $userDao = new UserDao($this->tdbmService);
549
        $user = $userDao->getById(1);
550
551
        $user->addRole($role);
552
        $this->assertCount(1, $user->getRoles());
553
        $userDao->save($user);
554
    }
555
556
    /**
557
     * Step 6: we check that save was done
558
     * Expected result: save done.
559
     *
560
     * @depends testJointureSave5
561
     */
562
    public function testJointureSave6(): void
563
    {
564
        $roleDao = new RoleDao($this->tdbmService);
565
        $role = $roleDao->getById(1);
566
        $this->assertCount(2, $role->getUsers());
567
        $userDao = new UserDao($this->tdbmService);
568
        $user = $userDao->getById(1);
569
        $this->assertCount(1, $user->getRoles());
570
    }
571
572
    /**
573
     * Step 7: we add a new role to user 1 and save user 1.
574
     * Expected result: save done, including the new role.
575
     *
576
     * @depends testJointureSave6
577
     */
578
    public function testJointureSave7(): void
579
    {
580
        $role = new RoleBean('my new role');
581
        $userDao = new UserDao($this->tdbmService);
582
        $user = $userDao->getById(1);
583
584
        $user->addRole($role);
585
        $userDao->save($user);
586
587
        $this->assertEquals(TDBMObjectStateEnum::STATE_LOADED, $role->_getStatus());
588
    }
589
590
    /**
591
     * Step 8: we check that save was done
592
     * Expected result: save done.
593
     *
594
     * @depends testJointureSave7
595
     */
596
    public function testJointureSave8(): void
597
    {
598
        $roleDao = new RoleDao($this->tdbmService);
599
        $userDao = new UserDao($this->tdbmService);
600
        $user = $userDao->getById(1);
601
602
        $roles = $user->getRoles();
603
        foreach ($roles as $role) {
604
            if ($role->getName() === 'my new role') {
605
                $selectedRole = $role;
606
                break;
607
            }
608
        }
609
        $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...
610
611
        $this->assertCount(2, $user->getRoles());
612
613
        // Expected: relationship removed!
614
        $roleDao->delete($selectedRole);
615
616
        $this->assertCount(1, $user->getRoles());
617
    }
618
619
    /**
620
     * Step 9: Let's test the setXXX method.
621
     *
622
     * @depends testJointureSave8
623
     */
624
    public function testJointureSave9(): void
625
    {
626
        $roleDao = new RoleDao($this->tdbmService);
627
        $userDao = new UserDao($this->tdbmService);
628
        $user = $userDao->getById(1);
629
630
        // At this point, user 1 is linked to role 1.
631
        // Let's bind it to role 2.
632
        $user->setRoles([$roleDao->getById(2)]);
633
        $userDao->save($user);
634
        $this->assertTrue($user->hasRole($roleDao->getById(2)));
635
    }
636
637
    /**
638
     * Step 10: Let's check results of 9.
639
     *
640
     * @depends testJointureSave9
641
     */
642
    public function testJointureSave10(): void
643
    {
644
        $userDao = new UserDao($this->tdbmService);
645
        $user = $userDao->getById(1);
646
647
        $roles = $user->getRoles();
648
649
        $this->assertCount(1, $roles);
650
        $this->assertEquals(2, $roles[0]->getId());
651
    }
652
653
    /**
654
     * Test jointure in a parent table in an inheritance relationship
655
     *
656
     * @depends testDaoGeneration
657
     */
658
    public function testJointureInParentTable(): void
659
    {
660
        $userDao = new UserDao($this->tdbmService);
661
        $user = $userDao->getById(1);
662
663
        $boats = $user->getBoats();
664
665
        $this->assertCount(1, $boats);
666
        $this->assertEquals(1, $boats[0]->getId());
667
    }
668
669
    /**
670
     * @depends testDaoGeneration
671
     */
672
    public function testFindOrderBy(): void
673
    {
674
        $userDao = new TestUserDao($this->tdbmService);
675
        $users = $userDao->getUsersByAlphabeticalOrder();
676
677
        $this->assertCount(6, $users);
678
        $this->assertEquals('bill.shakespeare', $users[0]->getLogin());
679
        $this->assertEquals('jean.dupont', $users[1]->getLogin());
680
681
        $users = $userDao->getUsersByCountryOrder();
682
        $this->assertCount(6, $users);
683
        $countryName1 = $users[0]->getCountry()->getLabel();
684
        for ($i = 1; $i < 6; $i++) {
685
            $countryName2 = $users[$i]->getCountry()->getLabel();
686
            $this->assertLessThanOrEqual(0, strcmp($countryName1, $countryName2));
687
            $countryName1 = $countryName2;
688
        }
689
    }
690
691
    /**
692
     * @depends testDaoGeneration
693
     */
694
    public function testFindFromSqlOrderBy(): void
695
    {
696
        $userDao = new TestUserDao($this->tdbmService);
697
        $users = $userDao->getUsersFromSqlByAlphabeticalOrder();
698
699
        $this->assertCount(6, $users);
700
        $this->assertEquals('bill.shakespeare', $users[0]->getLogin());
701
        $this->assertEquals('jean.dupont', $users[1]->getLogin());
702
703
        $users = $userDao->getUsersFromSqlByCountryOrder();
704
        $this->assertCount(6, $users);
705
        $countryName1 = $users[0]->getCountry()->getLabel();
706
        for ($i = 1; $i < 6; $i++) {
707
            $countryName2 = $users[$i]->getCountry()->getLabel();
708
            $this->assertLessThanOrEqual(0, strcmp($countryName1, $countryName2));
709
            $countryName1 = $countryName2;
710
        }
711
    }
712
713
    /**
714
     * @depends testDaoGeneration
715
     */
716
    public function testFindFromSqlOrderByOnInheritedBean(): void
717
    {
718
        $articleDao = new TestArticleDao($this->tdbmService);
719
        $articles = $articleDao->getArticlesByUserLogin();
720
721
        foreach ($articles as $article) {
722
            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...
723
        }
724
        $this->assertCount(0, $articles);
725
    }
726
727
728
    /**
729
     * @depends testDaoGeneration
730
     */
731
    public function testFindFromSqlOrderByJoinRole(): void
732
    {
733
        $roleDao = new TestRoleDao($this->tdbmService);
734
        $roles = $roleDao->getRolesByRightCanSing('roles.name DESC');
735
736
        $this->assertCount(2, $roles);
737
        $this->assertEquals('Singers', $roles[0]->getName());
738
        $this->assertEquals('Admins', $roles[1]->getName());
739
    }
740
741
    /**
742
     * @depends testDaoGeneration
743
     */
744
    public function testFindFromRawSqlOrderByUserCount(): void
745
    {
746
        $countryDao = new TestCountryDao($this->tdbmService);
747
        $countries = $countryDao->getCountriesByUserCount();
748
749
        $this->assertCount(4, $countries);
750
        for ($i = 1; $i < count($countries); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

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

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

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
751
            $this->assertLessThanOrEqual($countries[$i - 1]->getUsers()->count(), $countries[$i]->getUsers()->count());
752
        }
753
    }
754
755
    /**
756
     * @depends testDaoGeneration
757
     */
758
    public function testFindFromRawSqlWithUnion(): void
759
    {
760
        $countryDao = new TestCountryDao($this->tdbmService);
761
        $countries = $countryDao->getCountriesUsingUnion();
762
763
        $this->assertCount(2, $countries);
764
        $this->assertEquals(1, $countries[0]->getId());
765
    }
766
767
    /**
768
     * @depends testDaoGeneration
769
     */
770
    public function testFindFromRawSqlWithSimpleQuery(): void
771
    {
772
        $countryDao = new TestCountryDao($this->tdbmService);
773
        $countries = $countryDao->getCountriesUsingSimpleQuery();
774
775
        $this->assertCount(1, $countries);
776
        $this->assertEquals(1, $countries[0]->getId());
777
    }
778
779
    /**
780
     * @depends testDaoGeneration
781
     */
782
    public function testFindFromRawSqlWithDistinctQuery(): void
783
    {
784
        $countryDao = new TestCountryDao($this->tdbmService);
785
        $countries = $countryDao->getCountriesUsingDistinctQuery();
786
787
        $this->assertCount(1, $countries);
788
        $this->assertEquals(2, $countries[0]->getId());
789
    }
790
791
    /**
792
     * @depends testDaoGeneration
793
     */
794
    public function testFindFilters(): void
795
    {
796
        $userDao = new TestUserDao($this->tdbmService);
797
        $users = $userDao->getUsersByLoginStartingWith('bill');
798
799
        $this->assertCount(1, $users);
800
        $this->assertEquals('bill.shakespeare', $users[0]->getLogin());
801
    }
802
803
    /**
804
     * @depends testDaoGeneration
805
     */
806
    public function testFindMode(): void
807
    {
808
        $userDao = new TestUserDao($this->tdbmService);
809
        $users = $userDao->getUsersByLoginStartingWith('bill', TDBMService::MODE_CURSOR);
810
811
        $this->expectException('TheCodingMachine\TDBM\TDBMException');
812
        $users[0];
813
    }
814
815
    /**
816
     * @depends testDaoGeneration
817
     */
818
    public function testFindAll(): void
819
    {
820
        $userDao = new TestUserDao($this->tdbmService);
821
        $users = $userDao->findAll();
822
823
        $this->assertCount(6, $users);
824
    }
825
826
    /**
827
     * @depends testDaoGeneration
828
     */
829
    public function testFindOne(): void
830
    {
831
        $userDao = new TestUserDao($this->tdbmService);
832
        $user = $userDao->getUserByLogin('bill.shakespeare');
833
834
        $this->assertEquals('bill.shakespeare', $user->getLogin());
835
    }
836
837
    /**
838
     * @depends testDaoGeneration
839
     */
840
    public function testJsonEncodeBean(): void
841
    {
842
        $userDao = new TestUserDao($this->tdbmService);
843
        $user = $userDao->getUserByLogin('bill.shakespeare');
844
845
        $jsonEncoded = json_encode($user);
846
        $userDecoded = json_decode($jsonEncoded, true);
847
848
        $this->assertEquals('bill.shakespeare', $userDecoded['login']);
849
850
        // test serialization of dates.
851
        $this->assertTrue(is_string($userDecoded['createdAt']));
852
        $this->assertEquals('2015-10-24', (new \DateTimeImmutable($userDecoded['createdAt']))->format('Y-m-d'));
853
        $this->assertNull($userDecoded['modifiedAt']);
854
855
        // testing many to 1 relationships
856
        $this->assertEquals('UK', $userDecoded['country']['label']);
857
858
        // testing many to many relationships
859
        $this->assertCount(1, $userDecoded['roles']);
860
        $this->assertArrayNotHasKey('users', $userDecoded['roles'][0]);
861
        $this->assertArrayNotHasKey('rights', $userDecoded['roles'][0]);
862
    }
863
864
    /**
865
     * @depends testDaoGeneration
866
     */
867
    public function testNullableForeignKey(): void
868
    {
869
        $userDao = new TestUserDao($this->tdbmService);
870
        $user = $userDao->getUserByLogin('john.smith');
871
872
        $this->assertNull($user->getManager());
873
874
        $jsonEncoded = json_encode($user);
875
        $userDecoded = json_decode($jsonEncoded, true);
876
877
        $this->assertNull($userDecoded['manager']);
878
    }
879
880
    /**
881
     * Test that setting (and saving) objects' references (foreign keys relations) to null is working.
882
     *
883
     * @depends testDaoGeneration
884
     */
885
    public function testSetToNullForeignKey(): void
886
    {
887
        $userDao = new TestUserDao($this->tdbmService);
888
        $user = $userDao->getUserByLogin('john.smith');
889
        $manager = $userDao->getUserByLogin('jean.dupont');
890
891
        $user->setManager($manager);
892
        $userDao->save($user);
893
894
        $user->setManager(null);
895
        $userDao->save($user);
896
        $this->assertNull($user->getManager());
897
    }
898
899
    /**
900
     * @depends testDaoGeneration
901
     */
902
    public function testQueryOnWrongTableName(): void
903
    {
904
        $userDao = new TestUserDao($this->tdbmService);
905
        $users = $userDao->getUsersWrongTableName();
906
        $this->expectException('Mouf\Database\SchemaAnalyzer\SchemaAnalyzerTableNotFoundException');
907
        $this->expectExceptionMessage('Could not find table \'contacts\'. Did you mean \'contact\'?');
908
        $users->count();
909
    }
910
911
    /**
912
     * @depends testDaoGeneration
913
     */
914
    /*public function testQueryNullForeignKey(): void
915
    {
916
        $userDao = new TestUserDao($this->tdbmService);
917
        $users = $userDao->getUsersByManagerId(null);
918
        $this->assertCount(3, $users);
919
    }*/
920
921
    /**
922
     * @depends testDaoGeneration
923
     */
924
    public function testInnerJsonEncode(): void
925
    {
926
        $userDao = new TestUserDao($this->tdbmService);
927
        $user = $userDao->getUserByLogin('bill.shakespeare');
928
929
        $jsonEncoded = json_encode(['user' => $user]);
930
        $msgDecoded = json_decode($jsonEncoded, true);
931
932
        $this->assertEquals('bill.shakespeare', $msgDecoded['user']['login']);
933
    }
934
935
    /**
936
     * @depends testDaoGeneration
937
     */
938
    public function testArrayJsonEncode(): void
939
    {
940
        $userDao = new TestUserDao($this->tdbmService);
941
        $users = $userDao->getUsersByLoginStartingWith('bill');
942
943
        $jsonEncoded = json_encode($users);
944
        $msgDecoded = json_decode($jsonEncoded, true);
945
946
        $this->assertCount(1, $msgDecoded);
947
    }
948
949
    /**
950
     * @depends testDaoGeneration
951
     */
952
    public function testCursorJsonEncode(): void
953
    {
954
        $userDao = new TestUserDao($this->tdbmService);
955
        $users = $userDao->getUsersByLoginStartingWith('bill', TDBMService::MODE_CURSOR);
956
957
        $jsonEncoded = json_encode($users);
958
        $msgDecoded = json_decode($jsonEncoded, true);
959
960
        $this->assertCount(1, $msgDecoded);
961
    }
962
963
    /**
964
     * @depends testDaoGeneration
965
     */
966
    public function testPageJsonEncode(): void
967
    {
968
        $userDao = new TestUserDao($this->tdbmService);
969
        $users = $userDao->getUsersByLoginStartingWith('bill');
970
971
        $jsonEncoded = json_encode($users->take(0, 1));
972
        $msgDecoded = json_decode($jsonEncoded, true);
973
974
        $this->assertCount(1, $msgDecoded);
975
    }
976
977
    /**
978
     * @depends testDaoGeneration
979
     */
980
    public function testInnerResultIteratorCountAfterFetch(): void
981
    {
982
        $userDao = new TestUserDao($this->tdbmService);
983
        $users = $userDao->getUsersByLoginStartingWith('j')->take(0, 4);
984
        $users->toArray(); // We force to fetch
985
        $this->assertEquals(3, $users->count());
986
    }
987
988
    /**
989
     * @depends testDaoGeneration
990
     */
991
    public function testFirst(): void
992
    {
993
        $userDao = new TestUserDao($this->tdbmService);
994
        $users = $userDao->getUsersByLoginStartingWith('bill');
995
996
        $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...
997
        $this->assertEquals('bill.shakespeare', $bill->getLogin());
998
    }
999
1000
    /**
1001
     * @depends testDaoGeneration
1002
     */
1003
    public function testFirstNull(): void
1004
    {
1005
        $userDao = new TestUserDao($this->tdbmService);
1006
        $users = $userDao->getUsersByLoginStartingWith('mike');
1007
1008
        $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...
1009
        $this->assertNull($user);
1010
    }
1011
1012
    /**
1013
     * @depends testDaoGeneration
1014
     */
1015
    public function testCloneBeanAttachedBean(): void
1016
    {
1017
        $userDao = new TestUserDao($this->tdbmService);
1018
        $user = $userDao->getUserByLogin('bill.shakespeare');
1019
        $this->assertEquals(4, $user->getId());
1020
        $user2 = clone $user;
1021
        $this->assertNull($user2->getId());
1022
        $this->assertEquals('bill.shakespeare', $user2->getLogin());
1023
        $this->assertEquals('Bill Shakespeare', $user2->getName());
1024
        $this->assertEquals('UK', $user2->getCountry()->getLabel());
1025
1026
        // MANY 2 MANY must be duplicated
1027
        $this->assertEquals('Writers', $user2->getRoles()[0]->getName());
1028
1029
        // Let's test saving this clone
1030
        $user2->setLogin('william.shakespeare');
1031
        $userDao->save($user2);
1032
1033
        $user3 = $userDao->getUserByLogin('william.shakespeare');
1034
        $this->assertTrue($user3 === $user2);
1035
        $userDao->delete($user3);
1036
1037
        // Finally, let's test the origin user still exists!
1038
        $user4 = $userDao->getUserByLogin('bill.shakespeare');
1039
        $this->assertEquals('bill.shakespeare', $user4->getLogin());
1040
    }
1041
1042
    /**
1043
     * @depends testDaoGeneration
1044
     */
1045
    public function testCloneNewBean(): void
1046
    {
1047
        $countryDao = new CountryDao($this->tdbmService);
1048
        $roleDao = new RoleDao($this->tdbmService);
1049
        $role = $roleDao->getById(1);
1050
1051
        $userBean = new UserBean('John Doe', '[email protected]', $countryDao->getById(2), 'john.doe');
1052
        $userBean->addRole($role);
1053
1054
        $user2 = clone $userBean;
1055
1056
        $this->assertNull($user2->getId());
1057
        $this->assertEquals('john.doe', $user2->getLogin());
1058
        $this->assertEquals('John Doe', $user2->getName());
1059
        $this->assertEquals('UK', $user2->getCountry()->getLabel());
1060
1061
        // MANY 2 MANY must be duplicated
1062
        $this->assertEquals($role->getName(), $user2->getRoles()[0]->getName());
1063
    }
1064
1065
    /**
1066
     * @depends testDaoGeneration
1067
     */
1068
    public function testCascadeDelete(): void
1069
    {
1070
        $userDao = new TestUserDao($this->tdbmService);
1071
        $countryDao = new CountryDao($this->tdbmService);
1072
1073
        $spain = new CountryBean('Spain');
1074
        $sanchez = new UserBean('Manuel Sanchez', '[email protected]', $spain, 'manuel.sanchez');
1075
1076
        $countryDao->save($spain);
1077
        $userDao->save($sanchez);
1078
1079
        $speedy2 = $userDao->getUserByLogin('manuel.sanchez');
1080
        $this->assertTrue($sanchez === $speedy2);
1081
1082
        $exceptionTriggered = false;
1083
        try {
1084
            $countryDao->delete($spain);
1085
        } catch (ForeignKeyConstraintViolationException $e) {
1086
            $exceptionTriggered = true;
1087
        }
1088
        $this->assertTrue($exceptionTriggered);
1089
1090
        $countryDao->delete($spain, true);
1091
1092
        // Let's check that speed gonzalez was removed.
1093
        $speedy3 = $userDao->getUserByLogin('manuel.sanchez');
1094
        $this->assertNull($speedy3);
1095
    }
1096
1097
    /**
1098
     * @depends testDaoGeneration
1099
     */
1100
    public function testDiscardChanges(): void
1101
    {
1102
        $contactDao = new ContactDao($this->tdbmService);
1103
        $contactBean = $contactDao->getById(1);
1104
1105
        $oldName = $contactBean->getName();
1106
1107
        $contactBean->setName('MyNewName');
1108
1109
        $contactBean->discardChanges();
1110
1111
        $this->assertEquals($oldName, $contactBean->getName());
1112
    }
1113
1114
    /**
1115
     * @depends testDaoGeneration
1116
     */
1117
    public function testDiscardChangesOnNewBeanFails(): void
1118
    {
1119
        $person = new PersonBean('John Foo', new \DateTimeImmutable());
1120
        $this->expectException('TheCodingMachine\TDBM\TDBMException');
1121
        $person->discardChanges();
1122
    }
1123
1124
    /**
1125
     * @depends testDaoGeneration
1126
     */
1127
    public function testDiscardChangesOnDeletedBeanFails(): void
1128
    {
1129
        $userDao = new TestUserDao($this->tdbmService);
1130
        $countryDao = new CountryDao($this->tdbmService);
1131
1132
        $sanchez = new UserBean('Manuel Sanchez', '[email protected]', $countryDao->getById(1), 'manuel.sanchez');
1133
1134
        $userDao->save($sanchez);
1135
1136
        $userDao->delete($sanchez);
1137
1138
        $this->expectException('TheCodingMachine\TDBM\TDBMException');
1139
        // Cannot discard changes on a bean that is already deleted.
1140
        $sanchez->discardChanges();
1141
    }
1142
1143
    /**
1144
     * @depends testDaoGeneration
1145
     */
1146
    public function testUniqueIndexBasedSearch(): void
1147
    {
1148
        $userDao = new UserDao($this->tdbmService);
1149
        $user = $userDao->findOneByLogin('bill.shakespeare');
1150
1151
        $this->assertEquals('bill.shakespeare', $user->getLogin());
1152
        $this->assertEquals('Bill Shakespeare', $user->getName());
1153
    }
1154
1155
    /**
1156
     * @depends testDaoGeneration
1157
     */
1158
    public function testFindOneByRetunsNull(): void
1159
    {
1160
        // Let's assert that the findOneBy... methods can return null.
1161
        $userDao = new UserDao($this->tdbmService);
1162
        $userBean = $userDao->findOneByLogin('not_exist');
1163
1164
        $this->assertNull($userBean);
1165
    }
1166
1167
    /**
1168
     * @depends testDaoGeneration
1169
     */
1170
    public function testMultiColumnsIndexBasedSearch(): void
1171
    {
1172
        $countryDao = new CountryDao($this->tdbmService);
1173
        $userDao = new UserDao($this->tdbmService);
1174
        $users = $userDao->findByStatusAndCountry('on', $countryDao->getById(1));
1175
1176
        $this->assertEquals('jean.dupont', $users[0]->getLogin());
1177
    }
1178
1179
    /**
1180
     * @depends testDaoGeneration
1181
     */
1182
    public function testPartialMultiColumnsIndexBasedSearch(): void
1183
    {
1184
        $userDao = new UserDao($this->tdbmService);
1185
        $users = $userDao->findByStatusAndCountry('on');
1186
1187
        $this->assertCount(2, $users);
1188
    }
1189
1190
    /**
1191
     * @depends testDaoGeneration
1192
     */
1193
    public function testCreationInNullableDate(): void
1194
    {
1195
        $roleDao = new RoleDao($this->tdbmService);
1196
1197
        $role = new RoleBean('newbee');
1198
        $roleDao->save($role);
1199
1200
        $this->assertNull($role->getCreatedAt());
1201
    }
1202
1203
    /**
1204
     * @depends testDaoGeneration
1205
     */
1206
    public function testUpdateInNullableDate(): void
1207
    {
1208
        $roleDao = new RoleDao($this->tdbmService);
1209
1210
        $role = new RoleBean('newbee');
1211
        $roleDao->save($role);
1212
1213
        $role->setCreatedAt(null);
1214
        $roleDao->save($role);
1215
        $this->assertNull($role->getCreatedAt());
1216
    }
1217
1218
    /**
1219
     * @depends testDaoGeneration
1220
     */
1221
    public function testFindFromSql(): void
1222
    {
1223
        $roleDao = new TestRoleDao($this->tdbmService);
1224
1225
        $roles = $roleDao->getRolesByRightCanSing();
1226
        $this->assertCount(2, $roles);
1227
        $this->assertInstanceOf(RoleBean::class, $roles[0]);
1228
    }
1229
1230
    /**
1231
     * @depends testDaoGeneration
1232
     */
1233
    public function testFindOneFromSql(): void
1234
    {
1235
        $roleDao = new TestRoleDao($this->tdbmService);
1236
1237
        $role = $roleDao->getRoleByRightCanSingAndNameSinger();
1238
        $this->assertInstanceOf(RoleBean::class, $role);
1239
    }
1240
1241
    /**
1242
     * @depends testDaoGeneration
1243
     */
1244
    public function testCreateEmptyExtendedBean(): void
1245
    {
1246
        // This test cases checks issue https://github.com/thecodingmachine/database.tdbm/issues/92
1247
1248
        $dogDao = new DogDao($this->tdbmService);
1249
1250
        // We are not filling no field that is part of dog table.
1251
        $dog = new DogBean('Youki');
1252
        $dog->setOrder(1);
1253
1254
        $dogDao->save($dog);
1255
        $this->assertNull($dog->getRace());
1256
    }
1257
1258
    /**
1259
     * @depends testCreateEmptyExtendedBean
1260
     */
1261
    public function testFetchEmptyExtendedBean(): void
1262
    {
1263
        // This test cases checks issue https://github.com/thecodingmachine/database.tdbm/issues/92
1264
1265
        $animalDao = new AnimalDao($this->tdbmService);
1266
1267
        // We are not filling no field that is part of dog table.
1268
        $animalBean = $animalDao->getById(1);
1269
1270
        $this->assertInstanceOf(DogBean::class, $animalBean);
1271
    }
1272
1273
    /**
1274
     * @depends testDaoGeneration
1275
     */
1276
    public function testTwoBranchesHierarchy(): void
1277
    {
1278
        // This test cases checks issue https://github.com/thecodingmachine/mouf/issues/131
1279
1280
        $catDao = new CatDao($this->tdbmService);
1281
1282
        // We are not filling no field that is part of dog table.
1283
        $cat = new CatBean('Mew');
1284
        $cat->setOrder(2);
1285
1286
        $catDao->save($cat);
1287
        $this->assertNotNull($cat->getId());
1288
    }
1289
1290
    /**
1291
     * @depends testTwoBranchesHierarchy
1292
     */
1293
    public function testFetchTwoBranchesHierarchy(): void
1294
    {
1295
        // This test cases checks issue https://github.com/thecodingmachine/mouf/issues/131
1296
1297
        $animalDao = new AnimalDao($this->tdbmService);
1298
1299
        $animalBean = $animalDao->getById(2);
1300
1301
        $this->assertInstanceOf(CatBean::class, $animalBean);
1302
        /* @var $animalBean CatBean */
1303
        $animalBean->setCutenessLevel(999);
1304
        $animalBean->setUppercaseColumn('foobar');
1305
1306
        $animalDao->save($animalBean);
1307
    }
1308
1309
    /**
1310
     * @depends testDaoGeneration
1311
     */
1312
    public function testExceptionOnGetById(): void
1313
    {
1314
        $countryDao = new CountryDao($this->tdbmService);
1315
        $this->expectException(\TypeError::class);
1316
        $countryDao->getById(null);
1317
    }
1318
1319
    /**
1320
     * @depends testDaoGeneration
1321
     */
1322
    public function testDisconnectedManyToOne(): void
1323
    {
1324
        // This test cases checks issue https://github.com/thecodingmachine/database.tdbm/issues/99
1325
1326
        $country = new CountryBean('Spain');
1327
1328
        $user = new UserBean('John Doe', '[email protected]', $country, 'john.doe');
1329
1330
        $this->assertCount(1, $country->getUsers());
1331
        $this->assertSame($user, $country->getUsers()[0]);
1332
    }
1333
1334
    /**
1335
     * @depends testDaoGeneration
1336
     */
1337
    public function testOrderByExternalCol(): void
1338
    {
1339
        // This test cases checks issue https://github.com/thecodingmachine/database.tdbm/issues/106
1340
1341
        $userDao = new TestUserDao($this->tdbmService);
1342
        $users = $userDao->getUsersByCountryName();
1343
1344
        $this->assertEquals('UK', $users[0]->getCountry()->getLabel());
1345
    }
1346
1347
    /**
1348
     * @depends testDaoGeneration
1349
     */
1350
    public function testResultIteratorSort(): void
1351
    {
1352
        $userDao = new UserDao($this->tdbmService);
1353
        $users = $userDao->findAll()->withOrder('country.label DESC');
1354
1355
        $this->assertEquals('UK', $users[0]->getCountry()->getLabel());
1356
1357
        $users = $users->withOrder('country.label ASC');
1358
        $this->assertEquals('France', $users[0]->getCountry()->getLabel());
1359
    }
1360
1361
    /**
1362
     * @depends testDaoGeneration
1363
     */
1364
    public function testResultIteratorWithParameters(): void
1365
    {
1366
        $userDao = new TestUserDao($this->tdbmService);
1367
        $users = $userDao->getUsersByLoginStartingWith()->withParameters(['login' => 'bill%']);
1368
        $this->assertEquals('bill.shakespeare', $users[0]->getLogin());
1369
1370
        $users = $users->withParameters(['login' => 'jean%']);
1371
        $this->assertEquals('jean.dupont', $users[0]->getLogin());
1372
    }
1373
1374
    /**
1375
     * @depends testDaoGeneration
1376
     */
1377
    public function testOrderByExpression(): void
1378
    {
1379
        $userDao = new TestUserDao($this->tdbmService);
1380
        $users = $userDao->getUsersByReversedCountryName();
1381
1382
        $this->assertEquals('Jamaica', $users[0]->getCountry()->getLabel());
1383
    }
1384
1385
    /**
1386
     * @depends testDaoGeneration
1387
     */
1388
    public function testOrderByException(): void
1389
    {
1390
        $userDao = new TestUserDao($this->tdbmService);
1391
        $users = $userDao->getUsersByInvalidOrderBy();
1392
        $this->expectException(TDBMInvalidArgumentException::class);
1393
        $user = $users[0];
0 ignored issues
show
Unused Code introduced by
The assignment to $user is dead and can be removed.
Loading history...
1394
    }
1395
1396
    /**
1397
     * @depends testDaoGeneration
1398
     */
1399
    public function testOrderByProtectedColumn(): void
1400
    {
1401
        $animalDao = new AnimalDao($this->tdbmService);
1402
        $animals = $animalDao->findAll();
1403
        $animals = $animals->withOrder('`order` ASC');
1404
1405
        $this->assertInstanceOf(DogBean::class, $animals[0]);
1406
        $this->assertInstanceOf(CatBean::class, $animals[1]);
1407
1408
        $animals = $animals->withOrder('`order` DESC');
1409
1410
        $this->assertInstanceOf(CatBean::class, $animals[0]);
1411
        $this->assertInstanceOf(DogBean::class, $animals[1]);
1412
    }
1413
1414
    /**
1415
     * @depends testDaoGeneration
1416
     */
1417
    public function testGetOnAllNullableValues(): void
1418
    {
1419
        // Tests that a get performed on a column that has only nullable fields succeeds.
1420
        $allNullable = new AllNullableBean();
1421
        $this->assertNull($allNullable->getId());
1422
        $this->assertNull($allNullable->getLabel());
1423
        $this->assertNull($allNullable->getCountry());
1424
    }
1425
1426
    /**
1427
     * @depends testDaoGeneration
1428
     */
1429
    public function testExceptionOnMultipleInheritance(): void
1430
    {
1431
        $connection = self::getConnection();
1432
        self::insert($connection, 'animal', [
1433
            'id' => 99, 'name' => 'Snoofield',
1434
        ]);
1435
        self::insert($connection, 'dog', [
1436
            'id' => 99, 'race' => 'dog',
1437
        ]);
1438
        self::insert($connection, 'cat', [
1439
            'id' => 99, 'cuteness_level' => 0,
1440
        ]);
1441
1442
        $catched = false;
1443
        try {
1444
            $animalDao = new AnimalDao($this->tdbmService);
1445
            $animalDao->getById(99);
1446
        } catch (TDBMInheritanceException $e) {
1447
            $catched = true;
1448
        }
1449
        $this->assertTrue($catched, 'Exception TDBMInheritanceException was not caught');
1450
1451
        self::delete($connection, 'cat', ['id' => 99]);
1452
        self::delete($connection, 'dog', ['id' => 99]);
1453
        self::delete($connection, 'animal', ['id' => 99]);
1454
    }
1455
1456
    /**
1457
     * @depends testDaoGeneration
1458
     */
1459
    public function testReferenceNotSaved(): void
1460
    {
1461
        $boatDao = new BoatDao($this->tdbmService);
1462
1463
        $country = new CountryBean('Atlantis');
1464
        $boat = new BoatBean($country, 'Titanic');
1465
1466
        $boatDao->save($boat);
1467
        $this->assertNotNull($country->getId());
1468
    }
1469
1470
    /**
1471
     * @depends testReferenceNotSaved
1472
     */
1473
    public function testUniqueIndexOnForeignKeyThenScalar(): void
1474
    {
1475
        $boatDao = new BoatDao($this->tdbmService);
1476
        $countryDao = new CountryDao($this->tdbmService);
1477
1478
        $countryBean = $countryDao->findOneByLabel('Atlantis');
1479
        $boatBean = $boatDao->findOneByAnchorageCountryAndName($countryBean, 'Titanic');
1480
1481
        $this->assertNotNull($boatBean);
1482
    }
1483
1484
    /**
1485
     * @depends testDaoGeneration
1486
     */
1487
    public function testReferenceDeleted(): void
1488
    {
1489
        $countryDao = new CountryDao($this->tdbmService);
1490
        $boatDao = new BoatDao($this->tdbmService);
1491
1492
        $country = new CountryBean('Bikini Bottom');
1493
        $countryDao->save($country);
1494
1495
        $boat = new BoatBean($country, 'Squirrel boat');
1496
        $countryDao->delete($country);
1497
1498
        $this->expectException(TDBMMissingReferenceException::class);
1499
        $boatDao->save($boat);
1500
    }
1501
1502
    /**
1503
     * @depends testDaoGeneration
1504
     */
1505
    public function testCyclicReferenceWithInheritance(): void
1506
    {
1507
        $userDao = new UserDao($this->tdbmService);
1508
1509
        $country = new CountryBean('Norrisland');
1510
        $user = new UserBean('Chuck Norris', '[email protected]', $country, 'chuck.norris');
1511
1512
        $user->setManager($user);
1513
1514
        $this->expectException(TDBMCyclicReferenceException::class);
1515
        $userDao->save($user);
1516
    }
1517
1518
    /**
1519
     * @depends testDaoGeneration
1520
     */
1521
    public function testCyclicReference(): void
1522
    {
1523
        $categoryDao = new CategoryDao($this->tdbmService);
1524
1525
        $category = new CategoryBean('Root');
1526
1527
        $category->setParent($category);
1528
1529
        $this->expectException(TDBMCyclicReferenceException::class);
1530
        $categoryDao->save($category);
1531
    }
1532
1533
    /**
1534
     * @depends testDaoGeneration
1535
     */
1536
    public function testCorrectTypeForPrimaryKeyAfterSave(): void
1537
    {
1538
        // PosqtgreSQL does not particularly like empty inserts (i.e.: "INSERT INTO all_nullable () VALUES ()" )
1539
        $this->onlyMySql();
1540
1541
        $allNullableDao = new AllNullableDao($this->tdbmService);
1542
        $allNullable = new AllNullableBean();
1543
        $allNullableDao->save($allNullable);
1544
        $id = $allNullable->getId();
1545
1546
        $this->assertTrue(is_int($id));
1547
    }
1548
1549
    /**
1550
     * @depends testDaoGeneration
1551
     */
1552
    public function testPSR2Compliance(): void
1553
    {
1554
        $process = new Process('vendor/bin/php-cs-fixer fix src/Test/  --dry-run --diff --rules=@PSR2');
0 ignored issues
show
Bug introduced by
'vendor/bin/php-cs-fixer...n --diff --rules=@PSR2' of type string is incompatible with the type array expected by parameter $command of Symfony\Component\Process\Process::__construct(). ( Ignorable by Annotation )

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

1554
        $process = new Process(/** @scrutinizer ignore-type */ 'vendor/bin/php-cs-fixer fix src/Test/  --dry-run --diff --rules=@PSR2');
Loading history...
1555
        $process->run();
1556
1557
        // executes after the command finishes
1558
        if (!$process->isSuccessful()) {
1559
            echo $process->getOutput();
1560
            $this->fail('Generated code is not PSR-2 compliant');
1561
        }
1562
        $this->assertTrue($process->isSuccessful());
1563
    }
1564
1565
    /**
1566
     * @depends testDaoGeneration
1567
     */
1568
    public function testFindOneByGeneration(): void
1569
    {
1570
        $reflectionMethod = new \ReflectionMethod(UserBaseDao::class, 'findOneByLogin');
1571
        $parameters = $reflectionMethod->getParameters();
1572
1573
        $this->assertCount(2, $parameters);
1574
        $this->assertSame('login', $parameters[0]->getName());
1575
        $this->assertSame('additionalTablesFetch', $parameters[1]->getName());
1576
    }
1577
1578
    /**
1579
     * @depends testDaoGeneration
1580
     */
1581
    public function testUuid(): void
1582
    {
1583
        $article = new ArticleBean('content');
1584
        $this->assertSame('content', $article->getContent());
1585
        $this->assertNotEmpty($article->getId());
1586
        $uuid = Uuid::fromString($article->getId());
1587
        $this->assertSame(1, $uuid->getVersion());
1588
    }
1589
1590
    /**
1591
     * @depends testDaoGeneration
1592
     */
1593
    public function testUuidv4(): void
1594
    {
1595
        $article = new Article2Bean('content');
1596
        $this->assertSame('content', $article->getContent());
1597
        $this->assertNotEmpty($article->getId());
1598
        $uuid = Uuid::fromString($article->getId());
1599
        $this->assertSame(4, $uuid->getVersion());
1600
    }
1601
1602
    /**
1603
     * @depends testDaoGeneration
1604
     */
1605
    public function testTypeHintedConstructors(): void
1606
    {
1607
        $userBaseBeanReflectionConstructor = new \ReflectionMethod(UserBaseBean::class, '__construct');
1608
        $nameParam = $userBaseBeanReflectionConstructor->getParameters()[0];
1609
1610
        $this->assertSame('string', (string)$nameParam->getType());
1611
    }
1612
1613
    /**
1614
     * @depends testDaoGeneration
1615
     */
1616
    public function testSaveTransaction(): void
1617
    {
1618
        $countryDao = new CountryDao($this->tdbmService);
1619
1620
        $boatDao = new BoatDao($this->tdbmService);
1621
        $boatBean = $boatDao->getById(1);
1622
        $boatBean->setName('Bismark');
1623
1624
        $boatBean->getCountry();
1625
1626
        // Let's insert a row without telling TDBM to trigger an error!
1627
        self::insert($this->getConnection(), 'sailed_countries', [
1628
            'boat_id' => 1,
1629
            'country_id' => 2
1630
        ]);
1631
1632
        $boatBean->addCountry($countryDao->getById(2));
1633
1634
        $this->expectException(UniqueConstraintViolationException::class);
1635
1636
        $boatDao->save($boatBean);
1637
    }
1638
1639
    /**
1640
     * @depends testSaveTransaction
1641
     */
1642
    public function testSaveTransaction2(): void
1643
    {
1644
        $boatDao = new BoatDao($this->tdbmService);
1645
        $boatBean = $boatDao->getById(1);
1646
1647
        // The name should not have been saved because the transaction of the previous test should have rollbacked.
1648
        $this->assertNotSame('Bismark', $boatBean->getName());
1649
    }
1650
1651
    /**
1652
     * @depends testDaoGeneration
1653
     */
1654
    public function testForeignKeyPointingToNonPrimaryKey(): void
1655
    {
1656
        $dao = new RefNoPrimKeyDao($this->tdbmService);
1657
        $bean = $dao->getById(1);
1658
1659
        $this->assertSame('foo', $bean->getFrom()->getTo());
1660
1661
        $newBean = new RefNoPrimKeyBean($bean, 'baz');
1662
        $dao->save($newBean);
1663
        $this->assertSame('foo', $newBean->getFrom()->getTo());
1664
1665
        $resultSet = $bean->getRefNoPrimKey();
1666
        $this->assertCount(2, $resultSet);
1667
    }
1668
1669
    /**
1670
     * @depends testDaoGeneration
1671
     */
1672
    public function testCloningUuidBean(): void
1673
    {
1674
        $article = new ArticleBean('content');
1675
        $this->assertNotEmpty($article->getId());
1676
        $article2 = clone $article;
1677
        $this->assertNotEmpty($article2->getId());
1678
        $this->assertNotSame($article->getId(), $article2->getId());
1679
    }
1680
1681
    /**
1682
     * @depends testDaoGeneration
1683
     */
1684
    public function testRecursiveSave(): void
1685
    {
1686
        $categoryDao = new CategoryDao($this->tdbmService);
1687
1688
        $root1 = new CategoryBean('Root1');
1689
        $categoryDao->save($root1);
1690
        // Root 2 is not saved yet.
1691
        $root2 = new CategoryBean('Root2');
1692
        $intermediate = new CategoryBean('Intermediate');
1693
        $categoryDao->save($intermediate);
1694
1695
        // Let's switch the parent to a bean in detached state.
1696
        $intermediate->setParent($root2);
1697
1698
        // Now, let's save a new category that references the leaf category.
1699
        $leaf = new CategoryBean('Leaf');
1700
        $leaf->setParent($intermediate);
1701
        $categoryDao->save($leaf);
1702
        $this->assertNull($root2->getId());
1703
    }
1704
1705
    /**
1706
     * @depends testDaoGeneration
1707
     */
1708
    public function testBlob(): void
1709
    {
1710
        $fp = fopen(__FILE__, 'r');
1711
        $file = new FileBean($fp);
1712
1713
        $fileDao = new FileDao($this->tdbmService);
1714
1715
        $fileDao->save($file);
1716
1717
        $loadedFile = $fileDao->getById($file->getId());
1718
1719
        $resource = $loadedFile->getFile();
1720
        $result = fseek($resource, 0);
1721
        $this->assertSame(0, $result);
1722
        $this->assertInternalType('resource', $resource);
0 ignored issues
show
Deprecated Code introduced by
The function PHPUnit\Framework\Assert::assertInternalType() has been deprecated: https://github.com/sebastianbergmann/phpunit/issues/3369 ( Ignorable by Annotation )

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

1722
        /** @scrutinizer ignore-deprecated */ $this->assertInternalType('resource', $resource);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
1723
        $firstLine = fgets($resource);
1724
        $this->assertSame("<?php\n", $firstLine);
1725
    }
1726
1727
    /**
1728
     * @depends testBlob
1729
     */
1730
    public function testReadBlob(): void
1731
    {
1732
        $fileDao = new FileDao($this->tdbmService);
1733
        $loadedFile = $fileDao->getById(1);
1734
1735
        $resource = $loadedFile->getFile();
1736
        $this->assertInternalType('resource', $resource);
0 ignored issues
show
Deprecated Code introduced by
The function PHPUnit\Framework\Assert::assertInternalType() has been deprecated: https://github.com/sebastianbergmann/phpunit/issues/3369 ( Ignorable by Annotation )

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

1736
        /** @scrutinizer ignore-deprecated */ $this->assertInternalType('resource', $resource);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
1737
        $firstLine = fgets($resource);
1738
        $this->assertSame("<?php\n", $firstLine);
1739
1740
        stream_get_contents($resource);
1741
1742
        $loadedFile->setId($loadedFile->getId());
1743
1744
        $fileDao->save($loadedFile);
1745
    }
1746
1747
    /**
1748
     * @depends testReadBlob
1749
     */
1750
    public function testReadAndSaveBlob(): void
1751
    {
1752
        $fileDao = new FileDao($this->tdbmService);
1753
        $loadedFile = $fileDao->getById(1);
1754
1755
        $resource = $loadedFile->getFile();
1756
1757
        $firstLine = fgets($resource);
1758
        $this->assertSame("<?php\n", $firstLine);
1759
    }
1760
1761
    /**
1762
     * @depends testReadBlob
1763
     */
1764
    public function testProtectedGetterSetter(): void
1765
    {
1766
        $md5Getter = new ReflectionMethod(FileBaseBean::class, 'getMd5');
1767
        $md5Setter = new ReflectionMethod(FileBaseBean::class, 'setMd5');
1768
1769
        $this->assertTrue($md5Getter->isProtected());
1770
        $this->assertTrue($md5Setter->isProtected());
1771
1772
        $md5Getter2 = new ReflectionMethod(FileBaseBean::class, 'getArticle');
1773
        $md5Setter2 = new ReflectionMethod(FileBaseBean::class, 'setArticle');
1774
1775
        $this->assertTrue($md5Getter2->isProtected());
1776
        $this->assertTrue($md5Setter2->isProtected());
1777
1778
        $oneToManyGetter = new ReflectionMethod(ArticleBaseBean::class, 'getFiles');
1779
        $this->assertTrue($oneToManyGetter->isProtected());
1780
1781
        $fileDao = new FileDao($this->tdbmService);
1782
        $loadedFile = $fileDao->getById(1);
1783
1784
        // The md5 and article columns are not JSON serialized
1785
        $this->assertSame([
1786
            'id' => 1,
1787
        ], $loadedFile->jsonSerialize());
1788
    }
1789
1790
    /**
1791
     * @depends testDaoGeneration
1792
     */
1793
    public function testBlobResourceException(): void
1794
    {
1795
        $this->expectException(TDBMInvalidArgumentException::class);
1796
        $this->expectExceptionMessage('Invalid argument passed to \'TheCodingMachine\\TDBM\\Test\\Dao\\Bean\\Generated\\FileBaseBean::setFile\'. Expecting a resource. Got a string.');
1797
        new FileBean('foobar');
1798
    }
1799
1800
    /**
1801
     * @depends testDaoGeneration
1802
     */
1803
    public function testFilterBag(): void
1804
    {
1805
        $userDao = new TestUserDao($this->tdbmService);
1806
        $countryDao = new CountryDao($this->tdbmService);
1807
1808
        $country = $countryDao->getById(2);
1809
1810
        // Let's test filter bags by bean and filter bag with many values.
1811
        $users = $userDao->getUsersByComplexFilterBag($country, ['John Doe', 'Jane Doe']);
1812
1813
        $this->assertCount(1, $users);
1814
        $this->assertSame('John Doe', $users[0]->getName());
1815
    }
1816
1817
    /**
1818
     * @depends testDaoGeneration
1819
     */
1820
    public function testDecimalIsMappedToString(): void
1821
    {
1822
        $reflectionClass = new \ReflectionClass(BoatBaseBean::class);
1823
        $this->assertSame('string', (string) $reflectionClass->getMethod('getLength')->getReturnType());
1824
    }
1825
1826
    /**
1827
     * @depends testDaoGeneration
1828
     */
1829
    public function testInsertMultiPrimaryKeysBean(): void
1830
    {
1831
        $countryDao = new CountryDao($this->tdbmService);
1832
1833
        $country = $countryDao->getById(1);
1834
1835
        $stateDao = new StateDao($this->tdbmService);
1836
        $state = new StateBean($country, 'IDF', 'Ile de France');
1837
        $stateDao->save($state);
1838
1839
        $this->assertSame($state, $stateDao->findAll()[0]);
1840
    }
1841
1842
    /**
1843
     * @depends testInsertMultiPrimaryKeysBean
1844
     */
1845
    public function testDeleteMultiPrimaryKeysBean(): void
1846
    {
1847
        $stateDao = new StateDao($this->tdbmService);
1848
1849
        $state = $stateDao->findAll()[0];
1850
        $stateDao->delete($state);
1851
        $this->assertCount(0, $stateDao->findAll());
1852
    }
1853
1854
    /**
1855
     * @depends testDaoGeneration
1856
     */
1857
    public function testCompositePrimaryKeyGetter(): void
1858
    {
1859
        $stateDao = new StateDao($this->tdbmService);
1860
        $country = new CountryBean('USA');
1861
        $stateBean = new StateBean($country, 'CA', 'California');
1862
        $stateDao->save($stateBean);
1863
        $this->assertSame($stateBean, $stateDao->getById($country->getId(), 'CA'));
1864
    }
1865
1866
    /**
1867
     * @depends testDaoGeneration
1868
     */
1869
    public function testSortOnInheritedTable(): void
1870
    {
1871
        $animalDao = new AnimalDao($this->tdbmService);
1872
1873
        // Let's insert an animal that is nothing.
1874
        $animal = new AnimalBean('Mickey');
1875
        $animalDao->save($animal);
1876
1877
        $animals = $animalDao->findAll()->withOrder('dog.race ASC');
1878
1879
        $animalsArr = $animals->toArray();
1880
        $this->assertCount(3, $animalsArr);
1881
    }
1882
1883
    /**
1884
     * @depends testDaoGeneration
1885
     */
1886
    public function testJsonKey(): void
1887
    {
1888
        $node = new NodeBean('foo.html');
1889
        $json = $node->jsonSerialize();
1890
        self::assertTrue(isset($json['basename']));
1891
        self::assertEquals('foo.html', $json['basename']);
1892
    }
1893
1894
    /**
1895
     * @depends testDaoGeneration
1896
     */
1897
    public function testJsonIgnore(): void
1898
    {
1899
        $nodeDao = new NodeDao($this->tdbmService);
1900
        $index = $nodeDao->getById(6);
1901
        $json = $index->jsonSerialize();
1902
        // Ignored scalar 'id'
1903
        self::assertTrue(!isset($json['id']));
1904
        // Ignored object 'root'
1905
        self::assertTrue(!isset($json['root']));
1906
        self::assertTrue(isset($json['guests']));
1907
        self::assertTrue(!empty($json['guests']));
1908
        $account = $index->getAccounts()[0];
1909
        $json = $account->jsonSerialize();
1910
        // Ignored array 'nodes' (from nodes_users table)
1911
        self::assertTrue(!isset($json['nodes']));
1912
    }
1913
1914
    /**
1915
     * @depends testDaoGeneration
1916
     */
1917
    public function testJsonInclude(): void
1918
    {
1919
        $nodeDao = new NodeDao($this->tdbmService);
1920
        $index = $nodeDao->getById(6);
1921
        $json = $index->jsonSerialize();
1922
        // Whole chain of parents should be serialized
1923
        self::assertTrue(isset($json['parent']));
1924
        self::assertTrue(isset($json['parent']['parent']));
1925
        self::assertTrue(isset($json['parent']['parent']['parent']));
1926
        self::assertEquals('/', $json['parent']['parent']['parent']['basename']);
1927
    }
1928
1929
    /**
1930
     * @depends testDaoGeneration
1931
     */
1932
    public function testJsonRecursive(): void
1933
    {
1934
        $nodeDao = new NodeDao($this->tdbmService);
1935
        $index = $nodeDao->getById(8);
1936
        $json = $index->jsonSerialize();
1937
        // Original chain of aliases is recursively serialized, ...
1938
        self::assertTrue(isset($json['alias']));
1939
        self::assertTrue(isset($json['alias']['alias']));
1940
        self::assertEquals('index.html', $json['alias']['alias']['basename']);
1941
        // ... each alias even serializes its parents, ...
1942
        self::assertTrue(isset($json['alias']['alias']['parent']['parent']));
1943
        // ... however, parents aliases chains are not serialized, as parents are serialized with $stopRecursion=true
1944
        self::assertTrue(!isset($json['alias']['alias']['parent']['parent']['alias']));
1945
        self::assertNotNull($index->getAlias()->getAlias()->getParent()->getParent()->getAlias());
1946
    }
1947
1948
    /**
1949
     * @depends testDaoGeneration
1950
     */
1951
    public function testJsonFormat(): void
1952
    {
1953
        $nodeDao = new NodeDao($this->tdbmService);
1954
        $index = $nodeDao->getById(6);
1955
        $json = $index->jsonSerialize();
1956
        self::assertTrue(isset($json['size']));
1957
        self::assertEquals('512 o', $json['size']);
1958
        self::assertEquals('42.50g', $json['weight']);
1959
        self::assertEquals($index->getCreatedAt()->format('Y-m-d'), $json['createdAt']);
1960
        self::assertEquals($index->getOwner()->getName(), $json['owner']);
1961
        self::assertEquals($index->getAccounts()[1]->getName(), $json['guests'][1]);
1962
        self::assertTrue(isset($json['entries']));
1963
        self::assertEquals('Hello, World', $json['entries'][1]);
1964
        $www = $index->getParent();
1965
        $json = $www->jsonSerialize();
1966
        self::assertEquals('0 o', $json['size']);
1967
        self::assertNull($json['weight']);
1968
        self::assertNull($json['owner']);
1969
    }
1970
1971
    /**
1972
     * @depends testDaoGeneration
1973
     */
1974
    public function testJsonCollection(): void
1975
    {
1976
        $artists = new ArtistDao($this->tdbmService);
1977
        $pinkFloyd = $artists->getById(1);
1978
        $animals =  $pinkFloyd->getAlbums()[0];
1979
        $json = $pinkFloyd->jsonSerialize();
1980
        // Collection name properly handled ('discography' instead of default 'albums')
1981
        self::assertTrue(isset($json['discography']));
1982
        self::assertEquals($animals->getTitle(), $json['discography'][0]['title']);
1983
        // Make sure top object is not its own grandfather
1984
        self::assertTrue(!isset($json['discography'][0]['artist']));
1985
        $json = $animals->jsonSerialize();
1986
        // Nevertheless, artist should be serialized in album as top object...
1987
        self::assertTrue(isset($json['artist']));
1988
        // ... as should be tracks...
1989
        self::assertTrue(isset($json['tracks'][0]));
1990
        self::assertEquals('Pigs on the Wing 1', $json['tracks'][0]['title']);
1991
        // ... and, ultimately, list of featuring artists, since feat is included
1992
        self::assertTrue(isset($json['tracks'][0]['feat'][0]));
1993
        self::assertEquals('Roger Waters', $json['tracks'][0]['feat'][0]['name']);
1994
    }
1995
1996
    public function testFloydHasNoParent(): void
1997
    {
1998
        $artists = new ArtistDao($this->tdbmService);
1999
        $pinkFloyd = $artists->getById(1);
2000
        $parents = $pinkFloyd->getParents();
2001
2002
        $this->assertEquals([], $parents);
2003
    }
2004
2005
    public function testFloydHasOneChild(): void
2006
    {
2007
        $artists = new ArtistDao($this->tdbmService);
2008
        $pinkFloyd = $artists->getById(1);
2009
        $children = $pinkFloyd->getChildren();
2010
2011
        $this->assertEquals(1, count($children));
2012
        $this->assertEquals(2, $children[0]->getId());
2013
    }
2014
2015
    /**
2016
     * @depends testDaoGeneration
2017
     */
2018
    public function testAddInterfaceAnnotation(): void
2019
    {
2020
        if (!$this->tdbmService->getConnection()->getDatabasePlatform() instanceof MySqlPlatform) {
2021
            // See https://github.com/doctrine/dbal/pull/3512
2022
            $this->markTestSkipped('Only MySQL supports table level comments');
2023
        }
2024
2025
        $refClass = new ReflectionClass(UserBaseBean::class);
2026
        $this->assertTrue($refClass->implementsInterface(TestUserInterface::class));
2027
    }
2028
2029
    /**
2030
     * @depends testDaoGeneration
2031
     */
2032
    public function testAddInterfaceOnDaoAnnotation(): 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(UserBaseDao::class);
2040
        $this->assertTrue($refClass->implementsInterface(TestUserDaoInterface::class));
2041
    }
2042
2043
    /**
2044
     * @depends testDaoGeneration
2045
     */
2046
    public function testTrait(): 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
        $userDao = new UserDao($this->tdbmService);
2054
        $userBean = $userDao->getById(1);
2055
2056
        $this->assertSame('TestOtherUserTrait', $userBean->method1());
2057
        $this->assertSame('TestUserTrait', $userBean->method1renamed());
2058
2059
        $refClass = new ReflectionClass(UserBaseDao::class);
2060
        $this->assertTrue($refClass->hasMethod('findNothing'));
2061
    }
2062
2063
    /**
2064
     * @depends testDaoGeneration
2065
     */
2066
    public function testNonInstantiableAbstractDaosAndBeans(): void
2067
    {
2068
        $refClass = new ReflectionClass(UserBaseDao::class);
2069
        $this->assertFalse($refClass->isInstantiable());
2070
2071
        $refClass = new ReflectionClass(UserBaseBean::class);
2072
        $this->assertFalse($refClass->isInstantiable());
2073
    }
2074
2075
    /**
2076
     * @depends testDaoGeneration
2077
     */
2078
    public function testCanNullifyBlob(): void
2079
    {
2080
        $article = new ArticleBean('content');
2081
        $fp = fopen(__FILE__, 'r');
2082
        $article->setAttachment($fp);
2083
        $article->setAttachment(null);
2084
        $this->assertNull($article->getAttachment(null));
2085
        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

2085
        fclose(/** @scrutinizer ignore-type */ $fp);
Loading history...
2086
    }
2087
2088
    /**
2089
     * @depends testDaoGeneration
2090
     */
2091
    public function testOptionnalParametersCanBeNullInFindOneBy()
2092
    {
2093
        $albumDao = new AlbumDao($this->tdbmService);
2094
        $artist = new ArtistBean('Marcel');
2095
2096
        $albumDao->findOneByArtistAndNode($artist, null);
2097
        $this->assertEquals(1, 1);
2098
    }
2099
2100
    /**
2101
     * @depends testDaoGeneration
2102
     */
2103
    public function testRequiredParametersCannotBeNullInFindOneBy()
2104
    {
2105
        $albumDao = new AlbumDao($this->tdbmService);
2106
        $artist = new ArtistBean('Marcel');
2107
        $account = new AccountBean('Jamie');
2108
2109
        $albumDao->findOneByArtistAndAccount($artist, $account);
2110
2111
        $this->expectException('TypeError');
2112
        $albumDao->findOneByArtistAndAccount($artist, null);
2113
    }
2114
2115
    /**
2116
     * @depends testDaoGeneration
2117
     */
2118
    public function testLazyLoad(): void
2119
    {
2120
        $roleDao = new RoleDao($this->tdbmService);
2121
        $roleBean = $roleDao->getById(1, true);
2122
2123
        $this->assertSame(TDBMObjectStateEnum::STATE_NOT_LOADED, $roleBean->_getDbRows()['roles']->_getStatus());
2124
        $roleBean->getId();
2125
        $this->assertSame(TDBMObjectStateEnum::STATE_NOT_LOADED, $roleBean->_getDbRows()['roles']->_getStatus());
2126
    }
2127
2128
    /**
2129
     * @depends testDaoGeneration
2130
     */
2131
    public function testOneToOneInverseRelationGetter(): void
2132
    {
2133
        $objectBaseDao = new BaseObjectDao($this->tdbmService);
2134
        $objectInheritedDao = new InheritedObjectDao($this->tdbmService);
2135
        $objectBase = new BaseObjectBean('label');
2136
        $objectBaseDao->save($objectBase);
2137
        $this->assertNull($objectBase->getInheritedObject());
2138
        $objectInherited = new InheritedObjectBean($objectBase);
2139
        $objectInheritedDao->save($objectInherited);
2140
        $this->assertSame($objectInherited, $objectBase->getInheritedObject());
2141
        $this->assertEquals(1, $objectBase->jsonSerialize()['inheritedObject']['id']);
2142
    }
2143
}
2144