diff --git a/composer.json b/composer.json index 3e333caa..d47e5e9e 100644 --- a/composer.json +++ b/composer.json @@ -29,7 +29,7 @@ "doctrine/doctrine-bundle": "^2.11", "doctrine/doctrine-fixtures-bundle": "^3.5.1 || ^4.0", "doctrine/mongodb-odm-bundle": "^4.4 || ^5.0", - "doctrine/orm": "^2.7", + "doctrine/orm": "^2.14 || ^3.0", "doctrine/mongodb-odm": "^2.5", "monolog/monolog": "^1.25.1 || ^2.0 || ^3.0", "phpunit/phpunit": "^10.5.11 || ^11.0.4", @@ -42,7 +42,7 @@ "doctrine/annotations": "<1.13.1 || >=3.0", "doctrine/dbal": "<2.13.1 || ~3.0.0 || >=4.0", "doctrine/mongodb-odm": "<2.2 || >=3.0", - "doctrine/orm": "<2.14 || >=3.0" + "doctrine/orm": "<2.14 || >=4.0" }, "suggest": { "doctrine/dbal": "Required when using the fixture loading functionality with an ORM and SQLite", diff --git a/doc/contributing.md b/doc/contributing.md index 7607992c..c8c133b1 100644 --- a/doc/contributing.md +++ b/doc/contributing.md @@ -36,6 +36,18 @@ If one test fails, run it without the `--process-isolation` option docker-compose exec php-fpm ./vendor/bin/phpunit tests/Test/ConfigMongodbTest.php ``` +You can also use the `--filter` option to run tests matching a class and name: + +```bash +docker-compose exec php-fpm ./vendor/bin/phpunit --process-isolation --filter=ConfigSqliteTest::testAppendFixtures +``` + +You can also use the `--filter` option to run tests matching a name: + +```bash +docker-compose exec php-fpm ./vendor/bin/phpunit --process-isolation --filter=testAppendFixtures +``` + ## Delete the cache If you change the version of PHP or dependencies, the caches may cause issues, they can be deleted: diff --git a/tests/App/DataFixtures/ORM/LoadDependentUserData.php b/tests/App/DataFixtures/ORM/LoadDependentUserData.php index c866cd76..9da78c7e 100644 --- a/tests/App/DataFixtures/ORM/LoadDependentUserData.php +++ b/tests/App/DataFixtures/ORM/LoadDependentUserData.php @@ -22,17 +22,18 @@ class LoadDependentUserData extends AbstractFixture implements DependentFixtureI { public function load(ObjectManager $manager): void { - /** @var User $user */ - $user = clone $this->getReference('user'); - + $user = new User(); $user->setId(3); + $user->setName('alice bar'); + $user->setEmail('alice@bar.com'); $manager->persist($user); $manager->flush(); - $user = clone $this->getReference('user'); - + $user = new User(); $user->setId(4); + $user->setName('eve bar'); + $user->setEmail('eve@bar.com'); $manager->persist($user); $manager->flush(); diff --git a/tests/App/DataFixtures/ORM/LoadDependentUserWithServiceData.php b/tests/App/DataFixtures/ORM/LoadDependentUserWithServiceData.php index a0e36061..caff769f 100644 --- a/tests/App/DataFixtures/ORM/LoadDependentUserWithServiceData.php +++ b/tests/App/DataFixtures/ORM/LoadDependentUserWithServiceData.php @@ -31,18 +31,19 @@ public function __construct(DummyService $dummyService) public function load(ObjectManager $manager): void { - /** @var User $user */ - $user = clone $this->getReference('serviceUser'); - + $user = new User(); $user->setId(3); + $user->setName('alice bar'); + $user->setEmail('alice@bar.com'); $user->setDummyText($this->dummyService->getText()); $manager->persist($user); $manager->flush(); - $user = clone $this->getReference('serviceUser'); - + $user = new User(); $user->setId(4); + $user->setName('eve bar'); + $user->setEmail('eve@bar.com'); $manager->persist($user); $manager->flush(); diff --git a/tests/App/DataFixtures/ORM/LoadSecondUserData.php b/tests/App/DataFixtures/ORM/LoadSecondUserData.php index 6e76f556..afa15472 100644 --- a/tests/App/DataFixtures/ORM/LoadSecondUserData.php +++ b/tests/App/DataFixtures/ORM/LoadSecondUserData.php @@ -22,8 +22,9 @@ class LoadSecondUserData extends AbstractFixture public function load(ObjectManager $manager): void { $user = new User(); - $user->setName('bar foo'); - $user->setEmail('bar@foo.com'); + $user->setId(3); + $user->setName('alice bar'); + $user->setEmail('alice@bar.com'); $manager->persist($user); $manager->flush(); diff --git a/tests/App/DataFixtures/ORM/LoadUserData.php b/tests/App/DataFixtures/ORM/LoadUserData.php index 59b30983..c440d5ed 100644 --- a/tests/App/DataFixtures/ORM/LoadUserData.php +++ b/tests/App/DataFixtures/ORM/LoadUserData.php @@ -31,9 +31,10 @@ public function load(ObjectManager $manager): void $this->addReference('user', $user); - $user = clone $this->getReference('user'); - + $user = new User(); $user->setId(2); + $user->setName('bob bar'); + $user->setEmail('bob@bar.com'); $manager->persist($user); $manager->flush(); diff --git a/tests/App/DataFixtures/ORM/LoadUserDataInGroup.php b/tests/App/DataFixtures/ORM/LoadUserDataInGroup.php index 9baf023b..63bfa9f6 100644 --- a/tests/App/DataFixtures/ORM/LoadUserDataInGroup.php +++ b/tests/App/DataFixtures/ORM/LoadUserDataInGroup.php @@ -33,18 +33,18 @@ public function load(ObjectManager $manager): void $manager->persist($user); $manager->flush(); - $this->addReference('groupUser', $user); - - $user = clone $this->getReference('groupUser'); - + $user = new User(); $user->setId(2); + $user->setName('bob bar'); + $user->setEmail('bob@bar.com'); $manager->persist($user); $manager->flush(); - $user = clone $this->getReference('groupUser'); - + $user = new User(); $user->setId(3); + $user->setName('alice bar'); + $user->setEmail('alice@bar.com'); $manager->persist($user); $manager->flush(); diff --git a/tests/App/DataFixtures/ORM/LoadUserWithServiceData.php b/tests/App/DataFixtures/ORM/LoadUserWithServiceData.php index 0ef66829..9c42803d 100644 --- a/tests/App/DataFixtures/ORM/LoadUserWithServiceData.php +++ b/tests/App/DataFixtures/ORM/LoadUserWithServiceData.php @@ -42,11 +42,10 @@ public function load(ObjectManager $manager): void $manager->persist($user); $manager->flush(); - $this->addReference('serviceUser', $user); - - $user = clone $this->getReference('serviceUser'); - + $user = new User(); $user->setId(2); + $user->setName('bob bar'); + $user->setEmail('bob@bar.com'); $manager->persist($user); $manager->flush(); diff --git a/tests/App/DataFixtures/ORM/user.yml b/tests/App/DataFixtures/ORM/user.yml index 83a84573..ad0bf96d 100644 --- a/tests/App/DataFixtures/ORM/user.yml +++ b/tests/App/DataFixtures/ORM/user.yml @@ -1,4 +1,5 @@ Liip\Acme\Tests\App\Entity\User: - id{1..10}: + user_id_{1..10}: + id: name: email: diff --git a/tests/App/DataFixtures/ORM/user_with_custom_provider.yml b/tests/App/DataFixtures/ORM/user_with_custom_provider.yml index ac10e2f3..1fe7ab66 100644 --- a/tests/App/DataFixtures/ORM/user_with_custom_provider.yml +++ b/tests/App/DataFixtures/ORM/user_with_custom_provider.yml @@ -1,4 +1,5 @@ Liip\Acme\Tests\App\Entity\User: - id{1..10}: + custom_user_id_{11..20}: + id: name: email: diff --git a/tests/Test/ConfigMysqlTest.php b/tests/Test/ConfigMysqlTest.php index e7b055a9..9acf6d60 100644 --- a/tests/Test/ConfigMysqlTest.php +++ b/tests/Test/ConfigMysqlTest.php @@ -74,7 +74,7 @@ public function testLoadEmptyFixtures(): void ); } - public function testLoadFixtures(int $firstUserId = 1): void + public function testLoadFixtures(): void { $fixtures = $this->databaseTool->loadFixtures([ 'Liip\Acme\Tests\App\DataFixtures\ORM\LoadUserData', @@ -111,7 +111,7 @@ public function testLoadFixtures(int $firstUserId = 1): void ); } - public function testAppendFixtures(int $firstUserId = 1, int $thirdUserId = 3): void + public function testAppendFixtures(): void { $this->databaseTool->loadFixtures([ 'Liip\Acme\Tests\App\DataFixtures\ORM\LoadUserData', @@ -145,17 +145,31 @@ public function testAppendFixtures(int $firstUserId = 1, int $thirdUserId = 3): $user1->getEmail() ); - /** @var User $user */ + /** @var User $user2 */ + $user2 = $this->userRepository + ->findOneBy([ + 'email' => 'alice@bar.com', + ]) + ; + + $this->assertNotNull($user2); + + $this->assertSame( + 'alice@bar.com', + $user2->getEmail() + ); + + /** @var User $user3 */ $user3 = $this->userRepository ->findOneBy([ - 'email' => 'bar@foo.com', + 'email' => 'alice@bar.com', ]) ; $this->assertNotNull($user3); $this->assertSame( - 'bar@foo.com', + 'alice@bar.com', $user3->getEmail() ); } @@ -236,6 +250,8 @@ public function testLoadFixturesAndPurge(): void $users ); + $this->getTestContainer()->get('doctrine')->getManager()->clear(); + // Reload fixtures $this->databaseTool->loadFixtures([ 'Liip\Acme\Tests\App\DataFixtures\ORM\LoadUserData', @@ -303,6 +319,29 @@ public function testLoadFixturesFiles(): void $this->assertInstanceOf(User::class, $user); } + /** + * Load fixture which has a dependency. + */ + public function testLoadDependentFixtures(): void + { + $fixtures = $this->databaseTool->loadFixtures([ + 'Liip\Acme\Tests\App\DataFixtures\ORM\LoadDependentUserData', + ]); + + $this->assertInstanceOf( + 'Doctrine\Common\DataFixtures\Executor\ORMExecutor', + $fixtures + ); + + $users = $this->userRepository->findAll(); + + // The two files with fixtures have been loaded, there are 4 users. + $this->assertCount( + 4, + $users + ); + } + protected static function getKernelClass(): string { return AppConfigMysqlKernel::class; diff --git a/tests/Test/ConfigPgsqlTest.php b/tests/Test/ConfigPgsqlTest.php index 0d8f1853..4c7f3509 100644 --- a/tests/Test/ConfigPgsqlTest.php +++ b/tests/Test/ConfigPgsqlTest.php @@ -34,6 +34,29 @@ #[PreserveGlobalState(false)] class ConfigPgsqlTest extends ConfigMysqlTest { + /** + * Load fixture which has a dependency. + */ + public function testLoadDependentFixtures(): void + { + $fixtures = $this->databaseTool->loadFixtures([ + 'Liip\Acme\Tests\App\DataFixtures\ORM\LoadDependentUserData', + ]); + + $this->assertInstanceOf( + 'Doctrine\Common\DataFixtures\Executor\ORMExecutor', + $fixtures + ); + + $users = $this->userRepository->findAll(); + + // The two files with fixtures have been loaded, there are 4 users. + $this->assertCount( + 4, + $users + ); + } + public function testToolType(): void { $this->assertInstanceOf(ORMDatabaseTool::class, $this->databaseTool); diff --git a/tests/Test/ConfigSqliteTest.php b/tests/Test/ConfigSqliteTest.php index 15fd915f..8f5b7568 100644 --- a/tests/Test/ConfigSqliteTest.php +++ b/tests/Test/ConfigSqliteTest.php @@ -165,7 +165,7 @@ public function loadAllFixtures(): void $repository ); - $users = $this->userRepository->findAll(); + $users = $repository->findAll(); // The fixture group myGroup contains 3 users $this->assertCount( @@ -188,7 +188,7 @@ public function loadAllFixtures(): void $repository ); - $users = $this->userRepository->findAll(); + $users = $repository->findAll(); // Loading all fixtures results in 12 users. $this->assertCount( @@ -209,8 +209,17 @@ public function testAppendFixtures(): void ); // Load data from database - /** @var User $user */ - $user = $this->userRepository + $users = $this->userRepository->findAll(); + + // Check that there are 3 users. + $this->assertCount( + 3, + $users + ); + + // Load data from database + /** @var User $user1 */ + $user1 = $this->userRepository ->findOneBy([ 'id' => 1, ]) @@ -218,19 +227,31 @@ public function testAppendFixtures(): void $this->assertSame( 'foo@bar.com', - $user->getEmail() + $user1->getEmail() ); - /** @var User $user */ - $user = $this->userRepository + /** @var User $user2 */ + $user2 = $this->userRepository + ->findOneBy([ + 'id' => 2, + ]) + ; + + $this->assertSame( + 'bob@bar.com', + $user2->getEmail() + ); + + /** @var User $user3 */ + $user3 = $this->userRepository ->findOneBy([ 'id' => 3, ]) ; $this->assertSame( - 'bar@foo.com', - $user->getEmail() + 'alice@bar.com', + $user3->getEmail() ); } @@ -390,8 +411,12 @@ public function testLoadFixturesFilesPaths(): void $fixtures ); + $fixtureId = 'user_id_1'; + + $this->assertArrayHasKey($fixtureId, $fixtures); + /** @var User $user1 */ - $user1 = $fixtures['id1']; + $user1 = $fixtures[$fixtureId]; $this->assertIsString($user1->getEmail()); diff --git a/tests/Test/ConfigTest.php b/tests/Test/ConfigTest.php index ac2aa60f..1097bad2 100644 --- a/tests/Test/ConfigTest.php +++ b/tests/Test/ConfigTest.php @@ -75,27 +75,37 @@ public function testLoadFixturesFilesWithCustomProvider(): void $fixtures ); - /** @var User $user */ - $user = $fixtures['id1']; + $fixtureId = 'user_id_1'; + + $this->assertArrayHasKey($fixtureId, $fixtures); + + /** @var User $user1 */ + $user1 = $fixtures[$fixtureId]; // The custom provider has not been used successfully. $this->assertStringStartsNotWith( 'foo', - $user->getName() + $user1->getName() ); + $this->getTestContainer()->get('doctrine')->getManager()->clear(); + // Load Data Fixtures with custom loader defined in configuration. $fixtures = $this->databaseTool->loadAliceFixture([ '@AcmeBundle/DataFixtures/ORM/user_with_custom_provider.yml', ]); - /** @var User $user */ - $user = $fixtures['id1']; + $fixtureId = 'custom_user_id_11'; + + $this->assertArrayHasKey($fixtureId, $fixtures); + + /** @var User $user11 */ + $user11 = $fixtures[$fixtureId]; // The custom provider "foo" has been loaded and used successfully. $this->assertSame( 'fooa string', - $user->getName() + $user11->getName() ); }