Completed
Push — master ( 3633fe...906daf )
by David
17s queued 11s
created

TDBMDaoGeneratorTest::testCursorJsonEncode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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

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

filter:
    dependency_paths: ["lib/*"]

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

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

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

filter:
    dependency_paths: ["lib/*"]

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

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

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

filter:
    dependency_paths: ["lib/*"]

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

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

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

filter:
    dependency_paths: ["lib/*"]

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

Loading history...
43
use TheCodingMachine\TDBM\Test\Dao\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...
44
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...
45
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...
46
use TheCodingMachine\TDBM\Test\Dao\Bean\ArrayBean;
0 ignored issues
show
Bug introduced by
The type TheCodingMachine\TDBM\Test\Dao\Bean\ArrayBean was not found. Maybe you did not declare it correctly or list all dependencies?

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

filter:
    dependency_paths: ["lib/*"]

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

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

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

filter:
    dependency_paths: ["lib/*"]

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

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

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

filter:
    dependency_paths: ["lib/*"]

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

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

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

filter:
    dependency_paths: ["lib/*"]

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

Loading history...
50
use TheCodingMachine\TDBM\Test\Dao\Bean\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...
51
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...
52
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...
53
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...
54
use TheCodingMachine\TDBM\Test\Dao\Bean\DateArrayBean;
0 ignored issues
show
Bug introduced by
The type TheCodingMachine\TDBM\Test\Dao\Bean\DateArrayBean was not found. Maybe you did not declare it correctly or list all dependencies?

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

filter:
    dependency_paths: ["lib/*"]

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

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

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

filter:
    dependency_paths: ["lib/*"]

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

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

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

filter:
    dependency_paths: ["lib/*"]

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

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

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

filter:
    dependency_paths: ["lib/*"]

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

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

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

filter:
    dependency_paths: ["lib/*"]

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

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

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

filter:
    dependency_paths: ["lib/*"]

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

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

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

filter:
    dependency_paths: ["lib/*"]

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

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

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

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

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
738
            $this->assertLessThanOrEqual($countries[$i - 1]->getUsers()->count(), $countries[$i]->getUsers()->count());
739
        }
740
    }
741
742
    /**
743
     * @depends testDaoGeneration
744
     */
745
    public function testFindFromRawSqlWithUnion(): void
746
    {
747
        $countryDao = new TestCountryDao($this->tdbmService);
748
        $countries = $countryDao->getCountriesUsingUnion();
749
750
        $this->assertCount(2, $countries);
751
        $this->assertEquals(1, $countries[0]->getId());
752
    }
753
754
    /**
755
     * @depends testDaoGeneration
756
     */
757
    public function testFindFromRawSqlWithSimpleQuery(): void
758
    {
759
        $countryDao = new TestCountryDao($this->tdbmService);
760
        $countries = $countryDao->getCountriesUsingSimpleQuery();
761
762
        $this->assertCount(1, $countries);
763
        $this->assertEquals(1, $countries[0]->getId());
764
    }
765
766
    /**
767
     * @depends testDaoGeneration
768
     */
769
    public function testFindFromRawSqlWithDistinctQuery(): void
770
    {
771
        $countryDao = new TestCountryDao($this->tdbmService);
772
        $countries = $countryDao->getCountriesUsingDistinctQuery();
773
774
        $this->assertCount(1, $countries);
775
        $this->assertEquals(2, $countries[0]->getId());
776
    }
777
778
    /**
779
     * @depends testDaoGeneration
780
     */
781
    public function testFindFilters(): void
782
    {
783
        $userDao = new TestUserDao($this->tdbmService);
784
        $users = $userDao->getUsersByLoginStartingWith('bill');
785
786
        $this->assertCount(1, $users);
787
        $this->assertEquals('bill.shakespeare', $users[0]->getLogin());
788
    }
789
790
    /**
791
     * @depends testDaoGeneration
792
     */
793
    public function testFindMode(): void
794
    {
795
        $userDao = new TestUserDao($this->tdbmService);
796
        $users = $userDao->getUsersByLoginStartingWith('bill', TDBMService::MODE_CURSOR);
797
798
        $this->expectException('TheCodingMachine\TDBM\TDBMException');
799
        $users[0];
800
    }
801
802
    /**
803
     * @depends testDaoGeneration
804
     */
805
    public function testFindAll(): void
806
    {
807
        $userDao = new TestUserDao($this->tdbmService);
808
        $users = $userDao->findAll();
809
810
        $this->assertCount(6, $users);
811
    }
812
813
    /**
814
     * @depends testDaoGeneration
815
     */
816
    public function testFindOne(): void
817
    {
818
        $userDao = new TestUserDao($this->tdbmService);
819
        $user = $userDao->getUserByLogin('bill.shakespeare');
820
821
        $this->assertEquals('bill.shakespeare', $user->getLogin());
822
    }
823
824
    /**
825
     * @depends testDaoGeneration
826
     */
827
    public function testJsonEncodeBean(): void
828
    {
829
        $userDao = new TestUserDao($this->tdbmService);
830
        $user = $userDao->getUserByLogin('bill.shakespeare');
831
832
        $jsonEncoded = json_encode($user);
833
        $userDecoded = json_decode($jsonEncoded, true);
834
835
        $this->assertEquals('bill.shakespeare', $userDecoded['login']);
836
837
        // test serialization of dates.
838
        $this->assertTrue(is_string($userDecoded['createdAt']));
839
        $this->assertEquals('2015-10-24', (new \DateTimeImmutable($userDecoded['createdAt']))->format('Y-m-d'));
840
        $this->assertNull($userDecoded['modifiedAt']);
841
842
        // testing many to 1 relationships
843
        $this->assertEquals('UK', $userDecoded['country']['label']);
844
845
        // testing many to many relationships
846
        $this->assertCount(1, $userDecoded['roles']);
847
        $this->assertArrayNotHasKey('users', $userDecoded['roles'][0]);
848
        $this->assertArrayNotHasKey('rights', $userDecoded['roles'][0]);
849
    }
850
851
    /**
852
     * @depends testDaoGeneration
853
     */
854
    public function testNullableForeignKey(): void
855
    {
856
        $userDao = new TestUserDao($this->tdbmService);
857
        $user = $userDao->getUserByLogin('john.smith');
858
859
        $this->assertNull($user->getManager());
860
861
        $jsonEncoded = json_encode($user);
862
        $userDecoded = json_decode($jsonEncoded, true);
863
864
        $this->assertNull($userDecoded['manager']);
865
    }
866
867
    /**
868
     * Test that setting (and saving) objects' references (foreign keys relations) to null is working.
869
     *
870
     * @depends testDaoGeneration
871
     */
872
    public function testSetToNullForeignKey(): void
873
    {
874
        $userDao = new TestUserDao($this->tdbmService);
875
        $user = $userDao->getUserByLogin('john.smith');
876
        $manager = $userDao->getUserByLogin('jean.dupont');
877
878
        $user->setManager($manager);
879
        $userDao->save($user);
880
881
        $user->setManager(null);
882
        $userDao->save($user);
883
        $this->assertNull($user->getManager());
884
    }
885
886
    /**
887
     * @depends testDaoGeneration
888
     */
889
    public function testQueryOnWrongTableName(): void
890
    {
891
        $userDao = new TestUserDao($this->tdbmService);
892
        $users = $userDao->getUsersWrongTableName();
893
        $this->expectException('Mouf\Database\SchemaAnalyzer\SchemaAnalyzerTableNotFoundException');
894
        $this->expectExceptionMessage('Could not find table \'contacts\'. Did you mean \'contact\'?');
895
        $users->count();
896
    }
897
898
    /**
899
     * @depends testDaoGeneration
900
     */
901
    /*public function testQueryNullForeignKey(): void
902
    {
903
        $userDao = new TestUserDao($this->tdbmService);
904
        $users = $userDao->getUsersByManagerId(null);
905
        $this->assertCount(3, $users);
906
    }*/
907
908
    /**
909
     * @depends testDaoGeneration
910
     */
911
    public function testInnerJsonEncode(): void
912
    {
913
        $userDao = new TestUserDao($this->tdbmService);
914
        $user = $userDao->getUserByLogin('bill.shakespeare');
915
916
        $jsonEncoded = json_encode(['user' => $user]);
917
        $msgDecoded = json_decode($jsonEncoded, true);
918
919
        $this->assertEquals('bill.shakespeare', $msgDecoded['user']['login']);
920
    }
921
922
    /**
923
     * @depends testDaoGeneration
924
     */
925
    public function testArrayJsonEncode(): void
926
    {
927
        $userDao = new TestUserDao($this->tdbmService);
928
        $users = $userDao->getUsersByLoginStartingWith('bill');
929
930
        $jsonEncoded = json_encode($users);
931
        $msgDecoded = json_decode($jsonEncoded, true);
932
933
        $this->assertCount(1, $msgDecoded);
934
    }
935
936
    /**
937
     * @depends testDaoGeneration
938
     */
939
    public function testCursorJsonEncode(): void
940
    {
941
        $userDao = new TestUserDao($this->tdbmService);
942
        $users = $userDao->getUsersByLoginStartingWith('bill', TDBMService::MODE_CURSOR);
943
944
        $jsonEncoded = json_encode($users);
945
        $msgDecoded = json_decode($jsonEncoded, true);
946
947
        $this->assertCount(1, $msgDecoded);
948
    }
949
950
    /**
951
     * @depends testDaoGeneration
952
     */
953
    public function testPageJsonEncode(): void
954
    {
955
        $userDao = new TestUserDao($this->tdbmService);
956
        $users = $userDao->getUsersByLoginStartingWith('bill');
957
958
        $jsonEncoded = json_encode($users->take(0, 1));
959
        $msgDecoded = json_decode($jsonEncoded, true);
960
961
        $this->assertCount(1, $msgDecoded);
962
    }
963
964
    /**
965
     * @depends testDaoGeneration
966
     */
967
    public function testFirst(): void
968
    {
969
        $userDao = new TestUserDao($this->tdbmService);
970
        $users = $userDao->getUsersByLoginStartingWith('bill');
971
972
        $bill = $users->first();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $bill is correct as $users->first() targeting TheCodingMachine\TDBM\ResultIterator::first() seems to always return null.

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

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

}

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

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

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

Loading history...
973
        $this->assertEquals('bill.shakespeare', $bill->getLogin());
974
    }
975
976
    /**
977
     * @depends testDaoGeneration
978
     */
979
    public function testFirstNull(): void
980
    {
981
        $userDao = new TestUserDao($this->tdbmService);
982
        $users = $userDao->getUsersByLoginStartingWith('mike');
983
984
        $user = $users->first();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $user is correct as $users->first() targeting TheCodingMachine\TDBM\ResultIterator::first() seems to always return null.

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

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

}

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

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

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

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

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

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

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

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

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

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

Loading history...
1699
        $firstLine = fgets($resource);
1700
        $this->assertSame("<?php\n", $firstLine);
1701
    }
1702
1703
    /**
1704
     * @depends testBlob
1705
     */
1706
    public function testReadBlob(): void
1707
    {
1708
        $fileDao = new FileDao($this->tdbmService);
1709
        $loadedFile = $fileDao->getById(1);
1710
1711
        $resource = $loadedFile->getFile();
1712
        $this->assertInternalType('resource', $resource);
0 ignored issues
show
Deprecated Code introduced by
The function PHPUnit\Framework\Assert::assertInternalType() has been deprecated: https://github.com/sebastianbergmann/phpunit/issues/3369 ( Ignorable by Annotation )

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

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

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

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

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

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

2039
        fclose(/** @scrutinizer ignore-type */ $fp);
Loading history...
2040
    }
2041
2042
    /**
2043
     * @depends testDaoGeneration
2044
     */
2045
    public function testOptionnalParametersCanBeNullInFindOneBy()
2046
    {
2047
        $albumDao = new AlbumDao($this->tdbmService);
2048
        $artist = new ArtistBean('Marcel');
2049
2050
        $albumDao->findOneByArtistAndNode($artist, null);
2051
        $this->assertEquals(1, 1);
2052
    }
2053
2054
    /**
2055
     * @depends testDaoGeneration
2056
     */
2057
    public function testRequiredParametersCannotBeNullInFindOneBy()
2058
    {
2059
        $albumDao = new AlbumDao($this->tdbmService);
2060
        $artist = new ArtistBean('Marcel');
2061
        $account = new AccountBean('Jamie');
2062
2063
        $albumDao->findOneByArtistAndAccount($artist, $account);
2064
2065
        $this->expectException('TypeError');
2066
        $albumDao->findOneByArtistAndAccount($artist, null);
2067
    }
2068
2069
    /**
2070
     * @depends testDaoGeneration
2071
     */
2072
    public function testLazyLoad(): void
2073
    {
2074
        $roleDao = new RoleDao($this->tdbmService);
2075
        $roleBean = $roleDao->getById(1, true);
2076
2077
        $this->assertSame(TDBMObjectStateEnum::STATE_NOT_LOADED, $roleBean->_getDbRows()['roles']->_getStatus());
2078
        $roleBean->getId();
2079
        $this->assertSame(TDBMObjectStateEnum::STATE_NOT_LOADED, $roleBean->_getDbRows()['roles']->_getStatus());
2080
    }
2081
2082
    /**
2083
     * @depends testDaoGeneration
2084
     */
2085
    public function testOneToOneInverseRelationGetter(): void
2086
    {
2087
        $objectBaseDao = new ObjectBaseDao($this->tdbmService);
2088
        $objectInheritedDao = new ObjectInheritedDao($this->tdbmService);
2089
        $objectBase = new ObjectBaseBean('label');
2090
        $objectBaseDao->save($objectBase);
2091
        $this->assertNull($objectBase->getObjectInherited());
2092
        $objectInherited = new ObjectInheritedBean($objectBase);
2093
        $objectInheritedDao->save($objectInherited);
2094
        $this->assertInstanceOf(ObjectInheritedBean::class, $objectBase->getObjectInherited());
2095
        $this->assertEquals(1, $objectBase->jsonSerialize()['objectInherited']['id']);
2096
    }
2097
}
2098