Passed
Pull Request — master (#132)
by Dorian
06:58
created

TDBMDaoGeneratorTest::testJsonAnnotations()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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

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

1682
        /** @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...
1683
        $firstLine = fgets($resource);
1684
        $this->assertSame("<?php\n", $firstLine);
1685
    }
1686
1687
    /**
1688
     * @depends testBlob
1689
     */
1690
    public function testReadBlob()
1691
    {
1692
        $fileDao = new FileDao($this->tdbmService);
1693
        $loadedFile = $fileDao->getById(1);
1694
1695
        $resource = $loadedFile->getFile();
1696
        $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

1696
        /** @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...
1697
        $firstLine = fgets($resource);
1698
        $this->assertSame("<?php\n", $firstLine);
1699
1700
        stream_get_contents($resource);
1701
1702
        $loadedFile->setId($loadedFile->getId());
1703
1704
        $fileDao->save($loadedFile);
1705
    }
1706
1707
    /**
1708
     * @depends testReadBlob
1709
     */
1710
    public function testReadAndSaveBlob()
1711
    {
1712
        $fileDao = new FileDao($this->tdbmService);
1713
        $loadedFile = $fileDao->getById(1);
1714
1715
        $resource = $loadedFile->getFile();
1716
1717
        $firstLine = fgets($resource);
1718
        $this->assertSame("<?php\n", $firstLine);
1719
    }
1720
1721
    /**
1722
     * @depends testReadBlob
1723
     */
1724
    public function testProtectedGetterSetter()
1725
    {
1726
        $md5Getter = new ReflectionMethod(FileBaseBean::class, 'getMd5');
1727
        $md5Setter = new ReflectionMethod(FileBaseBean::class, 'setMd5');
1728
1729
        $this->assertTrue($md5Getter->isProtected());
1730
        $this->assertTrue($md5Setter->isProtected());
1731
1732
        $md5Getter2 = new ReflectionMethod(FileBaseBean::class, 'getArticle');
1733
        $md5Setter2 = new ReflectionMethod(FileBaseBean::class, 'setArticle');
1734
1735
        $this->assertTrue($md5Getter2->isProtected());
1736
        $this->assertTrue($md5Setter2->isProtected());
1737
1738
        $oneToManyGetter = new ReflectionMethod(ArticleBaseBean::class, 'getFiles');
1739
        $this->assertTrue($oneToManyGetter->isProtected());
1740
1741
        $fileDao = new FileDao($this->tdbmService);
1742
        $loadedFile = $fileDao->getById(1);
1743
1744
        // The md5 and article columns are not JSON serialized
1745
        $this->assertSame([
1746
            'id' => 1,
1747
        ], $loadedFile->jsonSerialize());
1748
    }
1749
1750
    /**
1751
     * @depends testDaoGeneration
1752
     */
1753
    public function testBlobResourceException()
1754
    {
1755
        $this->expectException(TDBMInvalidArgumentException::class);
1756
        $this->expectExceptionMessage('Invalid argument passed to \'TheCodingMachine\\TDBM\\Test\\Dao\\Bean\\Generated\\FileBaseBean::setFile\'. Expecting a resource. Got a string.');
1757
        new FileBean('foobar');
1758
    }
1759
1760
    /**
1761
     * @depends testDaoGeneration
1762
     */
1763
    public function testFilterBag()
1764
    {
1765
        $userDao = new TestUserDao($this->tdbmService);
1766
        $countryDao = new CountryDao($this->tdbmService);
1767
1768
        $country = $countryDao->getById(2);
1769
1770
        // Let's test filter bags by bean and filter bag with many values.
1771
        $users = $userDao->getUsersByComplexFilterBag($country, ['John Doe', 'Jane Doe']);
1772
1773
        $this->assertCount(1, $users);
1774
        $this->assertSame('John Doe', $users[0]->getName());
1775
    }
1776
1777
    /**
1778
     * @depends testDaoGeneration
1779
     */
1780
    public function testDecimalIsMappedToString()
1781
    {
1782
        $reflectionClass = new \ReflectionClass(BoatBaseBean::class);
1783
        $this->assertSame('string', (string) $reflectionClass->getMethod('getLength')->getReturnType());
1784
    }
1785
1786
    /**
1787
     * @depends testDaoGeneration
1788
     */
1789
    public function testNoGetByIdOnMultiPrimaryKeys()
1790
    {
1791
        $reflectionClass = new \ReflectionClass(StateDao::class);
1792
        $this->assertFalse($reflectionClass->hasMethod('getById'));
1793
    }
1794
1795
    /**
1796
     * @depends testDaoGeneration
1797
     */
1798
    public function testInsertMultiPrimaryKeysBean()
1799
    {
1800
        $countryDao = new CountryDao($this->tdbmService);
1801
1802
        $country = $countryDao->getById(1);
1803
1804
        $stateDao = new StateDao($this->tdbmService);
1805
        $state = new StateBean($country, 'IDF', 'Ile de France');
1806
        $stateDao->save($state);
1807
1808
        $this->assertSame($state, $stateDao->findAll()[0]);
1809
    }
1810
1811
    /**
1812
     * @depends testInsertMultiPrimaryKeysBean
1813
     */
1814
    public function testDeleteMultiPrimaryKeysBean()
1815
    {
1816
        $stateDao = new StateDao($this->tdbmService);
1817
1818
        $state = $stateDao->findAll()[0];
1819
        $stateDao->delete($state);
1820
        $this->assertCount(0, $stateDao->findAll());
1821
1822
    }
1823
1824
    /**
1825
     * @depends testDaoGeneration
1826
     */
1827
    public function testSortOnInheritedTable()
1828
    {
1829
        $animalDao = new AnimalDao($this->tdbmService);
1830
1831
        // Let's insert an animal that is nothing.
1832
        $animal = new AnimalBean('Mickey');
1833
        $animalDao->save($animal);
1834
1835
        $animals = $animalDao->findAll()->withOrder('dog.race ASC');
1836
1837
        $animalsArr = $animals->toArray();
1838
        $this->assertCount(3, $animalsArr);
1839
    }
1840
1841
    public function testJsonAnnotations()
1842
    {
1843
        $nodeDao = new NodeDao($this->tdbmService);
1844
        $index = $nodeDao->getById(4);
1845
        $json = $index->jsonSerialize();
1846
1847
        self::assertArrayNotHasKey('id', $json);
1848
        self::assertArrayHasKey('parent', $json);
1849
        self::assertArrayHasKey('createdAt', $json);
1850
        self::assertEquals($index->getCreatedAt()->format('Y-m-d'), $json['createdAt']);
1851
        self::assertArrayHasKey('parent', $json['parent']);
1852
    }
1853
}
1854