Passed
Pull Request — master (#125)
by David
03:21
created

TDBMDaoGeneratorTest::testProtectedGetterSetter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

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

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

1674
        /** @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...
1675
        $firstLine = fgets($resource);
1676
        $this->assertSame("<?php\n", $firstLine);
1677
    }
1678
1679
    /**
1680
     * @depends testBlob
1681
     */
1682
    public function testReadBlob()
1683
    {
1684
        $fileDao = new FileDao($this->tdbmService);
1685
        $loadedFile = $fileDao->getById(1);
1686
1687
        $resource = $loadedFile->getFile();
1688
        $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

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