123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497 |
- <?php
- declare(strict_types=1);
- namespace Doctrine\Bundle\FixturesBundle\Tests;
- use Doctrine\Bundle\FixturesBundle\Command\LoadDataFixturesDoctrineCommand;
- use Doctrine\Bundle\FixturesBundle\DependencyInjection\CompilerPass\FixturesCompilerPass;
- use Doctrine\Bundle\FixturesBundle\DependencyInjection\CompilerPass\PurgerFactoryCompilerPass;
- use Doctrine\Bundle\FixturesBundle\Purger\PurgerFactory;
- use Doctrine\Bundle\FixturesBundle\Tests\Fixtures\FooBundle\DataFixtures\DependentOnRequiredConstructorArgsFixtures;
- use Doctrine\Bundle\FixturesBundle\Tests\Fixtures\FooBundle\DataFixtures\OtherFixtures;
- use Doctrine\Bundle\FixturesBundle\Tests\Fixtures\FooBundle\DataFixtures\RequiredConstructorArgsFixtures;
- use Doctrine\Bundle\FixturesBundle\Tests\Fixtures\FooBundle\DataFixtures\WithDependenciesFixtures;
- use Doctrine\Common\DataFixtures\Loader;
- use Doctrine\Common\DataFixtures\Purger\ORMPurger;
- use Doctrine\Common\EventManager;
- use Doctrine\DBAL\Connection;
- use Doctrine\ORM\EntityManagerInterface;
- use Doctrine\Persistence\ManagerRegistry;
- use LogicException;
- use PHPUnit\Framework\TestCase;
- use RuntimeException;
- use Symfony\Bridge\Doctrine\DataFixtures\ContainerAwareLoader;
- use Symfony\Component\Console\Tester\CommandTester;
- use Symfony\Component\DependencyInjection\Alias;
- use Symfony\Component\DependencyInjection\ContainerBuilder;
- use Symfony\Component\DependencyInjection\Definition;
- use function array_map;
- use function get_class;
- use function method_exists;
- class IntegrationTest extends TestCase
- {
- public function testFixturesLoader() : void
- {
- $kernel = new IntegrationTestKernel('dev', true);
- $kernel->addServices(static function (ContainerBuilder $c) : void {
- $c->autowire(OtherFixtures::class)
- ->addTag(FixturesCompilerPass::FIXTURE_TAG);
- $c->autowire(WithDependenciesFixtures::class)
- ->addTag(FixturesCompilerPass::FIXTURE_TAG);
- $c->setAlias('test.doctrine.fixtures.loader', new Alias('doctrine.fixtures.loader', true));
- });
- $kernel->boot();
- $container = $kernel->getContainer();
- /** @var ContainerAwareLoader $loader */
- $loader = $container->get('test.doctrine.fixtures.loader');
- $actualFixtures = $loader->getFixtures();
- $this->assertCount(2, $actualFixtures);
- $actualFixtureClasses = array_map(static function ($fixture) {
- return get_class($fixture);
- }, $actualFixtures);
- $this->assertSame([
- OtherFixtures::class,
- WithDependenciesFixtures::class,
- ], $actualFixtureClasses);
- $this->assertInstanceOf(WithDependenciesFixtures::class, $actualFixtures[1]);
- }
- public function testFixturesLoaderWhenFixtureHasDepdencenyThatIsNotYetLoaded() : void
- {
- // See https://github.com/doctrine/DoctrineFixturesBundle/issues/215
- $kernel = new IntegrationTestKernel('dev', true);
- $kernel->addServices(static function (ContainerBuilder $c) : void {
- $c->autowire(WithDependenciesFixtures::class)
- ->addTag(FixturesCompilerPass::FIXTURE_TAG);
- $c->autowire(OtherFixtures::class)
- ->addTag(FixturesCompilerPass::FIXTURE_TAG);
- $c->setAlias('test.doctrine.fixtures.loader', new Alias('doctrine.fixtures.loader', true));
- });
- $kernel->boot();
- $container = $kernel->getContainer();
- /** @var ContainerAwareLoader $loader */
- $loader = $container->get('test.doctrine.fixtures.loader');
- $actualFixtures = $loader->getFixtures();
- $this->assertCount(2, $actualFixtures);
- $actualFixtureClasses = array_map(static function ($fixture) {
- return get_class($fixture);
- }, $actualFixtures);
- $this->assertSame([
- OtherFixtures::class,
- WithDependenciesFixtures::class,
- ], $actualFixtureClasses);
- $this->assertInstanceOf(WithDependenciesFixtures::class, $actualFixtures[1]);
- }
- public function testExceptionWithDependenciesWithRequiredArguments() : void
- {
- // see https://github.com/doctrine/data-fixtures/pull/274
- // When that is merged, this test will only run when using
- // an older version of that library.
- if (method_exists(Loader::class, 'createFixture')) {
- $this->markTestSkipped();
- }
- $kernel = new IntegrationTestKernel('dev', true);
- $kernel->addServices(static function (ContainerBuilder $c) : void {
- $c->autowire(DependentOnRequiredConstructorArgsFixtures::class)
- ->addTag(FixturesCompilerPass::FIXTURE_TAG);
- $c->autowire(RequiredConstructorArgsFixtures::class)
- ->setArgument(0, 'foo')
- ->addTag(FixturesCompilerPass::FIXTURE_TAG);
- $c->setAlias('test.doctrine.fixtures.loader', new Alias('doctrine.fixtures.loader', true));
- });
- $kernel->boot();
- $container = $kernel->getContainer();
- $this->expectException(LogicException::class);
- $this->expectExceptionMessage('The getDependencies() method returned a class (Doctrine\Bundle\FixturesBundle\Tests\Fixtures\FooBundle\DataFixtures\RequiredConstructorArgsFixtures) that has required constructor arguments. Upgrade to "doctrine/data-fixtures" version 1.3 or higher to support this.');
- /** @var ContainerAwareLoader $loader */
- $loader = $container->get('test.doctrine.fixtures.loader');
- $loader->getFixtures();
- }
- public function testExceptionIfDependentFixtureNotWired() : void
- {
- // only runs on newer versions of doctrine/data-fixtures
- if (! method_exists(Loader::class, 'createFixture')) {
- $this->markTestSkipped();
- }
- $kernel = new IntegrationTestKernel('dev', true);
- $kernel->addServices(static function (ContainerBuilder $c) : void {
- $c->autowire(DependentOnRequiredConstructorArgsFixtures::class)
- ->addTag(FixturesCompilerPass::FIXTURE_TAG);
- $c->setAlias('test.doctrine.fixtures.loader', new Alias('doctrine.fixtures.loader', true));
- });
- $kernel->boot();
- $container = $kernel->getContainer();
- $this->expectException(LogicException::class);
- $this->expectExceptionMessage('The "Doctrine\Bundle\FixturesBundle\Tests\Fixtures\FooBundle\DataFixtures\RequiredConstructorArgsFixtures" fixture class is trying to be loaded, but is not available. Make sure this class is defined as a service and tagged with "doctrine.fixture.orm".');
- /** @var ContainerAwareLoader $loader */
- $loader = $container->get('test.doctrine.fixtures.loader');
- $loader->getFixtures();
- }
- public function testFixturesLoaderWithGroupsOptionViaInterface() : void
- {
- $kernel = new IntegrationTestKernel('dev', true);
- $kernel->addServices(static function (ContainerBuilder $c) : void {
- // has a "staging" group via the getGroups() method
- $c->autowire(OtherFixtures::class)
- ->addTag(FixturesCompilerPass::FIXTURE_TAG);
- // no getGroups() method
- $c->autowire(WithDependenciesFixtures::class)
- ->addTag(FixturesCompilerPass::FIXTURE_TAG);
- $c->setAlias('test.doctrine.fixtures.loader', new Alias('doctrine.fixtures.loader', true));
- });
- $kernel->boot();
- $container = $kernel->getContainer();
- /** @var ContainerAwareLoader $loader */
- $loader = $container->get('test.doctrine.fixtures.loader');
- $actualFixtures = $loader->getFixtures(['staging']);
- $this->assertCount(1, $actualFixtures);
- $actualFixtureClasses = array_map(static function ($fixture) {
- return get_class($fixture);
- }, $actualFixtures);
- $this->assertSame([
- OtherFixtures::class,
- ], $actualFixtureClasses);
- $this->assertInstanceOf(OtherFixtures::class, $actualFixtures[0]);
- }
- public function testFixturesLoaderWithGroupsOptionViaTag() : void
- {
- $kernel = new IntegrationTestKernel('dev', true);
- $kernel->addServices(static function (ContainerBuilder $c) : void {
- // has a "staging" group via the getGroups() method
- $c->autowire(OtherFixtures::class)
- ->addTag(FixturesCompilerPass::FIXTURE_TAG, ['group' => 'group1'])
- ->addTag(FixturesCompilerPass::FIXTURE_TAG, ['group' => 'group2']);
- // no getGroups() method
- $c->autowire(WithDependenciesFixtures::class)
- ->addTag(FixturesCompilerPass::FIXTURE_TAG, ['group' => 'group2']);
- $c->setAlias('test.doctrine.fixtures.loader', new Alias('doctrine.fixtures.loader', true));
- });
- $kernel->boot();
- $container = $kernel->getContainer();
- /** @var ContainerAwareLoader $loader */
- $loader = $container->get('test.doctrine.fixtures.loader');
- $this->assertCount(1, $loader->getFixtures(['staging']));
- $this->assertCount(1, $loader->getFixtures(['group1']));
- $this->assertCount(2, $loader->getFixtures(['group2']));
- $this->assertCount(0, $loader->getFixtures(['group3']));
- }
- public function testLoadFixturesViaGroupWithMissingDependency() : void
- {
- $kernel = new IntegrationTestKernel('dev', true);
- $kernel->addServices(static function (ContainerBuilder $c) : void {
- // has a "staging" group via the getGroups() method
- $c->autowire(OtherFixtures::class)
- ->addTag(FixturesCompilerPass::FIXTURE_TAG);
- // no getGroups() method
- $c->autowire(WithDependenciesFixtures::class)
- ->addTag(FixturesCompilerPass::FIXTURE_TAG);
- $c->setAlias('test.doctrine.fixtures.loader', new Alias('doctrine.fixtures.loader', true));
- });
- $kernel->boot();
- $container = $kernel->getContainer();
- /** @var ContainerAwareLoader $loader */
- $loader = $container->get('test.doctrine.fixtures.loader');
- $this->expectException(RuntimeException::class);
- $this->expectExceptionMessage('Fixture "Doctrine\Bundle\FixturesBundle\Tests\Fixtures\FooBundle\DataFixtures\OtherFixtures" was declared as a dependency for fixture "Doctrine\Bundle\FixturesBundle\Tests\Fixtures\FooBundle\DataFixtures\WithDependenciesFixtures", but it was not included in any of the loaded fixture groups.');
- $loader->getFixtures(['missingDependencyGroup']);
- }
- public function testLoadFixturesViaGroupWithFulfilledDependency() : void
- {
- $kernel = new IntegrationTestKernel('dev', true);
- $kernel->addServices(static function (ContainerBuilder $c) : void {
- // has a "staging" group via the getGroups() method
- $c->autowire(OtherFixtures::class)
- ->addTag(FixturesCompilerPass::FIXTURE_TAG);
- // no getGroups() method
- $c->autowire(WithDependenciesFixtures::class)
- ->addTag(FixturesCompilerPass::FIXTURE_TAG);
- $c->setAlias('test.doctrine.fixtures.loader', new Alias('doctrine.fixtures.loader', true));
- });
- $kernel->boot();
- $container = $kernel->getContainer();
- /** @var ContainerAwareLoader $loader */
- $loader = $container->get('test.doctrine.fixtures.loader');
- $actualFixtures = $loader->getFixtures(['fulfilledDependencyGroup']);
- $this->assertCount(2, $actualFixtures);
- $actualFixtureClasses = array_map(static function ($fixture) {
- return get_class($fixture);
- }, $actualFixtures);
- $this->assertSame([
- OtherFixtures::class,
- WithDependenciesFixtures::class,
- ], $actualFixtureClasses);
- }
- public function testLoadFixturesByShortName() : void
- {
- $kernel = new IntegrationTestKernel('dev', true);
- $kernel->addServices(static function (ContainerBuilder $c) : void {
- // has a "staging" group via the getGroups() method
- $c->autowire(OtherFixtures::class)
- ->addTag(FixturesCompilerPass::FIXTURE_TAG);
- // no getGroups() method
- $c->autowire(WithDependenciesFixtures::class)
- ->addTag(FixturesCompilerPass::FIXTURE_TAG);
- $c->setAlias('test.doctrine.fixtures.loader', new Alias('doctrine.fixtures.loader', true));
- });
- $kernel->boot();
- $container = $kernel->getContainer();
- /** @var ContainerAwareLoader $loader */
- $loader = $container->get('test.doctrine.fixtures.loader');
- $actualFixtures = $loader->getFixtures(['OtherFixtures']);
- $this->assertCount(1, $actualFixtures);
- $actualFixtureClasses = array_map(static function ($fixture) {
- return get_class($fixture);
- }, $actualFixtures);
- $this->assertSame([
- OtherFixtures::class,
- ], $actualFixtureClasses);
- }
- public function testRunCommandWithDefaultPurger() : void
- {
- $kernel = new IntegrationTestKernel('dev', true);
- $kernel->addServices(static function (ContainerBuilder $c) : void {
- // has a "staging" group via the getGroups() method
- $c->autowire(OtherFixtures::class)
- ->addTag(FixturesCompilerPass::FIXTURE_TAG);
- // no getGroups() method
- $c->autowire(WithDependenciesFixtures::class)
- ->addTag(FixturesCompilerPass::FIXTURE_TAG);
- $c->getDefinition('doctrine')
- ->setPublic(true)
- ->setSynthetic(true);
- $c->setAlias('test.doctrine.fixtures.purger.orm_purger_factory', new Alias('doctrine.fixtures.purger.orm_purger_factory', true));
- $c->setAlias('test.doctrine.fixtures_load_command', new Alias('doctrine.fixtures_load_command', true));
- });
- $kernel->boot();
- $container = $kernel->getContainer();
- $em = $this->createConfiguredMock(EntityManagerInterface::class, ['getConnection' => $this->createMock(Connection::class), 'getEventManager' => $this->createMock(EventManager::class)]);
- $registry = $this->createMock(ManagerRegistry::class);
- $registry
- ->expects(self::once())
- ->method('getManager')
- ->with(null)
- ->willReturn($em);
- $container->set('doctrine', $registry);
- $purgerFactory = $this->createMock(PurgerFactory::class);
- $purger = $this->createMock(ORMPurger::class);
- $purgerFactory
- ->expects(self::once())
- ->method('createForEntityManager')
- ->with(null, $em, [])
- ->willReturn($purger);
- $container->set('test.doctrine.fixtures.purger.orm_purger_factory', $purgerFactory);
- /** @var LoadDataFixturesDoctrineCommand $command */
- $command = $container->get('test.doctrine.fixtures_load_command');
- $tester = new CommandTester($command);
- $tester->execute([], ['interactive' => false]);
- }
- public function testRunCommandWithPurgeExclusions() : void
- {
- $kernel = new IntegrationTestKernel('dev', true);
- $kernel->addServices(static function (ContainerBuilder $c) : void {
- // has a "staging" group via the getGroups() method
- $c->autowire(OtherFixtures::class)
- ->addTag(FixturesCompilerPass::FIXTURE_TAG);
- // no getGroups() method
- $c->autowire(WithDependenciesFixtures::class)
- ->addTag(FixturesCompilerPass::FIXTURE_TAG);
- $c->getDefinition('doctrine')
- ->setPublic(true)
- ->setSynthetic(true);
- $c->setAlias('test.doctrine.fixtures.purger.orm_purger_factory', new Alias('doctrine.fixtures.purger.orm_purger_factory', true));
- $c->setAlias('test.doctrine.fixtures_load_command', new Alias('doctrine.fixtures_load_command', true));
- });
- $kernel->boot();
- $container = $kernel->getContainer();
- $em = $this->createConfiguredMock(EntityManagerInterface::class, ['getConnection' => $this->createMock(Connection::class), 'getEventManager' => $this->createMock(EventManager::class)]);
- $registry = $this->createMock(ManagerRegistry::class);
- $registry
- ->expects(self::once())
- ->method('getManager')
- ->with(null)
- ->willReturn($em);
- $container->set('doctrine', $registry);
- $purgerFactory = $this->createMock(PurgerFactory::class);
- $purger = $this->createMock(ORMPurger::class);
- $purgerFactory
- ->expects(self::once())
- ->method('createForEntityManager')
- ->with(null, $em, ['excluded_table'])
- ->willReturn($purger);
- $container->set('test.doctrine.fixtures.purger.orm_purger_factory', $purgerFactory);
- /** @var LoadDataFixturesDoctrineCommand $command */
- $command = $container->get('test.doctrine.fixtures_load_command');
- $tester = new CommandTester($command);
- $tester->execute(['--purge-exclusions' => ['excluded_table']], ['interactive' => false]);
- }
- public function testRunCommandWithCustomPurgerAndCustomEntityManager() : void
- {
- $kernel = new IntegrationTestKernel('dev', true);
- $kernel->addServices(static function (ContainerBuilder $c) : void {
- // has a "staging" group via the getGroups() method
- $c->autowire(OtherFixtures::class)
- ->addTag(FixturesCompilerPass::FIXTURE_TAG);
- // no getGroups() method
- $c->autowire(WithDependenciesFixtures::class)
- ->addTag(FixturesCompilerPass::FIXTURE_TAG);
- $c->getDefinition('doctrine')
- ->setPublic(true)
- ->setSynthetic(true);
- $c->setDefinition('test.doctrine.fixtures.purger.test_factory', (new Definition())
- ->setPublic(true)
- ->setSynthetic(true)
- ->addTag(PurgerFactoryCompilerPass::PURGER_FACTORY_TAG, ['alias' => 'test']));
- $c->setAlias('test.doctrine.fixtures_load_command', new Alias('doctrine.fixtures_load_command', true));
- });
- $kernel->boot();
- $container = $kernel->getContainer();
- $em = $this->createConfiguredMock(EntityManagerInterface::class, ['getConnection' => $this->createMock(Connection::class), 'getEventManager' => $this->createMock(EventManager::class)]);
- $registry = $this->createMock(ManagerRegistry::class);
- $registry
- ->expects(self::once())
- ->method('getManager')
- ->with('alternative')
- ->willReturn($em);
- $container->set('doctrine', $registry);
- $purgerFactory = $this->createMock(PurgerFactory::class);
- $purger = $this->createMock(ORMPurger::class);
- $purgerFactory
- ->expects(self::once())
- ->method('createForEntityManager')
- ->with('alternative', $em, [])
- ->willReturn($purger);
- $container->set('test.doctrine.fixtures.purger.test_factory', $purgerFactory);
- /** @var LoadDataFixturesDoctrineCommand $command */
- $command = $container->get('test.doctrine.fixtures_load_command');
- $tester = new CommandTester($command);
- $tester->execute(['--purger' => 'test', '--em' => 'alternative'], ['interactive' => false]);
- }
- public function testRunCommandWithPurgeMode() : void
- {
- $kernel = new IntegrationTestKernel('dev', true);
- $kernel->addServices(static function (ContainerBuilder $c) : void {
- // has a "staging" group via the getGroups() method
- $c->autowire(OtherFixtures::class)
- ->addTag(FixturesCompilerPass::FIXTURE_TAG);
- // no getGroups() method
- $c->autowire(WithDependenciesFixtures::class)
- ->addTag(FixturesCompilerPass::FIXTURE_TAG);
- $c->getDefinition('doctrine')
- ->setPublic(true)
- ->setSynthetic(true);
- $c->setAlias('test.doctrine.fixtures.purger.orm_purger_factory', new Alias('doctrine.fixtures.purger.orm_purger_factory', true));
- $c->setAlias('test.doctrine.fixtures_load_command', new Alias('doctrine.fixtures_load_command', true));
- });
- $kernel->boot();
- $container = $kernel->getContainer();
- $em = $this->createConfiguredMock(EntityManagerInterface::class, ['getConnection' => $this->createMock(Connection::class), 'getEventManager' => $this->createMock(EventManager::class)]);
- $registry = $this->createMock(ManagerRegistry::class);
- $registry
- ->expects(self::once())
- ->method('getManager')
- ->with(null)
- ->willReturn($em);
- $container->set('doctrine', $registry);
- $purgerFactory = $this->createMock(PurgerFactory::class);
- $purger = $this->createMock(ORMPurger::class);
- $purgerFactory
- ->expects(self::once())
- ->method('createForEntityManager')
- ->with(null, $em, [], true)
- ->willReturn($purger);
- $container->set('test.doctrine.fixtures.purger.orm_purger_factory', $purgerFactory);
- /** @var LoadDataFixturesDoctrineCommand $command */
- $command = $container->get('test.doctrine.fixtures_load_command');
- $tester = new CommandTester($command);
- $tester->execute(['--purge-with-truncate' => true], ['interactive' => false]);
- }
- }
|