Completed
Push — master ( 5f252d...f05e36 )
by David
13s queued 10s
created

testDeleteMultiPrimaryKeysBean()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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

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

1310
        $this->assertEquals('bill.shakespeare', $users[0]->/** @scrutinizer ignore-call */ getLogin());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1311
1312
        $users = $users->withParameters(['login' => 'jean%']);
1313
        $this->assertEquals('jean.dupont', $users[0]->getLogin());
1314
    }
1315
1316
    /**
1317
     * @depends testDaoGeneration
1318
     */
1319
    public function testOrderByExpression()
1320
    {
1321
        $userDao = new TestUserDao($this->tdbmService);
1322
        $users = $userDao->getUsersByReversedCountryName();
1323
1324
        $this->assertEquals('Jamaica', $users[0]->getCountry()->getLabel());
1325
    }
1326
1327
    /**
1328
     * @depends testDaoGeneration
1329
     */
1330
    public function testOrderByException()
1331
    {
1332
        $userDao = new TestUserDao($this->tdbmService);
1333
        $users = $userDao->getUsersByInvalidOrderBy();
1334
        $this->expectException(TDBMInvalidArgumentException::class);
1335
        $user = $users[0];
0 ignored issues
show
Unused Code introduced by
The assignment to $user is dead and can be removed.
Loading history...
1336
    }
1337
1338
    /**
1339
     * @depends testDaoGeneration
1340
     */
1341
    public function testOrderByProtectedColumn()
1342
    {
1343
        $animalDao = new AnimalDao($this->tdbmService);
1344
        $animals = $animalDao->findAll();
1345
        $animals = $animals->withOrder('`order` ASC');
1346
1347
        $this->assertInstanceOf(DogBean::class, $animals[0]);
1348
        $this->assertInstanceOf(CatBean::class, $animals[1]);
1349
1350
        $animals = $animals->withOrder('`order` DESC');
1351
1352
        $this->assertInstanceOf(CatBean::class, $animals[0]);
1353
        $this->assertInstanceOf(DogBean::class, $animals[1]);
1354
    }
1355
1356
    /**
1357
     * @depends testDaoGeneration
1358
     */
1359
    public function testGetOnAllNullableValues()
1360
    {
1361
        // Tests that a get performed on a column that has only nullable fields succeeds.
1362
        $allNullable = new AllNullableBean();
1363
        $this->assertNull($allNullable->getId());
1364
        $this->assertNull($allNullable->getLabel());
1365
        $this->assertNull($allNullable->getCountry());
1366
    }
1367
1368
    /**
1369
     * @depends testDaoGeneration
1370
     */
1371
    public function testExceptionOnMultipleInheritance()
1372
    {
1373
        $connection = self::getConnection();
1374
        self::insert($connection, 'animal', [
1375
            'id' => 99, 'name' => 'Snoofield',
1376
        ]);
1377
        self::insert($connection, 'dog', [
1378
            'id' => 99, 'race' => 'dog',
1379
        ]);
1380
        self::insert($connection, 'cat', [
1381
            'id' => 99, 'cuteness_level' => 0,
1382
        ]);
1383
1384
        $catched = false;
1385
        try {
1386
            $animalDao = new AnimalDao($this->tdbmService);
1387
            $animalDao->getById(99);
1388
        } catch (TDBMInheritanceException $e) {
1389
            $catched = true;
1390
        }
1391
        $this->assertTrue($catched, 'Exception TDBMInheritanceException was not caught');
1392
1393
        self::delete($connection, 'cat', ['id' => 99]);
1394
        self::delete($connection, 'dog', ['id' => 99]);
1395
        self::delete($connection, 'animal', ['id' => 99]);
1396
    }
1397
1398
    /**
1399
     * @depends testDaoGeneration
1400
     */
1401
    public function testReferenceNotSaved()
1402
    {
1403
        $boatDao = new BoatDao($this->tdbmService);
1404
1405
        $country = new CountryBean('Atlantis');
1406
        $boat = new BoatBean($country, 'Titanic');
1407
1408
        $boatDao->save($boat);
1409
    }
1410
1411
    /**
1412
     * @depends testReferenceNotSaved
1413
     */
1414
    public function testUniqueIndexOnForeignKeyThenScalar()
1415
    {
1416
        $boatDao = new BoatDao($this->tdbmService);
1417
        $countryDao = new CountryDao($this->tdbmService);
1418
1419
        $countryBean = $countryDao->findOneByLabel('Atlantis');
1420
        $boatBean = $boatDao->findOneByAnchorageCountryAndName($countryBean, 'Titanic');
1421
1422
        $this->assertNotNull($boatBean);
1423
    }
1424
1425
    /**
1426
     * @depends testDaoGeneration
1427
     */
1428
    public function testReferenceDeleted()
1429
    {
1430
        $countryDao = new CountryDao($this->tdbmService);
1431
        $boatDao = new BoatDao($this->tdbmService);
1432
1433
        $country = new CountryBean('Bikini Bottom');
1434
        $countryDao->save($country);
1435
1436
        $boat = new BoatBean($country, 'Squirrel boat');
1437
        $countryDao->delete($country);
1438
1439
        $this->expectException(TDBMMissingReferenceException::class);
1440
        $boatDao->save($boat);
1441
    }
1442
1443
    /**
1444
     * @depends testDaoGeneration
1445
     */
1446
    public function testCyclicReferenceWithInheritance()
1447
    {
1448
        $userDao = new UserDao($this->tdbmService);
1449
1450
        $country = new CountryBean('Norrisland');
1451
        $user = new UserBean('Chuck Norris', '[email protected]', $country, 'chuck.norris');
1452
1453
        $user->setManager($user);
1454
1455
        $this->expectException(TDBMCyclicReferenceException::class);
1456
        $userDao->save($user);
1457
    }
1458
1459
    /**
1460
     * @depends testDaoGeneration
1461
     */
1462
    public function testCyclicReference()
1463
    {
1464
        $categoryDao = new CategoryDao($this->tdbmService);
1465
1466
        $category = new CategoryBean('Root');
1467
1468
        $category->setParent($category);
1469
1470
        $this->expectException(TDBMCyclicReferenceException::class);
1471
        $categoryDao->save($category);
1472
    }
1473
1474
    /**
1475
     * @depends testDaoGeneration
1476
     */
1477
    public function testCorrectTypeForPrimaryKeyAfterSave()
1478
    {
1479
        // PosqtgreSQL does not particularly like empty inserts (i.e.: "INSERT INTO all_nullable () VALUES ()" )
1480
        $this->onlyMySql();
1481
1482
        $allNullableDao = new AllNullableDao($this->tdbmService);
1483
        $allNullable = new AllNullableBean();
1484
        $allNullableDao->save($allNullable);
1485
        $id = $allNullable->getId();
1486
1487
        $this->assertTrue(is_int($id));
1488
    }
1489
1490
    /**
1491
     * @depends testDaoGeneration
1492
     */
1493
    public function testPSR2Compliance()
1494
    {
1495
        $process = new Process('vendor/bin/php-cs-fixer fix src/Test/  --dry-run --diff --rules=@PSR2');
1496
        $process->run();
1497
1498
        // executes after the command finishes
1499
        if (!$process->isSuccessful()) {
1500
            echo $process->getOutput();
1501
            $this->fail('Generated code is not PSR-2 compliant');
1502
        }
1503
    }
1504
1505
    /**
1506
     * @depends testDaoGeneration
1507
     */
1508
    public function testFindOneByGeneration()
1509
    {
1510
        $reflectionMethod = new \ReflectionMethod(UserBaseDao::class, 'findOneByLogin');
1511
        $parameters = $reflectionMethod->getParameters();
1512
1513
        $this->assertCount(2, $parameters);
1514
        $this->assertSame('login', $parameters[0]->getName());
1515
        $this->assertSame('additionalTablesFetch', $parameters[1]->getName());
1516
    }
1517
1518
    /**
1519
     * @depends testDaoGeneration
1520
     */
1521
    public function testUuid()
1522
    {
1523
        $article = new ArticleBean('content');
1524
        $this->assertSame('content', $article->getContent());
1525
        $this->assertNotEmpty($article->getId());
1526
        $uuid = Uuid::fromString($article->getId());
1527
        $this->assertSame(1, $uuid->getVersion());
1528
    }
1529
1530
    /**
1531
     * @depends testDaoGeneration
1532
     */
1533
    public function testUuidv4()
1534
    {
1535
        $article = new Article2Bean('content');
1536
        $this->assertSame('content', $article->getContent());
1537
        $this->assertNotEmpty($article->getId());
1538
        $uuid = Uuid::fromString($article->getId());
1539
        $this->assertSame(4, $uuid->getVersion());
1540
    }
1541
1542
    /**
1543
     * @depends testDaoGeneration
1544
     */
1545
    public function testTypeHintedConstructors()
1546
    {
1547
        $userBaseBeanReflectionConstructor = new \ReflectionMethod(UserBaseBean::class, '__construct');
1548
        $nameParam = $userBaseBeanReflectionConstructor->getParameters()[0];
1549
1550
        $this->assertSame('string', (string)$nameParam->getType());
1551
    }
1552
1553
    /**
1554
     * @depends testDaoGeneration
1555
     */
1556
    public function testSaveTransaction()
1557
    {
1558
        $countryDao = new CountryDao($this->tdbmService);
1559
1560
        $boatDao = new BoatDao($this->tdbmService);
1561
        $boatBean = $boatDao->getById(1);
1562
        $boatBean->setName('Bismark');
1563
1564
        $boatBean->getCountry();
1565
1566
        // Let's insert a row without telling TDBM to trigger an error!
1567
        self::insert($this->getConnection(), 'sailed_countries', [
1568
            'boat_id' => 1,
1569
            'country_id' => 2
1570
        ]);
1571
1572
        $boatBean->addCountry($countryDao->getById(2));
1573
1574
        $this->expectException(UniqueConstraintViolationException::class);
1575
1576
        $boatDao->save($boatBean);
1577
    }
1578
1579
    /**
1580
     * @depends testSaveTransaction
1581
     */
1582
    public function testSaveTransaction2()
1583
    {
1584
        $boatDao = new BoatDao($this->tdbmService);
1585
        $boatBean = $boatDao->getById(1);
1586
1587
        // The name should not have been saved because the transaction of the previous test should have rollbacked.
1588
        $this->assertNotSame('Bismark', $boatBean->getName());
1589
    }
1590
1591
    /**
1592
     * @depends testDaoGeneration
1593
     */
1594
    public function testForeignKeyPointingToNonPrimaryKey()
1595
    {
1596
        $dao = new RefNoPrimKeyDao($this->tdbmService);
1597
        $bean = $dao->getById(1);
1598
1599
        $this->assertSame('foo', $bean->getFrom()->getTo());
1600
1601
        $newBean = new RefNoPrimKeyBean($bean, 'baz');
1602
        $dao->save($newBean);
1603
        $this->assertSame('foo', $newBean->getFrom()->getTo());
1604
    }
1605
1606
    /**
1607
     * @depends testDaoGeneration
1608
     */
1609
    public function testCloningUuidBean()
1610
    {
1611
        $article = new ArticleBean('content');
1612
        $this->assertNotEmpty($article->getId());
1613
        $article2 = clone $article;
1614
        $this->assertNotEmpty($article2->getId());
1615
        $this->assertNotSame($article->getId(), $article2->getId());
1616
    }
1617
1618
    /**
1619
     * @depends testDaoGeneration
1620
     */
1621
    public function testRecursiveSave()
1622
    {
1623
        $categoryDao = new CategoryDao($this->tdbmService);
1624
1625
        $root1 = new CategoryBean('Root1');
1626
        $categoryDao->save($root1);
1627
        // Root 2 is not saved yet.
1628
        $root2 = new CategoryBean('Root2');
1629
        $intermediate = new CategoryBean('Intermediate');
1630
        $categoryDao->save($intermediate);
1631
1632
        // Let's switch the parent to a bean in detached state.
1633
        $intermediate->setParent($root2);
1634
1635
        // Now, let's save a new category that references the leaf category.
1636
        $leaf = new CategoryBean('Leaf');
1637
        $leaf->setParent($intermediate);
1638
        $categoryDao->save($leaf);
1639
    }
1640
1641
    /**
1642
     * @depends testDaoGeneration
1643
     */
1644
    public function testBlob()
1645
    {
1646
        $fp = fopen(__FILE__, 'r');
1647
        $file = new FileBean($fp);
1648
1649
        $fileDao = new FileDao($this->tdbmService);
1650
1651
        $fileDao->save($file);
1652
1653
        $loadedFile = $fileDao->getById($file->getId());
1654
1655
        $resource = $loadedFile->getFile();
1656
        $result = fseek($resource, 0);
1657
        $this->assertSame(0, $result);
1658
        $this->assertInternalType('resource', $resource);
1659
        $firstLine = fgets($resource);
1660
        $this->assertSame("<?php\n", $firstLine);
1661
    }
1662
1663
    /**
1664
     * @depends testBlob
1665
     */
1666
    public function testReadBlob()
1667
    {
1668
        $fileDao = new FileDao($this->tdbmService);
1669
        $loadedFile = $fileDao->getById(1);
1670
1671
        $resource = $loadedFile->getFile();
1672
        $this->assertInternalType('resource', $resource);
1673
        $firstLine = fgets($resource);
1674
        $this->assertSame("<?php\n", $firstLine);
1675
1676
        stream_get_contents($resource);
1677
1678
        $loadedFile->setId($loadedFile->getId());
1679
1680
        $fileDao->save($loadedFile);
1681
    }
1682
1683
    /**
1684
     * @depends testReadBlob
1685
     */
1686
    public function testReadAndSaveBlob()
1687
    {
1688
        $fileDao = new FileDao($this->tdbmService);
1689
        $loadedFile = $fileDao->getById(1);
1690
1691
        $resource = $loadedFile->getFile();
1692
1693
        $firstLine = fgets($resource);
1694
        $this->assertSame("<?php\n", $firstLine);
1695
    }
1696
1697
    /**
1698
     * @depends testDaoGeneration
1699
     */
1700
    public function testBlobResourceException()
1701
    {
1702
        $this->expectException(TDBMInvalidArgumentException::class);
1703
        $this->expectExceptionMessage('Invalid argument passed to \'TheCodingMachine\\TDBM\\Test\\Dao\\Bean\\Generated\\FileBaseBean::setFile\'. Expecting a resource. Got a string.');
1704
        new FileBean('foobar');
1705
    }
1706
1707
    /**
1708
     * @depends testDaoGeneration
1709
     */
1710
    public function testFilterBag()
1711
    {
1712
        $userDao = new TestUserDao($this->tdbmService);
1713
        $countryDao = new CountryDao($this->tdbmService);
1714
1715
        $country = $countryDao->getById(2);
1716
1717
        // Let's test filter bags by bean and filter bag with many values.
1718
        $users = $userDao->getUsersByComplexFilterBag($country, ['John Doe', 'Jane Doe']);
1719
1720
        $this->assertCount(1, $users);
1721
        $this->assertSame('John Doe', $users[0]->getName());
1722
    }
1723
1724
    /**
1725
     * @depends testDaoGeneration
1726
     */
1727
    public function testDecimalIsMappedToString()
1728
    {
1729
        $reflectionClass = new \ReflectionClass(BoatBaseBean::class);
1730
        $this->assertSame('string', (string) $reflectionClass->getMethod('getLength')->getReturnType());
1731
    }
1732
1733
    /**
1734
     * @depends testDaoGeneration
1735
     */
1736
    public function testNoGetByIdOnMultiPrimaryKeys()
1737
    {
1738
        $reflectionClass = new \ReflectionClass(StateDao::class);
1739
        $this->assertFalse($reflectionClass->hasMethod('getById'));
1740
    }
1741
1742
    /**
1743
     * @depends testDaoGeneration
1744
     */
1745
    public function testInsertMultiPrimaryKeysBean()
1746
    {
1747
        $countryDao = new CountryDao($this->tdbmService);
1748
1749
        $country = $countryDao->getById(1);
1750
1751
        $stateDao = new StateDao($this->tdbmService);
1752
        $state = new StateBean($country, 'IDF', 'Ile de France');
1753
        $stateDao->save($state);
1754
1755
        $this->assertSame($state, $stateDao->findAll()[0]);
1756
    }
1757
1758
    /**
1759
     * @depends testInsertMultiPrimaryKeysBean
1760
     */
1761
    public function testDeleteMultiPrimaryKeysBean()
1762
    {
1763
        $stateDao = new StateDao($this->tdbmService);
1764
1765
        $state = $stateDao->findAll()[0];
1766
        $stateDao->delete($state);
1767
        $this->assertCount(0, $stateDao->findAll());
1768
1769
    }
1770
}
1771