Passed
Pull Request — 5.0 (#112)
by David
03:34
created

TDBMDaoGeneratorTest::testSortOnInheritedTable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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