Skip to content

Commit b8b54ac

Browse files
Merge pull request #296 from alexislefebvre/feat-support-doctrine-orm-3
feat: support doctrine/orm 3
2 parents 1d01ede + 8bf6b90 commit b8b54ac

19 files changed

+193
-70
lines changed

.github/workflows/tests.yml

+2-3
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,7 @@ jobs:
132132
run: echo '127.0.0.1 mariadb postgres mongodb' | sudo tee -a /etc/hosts
133133

134134
- name: Run tests
135-
# In phpunit.xml.dist, tests annotated with "@group mysql" are excluded, revert this
136135
# Run tests twice to ensure that tests are idempotent even if database caching is enabled
137136
run: |
138-
php ./vendor/bin/phpunit --testdox --exclude-group ""
139-
php ./vendor/bin/phpunit --testdox --exclude-group ""
137+
php ./vendor/bin/phpunit --testdox --process-isolation
138+
php ./vendor/bin/phpunit --testdox --process-isolation

composer.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"doctrine/doctrine-fixtures-bundle": "^3.4.4 || ^4.0",
3333
"doctrine/mongodb-odm": "^2.2",
3434
"doctrine/mongodb-odm-bundle": "^4.2.1 || ^5.0",
35-
"doctrine/orm": "^2.14",
35+
"doctrine/orm": "^2.14 || ^3.0",
3636
"doctrine/phpcr-bundle": "^2.4.3 || ^3.0",
3737
"doctrine/phpcr-odm": "^1.7.2 || ^2.0",
3838
"jackalope/jackalope-doctrine-dbal": "^1.10.1 || ^2.0",
@@ -48,7 +48,7 @@
4848
"doctrine/annotations": "<1.13.1 || >=3.0",
4949
"doctrine/dbal": "<2.13.1 || ~3.0.0 || >=4.0",
5050
"doctrine/mongodb-odm": "<2.2 || >=3.0",
51-
"doctrine/orm": "<2.14 || >=3.0"
51+
"doctrine/orm": "<2.14 || >=4.0"
5252
},
5353
"suggest": {
5454
"doctrine/dbal": "Required when using the fixture loading functionality with an ORM and SQLite",

doc/contributing.md

+33-1
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,40 @@ Install the dependencies with composer:
1818
docker-compose exec php-fpm composer install
1919
```
2020

21+
Install the lowest dependencies with composer:
22+
23+
```bash
24+
docker-compose exec php-fpm composer update --prefer-lowest
25+
```
26+
2127
Now you can execute the tests with the following command:
2228

2329
```bash
24-
docker-compose exec php-fpm ./vendor/bin/phpunit --exclude-group ""
30+
docker-compose exec php-fpm ./vendor/bin/phpunit --process-isolation
31+
```
32+
33+
If one test fails, run it without the `--process-isolation` option
34+
35+
```bash
36+
docker-compose exec php-fpm ./vendor/bin/phpunit tests/Test/ConfigMongodbTest.php
37+
```
38+
39+
You can also use the `--filter` option to run tests matching a class and name:
40+
41+
```bash
42+
docker-compose exec php-fpm ./vendor/bin/phpunit --process-isolation --filter=ConfigSqliteTest::testAppendFixtures
43+
```
44+
45+
You can also use the `--filter` option to run tests matching a name:
46+
47+
```bash
48+
docker-compose exec php-fpm ./vendor/bin/phpunit --process-isolation --filter=testAppendFixtures
49+
```
50+
51+
## Delete the cache
52+
53+
If you change the version of PHP or dependencies, the caches may cause issues, they can be deleted:
54+
55+
```bash
56+
docker-compose exec php-fpm bash -c "rm -rf tests/App*/var/cache/*"
2557
```

phpunit.xml.dist

-7
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,4 @@
1818
<directory>tests/Test</directory>
1919
</testsuite>
2020
</testsuites>
21-
22-
<groups>
23-
<exclude>
24-
<group>mysql</group>
25-
<group>pgsql</group>
26-
</exclude>
27-
</groups>
2821
</phpunit>

tests/App/DataFixtures/ORM/LoadDependentUserData.php

+6-5
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,18 @@ class LoadDependentUserData extends AbstractFixture implements DependentFixtureI
2525
*/
2626
public function load(ObjectManager $manager): void
2727
{
28-
/** @var User $user */
29-
$user = clone $this->getReference('user');
30-
28+
$user = new User();
3129
$user->setId(3);
30+
$user->setName('alice bar');
31+
$user->setEmail('[email protected]');
3232

3333
$manager->persist($user);
3434
$manager->flush();
3535

36-
$user = clone $this->getReference('user');
37-
36+
$user = new User();
3837
$user->setId(4);
38+
$user->setName('eve bar');
39+
$user->setEmail('[email protected]');
3940

4041
$manager->persist($user);
4142
$manager->flush();

tests/App/DataFixtures/ORM/LoadDependentUserWithServiceData.php

+6-5
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,19 @@ public function __construct(DummyService $dummyService)
3434
*/
3535
public function load(ObjectManager $manager): void
3636
{
37-
/** @var User $user */
38-
$user = clone $this->getReference('serviceUser');
39-
37+
$user = new User();
4038
$user->setId(3);
39+
$user->setName('alice bar');
40+
$user->setEmail('[email protected]');
4141
$user->setDummyText($this->dummyService->getText());
4242

4343
$manager->persist($user);
4444
$manager->flush();
4545

46-
$user = clone $this->getReference('serviceUser');
47-
46+
$user = new User();
4847
$user->setId(4);
48+
$user->setName('eve bar');
49+
$user->setEmail('[email protected]');
4950

5051
$manager->persist($user);
5152
$manager->flush();

tests/App/DataFixtures/ORM/LoadSecondUserData.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ class LoadSecondUserData extends AbstractFixture
2525
public function load(ObjectManager $manager): void
2626
{
2727
$user = new User();
28-
$user->setName('bar foo');
29-
$user->setEmail('[email protected]');
28+
$user->setId(3);
29+
$user->setName('alice bar');
30+
$user->setEmail('[email protected]');
3031

3132
$manager->persist($user);
3233
$manager->flush();

tests/App/DataFixtures/ORM/LoadUserData.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,10 @@ public function load(ObjectManager $manager): void
3434

3535
$this->addReference('user', $user);
3636

37-
$user = clone $this->getReference('user');
38-
37+
$user = new User();
3938
$user->setId(2);
39+
$user->setName('bob bar');
40+
$user->setEmail('[email protected]');
4041

4142
$manager->persist($user);
4243
$manager->flush();

tests/App/DataFixtures/ORM/LoadUserDataInGroup.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,18 @@ public function load(ObjectManager $manager): void
3636
$manager->persist($user);
3737
$manager->flush();
3838

39-
$this->addReference('groupUser', $user);
40-
41-
$user = clone $this->getReference('groupUser');
42-
39+
$user = new User();
4340
$user->setId(2);
41+
$user->setName('bob bar');
42+
$user->setEmail('[email protected]');
4443

4544
$manager->persist($user);
4645
$manager->flush();
4746

48-
$user = clone $this->getReference('groupUser');
49-
47+
$user = new User();
5048
$user->setId(3);
49+
$user->setName('alice bar');
50+
$user->setEmail('[email protected]');
5151

5252
$manager->persist($user);
5353
$manager->flush();

tests/App/DataFixtures/ORM/LoadUserWithServiceData.php

+3-4
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,10 @@ public function load(ObjectManager $manager): void
4545
$manager->persist($user);
4646
$manager->flush();
4747

48-
$this->addReference('serviceUser', $user);
49-
50-
$user = clone $this->getReference('serviceUser');
51-
48+
$user = new User();
5249
$user->setId(2);
50+
$user->setName('bob bar');
51+
$user->setEmail('[email protected]');
5352

5453
$manager->persist($user);
5554
$manager->flush();

tests/App/DataFixtures/ORM/user.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
Liip\Acme\Tests\App\Entity\User:
2-
id{1..10}:
2+
user_id_{1..10}:
3+
id: <current()>
34
name: <name()>
45
email: <email()>
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
Liip\Acme\Tests\App\Entity\User:
2-
id{1..10}:
2+
custom_user_id_{11..20}:
3+
id: <current()>
34
name: <foo('a string')>
45
email: <email()>

tests/Test/ConfigMysqlCacheDbTest.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@
2424
* they are disabled by default (see phpunit.xml.dist).
2525
*
2626
* In order to run them, you have to set the MySQL connection
27-
* parameters in the Tests/AppConfigMysql/config.yml file and
28-
* add “--exclude-group ""” when running PHPUnit.
27+
* parameters in the Tests/AppConfigMysql/config.yml file.
2928
*
3029
* Use Tests/AppConfigMysql/AppConfigMysqlKernelCacheDb.php instead of
3130
* Tests/App/AppKernel.php.

tests/Test/ConfigMysqlKeepDatabaseAndSchemaTest.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@
2323
* they are disabled by default (see phpunit.xml.dist).
2424
*
2525
* In order to run them, you have to set the MySQL connection
26-
* parameters in the Tests/AppConfigMysql/config.yml file and
27-
* add “--exclude-group ""” when running PHPUnit.
26+
* parameters in the Tests/AppConfigMysql/config.yml file.
2827
*
2928
* Use Tests/AppConfigMysqlKeepDatabaseAndSchema/AppConfigMysqlKernelKeepDatabaseAndSchema.php instead of
3029
* Tests/App/AppKernel.php.

tests/Test/ConfigMysqlTest.php

+45-7
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ class_alias('\Doctrine\Persistence\ObjectManager', '\Doctrine\Common\Persistence
3737
* they are disabled by default (see phpunit.xml.dist).
3838
*
3939
* In order to run them, you have to set the MySQL connection
40-
* parameters in the Tests/AppConfigMysql/config.yml file and
41-
* add “--exclude-group ""” when running PHPUnit.
40+
* parameters in the Tests/AppConfigMysql/config.yml file.
4241
*
4342
* Use Tests/AppConfigMysql/AppConfigMysqlKernel.php instead of
4443
* Tests/App/AppKernel.php.
@@ -95,7 +94,7 @@ public function testLoadEmptyFixtures(): void
9594
/**
9695
* @group mysql
9796
*/
98-
public function testLoadFixtures(int $firstUserId = 1): void
97+
public function testLoadFixtures(): void
9998
{
10099
$fixtures = $this->databaseTool->loadFixtures([
101100
'Liip\Acme\Tests\App\DataFixtures\ORM\LoadUserData',
@@ -135,7 +134,7 @@ public function testLoadFixtures(int $firstUserId = 1): void
135134
/**
136135
* @group mysql
137136
*/
138-
public function testAppendFixtures(int $firstUserId = 1, int $thirdUserId = 3): void
137+
public function testAppendFixtures(): void
139138
{
140139
$this->databaseTool->loadFixtures([
141140
'Liip\Acme\Tests\App\DataFixtures\ORM\LoadUserData',
@@ -169,17 +168,31 @@ public function testAppendFixtures(int $firstUserId = 1, int $thirdUserId = 3):
169168
$user1->getEmail()
170169
);
171170

172-
/** @var User $user */
171+
/** @var User $user2 */
172+
$user2 = $this->userRepository
173+
->findOneBy([
174+
'email' => '[email protected]',
175+
])
176+
;
177+
178+
$this->assertNotNull($user2);
179+
180+
$this->assertSame(
181+
182+
$user2->getEmail()
183+
);
184+
185+
/** @var User $user3 */
173186
$user3 = $this->userRepository
174187
->findOneBy([
175-
'email' => 'bar@foo.com',
188+
'email' => 'alice@bar.com',
176189
])
177190
;
178191

179192
$this->assertNotNull($user3);
180193

181194
$this->assertSame(
182-
'bar@foo.com',
195+
'alice@bar.com',
183196
$user3->getEmail()
184197
);
185198
}
@@ -262,6 +275,8 @@ public function testLoadFixturesAndPurge(): void
262275
$users
263276
);
264277

278+
$this->getTestContainer()->get('doctrine')->getManager()->clear();
279+
265280
// Reload fixtures
266281
$this->databaseTool->loadFixtures([
267282
'Liip\Acme\Tests\App\DataFixtures\ORM\LoadUserData',
@@ -331,6 +346,29 @@ public function testLoadFixturesFiles(): void
331346
$this->assertInstanceOf(User::class, $user);
332347
}
333348

349+
/**
350+
* Load fixture which has a dependency.
351+
*/
352+
public function testLoadDependentFixtures(): void
353+
{
354+
$fixtures = $this->databaseTool->loadFixtures([
355+
'Liip\Acme\Tests\App\DataFixtures\ORM\LoadDependentUserData',
356+
]);
357+
358+
$this->assertInstanceOf(
359+
'Doctrine\Common\DataFixtures\Executor\ORMExecutor',
360+
$fixtures
361+
);
362+
363+
$users = $this->userRepository->findAll();
364+
365+
// The two files with fixtures have been loaded, there are 4 users.
366+
$this->assertCount(
367+
4,
368+
$users
369+
);
370+
}
371+
334372
protected static function getKernelClass(): string
335373
{
336374
return AppConfigMysqlKernel::class;

tests/Test/ConfigMysqlUrlTest.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@
2222
* they are disabled by default (see phpunit.xml.dist).
2323
*
2424
* In order to run them, you have to set the MySQL connection
25-
* parameters in the Tests/AppConfigMysql/config.yml file and
26-
* add “--exclude-group ""” when running PHPUnit.
25+
* parameters in the Tests/AppConfigMysql/config.yml file.
2726
*
2827
* Use Tests/AppConfigMysql/AppConfigMysqlUrlKernel.php instead of
2928
* Tests/App/AppKernel.php.

tests/Test/ConfigPgsqlTest.php

+24-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@
2323
* they are disabled by default (see phpunit.xml.dist).
2424
*
2525
* In order to run them, you have to set the PostgreSQL connection
26-
* parameters in the Tests/AppConfigPgsql/config.yml file and
27-
* add “--exclude-group ""” when running PHPUnit.
26+
* parameters in the Tests/AppConfigPgsql/config.yml file.
2827
*
2928
* Use Tests/AppConfigPgsql/AppConfigPgsqlKernel.php instead of
3029
* Tests/App/AppKernel.php.
@@ -38,6 +37,29 @@
3837
*/
3938
class ConfigPgsqlTest extends ConfigMysqlTest
4039
{
40+
/**
41+
* Load fixture which has a dependency.
42+
*/
43+
public function testLoadDependentFixtures(): void
44+
{
45+
$fixtures = $this->databaseTool->loadFixtures([
46+
'Liip\Acme\Tests\App\DataFixtures\ORM\LoadDependentUserData',
47+
]);
48+
49+
$this->assertInstanceOf(
50+
'Doctrine\Common\DataFixtures\Executor\ORMExecutor',
51+
$fixtures
52+
);
53+
54+
$users = $this->userRepository->findAll();
55+
56+
// The two files with fixtures have been loaded, there are 4 users.
57+
$this->assertCount(
58+
4,
59+
$users
60+
);
61+
}
62+
4163
public function testToolType(): void
4264
{
4365
$this->assertInstanceOf(ORMDatabaseTool::class, $this->databaseTool);

0 commit comments

Comments
 (0)