Passed
Pull Request — 5.1 (#228)
by
unknown
04:15
created

TDBMDaoGeneratorTest::testCascadeDelete()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 27
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 27
rs 9.7
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\Driver\Mysqli\MysqliConnection;
26
use Doctrine\DBAL\Exception\ForeignKeyConstraintViolationException;
27
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
28
use Doctrine\DBAL\Platforms\MySQL57Platform;
29
use Doctrine\DBAL\Platforms\MySqlPlatform;
30
use Mouf\Database\SchemaAnalyzer\SchemaAnalyzer;
31
use Ramsey\Uuid\Uuid;
32
use ReflectionClass;
33
use ReflectionMethod;
34
use ReflectionNamedType;
35
use TheCodingMachine\TDBM\Dao\TestAlbumDao;
36
use TheCodingMachine\TDBM\Dao\TestArticleDao;
37
use TheCodingMachine\TDBM\Dao\TestCountryDao;
38
use TheCodingMachine\TDBM\Dao\TestPersonDao;
39
use TheCodingMachine\TDBM\Dao\TestRoleDao;
40
use TheCodingMachine\TDBM\Dao\TestUserDao;
41
use TheCodingMachine\TDBM\Fixtures\Interfaces\TestUserDaoInterface;
42
use TheCodingMachine\TDBM\Fixtures\Interfaces\TestUserInterface;
43
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...
44
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...
45
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...
46
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...
47
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...
48
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...
49
use TheCodingMachine\TDBM\Test\Dao\Bean\AlbumBean;
0 ignored issues
show
Bug introduced by
The type TheCodingMachine\TDBM\Test\Dao\Bean\AlbumBean was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

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

Loading history...
50
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...
51
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...
52
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...
53
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...
54
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...
55
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...
56
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...
57
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...
58
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...
59
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...
60
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...
61
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...
62
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...
63
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...
64
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...
65
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...
66
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...
67
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...
68
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...
69
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...
70
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...
71
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...
72
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...
73
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...
74
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...
75
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...
76
use TheCodingMachine\TDBM\Test\Dao\CompositeFkSourceDao;
0 ignored issues
show
Bug introduced by
The type TheCodingMachine\TDBM\Te...ao\CompositeFkSourceDao was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

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

Loading history...
77
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...
78
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...
79
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...
80
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...
81
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...
82
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...
83
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...
84
use TheCodingMachine\TDBM\Test\Dao\PersonDao;
0 ignored issues
show
Bug introduced by
The type TheCodingMachine\TDBM\Test\Dao\PersonDao was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

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

Loading history...
85
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...
86
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...
87
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...
88
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...
89
use TheCodingMachine\TDBM\Utils\PathFinder\NoPathFoundException;
90
use TheCodingMachine\TDBM\Utils\PathFinder\PathFinder;
91
use TheCodingMachine\TDBM\Utils\TDBMDaoGenerator;
92
use Symfony\Component\Process\Process;
93
use function gettype;
94
95
class TDBMDaoGeneratorTest extends TDBMAbstractServiceTest
96
{
97
    /** @var TDBMDaoGenerator $tdbmDaoGenerator */
98
    protected $tdbmDaoGenerator;
99
100
    private $rootPath;
101
102
    protected function setUp(): void
103
    {
104
        parent::setUp();
105
        $schemaManager = $this->tdbmService->getConnection()->getSchemaManager();
106
        $schemaAnalyzer = new SchemaAnalyzer($schemaManager);
107
        $tdbmSchemaAnalyzer = new TDBMSchemaAnalyzer($this->tdbmService->getConnection(), new ArrayCache(), $schemaAnalyzer);
108
        $this->tdbmDaoGenerator = new TDBMDaoGenerator($this->getConfiguration(), $tdbmSchemaAnalyzer);
109
        $this->rootPath = __DIR__ . '/../';
110
        //$this->tdbmDaoGenerator->setComposerFile($this->rootPath.'composer.json');
111
    }
112
113
    public function testDaoGeneration(): void
114
    {
115
        // Remove all previously generated files.
116
        $this->recursiveDelete($this->rootPath . 'src/Test/Dao/');
117
        mkdir($this->rootPath . 'src/Test/Dao/Generated', 0755, true);
118
        // Let's generate a dummy file to see it is indeed removed.
119
        $dummyFile = $this->rootPath . 'src/Test/Dao/Generated/foobar.php';
120
        touch($dummyFile);
121
        $this->assertFileExists($dummyFile);
122
123
        $this->tdbmDaoGenerator->generateAllDaosAndBeans();
124
125
        $this->assertFileNotExists($dummyFile);
126
127
        // Let's require all files to check they are valid PHP!
128
        // Test the daoFactory
129
        require_once $this->rootPath . 'src/Test/Dao/Generated/DaoFactory.php';
130
        // Test the others
131
132
        $beanDescriptors = $this->getDummyGeneratorListener()->getBeanDescriptors();
133
134
        foreach ($beanDescriptors as $beanDescriptor) {
135
            $daoName = $beanDescriptor->getDaoClassName();
136
            $daoBaseName = $beanDescriptor->getBaseDaoClassName();
137
            $beanName = $beanDescriptor->getBeanClassName();
138
            $baseBeanName = $beanDescriptor->getBaseBeanClassName();
139
            require_once $this->rootPath . 'src/Test/Dao/Bean/Generated/' . $baseBeanName . '.php';
140
            require_once $this->rootPath . 'src/Test/Dao/Bean/' . $beanName . '.php';
141
            require_once $this->rootPath . 'src/Test/Dao/Generated/' . $daoBaseName . '.php';
142
            require_once $this->rootPath . 'src/Test/Dao/' . $daoName . '.php';
143
        }
144
145
        // Check that pivot tables do not generate DAOs or beans.
146
        $this->assertFalse(class_exists('TheCodingMachine\\TDBM\\Test\\Dao\\RolesRightDao'));
147
    }
148
149
    public function testGenerationException(): void
150
    {
151
        $configuration = new Configuration('UnknownVendor\\Dao', 'UnknownVendor\\Bean', self::getConnection(), $this->getNamingStrategy());
152
153
        $schemaManager = $this->tdbmService->getConnection()->getSchemaManager();
154
        $schemaAnalyzer = new SchemaAnalyzer($schemaManager);
155
        $tdbmSchemaAnalyzer = new TDBMSchemaAnalyzer($this->tdbmService->getConnection(), new ArrayCache(), $schemaAnalyzer);
156
        $tdbmDaoGenerator = new TDBMDaoGenerator($configuration, $tdbmSchemaAnalyzer);
157
        $this->rootPath = __DIR__ . '/../../../../';
158
        //$tdbmDaoGenerator->setComposerFile($this->rootPath.'composer.json');
159
160
        $this->expectException(NoPathFoundException::class);
161
        $tdbmDaoGenerator->generateAllDaosAndBeans();
162
    }
163
164
    /**
165
     * Delete a file or recursively delete a directory.
166
     *
167
     * @param string $str Path to file or directory
168
     * @return bool
169
     */
170
    private function recursiveDelete(string $str): bool
171
    {
172
        if (is_file($str)) {
173
            return @unlink($str);
174
        } elseif (is_dir($str)) {
175
            $scan = glob(rtrim($str, '/') . '/*');
176
            foreach ($scan as $index => $path) {
177
                $this->recursiveDelete($path);
178
            }
179
180
            return @rmdir($str);
181
        }
182
        return false;
183
    }
184
185
    /**
186
     * @depends testDaoGeneration
187
     */
188
    public function testGetBeanClassName(): void
189
    {
190
        $this->assertEquals(UserBean::class, $this->tdbmService->getBeanClassName('users'));
191
192
        // Let's create another TDBMService to test the cache.
193
        $configuration = new Configuration('TheCodingMachine\\TDBM\\Test\\Dao\\Bean', 'TheCodingMachine\\TDBM\\Test\\Dao', self::getConnection(), $this->getNamingStrategy(), $this->getCache(), null, null, [$this->getDummyGeneratorListener()]);
194
        $configuration->setPathFinder(new PathFinder(null, dirname(__DIR__, 4)));
195
        $newTdbmService = new TDBMService($configuration);
196
        $this->assertEquals(UserBean::class, $newTdbmService->getBeanClassName('users'));
197
    }
198
199
    /**
200
     * @depends testDaoGeneration
201
     */
202
    public function testGeneratedGetById(): void
203
    {
204
        $contactDao = new ContactDao($this->tdbmService);
205
        $contactBean = $contactDao->getById(1);
206
        $this->assertEquals(1, $contactBean->getId());
207
        $this->assertInstanceOf('\\DateTimeInterface', $contactBean->getCreatedAt());
208
209
        // FIXME: Question: que faire du paramètre stockage "UTC"????
210
    }
211
212
    /**
213
     * @depends testDaoGeneration
214
     */
215
    public function testGeneratedGetByIdLazyLoaded(): void
216
    {
217
        $roleDao = new RoleDao($this->tdbmService);
218
        $roleBean = $roleDao->getById(1, true);
219
        $this->assertEquals(1, $roleBean->getId());
220
        $this->assertInstanceOf('\\DateTimeInterface', $roleBean->getCreatedAt());
221
222
        $roleBean2 = $roleDao->getById(1, true);
223
        $this->assertTrue($roleBean === $roleBean2);
224
    }
225
226
    /**
227
     * @depends testDaoGeneration
228
     */
229
    public function testDefaultValueOnNewBean(): void
230
    {
231
        $roleBean = new RoleBean('my_role');
232
        $this->assertEquals(1, $roleBean->getStatus());
233
    }
234
235
    /**
236
     * @depends testDaoGeneration
237
     */
238
    public function testForeignKeyInBean(): void
239
    {
240
        $userDao = new UserDao($this->tdbmService);
241
        $userBean = $userDao->getById(1);
242
        $country = $userBean->getCountry();
243
244
        $this->assertEquals('UK', $country->getLabel());
245
246
        $userBean2 = $userDao->getById(1);
247
        $this->assertTrue($userBean === $userBean2);
248
249
        $contactDao = new ContactDao($this->tdbmService);
250
        $contactBean = $contactDao->getById(1);
251
252
        $this->assertTrue($userBean === $contactBean);
253
    }
254
255
    /**
256
     * @depends testDaoGeneration
257
     */
258
    public function testNewBeans(): void
259
    {
260
        $countryDao = new CountryDao($this->tdbmService);
261
        $userDao = new UserDao($this->tdbmService);
262
        $userBean = new UserBean('John Doe', '[email protected]', $countryDao->getById(2), 'john.doe');
263
        $userBean->setOrder(1); // Let's set a "protected keyword" column.
264
265
        $userDao->save($userBean);
266
267
        $this->assertNull($userBean->getManager());
268
    }
269
270
    /**
271
     * @depends testDaoGeneration
272
     */
273
    public function testDateTimeImmutableGetter(): void
274
    {
275
        $userDao = new UserDao($this->tdbmService);
276
        $user = $userDao->getById(1);
277
278
        $this->assertInstanceOf('\DateTimeImmutable', $user->getCreatedAt());
279
    }
280
281
    /**
282
     * @depends testDaoGeneration
283
     */
284
    public function testAssigningNewBeans(): void
285
    {
286
        $userDao = new UserDao($this->tdbmService);
287
        $countryBean = new CountryBean('Mexico');
288
        $userBean = new UserBean('Speedy Gonzalez', '[email protected]', $countryBean, 'speedy.gonzalez');
289
        $this->assertEquals($countryBean, $userBean->getCountry());
290
291
        $userDao->save($userBean);
292
    }
293
294
    /**
295
     * @depends testAssigningNewBeans
296
     */
297
    public function testUpdatingProtectedColumn(): void
298
    {
299
        $userDao = new UserDao($this->tdbmService);
300
        $userBean = $userDao->findOneByLogin('speedy.gonzalez');
301
        $userBean->setOrder(2);
302
        $userDao->save($userBean);
303
        $this->assertSame(2, $userBean->getOrder());
304
    }
305
306
    /**
307
     * @depends testDaoGeneration
308
     */
309
    public function testAssigningExistingRelationship(): void
310
    {
311
        $userDao = new UserDao($this->tdbmService);
312
        $user = $userDao->getById(1);
313
        $countryDao = new CountryDao($this->tdbmService);
314
        $country = $countryDao->getById(2);
315
316
        $user->setCountry($country);
317
        $this->assertEquals(TDBMObjectStateEnum::STATE_DIRTY, $user->_getStatus());
318
    }
319
320
    /**
321
     * @depends testDaoGeneration
322
     */
323
    public function testDirectReversedRelationship(): void
324
    {
325
        $countryDao = new CountryDao($this->tdbmService);
326
        $country = $countryDao->getById(1);
327
        $users = $country->getUsers();
328
329
        $arr = $users->toArray();
330
331
        $this->assertCount(1, $arr);
332
        $this->assertInstanceOf('TheCodingMachine\\TDBM\\Test\\Dao\\Bean\\UserBean', $arr[0]);
333
        $this->assertEquals('jean.dupont', $arr[0]->getLogin());
334
335
        $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...
336
        $users = $country->getUsers();
337
338
        $arr = $users->toArray();
339
340
        $this->assertCount(2, $arr);
341
        $this->assertInstanceOf('TheCodingMachine\\TDBM\\Test\\Dao\\Bean\\UserBean', $arr[1]);
342
        $this->assertEquals('speedy.gonzalez', $arr[1]->getLogin());
343
    }
344
345
    /**
346
     * @depends testDaoGeneration
347
     */
348
    public function testDeleteInDirectReversedRelationship(): void
349
    {
350
        $countryDao = new CountryDao($this->tdbmService);
351
        $country = $countryDao->getById(1);
352
353
        $userDao = new UserDao($this->tdbmService);
354
        $newUser = new UserBean('John Snow', '[email protected]', $country, 'john.snow');
355
        $userDao->save($newUser);
356
357
        $users = $country->getUsers();
358
359
        $arr = $users->toArray();
360
361
        $this->assertCount(2, $arr);
362
363
        $userDao->delete($arr[1]);
364
365
        $users = $country->getUsers();
366
        $arr = $users->toArray();
367
        $this->assertCount(1, $arr);
368
    }
369
370
    /**
371
     * @depends testDaoGeneration
372
     */
373
    public function testJointureGetters(): void
374
    {
375
        $roleDao = new RoleDao($this->tdbmService);
376
        $role = $roleDao->getById(1);
377
        $users = $role->getUsers();
378
379
        $this->assertCount(2, $users);
380
        $this->assertInstanceOf('TheCodingMachine\\TDBM\\Test\\Dao\\Bean\\UserBean', $users[0]);
381
382
        $rights = $role->getRights();
383
384
        $this->assertCount(2, $rights);
385
        $this->assertInstanceOf('TheCodingMachine\\TDBM\\Test\\Dao\\Bean\\RightBean', $rights[0]);
386
    }
387
388
    /**
389
     * @depends testDaoGeneration
390
     */
391
    public function testNestedIterationOnAlterableResultIterator(): void
392
    {
393
        $countryDao = new CountryDao($this->tdbmService);
394
        $country = $countryDao->getById(2);
395
396
        $count = 0;
397
        // Let's perform 2 nested calls to check that iterators do not melt.
398
        foreach ($country->getUsers() as $user) {
399
            foreach ($country->getUsers() as $user2) {
400
                $count++;
401
            }
402
        }
403
        // There are 3 users linked to country 2.
404
        $this->assertSame(9, $count);
405
    }
406
407
    /**
408
     * @depends testDaoGeneration
409
     */
410
    public function testNewBeanConstructor(): void
411
    {
412
        $role = new RoleBean('Newrole');
413
        $this->assertEquals(TDBMObjectStateEnum::STATE_DETACHED, $role->_getStatus());
414
    }
415
416
    /**
417
     * @depends testDaoGeneration
418
     */
419
    public function testJointureAdderOnNewBean(): void
420
    {
421
        $countryDao = new CountryDao($this->tdbmService);
422
        $country = $countryDao->getById(1);
423
        $user = new UserBean('Speedy Gonzalez', '[email protected]', $country, 'speedy.gonzalez');
424
        $role = new RoleBean('Mouse');
425
        $user->addRole($role);
426
        $roles = $user->getRoles();
427
        $this->assertCount(1, $roles);
428
        $role = $roles[0];
429
        $this->assertInstanceOf('TheCodingMachine\\TDBM\\Test\\Dao\\Bean\\RoleBean', $role);
430
        $users = $role->getUsers();
431
        $this->assertCount(1, $users);
432
        $this->assertEquals($user, $users[0]);
433
434
        $role->removeUser($user);
435
        $this->assertCount(0, $role->getUsers());
436
        $this->assertCount(0, $user->getRoles());
437
    }
438
439
    /**
440
     * @depends testDaoGeneration
441
     */
442
    public function testJointureDeleteBeforeGetters(): void
443
    {
444
        $roleDao = new RoleDao($this->tdbmService);
445
        $userDao = new UserDao($this->tdbmService);
446
        $role = $roleDao->getById(1);
447
        $user = $userDao->getById(1);
448
449
        // We call removeUser BEFORE calling getUsers
450
        // This should work as expected.
451
        $role->removeUser($user);
452
        $users = $role->getUsers();
453
454
        $this->assertCount(1, $users);
455
    }
456
457
    /**
458
     * @depends testDaoGeneration
459
     */
460
    public function testJointureMultiAdd(): void
461
    {
462
        $countryDao = new CountryDao($this->tdbmService);
463
        $country = $countryDao->getById(1);
464
        $user = new UserBean('Speedy Gonzalez', '[email protected]', $country, 'speedy.gonzalez');
465
        $role = new RoleBean('Mouse');
466
        $user->addRole($role);
467
        $role->addUser($user);
468
        $user->addRole($role);
469
470
        $this->assertCount(1, $user->getRoles());
471
    }
472
473
    /**
474
     * Step 1: we remove the role 1 from user 1 but save role 1.
475
     * Expected result: no save done.
476
     *
477
     * @depends testDaoGeneration
478
     */
479
    public function testJointureSave1(): void
480
    {
481
        $roleDao = new RoleDao($this->tdbmService);
482
        $role = $roleDao->getById(1);
483
        $userDao = new UserDao($this->tdbmService);
484
        $user = $userDao->getById(1);
485
486
        $this->assertTrue($user->hasRole($role));
487
        $this->assertTrue($role->hasUser($user));
488
        $user->removeRole($role);
489
        $this->assertFalse($user->hasRole($role));
490
        $this->assertFalse($role->hasUser($user));
491
        $roleDao->save($role);
492
493
        $this->assertEquals(TDBMObjectStateEnum::STATE_DIRTY, $user->_getStatus());
494
        $this->assertEquals(TDBMObjectStateEnum::STATE_LOADED, $role->_getStatus());
495
    }
496
497
    /**
498
     * Step 2: we check that nothing was saved
499
     * Expected result: no save done.
500
     *
501
     * @depends testJointureSave1
502
     */
503
    public function testJointureSave2(): void
504
    {
505
        $roleDao = new RoleDao($this->tdbmService);
506
        $role = $roleDao->getById(1);
507
        $this->assertCount(2, $role->getUsers());
508
    }
509
510
    /**
511
     * Step 3: we remove the role 1 from user 1 and save user 1.
512
     * Expected result: save done.
513
     *
514
     * @depends testJointureSave2
515
     */
516
    public function testJointureSave3(): void
517
    {
518
        $roleDao = new RoleDao($this->tdbmService);
519
        $role = $roleDao->getById(1);
520
        $userDao = new UserDao($this->tdbmService);
521
        $user = $userDao->getById(1);
522
523
        $this->assertCount(1, $user->getRoles());
524
        $user->removeRole($role);
525
        $this->assertCount(0, $user->getRoles());
526
        $userDao->save($user);
527
        $this->assertCount(0, $user->getRoles());
528
    }
529
530
    /**
531
     * Step 4: we check that save was done
532
     * Expected result: save done.
533
     *
534
     * @depends testJointureSave3
535
     */
536
    public function testJointureSave4(): void
537
    {
538
        $roleDao = new RoleDao($this->tdbmService);
539
        $role = $roleDao->getById(1);
540
        $this->assertCount(1, $role->getUsers());
541
        $userDao = new UserDao($this->tdbmService);
542
        $user = $userDao->getById(1);
543
        $this->assertCount(0, $user->getRoles());
544
    }
545
546
    /**
547
     * Step 5: we add the role 1 from user 1 and save user 1.
548
     * Expected result: save done.
549
     *
550
     * @depends testJointureSave4
551
     */
552
    public function testJointureSave5(): void
553
    {
554
        $roleDao = new RoleDao($this->tdbmService);
555
        $role = $roleDao->getById(1);
556
        $userDao = new UserDao($this->tdbmService);
557
        $user = $userDao->getById(1);
558
559
        $user->addRole($role);
560
        $this->assertCount(1, $user->getRoles());
561
        $userDao->save($user);
562
    }
563
564
    /**
565
     * Step 6: we check that save was done
566
     * Expected result: save done.
567
     *
568
     * @depends testJointureSave5
569
     */
570
    public function testJointureSave6(): void
571
    {
572
        $roleDao = new RoleDao($this->tdbmService);
573
        $role = $roleDao->getById(1);
574
        $this->assertCount(2, $role->getUsers());
575
        $userDao = new UserDao($this->tdbmService);
576
        $user = $userDao->getById(1);
577
        $this->assertCount(1, $user->getRoles());
578
    }
579
580
    /**
581
     * Step 7: we add a new role to user 1 and save user 1.
582
     * Expected result: save done, including the new role.
583
     *
584
     * @depends testJointureSave6
585
     */
586
    public function testJointureSave7(): void
587
    {
588
        $role = new RoleBean('my new role');
589
        $userDao = new UserDao($this->tdbmService);
590
        $user = $userDao->getById(1);
591
592
        $user->addRole($role);
593
        $userDao->save($user);
594
595
        $this->assertEquals(TDBMObjectStateEnum::STATE_LOADED, $role->_getStatus());
596
    }
597
598
    /**
599
     * Step 8: we check that save was done
600
     * Expected result: save done.
601
     *
602
     * @depends testJointureSave7
603
     */
604
    public function testJointureSave8(): void
605
    {
606
        $roleDao = new RoleDao($this->tdbmService);
607
        $userDao = new UserDao($this->tdbmService);
608
        $user = $userDao->getById(1);
609
610
        $roles = $user->getRoles();
611
        foreach ($roles as $role) {
612
            if ($role->getName() === 'my new role') {
613
                $selectedRole = $role;
614
                break;
615
            }
616
        }
617
        $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...
618
619
        $this->assertCount(2, $user->getRoles());
620
621
        // Expected: relationship removed!
622
        $roleDao->delete($selectedRole);
623
624
        $this->assertCount(1, $user->getRoles());
625
    }
626
627
    /**
628
     * Step 9: Let's test the setXXX method.
629
     *
630
     * @depends testJointureSave8
631
     */
632
    public function testJointureSave9(): void
633
    {
634
        $roleDao = new RoleDao($this->tdbmService);
635
        $userDao = new UserDao($this->tdbmService);
636
        $user = $userDao->getById(1);
637
638
        // At this point, user 1 is linked to role 1.
639
        // Let's bind it to role 2.
640
        $user->setRoles([$roleDao->getById(2)]);
641
        $userDao->save($user);
642
        $this->assertTrue($user->hasRole($roleDao->getById(2)));
643
    }
644
645
    /**
646
     * Step 10: Let's check results of 9.
647
     *
648
     * @depends testJointureSave9
649
     */
650
    public function testJointureSave10(): void
651
    {
652
        $userDao = new UserDao($this->tdbmService);
653
        $user = $userDao->getById(1);
654
655
        $roles = $user->getRoles();
656
657
        $this->assertCount(1, $roles);
658
        $this->assertEquals(2, $roles[0]->getId());
659
    }
660
661
    /**
662
     * Test jointure in a parent table in an inheritance relationship
663
     *
664
     * @depends testDaoGeneration
665
     */
666
    public function testJointureInParentTable(): void
667
    {
668
        $userDao = new UserDao($this->tdbmService);
669
        $user = $userDao->getById(1);
670
671
        $boats = $user->getBoats();
672
673
        $this->assertCount(1, $boats);
674
        $this->assertEquals(1, $boats[0]->getId());
675
    }
676
677
    /**
678
     * @depends testDaoGeneration
679
     */
680
    public function testFindOrderBy(): void
681
    {
682
        $userDao = new TestUserDao($this->tdbmService);
683
        $users = $userDao->getUsersByAlphabeticalOrder();
684
685
        $this->assertCount(6, $users);
686
        $this->assertEquals('bill.shakespeare', $users[0]->getLogin());
687
        $this->assertEquals('jean.dupont', $users[1]->getLogin());
688
689
        $users = $userDao->getUsersByCountryOrder();
690
        $this->assertCount(6, $users);
691
        $countryName1 = $users[0]->getCountry()->getLabel();
692
        for ($i = 1; $i < 6; $i++) {
693
            $countryName2 = $users[$i]->getCountry()->getLabel();
694
            $this->assertLessThanOrEqual(0, strcmp($countryName1, $countryName2));
695
            $countryName1 = $countryName2;
696
        }
697
    }
698
699
    /**
700
     * @depends testDaoGeneration
701
     */
702
    public function testFindFromSqlOrderBy(): void
703
    {
704
        $userDao = new TestUserDao($this->tdbmService);
705
        $users = $userDao->getUsersFromSqlByAlphabeticalOrder();
706
707
        $this->assertCount(6, $users);
708
        $this->assertEquals('bill.shakespeare', $users[0]->getLogin());
709
        $this->assertEquals('jean.dupont', $users[1]->getLogin());
710
711
        $users = $userDao->getUsersFromSqlByCountryOrder();
712
        $this->assertCount(6, $users);
713
        $countryName1 = $users[0]->getCountry()->getLabel();
714
        for ($i = 1; $i < 6; $i++) {
715
            $countryName2 = $users[$i]->getCountry()->getLabel();
716
            $this->assertLessThanOrEqual(0, strcmp($countryName1, $countryName2));
717
            $countryName1 = $countryName2;
718
        }
719
    }
720
721
    /**
722
     * @depends testDaoGeneration
723
     */
724
    public function testFindFromSqlOrderByOnInheritedBean(): void
725
    {
726
        $articleDao = new TestArticleDao($this->tdbmService);
727
        $articles = $articleDao->getArticlesByUserLogin();
728
729
        foreach ($articles as $article) {
730
            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...
731
        }
732
        $this->assertCount(0, $articles);
733
    }
734
735
736
    /**
737
     * @depends testDaoGeneration
738
     */
739
    public function testFindFromSqlOrderByJoinRole(): void
740
    {
741
        $roleDao = new TestRoleDao($this->tdbmService);
742
        $roles = $roleDao->getRolesByRightCanSing('roles.name DESC');
743
744
        $this->assertCount(2, $roles);
745
        $this->assertEquals('Singers', $roles[0]->getName());
746
        $this->assertEquals('Admins', $roles[1]->getName());
747
    }
748
749
    /**
750
     * @depends testDaoGeneration
751
     */
752
    public function testFindFromRawSqlOrderByUserCount(): void
753
    {
754
        $countryDao = new TestCountryDao($this->tdbmService);
755
        $countries = $countryDao->getCountriesByUserCount();
756
757
        $nbCountries = 4;
758
        $this->assertCount($nbCountries, $countries);
759
        for ($i = 1; $i < $nbCountries; $i++) {
760
            $this->assertLessThanOrEqual($countries[$i - 1]->getUsers()->count(), $countries[$i]->getUsers()->count());
761
        }
762
    }
763
764
    /**
765
     * @depends testDaoGeneration
766
     */
767
    public function testFindFromRawSqlWithUnion(): void
768
    {
769
        $countryDao = new TestCountryDao($this->tdbmService);
770
        $countries = $countryDao->getCountriesUsingUnion();
771
772
        $this->assertCount(2, $countries);
773
        $this->assertEquals(1, $countries[0]->getId());
774
    }
775
776
    /**
777
     * @depends testDaoGeneration
778
     */
779
    public function testFindFromRawSqlWithSimpleQuery(): void
780
    {
781
        $countryDao = new TestCountryDao($this->tdbmService);
782
        $countries = $countryDao->getCountriesUsingSimpleQuery();
783
784
        $this->assertCount(1, $countries);
785
        $this->assertEquals(1, $countries[0]->getId());
786
    }
787
788
    /**
789
     * @depends testDaoGeneration
790
     */
791
    public function testFindFromRawSqlWithDistinctQuery(): void
792
    {
793
        $countryDao = new TestCountryDao($this->tdbmService);
794
        $countries = $countryDao->getCountriesUsingDistinctQuery();
795
796
        $this->assertCount(1, $countries);
797
        $this->assertEquals(2, $countries[0]->getId());
798
    }
799
800
    /**
801
     * @depends testDaoGeneration
802
     */
803
    public function testFindFilters(): void
804
    {
805
        $userDao = new TestUserDao($this->tdbmService);
806
        $users = $userDao->getUsersByLoginStartingWith('bill');
807
808
        $this->assertCount(1, $users);
809
        $this->assertEquals('bill.shakespeare', $users[0]->getLogin());
810
    }
811
812
    /**
813
     * @depends testDaoGeneration
814
     */
815
    public function testFindMode(): void
816
    {
817
        $userDao = new TestUserDao($this->tdbmService);
818
        $users = $userDao->getUsersByLoginStartingWith('bill', TDBMService::MODE_CURSOR);
819
820
        $this->expectException('TheCodingMachine\TDBM\TDBMException');
821
        $users[0];
822
    }
823
824
    /**
825
     * @depends testDaoGeneration
826
     */
827
    public function testFindAll(): void
828
    {
829
        $userDao = new TestUserDao($this->tdbmService);
830
        $users = $userDao->findAll();
831
832
        $this->assertCount(6, $users);
833
    }
834
835
    /**
836
     * @depends testDaoGeneration
837
     */
838
    public function testFindOne(): void
839
    {
840
        $userDao = new TestUserDao($this->tdbmService);
841
        $user = $userDao->getUserByLogin('bill.shakespeare');
842
843
        $this->assertEquals('bill.shakespeare', $user->getLogin());
844
    }
845
846
    /**
847
     * @depends testDaoGeneration
848
     */
849
    public function testJsonEncodeBean(): void
850
    {
851
        $userDao = new TestUserDao($this->tdbmService);
852
        $user = $userDao->getUserByLogin('bill.shakespeare');
853
854
        $jsonEncoded = json_encode($user);
855
        $userDecoded = json_decode($jsonEncoded, true);
856
857
        $this->assertEquals('bill.shakespeare', $userDecoded['login']);
858
859
        // test serialization of dates.
860
        $this->assertTrue(is_string($userDecoded['createdAt']));
861
        $this->assertEquals('2015-10-24', (new \DateTimeImmutable($userDecoded['createdAt']))->format('Y-m-d'));
862
        $this->assertNull($userDecoded['modifiedAt']);
863
864
        // testing many to 1 relationships
865
        $this->assertEquals('UK', $userDecoded['country']['label']);
866
867
        // testing many to many relationships
868
        $this->assertCount(1, $userDecoded['roles']);
869
        $this->assertArrayNotHasKey('users', $userDecoded['roles'][0]);
870
        $this->assertArrayNotHasKey('rights', $userDecoded['roles'][0]);
871
    }
872
873
    /**
874
     * @depends testDaoGeneration
875
     */
876
    public function testNullableForeignKey(): void
877
    {
878
        $userDao = new TestUserDao($this->tdbmService);
879
        $user = $userDao->getUserByLogin('john.smith');
880
881
        $this->assertNull($user->getManager());
882
883
        $jsonEncoded = json_encode($user);
884
        $userDecoded = json_decode($jsonEncoded, true);
885
886
        $this->assertNull($userDecoded['manager']);
887
    }
888
889
    /**
890
     * Test that setting (and saving) objects' references (foreign keys relations) to null is working.
891
     *
892
     * @depends testDaoGeneration
893
     */
894
    public function testSetToNullForeignKey(): void
895
    {
896
        $userDao = new TestUserDao($this->tdbmService);
897
        $user = $userDao->getUserByLogin('john.smith');
898
        $manager = $userDao->getUserByLogin('jean.dupont');
899
900
        $user->setManager($manager);
901
        $userDao->save($user);
902
903
        $user->setManager(null);
904
        $userDao->save($user);
905
        $this->assertNull($user->getManager());
906
    }
907
908
    /**
909
     * @depends testDaoGeneration
910
     */
911
    public function testQueryOnWrongTableName(): void
912
    {
913
        $userDao = new TestUserDao($this->tdbmService);
914
        $users = $userDao->getUsersWrongTableName();
915
        $this->expectException('Mouf\Database\SchemaAnalyzer\SchemaAnalyzerTableNotFoundException');
916
        $this->expectExceptionMessage('Could not find table \'contacts\'. Did you mean \'contact\'?');
917
        $users->count();
918
    }
919
920
    /**
921
     * @depends testDaoGeneration
922
     */
923
    /*public function testQueryNullForeignKey(): void
924
    {
925
        $userDao = new TestUserDao($this->tdbmService);
926
        $users = $userDao->getUsersByManagerId(null);
927
        $this->assertCount(3, $users);
928
    }*/
929
930
    /**
931
     * @depends testDaoGeneration
932
     */
933
    public function testInnerJsonEncode(): void
934
    {
935
        $userDao = new TestUserDao($this->tdbmService);
936
        $user = $userDao->getUserByLogin('bill.shakespeare');
937
938
        $jsonEncoded = json_encode(['user' => $user]);
939
        $msgDecoded = json_decode($jsonEncoded, true);
940
941
        $this->assertEquals('bill.shakespeare', $msgDecoded['user']['login']);
942
    }
943
944
    /**
945
     * @depends testDaoGeneration
946
     */
947
    public function testArrayJsonEncode(): void
948
    {
949
        $userDao = new TestUserDao($this->tdbmService);
950
        $users = $userDao->getUsersByLoginStartingWith('bill');
951
952
        $jsonEncoded = json_encode($users);
953
        $msgDecoded = json_decode($jsonEncoded, true);
954
955
        $this->assertCount(1, $msgDecoded);
956
    }
957
958
    /**
959
     * @depends testDaoGeneration
960
     */
961
    public function testCursorJsonEncode(): void
962
    {
963
        $userDao = new TestUserDao($this->tdbmService);
964
        $users = $userDao->getUsersByLoginStartingWith('bill', TDBMService::MODE_CURSOR);
965
966
        $jsonEncoded = json_encode($users);
967
        $msgDecoded = json_decode($jsonEncoded, true);
968
969
        $this->assertCount(1, $msgDecoded);
970
    }
971
972
    /**
973
     * @depends testDaoGeneration
974
     */
975
    public function testPageJsonEncode(): void
976
    {
977
        $userDao = new TestUserDao($this->tdbmService);
978
        $users = $userDao->getUsersByLoginStartingWith('bill');
979
980
        $jsonEncoded = json_encode($users->take(0, 1));
981
        $msgDecoded = json_decode($jsonEncoded, true);
982
983
        $this->assertCount(1, $msgDecoded);
984
    }
985
986
    /**
987
     * @depends testDaoGeneration
988
     */
989
    public function testInnerResultIteratorCountAfterFetch(): void
990
    {
991
        $userDao = new TestUserDao($this->tdbmService);
992
        $users = $userDao->getUsersByLoginStartingWith('j')->take(0, 4);
993
        $users->toArray(); // We force to fetch
994
        $this->assertEquals(3, $users->count());
995
    }
996
997
    /**
998
     * @depends testDaoGeneration
999
     */
1000
    public function testFirst(): void
1001
    {
1002
        $userDao = new TestUserDao($this->tdbmService);
1003
        $users = $userDao->getUsersByLoginStartingWith('bill');
1004
1005
        $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...
1006
        $this->assertEquals('bill.shakespeare', $bill->getLogin());
1007
    }
1008
1009
    /**
1010
     * @depends testDaoGeneration
1011
     */
1012
    public function testFirstNull(): void
1013
    {
1014
        $userDao = new TestUserDao($this->tdbmService);
1015
        $users = $userDao->getUsersByLoginStartingWith('mike');
1016
1017
        $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...
1018
        $this->assertNull($user);
1019
    }
1020
1021
    /**
1022
     * @depends testDaoGeneration
1023
     */
1024
    public function testCloneBeanAttachedBean(): void
1025
    {
1026
        $userDao = new TestUserDao($this->tdbmService);
1027
        $user = $userDao->getUserByLogin('bill.shakespeare');
1028
        $this->assertEquals(4, $user->getId());
1029
        $user2 = clone $user;
1030
        $this->assertNull($user2->getId());
1031
        $this->assertEquals('bill.shakespeare', $user2->getLogin());
1032
        $this->assertEquals('Bill Shakespeare', $user2->getName());
1033
        $this->assertEquals('UK', $user2->getCountry()->getLabel());
1034
1035
        // MANY 2 MANY must be duplicated
1036
        $this->assertEquals('Writers', $user2->getRoles()[0]->getName());
1037
1038
        // Let's test saving this clone
1039
        $user2->setLogin('william.shakespeare');
1040
        $userDao->save($user2);
1041
1042
        $user3 = $userDao->getUserByLogin('william.shakespeare');
1043
        $this->assertTrue($user3 === $user2);
1044
        $userDao->delete($user3);
1045
1046
        // Finally, let's test the origin user still exists!
1047
        $user4 = $userDao->getUserByLogin('bill.shakespeare');
1048
        $this->assertEquals('bill.shakespeare', $user4->getLogin());
1049
    }
1050
1051
    /**
1052
     * @depends testDaoGeneration
1053
     */
1054
    public function testCloneNewBean(): void
1055
    {
1056
        $countryDao = new CountryDao($this->tdbmService);
1057
        $roleDao = new RoleDao($this->tdbmService);
1058
        $role = $roleDao->getById(1);
1059
1060
        $userBean = new UserBean('John Doe', '[email protected]', $countryDao->getById(2), 'john.doe');
1061
        $userBean->addRole($role);
1062
1063
        $user2 = clone $userBean;
1064
1065
        $this->assertNull($user2->getId());
1066
        $this->assertEquals('john.doe', $user2->getLogin());
1067
        $this->assertEquals('John Doe', $user2->getName());
1068
        $this->assertEquals('UK', $user2->getCountry()->getLabel());
1069
1070
        // MANY 2 MANY must be duplicated
1071
        $this->assertEquals($role->getName(), $user2->getRoles()[0]->getName());
1072
    }
1073
1074
    /**
1075
     * @depends testDaoGeneration
1076
     */
1077
    public function testCascadeDelete(): void
1078
    {
1079
        $userDao = new TestUserDao($this->tdbmService);
1080
        $countryDao = new CountryDao($this->tdbmService);
1081
1082
        $spain = new CountryBean('Spain');
1083
        $sanchez = new UserBean('Manuel Sanchez', '[email protected]', $spain, 'manuel.sanchez');
1084
1085
        $countryDao->save($spain);
1086
        $userDao->save($sanchez);
1087
1088
        $speedy2 = $userDao->getUserByLogin('manuel.sanchez');
1089
        $this->assertTrue($sanchez === $speedy2);
1090
1091
        $exceptionTriggered = false;
1092
        try {
1093
            $countryDao->delete($spain);
1094
        } catch (ForeignKeyConstraintViolationException $e) {
1095
            $exceptionTriggered = true;
1096
        }
1097
        $this->assertTrue($exceptionTriggered);
1098
1099
        $countryDao->delete($spain, true);
1100
1101
        // Let's check that speed gonzalez was removed.
1102
        $speedy3 = $userDao->getUserByLogin('manuel.sanchez');
1103
        $this->assertNull($speedy3);
1104
    }
1105
1106
    /**
1107
     * @depends testDaoGeneration
1108
     */
1109
    public function testDiscardChanges(): void
1110
    {
1111
        $contactDao = new ContactDao($this->tdbmService);
1112
        $contactBean = $contactDao->getById(1);
1113
1114
        $oldName = $contactBean->getName();
1115
1116
        $contactBean->setName('MyNewName');
1117
1118
        $contactBean->discardChanges();
1119
1120
        $this->assertEquals($oldName, $contactBean->getName());
1121
    }
1122
1123
    /**
1124
     * @depends testDaoGeneration
1125
     */
1126
    public function testDiscardChangesOnNewBeanFails(): void
1127
    {
1128
        $person = new PersonBean('John Foo', new \DateTimeImmutable());
1129
        $this->expectException('TheCodingMachine\TDBM\TDBMException');
1130
        $person->discardChanges();
1131
    }
1132
1133
    /**
1134
     * @depends testDaoGeneration
1135
     */
1136
    public function testDiscardChangesOnDeletedBeanFails(): void
1137
    {
1138
        $userDao = new TestUserDao($this->tdbmService);
1139
        $countryDao = new CountryDao($this->tdbmService);
1140
1141
        $sanchez = new UserBean('Manuel Sanchez', '[email protected]', $countryDao->getById(1), 'manuel.sanchez');
1142
1143
        $userDao->save($sanchez);
1144
1145
        $userDao->delete($sanchez);
1146
1147
        $this->expectException('TheCodingMachine\TDBM\TDBMException');
1148
        // Cannot discard changes on a bean that is already deleted.
1149
        $sanchez->discardChanges();
1150
    }
1151
1152
    /**
1153
     * @depends testDaoGeneration
1154
     */
1155
    public function testUniqueIndexBasedSearch(): void
1156
    {
1157
        $userDao = new UserDao($this->tdbmService);
1158
        $user = $userDao->findOneByLogin('bill.shakespeare');
1159
1160
        $this->assertEquals('bill.shakespeare', $user->getLogin());
1161
        $this->assertEquals('Bill Shakespeare', $user->getName());
1162
    }
1163
1164
    /**
1165
     * @depends testDaoGeneration
1166
     */
1167
    public function testFindOneByRetunsNull(): void
1168
    {
1169
        // Let's assert that the findOneBy... methods can return null.
1170
        $userDao = new UserDao($this->tdbmService);
1171
        $userBean = $userDao->findOneByLogin('not_exist');
1172
1173
        $this->assertNull($userBean);
1174
    }
1175
1176
    /**
1177
     * @depends testDaoGeneration
1178
     */
1179
    public function testMultiColumnsIndexBasedSearch(): void
1180
    {
1181
        $countryDao = new CountryDao($this->tdbmService);
1182
        $userDao = new UserDao($this->tdbmService);
1183
        $users = $userDao->findByStatusAndCountry('on', $countryDao->getById(1));
1184
1185
        $this->assertEquals('jean.dupont', $users[0]->getLogin());
1186
    }
1187
1188
    /**
1189
     * @depends testDaoGeneration
1190
     */
1191
    public function testPartialMultiColumnsIndexBasedSearch(): void
1192
    {
1193
        $userDao = new UserDao($this->tdbmService);
1194
        $users = $userDao->findByStatusAndCountry('on');
1195
1196
        $this->assertCount(2, $users);
1197
    }
1198
1199
    /**
1200
     * @depends testDaoGeneration
1201
     */
1202
    public function testCreationInNullableDate(): void
1203
    {
1204
        $roleDao = new RoleDao($this->tdbmService);
1205
1206
        $role = new RoleBean('newbee');
1207
        $roleDao->save($role);
1208
1209
        $this->assertNull($role->getCreatedAt());
1210
    }
1211
1212
    /**
1213
     * @depends testDaoGeneration
1214
     */
1215
    public function testUpdateInNullableDate(): void
1216
    {
1217
        $roleDao = new RoleDao($this->tdbmService);
1218
1219
        $role = new RoleBean('newbee');
1220
        $roleDao->save($role);
1221
1222
        $role->setCreatedAt(null);
1223
        $roleDao->save($role);
1224
        $this->assertNull($role->getCreatedAt());
1225
    }
1226
1227
    /**
1228
     * @depends testDaoGeneration
1229
     */
1230
    public function testFindFromSql(): void
1231
    {
1232
        $roleDao = new TestRoleDao($this->tdbmService);
1233
1234
        $roles = $roleDao->getRolesByRightCanSing();
1235
        $this->assertCount(2, $roles);
1236
        $this->assertInstanceOf(RoleBean::class, $roles[0]);
1237
    }
1238
1239
    /**
1240
     * @depends testDaoGeneration
1241
     */
1242
    public function testFindOneFromSql(): void
1243
    {
1244
        $roleDao = new TestRoleDao($this->tdbmService);
1245
1246
        $role = $roleDao->getRoleByRightCanSingAndNameSinger();
1247
        $this->assertInstanceOf(RoleBean::class, $role);
1248
    }
1249
1250
    /**
1251
     * @depends testDaoGeneration
1252
     */
1253
    public function testCreateEmptyExtendedBean(): void
1254
    {
1255
        // This test cases checks issue https://github.com/thecodingmachine/database.tdbm/issues/92
1256
1257
        $dogDao = new DogDao($this->tdbmService);
1258
1259
        // We are not filling no field that is part of dog table.
1260
        $dog = new DogBean('Youki');
1261
        $dog->setOrder(1);
1262
1263
        $dogDao->save($dog);
1264
        $this->assertNull($dog->getRace());
1265
    }
1266
1267
    /**
1268
     * @depends testCreateEmptyExtendedBean
1269
     */
1270
    public function testFetchEmptyExtendedBean(): void
1271
    {
1272
        // This test cases checks issue https://github.com/thecodingmachine/database.tdbm/issues/92
1273
1274
        $animalDao = new AnimalDao($this->tdbmService);
1275
1276
        // We are not filling no field that is part of dog table.
1277
        $animalBean = $animalDao->getById(1);
1278
1279
        $this->assertInstanceOf(DogBean::class, $animalBean);
1280
    }
1281
1282
    /**
1283
     * @depends testDaoGeneration
1284
     */
1285
    public function testTwoBranchesHierarchy(): void
1286
    {
1287
        // This test cases checks issue https://github.com/thecodingmachine/mouf/issues/131
1288
1289
        $catDao = new CatDao($this->tdbmService);
1290
1291
        // We are not filling no field that is part of dog table.
1292
        $cat = new CatBean('Mew');
1293
        $cat->setOrder(2);
1294
1295
        $catDao->save($cat);
1296
        $this->assertNotNull($cat->getId());
1297
    }
1298
1299
    /**
1300
     * @depends testTwoBranchesHierarchy
1301
     */
1302
    public function testFetchTwoBranchesHierarchy(): void
1303
    {
1304
        // This test cases checks issue https://github.com/thecodingmachine/mouf/issues/131
1305
1306
        $animalDao = new AnimalDao($this->tdbmService);
1307
1308
        $animalBean = $animalDao->getById(2);
1309
1310
        $this->assertInstanceOf(CatBean::class, $animalBean);
1311
        /* @var $animalBean CatBean */
1312
        $animalBean->setCutenessLevel(999);
1313
        $animalBean->setUppercaseColumn('foobar');
1314
1315
        $animalDao->save($animalBean);
1316
    }
1317
1318
    /**
1319
     * @depends testDaoGeneration
1320
     */
1321
    public function testExceptionOnGetById(): void
1322
    {
1323
        $countryDao = new CountryDao($this->tdbmService);
1324
        $this->expectException(\TypeError::class);
1325
        $countryDao->getById(null);
1326
    }
1327
1328
    /**
1329
     * @depends testDaoGeneration
1330
     */
1331
    public function testDisconnectedManyToOne(): void
1332
    {
1333
        // This test cases checks issue https://github.com/thecodingmachine/database.tdbm/issues/99
1334
1335
        $country = new CountryBean('Spain');
1336
1337
        $user = new UserBean('John Doe', '[email protected]', $country, 'john.doe');
1338
1339
        $this->assertCount(1, $country->getUsers());
1340
        $this->assertSame($user, $country->getUsers()[0]);
1341
    }
1342
1343
    /**
1344
     * @depends testDaoGeneration
1345
     */
1346
    public function testOrderByExternalCol(): void
1347
    {
1348
        // This test cases checks issue https://github.com/thecodingmachine/database.tdbm/issues/106
1349
1350
        $userDao = new TestUserDao($this->tdbmService);
1351
        $users = $userDao->getUsersByCountryName();
1352
1353
        $this->assertEquals('UK', $users[0]->getCountry()->getLabel());
1354
    }
1355
1356
    /**
1357
     * @depends testDaoGeneration
1358
     */
1359
    public function testResultIteratorSort(): void
1360
    {
1361
        $userDao = new UserDao($this->tdbmService);
1362
        $users = $userDao->findAll()->withOrder('country.label DESC');
1363
1364
        $this->assertEquals('UK', $users[0]->getCountry()->getLabel());
1365
1366
        $users = $users->withOrder('country.label ASC');
1367
        $this->assertEquals('France', $users[0]->getCountry()->getLabel());
1368
    }
1369
1370
    /**
1371
     * @depends testDaoGeneration
1372
     */
1373
    public function testResultIteratorWithParameters(): void
1374
    {
1375
        $userDao = new TestUserDao($this->tdbmService);
1376
        $users = $userDao->getUsersByLoginStartingWith()->withParameters(['login' => 'bill%']);
1377
        $this->assertEquals('bill.shakespeare', $users[0]->getLogin());
1378
1379
        $users = $users->withParameters(['login' => 'jean%']);
1380
        $this->assertEquals('jean.dupont', $users[0]->getLogin());
1381
    }
1382
1383
    /**
1384
     * @depends testDaoGeneration
1385
     */
1386
    public function testOrderByExpression(): void
1387
    {
1388
        $userDao = new TestUserDao($this->tdbmService);
1389
        $users = $userDao->getUsersByReversedCountryName();
1390
1391
        $this->assertEquals('Jamaica', $users[0]->getCountry()->getLabel());
1392
    }
1393
1394
    /**
1395
     * @depends testDaoGeneration
1396
     */
1397
    public function testOrderByException(): void
1398
    {
1399
        $userDao = new TestUserDao($this->tdbmService);
1400
        $users = $userDao->getUsersByInvalidOrderBy();
1401
        $this->expectException(TDBMInvalidArgumentException::class);
1402
        $user = $users[0];
0 ignored issues
show
Unused Code introduced by
The assignment to $user is dead and can be removed.
Loading history...
1403
    }
1404
1405
    /**
1406
     * @depends testDaoGeneration
1407
     */
1408
    public function testOrderByProtectedColumn(): void
1409
    {
1410
        $animalDao = new AnimalDao($this->tdbmService);
1411
        $animals = $animalDao->findAll();
1412
        $animals = $animals->withOrder('`order` ASC');
1413
1414
        $this->assertInstanceOf(DogBean::class, $animals[0]);
1415
        $this->assertInstanceOf(CatBean::class, $animals[1]);
1416
1417
        $animals = $animals->withOrder('`order` DESC');
1418
1419
        $this->assertInstanceOf(CatBean::class, $animals[0]);
1420
        $this->assertInstanceOf(DogBean::class, $animals[1]);
1421
    }
1422
1423
    /**
1424
     * @depends testDaoGeneration
1425
     */
1426
    public function testGetOnAllNullableValues(): void
1427
    {
1428
        // Tests that a get performed on a column that has only nullable fields succeeds.
1429
        $allNullable = new AllNullableBean();
1430
        $this->assertNull($allNullable->getId());
1431
        $this->assertNull($allNullable->getLabel());
1432
        $this->assertNull($allNullable->getCountry());
1433
    }
1434
1435
    /**
1436
     * @depends testDaoGeneration
1437
     */
1438
    public function testExceptionOnMultipleInheritance(): void
1439
    {
1440
        $connection = self::getConnection();
1441
        self::insert($connection, 'animal', [
1442
            'id' => 99, 'name' => 'Snoofield',
1443
        ]);
1444
        self::insert($connection, 'dog', [
1445
            'id' => 99, 'race' => 'dog',
1446
        ]);
1447
        self::insert($connection, 'cat', [
1448
            'id' => 99, 'cuteness_level' => 0,
1449
        ]);
1450
1451
        $catched = false;
1452
        try {
1453
            $animalDao = new AnimalDao($this->tdbmService);
1454
            $animalDao->getById(99);
1455
        } catch (TDBMInheritanceException $e) {
1456
            $catched = true;
1457
        }
1458
        $this->assertTrue($catched, 'Exception TDBMInheritanceException was not caught');
1459
1460
        self::delete($connection, 'cat', ['id' => 99]);
1461
        self::delete($connection, 'dog', ['id' => 99]);
1462
        self::delete($connection, 'animal', ['id' => 99]);
1463
    }
1464
1465
    /**
1466
     * @depends testDaoGeneration
1467
     */
1468
    public function testReferenceNotSaved(): void
1469
    {
1470
        $boatDao = new BoatDao($this->tdbmService);
1471
1472
        $country = new CountryBean('Atlantis');
1473
        $boat = new BoatBean($country, 'Titanic');
1474
1475
        $boatDao->save($boat);
1476
        $this->assertNotNull($country->getId());
1477
    }
1478
1479
    /**
1480
     * @depends testReferenceNotSaved
1481
     */
1482
    public function testUniqueIndexOnForeignKeyThenScalar(): void
1483
    {
1484
        $boatDao = new BoatDao($this->tdbmService);
1485
        $countryDao = new CountryDao($this->tdbmService);
1486
1487
        $countryBean = $countryDao->findOneByLabel('Atlantis');
1488
        $boatBean = $boatDao->findOneByAnchorageCountryAndName($countryBean, 'Titanic');
1489
1490
        $this->assertNotNull($boatBean);
1491
    }
1492
1493
    /**
1494
     * @depends testDaoGeneration
1495
     */
1496
    public function testReferenceDeleted(): void
1497
    {
1498
        $countryDao = new CountryDao($this->tdbmService);
1499
        $boatDao = new BoatDao($this->tdbmService);
1500
1501
        $country = new CountryBean('Bikini Bottom');
1502
        $countryDao->save($country);
1503
1504
        $boat = new BoatBean($country, 'Squirrel boat');
1505
        $countryDao->delete($country);
1506
1507
        $this->expectException(TDBMMissingReferenceException::class);
1508
        $boatDao->save($boat);
1509
    }
1510
1511
    /**
1512
     * @depends testDaoGeneration
1513
     */
1514
    public function testCyclicReferenceWithInheritance(): void
1515
    {
1516
        $userDao = new UserDao($this->tdbmService);
1517
1518
        $country = new CountryBean('Norrisland');
1519
        $user = new UserBean('Chuck Norris', '[email protected]', $country, 'chuck.norris');
1520
1521
        $user->setManager($user);
1522
1523
        $this->expectException(TDBMCyclicReferenceException::class);
1524
        $userDao->save($user);
1525
    }
1526
1527
    /**
1528
     * @depends testDaoGeneration
1529
     */
1530
    public function testCyclicReference(): void
1531
    {
1532
        $categoryDao = new CategoryDao($this->tdbmService);
1533
1534
        $category = new CategoryBean('Root');
1535
1536
        $category->setParent($category);
1537
1538
        $this->expectException(TDBMCyclicReferenceException::class);
1539
        $categoryDao->save($category);
1540
    }
1541
1542
    /**
1543
     * @depends testDaoGeneration
1544
     */
1545
    public function testCorrectTypeForPrimaryKeyAfterSave(): void
1546
    {
1547
        // PosqtgreSQL does not particularly like empty inserts (i.e.: "INSERT INTO all_nullable () VALUES ()" )
1548
        $this->onlyMySql();
1549
1550
        $allNullableDao = new AllNullableDao($this->tdbmService);
1551
        $allNullable = new AllNullableBean();
1552
        $allNullableDao->save($allNullable);
1553
        $id = $allNullable->getId();
1554
1555
        $this->assertTrue(is_int($id));
1556
    }
1557
1558
    /**
1559
     * @depends testDaoGeneration
1560
     */
1561
    public function testPSR2Compliance(): void
1562
    {
1563
        $process = new Process(['vendor/bin/php-cs-fixer', 'fix', 'src/Test/', '--dry-run', '--diff', '--rules=@PSR2']);
1564
        $process->run();
1565
1566
        // executes after the command finishes
1567
        if (!$process->isSuccessful()) {
1568
            echo $process->getOutput();
1569
            $this->fail('Generated code is not PSR-2 compliant');
1570
        }
1571
        $this->assertTrue($process->isSuccessful());
1572
    }
1573
1574
    /**
1575
     * @depends testDaoGeneration
1576
     */
1577
    public function testFindOneByGeneration(): void
1578
    {
1579
        $reflectionMethod = new \ReflectionMethod(UserBaseDao::class, 'findOneByLogin');
1580
        $parameters = $reflectionMethod->getParameters();
1581
1582
        $this->assertCount(2, $parameters);
1583
        $this->assertSame('login', $parameters[0]->getName());
1584
        $this->assertSame('additionalTablesFetch', $parameters[1]->getName());
1585
    }
1586
1587
    /**
1588
     * @depends testDaoGeneration
1589
     */
1590
    public function testUuid(): void
1591
    {
1592
        $article = new ArticleBean('content');
1593
        $this->assertSame('content', $article->getContent());
1594
        $this->assertNotEmpty($article->getId());
1595
        $uuid = Uuid::fromString($article->getId());
1596
        $this->assertSame(1, $uuid->getVersion());
1597
    }
1598
1599
    /**
1600
     * @depends testDaoGeneration
1601
     */
1602
    public function testUuidv4(): void
1603
    {
1604
        $article = new Article2Bean('content');
1605
        $this->assertSame('content', $article->getContent());
1606
        $this->assertNotEmpty($article->getId());
1607
        $uuid = Uuid::fromString($article->getId());
1608
        $this->assertSame(4, $uuid->getVersion());
1609
    }
1610
1611
    /**
1612
     * @depends testDaoGeneration
1613
     */
1614
    public function testTypeHintedConstructors(): void
1615
    {
1616
        $userBaseBeanReflectionConstructor = new \ReflectionMethod(UserBaseBean::class, '__construct');
1617
        /** @var ReflectionNamedType $nameParam */
1618
        $nameParam = $userBaseBeanReflectionConstructor->getParameters()[0];
1619
1620
        $this->assertSame('string', $nameParam->getType()->getName());
0 ignored issues
show
Bug introduced by
The method getType() does not exist on ReflectionNamedType. ( Ignorable by Annotation )

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

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

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

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

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

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

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

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

}

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

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

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

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

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

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

}

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

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

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

Loading history...
2210
        $this->assertEquals(6, $objects->count());
2211
    }
2212
2213
    public function testMysqlStatementCount(): void
2214
    {
2215
        if (! $this->tdbmService->getConnection() instanceof MysqliConnection) {
0 ignored issues
show
introduced by
$this->tdbmService->getConnection() is never a sub-type of Doctrine\DBAL\Driver\Mysqli\MysqliConnection.
Loading history...
2216
            $this->markTestSkipped('This test only applies for MySQLi driver.');
2217
        }
2218
        try {
2219
            $this->tdbmService->getConnection()->exec('set global max_prepared_stmt_count = 2;');
2220
2221
            $objectBaseDao = new BaseObjectDao($this->tdbmService);
2222
            $objectInheritedDao = new InheritedObjectDao($this->tdbmService);
2223
2224
            $objectBase = new BaseObjectBean('label-1');
2225
            $objectBaseDao->save($objectBase);
2226
            $objectInherited = new InheritedObjectBean($objectBase);
2227
            $objectInheritedDao->save($objectInherited);
2228
2229
            $objectBase = new BaseObjectBean('label-2');
2230
            $objectBaseDao->save($objectBase);
2231
            $objectInherited = new InheritedObjectBean($objectBase);
2232
            $objectInheritedDao->save($objectInherited);
2233
2234
            $objectBase = new BaseObjectBean('label-3');
2235
            $objectBaseDao->save($objectBase);
2236
            $objectInherited = new InheritedObjectBean($objectBase);
2237
            $objectInheritedDao->save($objectInherited);
2238
2239
            $objects = $objectBaseDao->findAll();
2240
            $this->assertGreaterThanOrEqual(3, $objects->count());
2241
            foreach ($objects->take(0, 1) as $objectBean) { $objectBean->getInheritedObject()->getId(); }
2242
            foreach ($objects->take(1, 2) as $objectBean) { $objectBean->getInheritedObject()->getId(); }
2243
            foreach ($objects->take(2, 3) as $objectBean) { $objectBean->getInheritedObject()->getId(); }
2244
        } finally {
2245
            $this->tdbmService->getConnection()->exec('set global max_prepared_stmt_count = 16382;');
2246
        }
2247
    }
2248
}
2249