Passed
Pull Request — master (#268)
by
unknown
05:53 queued 02:34
created

testDiscardChangesDiscardsRelations()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 10
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 17
rs 9.9332
1
<?php
2
declare(strict_types=1);
3
4
/*
5
 Copyright (C) 2006-2018 David Négrier - THE CODING MACHINE
6
7
This program is free software; you can redistribute it and/or modify
8
it under the terms of the GNU General Public License as published by
9
the Free Software Foundation; either version 2 of the License, or
10
(at your option) any later version.
11
12
This program is distributed in the hope that it will be useful,
13
but WITHOUT ANY WARRANTY; without even the implied warranty of
14
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
GNU General Public License for more details.
16
17
You should have received a copy of the GNU General Public License
18
along with this program; if not, write to the Free Software
19
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20
*/
21
22
namespace TheCodingMachine\TDBM;
23
24
use Doctrine\Common\Cache\ArrayCache;
25
use Doctrine\DBAL\Exception\ForeignKeyConstraintViolationException;
26
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
27
use Doctrine\DBAL\Platforms\MySQL57Platform;
28
use Doctrine\DBAL\Platforms\MySqlPlatform;
29
use Doctrine\DBAL\Platforms\OraclePlatform;
30
use Mouf\Database\SchemaAnalyzer\SchemaAnalyzer;
31
use Ramsey\Uuid\Uuid;
32
use ReflectionClass;
33
use ReflectionMethod;
34
use ReflectionNamedType;
35
use TheCodingMachine\TDBM\Dao\TestAlbumDao;
36
use TheCodingMachine\TDBM\Dao\TestArticleDao;
37
use TheCodingMachine\TDBM\Dao\TestArticleSubQueryDao;
38
use TheCodingMachine\TDBM\Dao\TestCountryDao;
39
use TheCodingMachine\TDBM\Dao\TestPersonDao;
40
use TheCodingMachine\TDBM\Dao\TestRoleDao;
41
use TheCodingMachine\TDBM\Dao\TestUserDao;
42
use TheCodingMachine\TDBM\Fixtures\Interfaces\TestUserDaoInterface;
43
use TheCodingMachine\TDBM\Fixtures\Interfaces\TestUserInterface;
44
use TheCodingMachine\TDBM\Test\Dao\AccessibleDao;
0 ignored issues
show
Bug introduced by
The type TheCodingMachine\TDBM\Test\Dao\AccessibleDao 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\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...
46
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...
47
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...
48
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...
49
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...
50
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...
51
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...
52
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...
53
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...
54
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...
55
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...
56
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...
57
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...
58
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...
59
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...
60
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...
61
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...
62
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...
63
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...
64
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...
65
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...
66
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...
67
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...
68
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...
69
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...
70
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...
71
use TheCodingMachine\TDBM\Test\Dao\Bean\PlayerBean;
0 ignored issues
show
Bug introduced by
The type TheCodingMachine\TDBM\Test\Dao\Bean\PlayerBean 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\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...
73
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...
74
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...
75
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...
76
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...
77
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...
78
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...
79
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...
80
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...
81
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...
82
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...
83
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...
84
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...
85
use TheCodingMachine\TDBM\Test\Dao\InheritedObjectDao;
0 ignored issues
show
Bug introduced by
The type TheCodingMachine\TDBM\Test\Dao\InheritedObjectDao was not found. Maybe you did not declare it correctly or list all dependencies?

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

filter:
    dependency_paths: ["lib/*"]

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

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

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

filter:
    dependency_paths: ["lib/*"]

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

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

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

filter:
    dependency_paths: ["lib/*"]

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

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

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

filter:
    dependency_paths: ["lib/*"]

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

Loading history...
89
use TheCodingMachine\TDBM\Test\Dao\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...
90
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...
91
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...
92
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...
93
use TheCodingMachine\TDBM\Test\Dao\ValueDao;
0 ignored issues
show
Bug introduced by
The type TheCodingMachine\TDBM\Test\Dao\ValueDao 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...
94
use TheCodingMachine\TDBM\Utils\PathFinder\NoPathFoundException;
95
use TheCodingMachine\TDBM\Utils\PathFinder\PathFinder;
96
use TheCodingMachine\TDBM\Utils\TDBMDaoGenerator;
97
use Symfony\Component\Process\Process;
98
use function get_class;
99
100
class TDBMDaoGeneratorTest extends TDBMAbstractServiceTest
101
{
102
    /** @var TDBMDaoGenerator $tdbmDaoGenerator */
103
    protected $tdbmDaoGenerator;
104
105
    private $rootPath;
106
107
    protected function setUp(): void
108
    {
109
        parent::setUp();
110
        $schemaManager = $this->tdbmService->getConnection()->getSchemaManager();
111
        $schemaAnalyzer = new SchemaAnalyzer($schemaManager);
112
        $schemaLockFileDumper = new SchemaLockFileDumper($this->tdbmService->getConnection(), new ArrayCache(), Configuration::getDefaultLockFilePath());
0 ignored issues
show
Deprecated Code introduced by
The class Doctrine\Common\Cache\ArrayCache has been deprecated: Deprecated without replacement in doctrine/cache 1.11. This class will be dropped in 2.0 ( Ignorable by Annotation )

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

112
        $schemaLockFileDumper = new SchemaLockFileDumper($this->tdbmService->getConnection(), /** @scrutinizer ignore-deprecated */ new ArrayCache(), Configuration::getDefaultLockFilePath());
Loading history...
113
        $tdbmSchemaAnalyzer = new TDBMSchemaAnalyzer($this->tdbmService->getConnection(), new ArrayCache(), $schemaAnalyzer, $schemaLockFileDumper);
0 ignored issues
show
Deprecated Code introduced by
The class Doctrine\Common\Cache\ArrayCache has been deprecated: Deprecated without replacement in doctrine/cache 1.11. This class will be dropped in 2.0 ( Ignorable by Annotation )

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

113
        $tdbmSchemaAnalyzer = new TDBMSchemaAnalyzer($this->tdbmService->getConnection(), /** @scrutinizer ignore-deprecated */ new ArrayCache(), $schemaAnalyzer, $schemaLockFileDumper);
Loading history...
114
        $this->tdbmDaoGenerator = new TDBMDaoGenerator($this->getConfiguration(), $tdbmSchemaAnalyzer);
115
        $this->rootPath = __DIR__ . '/../';
116
        //$this->tdbmDaoGenerator->setComposerFile($this->rootPath.'composer.json');
117
    }
118
119
    public function testGetSchemaCrashWithoutLock()
120
    {
121
        //let's delete the lock file
122
        $schemaFilePath = Configuration::getDefaultLockFilePath();
123
        if (file_exists($schemaFilePath)) {
124
            unlink($schemaFilePath);
125
        }
126
        //let's check we cannot call get schema without a lock file
127
        $schemaAnalyzer = new SchemaAnalyzer(self::getConnection()->getSchemaManager(), new ArrayCache(), 'prefix_');
0 ignored issues
show
Deprecated Code introduced by
The class Doctrine\Common\Cache\ArrayCache has been deprecated: Deprecated without replacement in doctrine/cache 1.11. This class will be dropped in 2.0 ( Ignorable by Annotation )

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

127
        $schemaAnalyzer = new SchemaAnalyzer(self::getConnection()->getSchemaManager(), /** @scrutinizer ignore-deprecated */ new ArrayCache(), 'prefix_');
Loading history...
128
        $schemaLockFileDumper = new SchemaLockFileDumper(self::getConnection(), new ArrayCache(), Configuration::getDefaultLockFilePath());
0 ignored issues
show
Deprecated Code introduced by
The class Doctrine\Common\Cache\ArrayCache has been deprecated: Deprecated without replacement in doctrine/cache 1.11. This class will be dropped in 2.0 ( Ignorable by Annotation )

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

128
        $schemaLockFileDumper = new SchemaLockFileDumper(self::getConnection(), /** @scrutinizer ignore-deprecated */ new ArrayCache(), Configuration::getDefaultLockFilePath());
Loading history...
129
        $tdbmSchemaAnalyzer = new TDBMSchemaAnalyzer(self::getConnection(), new ArrayCache(), $schemaAnalyzer, $schemaLockFileDumper);
0 ignored issues
show
Deprecated Code introduced by
The class Doctrine\Common\Cache\ArrayCache has been deprecated: Deprecated without replacement in doctrine/cache 1.11. This class will be dropped in 2.0 ( Ignorable by Annotation )

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

129
        $tdbmSchemaAnalyzer = new TDBMSchemaAnalyzer(self::getConnection(), /** @scrutinizer ignore-deprecated */ new ArrayCache(), $schemaAnalyzer, $schemaLockFileDumper);
Loading history...
130
        $this->expectException('TheCodingMachine\TDBM\TDBMException');
131
        $schema1 = $tdbmSchemaAnalyzer->getSchema(true);
0 ignored issues
show
Unused Code introduced by
The assignment to $schema1 is dead and can be removed.
Loading history...
Deprecated Code introduced by
The function TheCodingMachine\TDBM\TD...maAnalyzer::getSchema() has been deprecated. ( Ignorable by Annotation )

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

131
        $schema1 = /** @scrutinizer ignore-deprecated */ $tdbmSchemaAnalyzer->getSchema(true);
Loading history...
132
    }
133
134
    public function testDaoGeneration(): void
135
    {
136
        // Remove all previously generated files.
137
        $this->recursiveDelete($this->rootPath . 'src/Test/Dao/');
138
        mkdir($this->rootPath . 'src/Test/Dao/Generated', 0755, true);
139
        // Let's generate a dummy file to see it is indeed removed.
140
        $dummyFile = $this->rootPath . 'src/Test/Dao/Generated/foobar.php';
141
        touch($dummyFile);
142
        $this->assertFileExists($dummyFile);
143
144
        //let's delete the lock file
145
        $schemaFilePath = Configuration::getDefaultLockFilePath();
146
        if (file_exists($schemaFilePath)) {
147
            unlink($schemaFilePath);
148
        }
149
150
        $this->tdbmDaoGenerator->generateAllDaosAndBeans();
151
152
        $this->assertFileNotExists($dummyFile);
153
154
        //Check that the lock file was generated
155
        $this->assertFileExists($schemaFilePath);
156
157
        // Let's require all files to check they are valid PHP!
158
        // Test the daoFactory
159
        require_once $this->rootPath . 'src/Test/Dao/Generated/DaoFactory.php';
160
        // Test the others
161
162
        $beanDescriptors = $this->getDummyGeneratorListener()->getBeanDescriptors();
163
164
        foreach ($beanDescriptors as $beanDescriptor) {
165
            $daoName = $beanDescriptor->getDaoClassName();
166
            $daoBaseName = $beanDescriptor->getBaseDaoClassName();
167
            $beanName = $beanDescriptor->getBeanClassName();
168
            $baseBeanName = $beanDescriptor->getBaseBeanClassName();
169
            require_once $this->rootPath . 'src/Test/Dao/Bean/Generated/' . $baseBeanName . '.php';
170
            require_once $this->rootPath . 'src/Test/Dao/Bean/' . $beanName . '.php';
171
            require_once $this->rootPath . 'src/Test/Dao/Generated/' . $daoBaseName . '.php';
172
            require_once $this->rootPath . 'src/Test/Dao/' . $daoName . '.php';
173
        }
174
175
        // Check that pivot tables do not generate DAOs or beans.
176
        $this->assertFalse(class_exists('TheCodingMachine\\TDBM\\Test\\Dao\\RolesRightDao'));
177
    }
178
179
    public function testGenerationException(): void
180
    {
181
        $configuration = new Configuration('UnknownVendor\\Dao', 'UnknownVendor\\Bean', self::getConnection(), $this->getNamingStrategy());
182
183
        $schemaManager = $this->tdbmService->getConnection()->getSchemaManager();
184
        $schemaAnalyzer = new SchemaAnalyzer($schemaManager);
185
        $schemaLockFileDumper = new SchemaLockFileDumper($this->tdbmService->getConnection(), new ArrayCache(), Configuration::getDefaultLockFilePath());
0 ignored issues
show
Deprecated Code introduced by
The class Doctrine\Common\Cache\ArrayCache has been deprecated: Deprecated without replacement in doctrine/cache 1.11. This class will be dropped in 2.0 ( Ignorable by Annotation )

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

185
        $schemaLockFileDumper = new SchemaLockFileDumper($this->tdbmService->getConnection(), /** @scrutinizer ignore-deprecated */ new ArrayCache(), Configuration::getDefaultLockFilePath());
Loading history...
186
        $tdbmSchemaAnalyzer = new TDBMSchemaAnalyzer($this->tdbmService->getConnection(), new ArrayCache(), $schemaAnalyzer, $schemaLockFileDumper);
0 ignored issues
show
Deprecated Code introduced by
The class Doctrine\Common\Cache\ArrayCache has been deprecated: Deprecated without replacement in doctrine/cache 1.11. This class will be dropped in 2.0 ( Ignorable by Annotation )

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

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

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

1652
        $this->assertSame(1, /** @scrutinizer ignore-deprecated */ $uuid->getVersion());

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

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

Loading history...
1653
    }
1654
1655
    /**
1656
     * @depends testDaoGeneration
1657
     */
1658
    public function testUuidv4(): void
1659
    {
1660
        $article = new Article2Bean('content');
1661
        $this->assertSame('content', $article->getContent());
1662
        $this->assertNotEmpty($article->getId());
1663
        $uuid = Uuid::fromString($article->getId());
1664
        $this->assertSame(4, $uuid->getVersion());
0 ignored issues
show
Deprecated Code introduced by
The function Ramsey\Uuid\DeprecatedUuidInterface::getVersion() has been deprecated: Use {@see UuidInterface::getFields()} to get a {@see FieldsInterface} instance. If it is a {@see \Ramsey\Uuid\Rfc4122\FieldsInterface} instance, you may call {@see \Ramsey\Uuid\Rfc4122\FieldsInterface::getVersion()}. ( Ignorable by Annotation )

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

1664
        $this->assertSame(4, /** @scrutinizer ignore-deprecated */ $uuid->getVersion());

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

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

Loading history...
1665
    }
1666
1667
    /**
1668
     * @depends testDaoGeneration
1669
     */
1670
    public function testTypeHintedConstructors(): void
1671
    {
1672
        $userBaseBeanReflectionConstructor = new \ReflectionMethod(UserBaseBean::class, '__construct');
1673
        /** @var ReflectionNamedType $nameParam */
1674
        $nameParam = $userBaseBeanReflectionConstructor->getParameters()[0];
1675
1676
        $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

1676
        $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...
1677
    }
1678
1679
    /**
1680
     * @depends testDaoGeneration
1681
     */
1682
    public function testSaveTransaction(): void
1683
    {
1684
        $countryDao = new CountryDao($this->tdbmService);
1685
1686
        $boatDao = new BoatDao($this->tdbmService);
1687
        $boatBean = $boatDao->getById(1);
1688
        $boatBean->setName('Bismark');
1689
1690
        $boatBean->getCountry();
1691
1692
        // Let's insert a row without telling TDBM to trigger an error!
1693
        self::insert($this->getConnection(), 'sailed_countries', [
1694
            'boat_id' => 1,
1695
            'country_id' => 2
1696
        ]);
1697
1698
        $boatBean->addCountry($countryDao->getById(2));
1699
1700
        $this->expectException(UniqueConstraintViolationException::class);
1701
1702
        $boatDao->save($boatBean);
1703
    }
1704
1705
    /**
1706
     * @depends testSaveTransaction
1707
     */
1708
    public function testSaveTransaction2(): void
1709
    {
1710
        $boatDao = new BoatDao($this->tdbmService);
1711
        $boatBean = $boatDao->getById(1);
1712
1713
        // The name should not have been saved because the transaction of the previous test should have rollbacked.
1714
        $this->assertNotSame('Bismark', $boatBean->getName());
1715
    }
1716
1717
    /**
1718
     * @depends testDaoGeneration
1719
     */
1720
    public function testForeignKeyPointingToNonPrimaryKey(): void
1721
    {
1722
        $dao = new RefNoPrimKeyDao($this->tdbmService);
1723
        $bean = $dao->getById(1);
1724
1725
        $this->assertSame('foo', $bean->getFrom()->getTo());
1726
1727
        $newBean = new RefNoPrimKeyBean($bean, 'baz');
1728
        $dao->save($newBean);
1729
        $this->assertSame('foo', $newBean->getFrom()->getTo());
1730
1731
        $resultSet = $bean->getRefNoPrimKey();
1732
        $this->assertCount(2, $resultSet);
1733
    }
1734
1735
    /**
1736
     * @depends testDaoGeneration
1737
     */
1738
    public function testCloningUuidBean(): void
1739
    {
1740
        $article = new ArticleBean('content');
1741
        $this->assertNotEmpty($article->getId());
1742
        $article2 = clone $article;
1743
        $this->assertNotEmpty($article2->getId());
1744
        $this->assertNotSame($article->getId(), $article2->getId());
1745
    }
1746
1747
    /**
1748
     * @depends testDaoGeneration
1749
     */
1750
    public function testRecursiveSave(): void
1751
    {
1752
        $categoryDao = new CategoryDao($this->tdbmService);
1753
1754
        $root1 = new CategoryBean('Root1');
1755
        $categoryDao->save($root1);
1756
        // Root 2 is not saved yet.
1757
        $root2 = new CategoryBean('Root2');
1758
        $intermediate = new CategoryBean('Intermediate');
1759
        $categoryDao->save($intermediate);
1760
1761
        // Let's switch the parent to a bean in detached state.
1762
        $intermediate->setParent($root2);
1763
1764
        // Now, let's save a new category that references the leaf category.
1765
        $leaf = new CategoryBean('Leaf');
1766
        $leaf->setParent($intermediate);
1767
        $categoryDao->save($leaf);
1768
        $this->assertNull($root2->getId());
1769
    }
1770
1771
    /**
1772
     * @depends testDaoGeneration
1773
     */
1774
    public function testBlob(): void
1775
    {
1776
        // An issue in DBAL makes using BLOB type impossible with resources.
1777
        // See https://github.com/doctrine/dbal/issues/3290
1778
        $this->skipOracle();
1779
1780
        $fp = fopen(__FILE__, 'r');
1781
        $file = new FileBean($fp);
1782
1783
        $fileDao = new FileDao($this->tdbmService);
1784
1785
        $fileDao->save($file);
1786
1787
        $loadedFile = $fileDao->getById($file->getId());
1788
1789
        $resource = $loadedFile->getFile();
1790
        $result = fseek($resource, 0);
1791
        $this->assertSame(0, $result);
1792
        $this->assertIsResource($resource);
1793
        $firstLine = fgets($resource);
1794
        $this->assertSame("<?php\n", $firstLine);
1795
    }
1796
1797
    /**
1798
     * @depends testBlob
1799
     */
1800
    public function testReadBlob(): void
1801
    {
1802
        // An issue in DBAL makes using BLOB type impossible with resources.
1803
        // See https://github.com/doctrine/dbal/issues/3290
1804
        $this->skipOracle();
1805
1806
        $fileDao = new FileDao($this->tdbmService);
1807
        $loadedFile = $fileDao->getById(1);
1808
1809
        $resource = $loadedFile->getFile();
1810
        $this->assertIsResource($resource);
1811
        $firstLine = fgets($resource);
1812
        $this->assertSame("<?php\n", $firstLine);
1813
1814
        stream_get_contents($resource);
1815
1816
        $loadedFile->setId($loadedFile->getId());
1817
1818
        $fileDao->save($loadedFile);
1819
    }
1820
1821
    /**
1822
     * @depends testReadBlob
1823
     */
1824
    public function testReadAndSaveBlob(): void
1825
    {
1826
        // An issue in DBAL makes using BLOB type impossible with resources.
1827
        // See https://github.com/doctrine/dbal/issues/3290
1828
        $this->skipOracle();
1829
1830
        $fileDao = new FileDao($this->tdbmService);
1831
        $loadedFile = $fileDao->getById(1);
1832
1833
        $resource = $loadedFile->getFile();
1834
1835
        $firstLine = fgets($resource);
1836
        $this->assertSame("<?php\n", $firstLine);
1837
    }
1838
1839
    /**
1840
     * @depends testReadBlob
1841
     */
1842
    public function testProtectedGetterSetter(): void
1843
    {
1844
        // An issue in DBAL makes using BLOB type impossible with resources.
1845
        // See https://github.com/doctrine/dbal/issues/3290
1846
        $this->skipOracle();
1847
1848
        $md5Getter = new ReflectionMethod(FileBaseBean::class, 'getMd5');
1849
        $md5Setter = new ReflectionMethod(FileBaseBean::class, 'setMd5');
1850
1851
        $this->assertTrue($md5Getter->isProtected());
1852
        $this->assertTrue($md5Setter->isProtected());
1853
1854
        $md5Getter2 = new ReflectionMethod(FileBaseBean::class, 'getArticle');
1855
        $md5Setter2 = new ReflectionMethod(FileBaseBean::class, 'setArticle');
1856
1857
        $this->assertTrue($md5Getter2->isProtected());
1858
        $this->assertTrue($md5Setter2->isProtected());
1859
1860
        $oneToManyGetter = new ReflectionMethod(ArticleBaseBean::class, 'getFiles');
1861
        $this->assertTrue($oneToManyGetter->isProtected());
1862
1863
        $fileDao = new FileDao($this->tdbmService);
1864
        $loadedFile = $fileDao->getById(1);
1865
1866
        // The md5 and article columns are not JSON serialized
1867
        $this->assertSame([
1868
            'id' => 1,
1869
        ], $loadedFile->jsonSerialize());
1870
    }
1871
1872
    /**
1873
     * @depends testDaoGeneration
1874
     */
1875
    public function testBlobResourceException(): void
1876
    {
1877
        $this->expectException(TDBMInvalidArgumentException::class);
1878
        $this->expectExceptionMessage('Invalid argument passed to \'TheCodingMachine\\TDBM\\Test\\Dao\\Bean\\Generated\\FileBaseBean::setFile\'. Expecting a resource. Got a string.');
1879
        new FileBean('foobar');
1880
    }
1881
1882
    /**
1883
     * @depends testDaoGeneration
1884
     */
1885
    public function testFilterBag(): void
1886
    {
1887
        $userDao = new TestUserDao($this->tdbmService);
1888
        $countryDao = new CountryDao($this->tdbmService);
1889
1890
        $country = $countryDao->getById(2);
1891
1892
        // Let's test filter bags by bean and filter bag with many values.
1893
        $users = $userDao->getUsersByComplexFilterBag($country, ['John Doe', 'Jane Doe']);
1894
1895
        $this->assertCount(1, $users);
1896
        $this->assertSame('John Doe', $users[0]->getName());
1897
    }
1898
1899
    /**
1900
     * @depends testDaoGeneration
1901
     */
1902
    public function testDecimalIsMappedToString(): void
1903
    {
1904
        $reflectionClass = new \ReflectionClass(BoatBaseBean::class);
1905
        $this->assertSame('string', $reflectionClass->getMethod('getLength')->getReturnType()->getName());
0 ignored issues
show
Bug introduced by
The method getName() does not exist on ReflectionType. It seems like you code against a sub-type of ReflectionType such as ReflectionNamedType. ( Ignorable by Annotation )

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

1905
        $this->assertSame('string', $reflectionClass->getMethod('getLength')->getReturnType()->/** @scrutinizer ignore-call */ getName());
Loading history...
1906
    }
1907
1908
    /**
1909
     * @depends testDaoGeneration
1910
     */
1911
    public function testInsertMultiPrimaryKeysBean(): void
1912
    {
1913
        $countryDao = new CountryDao($this->tdbmService);
1914
1915
        $country = $countryDao->getById(1);
1916
1917
        $stateDao = new StateDao($this->tdbmService);
1918
        $state = new StateBean($country, 'IDF', 'Ile de France');
1919
        $stateDao->save($state);
1920
1921
        $this->assertSame($state, $stateDao->findAll()[0]);
1922
    }
1923
1924
    /**
1925
     * @depends testInsertMultiPrimaryKeysBean
1926
     */
1927
    public function testDeleteMultiPrimaryKeysBean(): void
1928
    {
1929
        $stateDao = new StateDao($this->tdbmService);
1930
1931
        $state = $stateDao->findAll()[0];
1932
        $stateDao->delete($state);
1933
        $this->assertCount(0, $stateDao->findAll());
1934
    }
1935
1936
    /**
1937
     * @depends testDaoGeneration
1938
     */
1939
    public function testCompositePrimaryKeyGetter(): void
1940
    {
1941
        $stateDao = new StateDao($this->tdbmService);
1942
        $country = new CountryBean('USA');
1943
        $stateBean = new StateBean($country, 'CA', 'California');
1944
        $stateDao->save($stateBean);
1945
        $this->assertSame($stateBean, $stateDao->getById($country->getId(), 'CA'));
1946
    }
1947
1948
    /**
1949
     * @depends testDaoGeneration
1950
     */
1951
    public function testSortOnInheritedTable(): void
1952
    {
1953
        $animalDao = new AnimalDao($this->tdbmService);
1954
1955
        // Let's insert an animal that is nothing.
1956
        $animal = new AnimalBean('Mickey');
1957
        $animalDao->save($animal);
1958
1959
        $animals = $animalDao->findAll()->withOrder('dog.race ASC');
1960
1961
        $animalsArr = $animals->toArray();
1962
        $this->assertCount(3, $animalsArr);
1963
    }
1964
1965
    /**
1966
     * @depends testDaoGeneration
1967
     */
1968
    public function testJsonKey(): void
1969
    {
1970
        $node = new NodeBean('foo.html');
1971
        $json = $node->jsonSerialize();
1972
        self::assertTrue(isset($json['basename']));
1973
        self::assertEquals('foo.html', $json['basename']);
1974
    }
1975
1976
    /**
1977
     * @depends testDaoGeneration
1978
     */
1979
    public function testJsonIgnore(): void
1980
    {
1981
        $nodeDao = new NodeDao($this->tdbmService);
1982
        $index = $nodeDao->getById(6);
1983
        $json = $index->jsonSerialize();
1984
        // Ignored scalar 'id'
1985
        self::assertTrue(!isset($json['id']));
1986
        // Ignored object 'root'
1987
        self::assertTrue(!isset($json['root']));
1988
        self::assertTrue(isset($json['guests']));
1989
        self::assertTrue(!empty($json['guests']));
1990
        $account = $index->getAccounts()[0];
1991
        $json = $account->jsonSerialize();
1992
        // Ignored array 'nodes' (from nodes_users table)
1993
        self::assertTrue(!isset($json['nodes']));
1994
    }
1995
1996
    /**
1997
     * @depends testDaoGeneration
1998
     */
1999
    public function testJsonInclude(): void
2000
    {
2001
        $nodeDao = new NodeDao($this->tdbmService);
2002
        $index = $nodeDao->getById(6);
2003
        $json = $index->jsonSerialize();
2004
        // Whole chain of parents should be serialized
2005
        self::assertTrue(isset($json['parent']));
2006
        self::assertTrue(isset($json['parent']['parent']));
2007
        self::assertTrue(isset($json['parent']['parent']['parent']));
2008
        self::assertEquals('/', $json['parent']['parent']['parent']['basename']);
2009
    }
2010
2011
    /**
2012
     * @depends testDaoGeneration
2013
     */
2014
    public function testJsonRecursive(): void
2015
    {
2016
        $nodeDao = new NodeDao($this->tdbmService);
2017
        $index = $nodeDao->getById(8);
2018
        $json = $index->jsonSerialize();
2019
        // Original chain of aliases is recursively serialized, ...
2020
        self::assertTrue(isset($json['alias']));
2021
        self::assertTrue(isset($json['alias']['alias']));
2022
        self::assertEquals('index.html', $json['alias']['alias']['basename']);
2023
        // ... each alias even serializes its parents, ...
2024
        self::assertTrue(isset($json['alias']['alias']['parent']['parent']));
2025
        // ... however, parents aliases chains have just their foreign key (id), as parents are serialized with $stopRecursion=true
2026
        self::assertEquals(3, $json['alias']['alias']['parent']['parent']['alias']['id']);
2027
        self::assertCount(1, $json['alias']['alias']['parent']['parent']['alias']);
2028
    }
2029
2030
    /**
2031
     * @depends testDaoGeneration
2032
     */
2033
    public function testJsonFormat(): void
2034
    {
2035
        $nodeDao = new NodeDao($this->tdbmService);
2036
        $index = $nodeDao->getById(6);
2037
        $json = $index->jsonSerialize();
2038
        self::assertTrue(isset($json['size']));
2039
        self::assertEquals('512 o', $json['size']);
2040
        self::assertEquals('42.50g', $json['weight']);
2041
        self::assertEquals($index->getCreatedAt()->format('Y-m-d'), $json['createdAt']);
2042
        self::assertEquals($index->getOwner()->getName(), $json['owner']);
2043
        self::assertEquals($index->getAccounts()[1]->getName(), $json['guests'][1]);
2044
        self::assertTrue(isset($json['entries']));
2045
        self::assertEquals('Hello, World', $json['entries'][1]);
2046
        $www = $index->getParent();
2047
        $json = $www->jsonSerialize();
2048
        self::assertEquals('0 o', $json['size']);
2049
        self::assertNull($json['weight']);
2050
        self::assertNull($json['owner']);
2051
    }
2052
2053
    /**
2054
     * @depends testDaoGeneration
2055
     */
2056
    public function testJsonCollection(): void
2057
    {
2058
        // This test tries to perform a SELECT DISTINCT on a JSON column (which is represented as a CLOB column in Oracle)
2059
        // DISTINCT statements cannot be applied on CLOB columns. As a result, JSON columns are not supported in Oracle + TDBM 5 for now.
2060
        $this->skipOracle();
2061
2062
        $artists = new ArtistDao($this->tdbmService);
2063
        $pinkFloyd = $artists->getById(1);
2064
        $animals =  $pinkFloyd->getAlbums()[0];
2065
        $json = $pinkFloyd->jsonSerialize();
2066
        // Collection name properly handled ('discography' instead of default 'albums')
2067
        self::assertTrue(isset($json['discography']));
2068
        self::assertEquals($animals->getTitle(), $json['discography'][0]['title']);
2069
        // Make sure top object have just its primary key
2070
        self::assertEquals(1, $json['discography'][0]['artist']['id']);
2071
        self::assertCount(1, $json['discography'][0]['artist']);
2072
        $json = $animals->jsonSerialize();
2073
        // Nevertheless, artist should be serialized in album as top object...
2074
        self::assertTrue(isset($json['artist']));
2075
        // ... as should be tracks...
2076
        self::assertTrue(isset($json['tracks'][0]));
2077
        self::assertEquals('Pigs on the Wing 1', $json['tracks'][0]['title']);
2078
        // ... and, ultimately, list of featuring artists, since feat is included
2079
        self::assertTrue(isset($json['tracks'][0]['feat'][0]));
2080
        self::assertEquals('Roger Waters', $json['tracks'][0]['feat'][0]['name']);
2081
    }
2082
2083
    public function testFloydHasNoParent(): void
2084
    {
2085
        // This test tries to perform a SELECT DISTINCT on a JSON column (which is represented as a CLOB column in Oracle)
2086
        // DISTINCT statements cannot be applied on CLOB columns. As a result, JSON columns are not supported in Oracle + TDBM 5 for now.
2087
        $this->skipOracle();
2088
2089
        $artists = new ArtistDao($this->tdbmService);
2090
        $pinkFloyd = $artists->getById(1);
2091
        $parents = $pinkFloyd->getParents();
2092
2093
        $this->assertEquals([], $parents);
2094
    }
2095
2096
    public function testFloydHasOneChild(): void
2097
    {
2098
        // This test tries to perform a SELECT DISTINCT on a JSON column (which is represented as a CLOB column in Oracle)
2099
        // DISTINCT statements cannot be applied on CLOB columns. As a result, JSON columns are not supported in Oracle + TDBM 5 for now.
2100
        $this->skipOracle();
2101
2102
        $artists = new ArtistDao($this->tdbmService);
2103
        $pinkFloyd = $artists->getById(1);
2104
        $children = $pinkFloyd->getChildrenByArtistsRelations();
2105
2106
        $this->assertEquals(1, count($children));
2107
        $this->assertEquals(2, $children[0]->getId());
2108
    }
2109
2110
    /**
2111
     * @depends testDaoGeneration
2112
     */
2113
    public function testAddInterfaceAnnotation(): void
2114
    {
2115
        $refClass = new ReflectionClass(UserBaseBean::class);
2116
        $this->assertTrue($refClass->implementsInterface(TestUserInterface::class));
2117
    }
2118
2119
    /**
2120
     * @depends testDaoGeneration
2121
     */
2122
    public function testAddInterfaceOnDaoAnnotation(): void
2123
    {
2124
        $refClass = new ReflectionClass(UserBaseDao::class);
2125
        $this->assertTrue($refClass->implementsInterface(TestUserDaoInterface::class));
2126
    }
2127
2128
    /**
2129
     * @depends testDaoGeneration
2130
     */
2131
    public function testTrait(): void
2132
    {
2133
        $userDao = new UserDao($this->tdbmService);
2134
        $userBean = $userDao->getById(1);
2135
2136
        $this->assertSame('TestOtherUserTrait', $userBean->method1());
2137
        $this->assertSame('TestUserTrait', $userBean->method1renamed());
2138
2139
        $refClass = new ReflectionClass(UserBaseDao::class);
2140
        $this->assertTrue($refClass->hasMethod('findNothing'));
2141
    }
2142
2143
    /**
2144
     * @depends testDaoGeneration
2145
     */
2146
    public function testNonInstantiableAbstractDaosAndBeans(): void
2147
    {
2148
        $refClass = new ReflectionClass(UserBaseDao::class);
2149
        $this->assertFalse($refClass->isInstantiable());
2150
2151
        $refClass = new ReflectionClass(UserBaseBean::class);
2152
        $this->assertFalse($refClass->isInstantiable());
2153
    }
2154
2155
    /**
2156
     * @depends testDaoGeneration
2157
     */
2158
    public function testCanNullifyBlob(): void
2159
    {
2160
        $article = new ArticleBean('content');
2161
        $fp = fopen(__FILE__, 'r');
2162
        $article->setAttachment($fp);
2163
        $article->setAttachment(null);
2164
        $this->assertNull($article->getAttachment(null));
2165
        fclose($fp);
2166
    }
2167
2168
    /**
2169
     * @depends testDaoGeneration
2170
     */
2171
    public function testOptionnalParametersCanBeNullInFindOneBy()
2172
    {
2173
        $albumDao = new AlbumDao($this->tdbmService);
2174
        $artist = new ArtistBean('Marcel');
2175
2176
        $albumDao->findOneByArtistAndNode($artist, null);
2177
        $this->assertEquals(1, 1);
2178
    }
2179
2180
    /**
2181
     * @depends testDaoGeneration
2182
     */
2183
    public function testRequiredParametersCannotBeNullInFindOneBy()
2184
    {
2185
        $albumDao = new AlbumDao($this->tdbmService);
2186
        $artist = new ArtistBean('Marcel');
2187
        $account = new AccountBean('Jamie');
2188
2189
        $albumDao->findOneByArtistAndAccount($artist, $account);
2190
2191
        $this->expectException('TypeError');
2192
        $albumDao->findOneByArtistAndAccount($artist, null);
2193
    }
2194
2195
    /**
2196
     * @depends testDaoGeneration
2197
     */
2198
    public function testLazyLoad(): void
2199
    {
2200
        $roleDao = new RoleDao($this->tdbmService);
2201
        $roleBean = $roleDao->getById(1, true);
2202
2203
        $this->assertSame(TDBMObjectStateEnum::STATE_NOT_LOADED, $roleBean->_getDbRows()['roles']->_getStatus());
2204
        $roleBean->getId();
2205
        $this->assertSame(TDBMObjectStateEnum::STATE_NOT_LOADED, $roleBean->_getDbRows()['roles']->_getStatus());
2206
    }
2207
2208
    /**
2209
     * @depends testDaoGeneration
2210
     */
2211
    public function testOneToOneInverseRelationGetter(): void
2212
    {
2213
        $this->skipOracle();
2214
2215
        $objectBaseDao = new BaseObjectDao($this->tdbmService);
2216
        $objectInheritedDao = new InheritedObjectDao($this->tdbmService);
2217
        $objectBase = new BaseObjectBean('label');
2218
        $objectBaseDao->save($objectBase);
2219
        $this->assertNull($objectBase->getInheritedObject());
2220
        $objectInherited = new InheritedObjectBean($objectBase);
2221
        $objectInheritedDao->save($objectInherited);
2222
        $this->assertSame($objectInherited, $objectBase->getInheritedObject());
2223
        $this->assertEquals(1, $objectBase->jsonSerialize()['inheritedObject']['id']);
2224
    }
2225
2226
    public function testLazyStopRecursion(): void
2227
    {
2228
        $albumDao = new AlbumDao($this->tdbmService);
2229
        $albumBean = $albumDao->getById(1);
2230
        $json = $albumBean->jsonSerialize(true);
2231
        $this->assertArrayHasKey('id', $json['artist']);
2232
        $this->assertArrayNotHasKey('name', $json['artist']);
2233
    }
2234
2235
    public function testLazyStopRecursionOnCompositeForeignKey(): void
2236
    {
2237
        $compositeFkSourceDao = new CompositeFkSourceDao($this->tdbmService);
2238
        $compositeFkSourceBean = $compositeFkSourceDao->getById(1);
2239
        $json = $compositeFkSourceBean->jsonSerialize(true);
2240
        $this->assertEquals(1, $json['compositeFkTarget']['1']['id']);
2241
        $this->assertEquals(1, $json['compositeFkTarget']['id2']);
2242
    }
2243
2244
    public function testMethodNameConflictsBetweenRegularAndAutoPivotProperties(): void
2245
    {
2246
        $artist = new ArtistBean('Super');
2247
        $artist->getChildren(); // regular property
2248
        $artist->getChildrenByArtistId(); // one-to-may relationship
2249
        $artist->getChildrenByArtistsRelations(); // auto-pivot relationship
2250
        $this->assertEquals(1, 1);
2251
    }
2252
2253
    /**
2254
     * @depends testDaoGeneration
2255
     */
2256
    public function testSQLCountWithArray(): void
2257
    {
2258
        $userDao = new TestUserDao($this->tdbmService);
2259
        $countryDao = new CountryDao($this->tdbmService);
2260
2261
        $country = $countryDao->getById(2);
2262
2263
        // Let's test filter bags by bean and filter bag with many values.
2264
        $users = $userDao->getUsersByComplexFilterBag($country, ['John Doe', 'John Smith'])->take(0, 1);
2265
        $this->assertEquals(1, $users->count());
2266
    }
2267
2268
    /**
2269
     * @depends testDaoGeneration
2270
     */
2271
    public function testSubQueryWithFind(): void
2272
    {
2273
        $userDao = new TestUserDao($this->tdbmService);
2274
        $articleDao = new TestArticleSubQueryDao($this->tdbmService, $userDao);
2275
2276
        $bill = $userDao->getById(4);
2277
        $article = new ArticleBean('Foo');
2278
        $article->setAuthor($bill);
2279
        $articleDao->save($article);
2280
2281
        $results = $articleDao->getArticlesByUserLoginStartingWith('bill');
2282
2283
        $this->assertCount(1, $results);
2284
        $this->assertSame('Foo', $results[0]->getContent());
2285
    }
2286
2287
    public function testSubQueryExceptionOnPrimaryKeysWithMultipleColumns(): void
2288
    {
2289
        $stateDao = new StateDao($this->tdbmService);
2290
        $states = $stateDao->findAll();
2291
        $this->expectException(TDBMException::class);
2292
        $this->expectExceptionMessage('You cannot use in a sub-query a table that has a primary key on more that 1 column.');
2293
        $states->_getSubQuery();
2294
    }
2295
2296
    public function testFindByDateTime(): void
2297
    {
2298
        $personDao = new PersonDao($this->tdbmService);
2299
        $personDao->findByModifiedAt(new \DateTimeImmutable())->count();
2300
        $this->assertTrue(true);
2301
    }
2302
2303
    /**
2304
     * Bug: find from sql use a `COUNT(DISTINCT *)` which fails because of null values.
2305
     */
2306
    public function testFindFromRawSqlCount(): void
2307
    {
2308
        $dao = new TestAlbumDao($this->tdbmService);
2309
        $albums = $dao->findAllFromRawSql();
2310
2311
        $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...
2312
        assert($firstAlbum instanceof AlbumBean);
2313
        $this->assertNull($firstAlbum->getNode()); // This null ensure reproducibility of the bug
2314
        $expectedCount = $dao->findAllFromRawSqlWithCount()->count();
2315
        $this->assertEquals($expectedCount, $albums->count());
2316
    }
2317
2318
    public function testFindFromRawSQLOnInheritance(): void
2319
    {
2320
        $dao = new TestPersonDao($this->tdbmService);
2321
        $objects = $dao->testFindFromRawSQLOnInherited();
2322
2323
        $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...
2324
        $this->assertNotEquals(0, $objects->count());
2325
    }
2326
2327
    public function testGeneratedColumnsAreNotPartOfTheConstructor(): void
2328
    {
2329
        if (!$this->tdbmService->getConnection()->getDatabasePlatform() instanceof MySqlPlatform || self::isMariaDb($this->tdbmService->getConnection())) {
2330
            $this->markTestSkipped('ReadOnly column is only tested with MySQL');
2331
        }
2332
2333
        $dao = new PlayerDao($this->tdbmService);
2334
2335
        $player = new PlayerBean([
2336
            'id' => 1,
2337
            'name' => 'Sally',
2338
            'games_played' =>
2339
                [
2340
                    'Battlefield' =>
2341
                        [
2342
                            'weapon' => 'sniper rifle',
2343
                            'rank' => 'Sergeant V',
2344
                            'level' => 20,
2345
                        ],
2346
                    'Crazy Tennis' =>
2347
                        [
2348
                            'won' => 4,
2349
                            'lost' => 1,
2350
                        ],
2351
                    'Puzzler' =>
2352
                        [
2353
                            'time' => 7,
2354
                        ],
2355
                ],
2356
        ]);
2357
2358
        $dao->save($player);
2359
2360
        $this->assertTrue(true);
2361
    }
2362
2363
    public function testCanReadVirtualColumn(): void
2364
    {
2365
        if (!$this->tdbmService->getConnection()->getDatabasePlatform() instanceof MySqlPlatform || self::isMariaDb($this->tdbmService->getConnection())) {
2366
            $this->markTestSkipped('ReadOnly column is only tested with MySQL');
2367
        }
2368
2369
        $dao = new PlayerDao($this->tdbmService);
2370
2371
        $player = $dao->getById(1);
2372
        $this->assertSame('Sally', $player->getNamesVirtual());
2373
    }
2374
2375
    public function testPivotTableAreProperlyEscaped(): void
2376
    {
2377
        $valueDao = new ValueDao($this->tdbmService);
2378
        $accessibleDao = new AccessibleDao($this->tdbmService);
2379
2380
        $value = $valueDao->getById(1);
2381
        $accessible = $accessibleDao->getById(1);
2382
        $this->assertSame(1, $value->getKey());
2383
        $this->assertSame(1, $accessible->getAdd());
2384
        $this->assertCount(1, $value->getAccessible());
2385
        $this->assertCount(1, $accessible->getValues());
2386
    }
2387
2388
    private function skipOracle(): void
2389
    {
2390
        if (self::getConnection()->getDatabasePlatform() instanceof OraclePlatform) {
2391
            $this->markTestSkipped('Not supported in Oracle');
2392
        }
2393
    }
2394
}
2395