diff --git a/CHANGELOG.md b/CHANGELOG.md index 1e6e387a..18543135 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ This new major version introduces a number of breaking changes; see the [upgrade * Added support for Symfony 4 * Added `.gitattributes` to make package slimmer * Applied `declare(strict_types=1)` everywhere + * Added append fixture feature on `LoadFixtues` ### Changed * Switched to PSR-4 dir structure with `src` and `tests` subfolders diff --git a/README.md b/README.md index 006fbbf3..b1dd0701 100644 --- a/README.md +++ b/README.md @@ -379,7 +379,26 @@ Tips for Fixture Loading Tests } ``` - 6. This bundle uses Doctrine ORM by default. If you are using another driver just + 6. If you want to append fixtures instead of clean database and load them, you have + to consider use the second parameter $append with value true. + + ```php + use Liip\FunctionalTestBundle\Test\WebTestCase; + + class MyControllerTest extends WebTestCase + { + public function testIndex() + { + $this->loadFixtures(array( + 'Me\MyBundle\DataFixtures\ORM\LoadAnotherObjectData', + true + )); + // ... + } + } + ``` + + 7. This bundle uses Doctrine ORM by default. If you are using another driver just specify the service id of the registry manager: ```php @@ -393,7 +412,7 @@ Tips for Fixture Loading Tests 'Me\MyBundle\DataFixtures\MongoDB\LoadData' ); - $this->loadFixtures($fixtures, null, 'doctrine_mongodb'); + $this->loadFixtures($fixtures, false, null, 'doctrine_mongodb'); $client = $this->createClient(); } diff --git a/UPGRADE-2.0.md b/UPGRADE-2.0.md index 47fcaeba..5ba00be7 100644 --- a/UPGRADE-2.0.md +++ b/UPGRADE-2.0.md @@ -8,3 +8,7 @@ This is the list of actions that you need to take when upgrading this bundle fro composer remove --dev nelmio/alice composer require --dev liip/functional-test-bundle "~2.0" ``` + + * The interface of `LoadFixtures` had to be changed to allow append fixtures. The main difference is it had been added + a boolean second parameter. You will have to add it to `false` if you had changed the default manager, driver + or purge mode. diff --git a/src/Test/WebTestCase.php b/src/Test/WebTestCase.php index c762c2fd..dac17b08 100644 --- a/src/Test/WebTestCase.php +++ b/src/Test/WebTestCase.php @@ -309,13 +309,14 @@ protected function isBackupUpToDate(array $classNames, string $backup): bool * class path. * * @param array $classNames List of fully qualified class names of fixtures to load + * @param bool $append * @param string $omName The name of object manager to use * @param string $registryName The service id of manager registry to use * @param int $purgeMode Sets the ORM purge mode * * @return null|AbstractExecutor */ - protected function loadFixtures(array $classNames = [], ?string $omName = null, string $registryName = 'doctrine', ?int $purgeMode = null): ?AbstractExecutor + protected function loadFixtures(array $classNames = [], bool $append = false, ?string $omName = null, string $registryName = 'doctrine', ?int $purgeMode = null): ?AbstractExecutor { $container = $this->getContainer(); /** @var ManagerRegistry $registry */ @@ -417,7 +418,9 @@ protected function loadFixtures(array $classNames = [], ?string $omName = null, } $executor->setReferenceRepository($referenceRepository); - $executor->purge(); + if (false === $append) { + $executor->purge(); + } } $loader = $this->getFixtureLoader($container, $classNames); @@ -456,7 +459,7 @@ private function cleanDatabase(ManagerRegistry $registry, EntityManager $om, ?st $connection->query('SET FOREIGN_KEY_CHECKS=0'); } - $this->loadFixtures([], $omName, $registryName, $purgeMode); + $this->loadFixtures([], false, $omName, $registryName, $purgeMode); if ($mysql) { $connection->query('SET FOREIGN_KEY_CHECKS=1'); diff --git a/tests/App/DataFixtures/ORM/LoadSecondUserData.php b/tests/App/DataFixtures/ORM/LoadSecondUserData.php new file mode 100644 index 00000000..ad9958eb --- /dev/null +++ b/tests/App/DataFixtures/ORM/LoadSecondUserData.php @@ -0,0 +1,54 @@ + + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +namespace Liip\FunctionalTestBundle\Tests\App\DataFixtures\ORM; + +use Doctrine\Common\DataFixtures\AbstractFixture; +use Doctrine\Common\DataFixtures\FixtureInterface; +use Doctrine\Common\Persistence\ObjectManager; +use Liip\FunctionalTestBundle\Tests\App\Entity\User; +use Symfony\Component\DependencyInjection\ContainerInterface; + +class LoadSecondUserData extends AbstractFixture implements FixtureInterface +{ + /** + * @var ContainerInterface + */ + private $container; + + /** + * {@inheritdoc} + */ + public function setContainer(ContainerInterface $container = null): void + { + $this->container = $container; + } + + /** + * {@inheritdoc} + */ + public function load(ObjectManager $manager): void + { + /** @var \Liip\FunctionalTestBundle\Tests\App\Entity\User $user */ + $user = new User(); + $user->setName('bar foo'); + $user->setEmail('bar@foo.com'); + $user->setPassword('12341234'); + $user->setAlgorithm('plaintext'); + $user->setEnabled(true); + $user->setConfirmationToken(null); + + $manager->persist($user); + $manager->flush(); + } +} diff --git a/tests/Test/WebTestCaseConfigMysqlTest.php b/tests/Test/WebTestCaseConfigMysqlTest.php index 74cb0c21..0827ce70 100644 --- a/tests/Test/WebTestCaseConfigMysqlTest.php +++ b/tests/Test/WebTestCaseConfigMysqlTest.php @@ -118,6 +118,55 @@ public function testLoadFixtures(): void ); } + /** + * @group mysql + */ + public function testAppendFixtures(): void + { + $this->loadFixtures([ + 'Liip\FunctionalTestBundle\Tests\App\DataFixtures\ORM\LoadUserData', + ]); + + $this->loadFixtures( + ['Liip\FunctionalTestBundle\Tests\App\DataFixtures\ORM\LoadSecondUserData'], + true + ); + + // Load data from database + $em = $this->getContainer() + ->get('doctrine.orm.entity_manager'); + + /** @var \Liip\FunctionalTestBundle\Tests\App\Entity\User $user */ + $user = $em->getRepository('LiipFunctionalTestBundle:User') + ->findOneBy([ + 'id' => 1, + ]); + + $this->assertSame( + 'foo@bar.com', + $user->getEmail() + ); + + $this->assertTrue( + $user->getEnabled() + ); + + /** @var \Liip\FunctionalTestBundle\Tests\App\Entity\User $user */ + $user = $em->getRepository('LiipFunctionalTestBundle:User') + ->findOneBy([ + 'id' => 3, + ]); + + $this->assertSame( + 'bar@foo.com', + $user->getEmail() + ); + + $this->assertTrue( + $user->getEnabled() + ); + } + /** * Data fixtures and purge. * @@ -148,7 +197,7 @@ public function testLoadFixturesAndExcludeFromPurge(): void ); $this->setExcludedDoctrineTables(['liip_user']); - $this->loadFixtures([], null, 'doctrine', 2); + $this->loadFixtures([], false, null, 'doctrine', 2); // The exclusion from purge worked, the user table is still alive and well. $this->assertSame( @@ -188,7 +237,7 @@ public function testLoadFixturesAndPurge(): void ); // 1 → ORMPurger::PURGE_MODE_DELETE - $this->loadFixtures([], null, 'doctrine', 1); + $this->loadFixtures([], false, null, 'doctrine', 1); // The purge worked: there is no user. $this->assertSame( @@ -210,7 +259,7 @@ public function testLoadFixturesAndPurge(): void ); // 2 → ORMPurger::PURGE_MODE_TRUNCATE - $this->loadFixtures([], null, 'doctrine', 2); + $this->loadFixtures([], false, null, 'doctrine', 2); // The purge worked: there is no user. $this->assertSame( diff --git a/tests/Test/WebTestCaseConfigPhpcrTest.php b/tests/Test/WebTestCaseConfigPhpcrTest.php index 290b5eba..5b2e6b32 100644 --- a/tests/Test/WebTestCaseConfigPhpcrTest.php +++ b/tests/Test/WebTestCaseConfigPhpcrTest.php @@ -60,7 +60,7 @@ public function testLoadFixturesPhPCr(): void { $fixtures = $this->loadFixtures([ 'Liip\FunctionalTestBundle\Tests\AppConfigPhpcr\DataFixtures\PHPCR\LoadTaskData', - ], null, 'doctrine_phpcr'); + ], false, null, 'doctrine_phpcr'); $this->assertInstanceOf( 'Doctrine\Bundle\PHPCRBundle\DataFixtures\PHPCRExecutor',