From 0b65790fc70095dacb9c54164996ea0559ef40a8 Mon Sep 17 00:00:00 2001 From: Alexis Lefebvre Date: Thu, 11 Feb 2016 01:11:07 +0100 Subject: [PATCH] Tests: Add DependentFixtureInterface test Ignore code coverage for unreachable code --- Test/WebTestCase.php | 6 ++ .../ORM/LoadDependentUserData.php | 64 +++++++++++++++++++ Tests/App/DataFixtures/ORM/LoadUserData.php | 12 +--- Tests/Test/WebTestCaseTest.php | 36 +++++++++++ 4 files changed, 107 insertions(+), 11 deletions(-) create mode 100644 Tests/App/DataFixtures/ORM/LoadDependentUserData.php diff --git a/Test/WebTestCase.php b/Test/WebTestCase.php index 8c699926..77563f2d 100644 --- a/Test/WebTestCase.php +++ b/Test/WebTestCase.php @@ -522,7 +522,10 @@ private function locateResources($paths) public function loadFixtureFiles(array $paths = array(), $append = false, $omName = null, $registryName = 'doctrine') { if (!class_exists('Nelmio\Alice\Fixtures')) { + // This class is available during tests, no exception will be thrown. + // @codeCoverageIgnoreStart throw new \BadMethodCallException('nelmio/alice should be installed to use this method.'); + // @codeCoverageIgnoreEnd } /** @var ManagerRegistry $registry */ @@ -607,7 +610,10 @@ protected function getFixtureLoader(ContainerInterface $container, array $classN $loaderClass = class_exists('Symfony\Bridge\Doctrine\DataFixtures\ContainerAwareLoader') ? 'Symfony\Bridge\Doctrine\DataFixtures\ContainerAwareLoader' : (class_exists('Doctrine\Bundle\FixturesBundle\Common\DataFixtures\Loader') + // This class is not available during tests. + // @codeCoverageIgnoreStart ? 'Doctrine\Bundle\FixturesBundle\Common\DataFixtures\Loader' + // @codeCoverageIgnoreEnd : 'Symfony\Bundle\DoctrineFixturesBundle\Common\DataFixtures\Loader'); $loader = new $loaderClass($container); diff --git a/Tests/App/DataFixtures/ORM/LoadDependentUserData.php b/Tests/App/DataFixtures/ORM/LoadDependentUserData.php new file mode 100644 index 00000000..615bf29e --- /dev/null +++ b/Tests/App/DataFixtures/ORM/LoadDependentUserData.php @@ -0,0 +1,64 @@ + + * + * 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\DependentFixtureInterface; +use Symfony\Component\DependencyInjection\ContainerInterface; +use Doctrine\Common\Persistence\ObjectManager; + +class LoadDependentUserData extends AbstractFixture implements DependentFixtureInterface +{ + /** + * @var ContainerInterface + */ + private $container; + + /** + * {@inheritdoc} + */ + public function setContainer(ContainerInterface $container = null) + { + $this->container = $container; + } + + /** + * {@inheritdoc} + */ + public function load(ObjectManager $manager) + { + /** @var \Liip\FunctionalTestBundle\Tests\App\Entity\User $user */ + $user = clone $this->getReference('user'); + + $user->setId(3); + + $manager->persist($user); + $manager->flush(); + + $user = clone $this->getReference('user'); + + $user->setId(4); + + $manager->persist($user); + $manager->flush(); + } + + /** + * {@inheritdoc} + */ + public function getDependencies() + { + return array( + 'Liip\FunctionalTestBundle\Tests\App\DataFixtures\ORM\LoadUserData', + ); + } +} diff --git a/Tests/App/DataFixtures/ORM/LoadUserData.php b/Tests/App/DataFixtures/ORM/LoadUserData.php index f5a46c86..291c6141 100644 --- a/Tests/App/DataFixtures/ORM/LoadUserData.php +++ b/Tests/App/DataFixtures/ORM/LoadUserData.php @@ -12,14 +12,12 @@ namespace Liip\FunctionalTestBundle\Tests\App\DataFixtures\ORM; use Doctrine\Common\DataFixtures\AbstractFixture; -use Doctrine\Common\DataFixtures\OrderedFixtureInterface; use Doctrine\Common\DataFixtures\FixtureInterface; -use Symfony\Component\DependencyInjection\ContainerAwareInterface; use Symfony\Component\DependencyInjection\ContainerInterface; use Doctrine\Common\Persistence\ObjectManager; use Liip\FunctionalTestBundle\Tests\App\Entity\User; -class LoadUserData extends AbstractFixture implements OrderedFixtureInterface, FixtureInterface, ContainerAwareInterface +class LoadUserData extends AbstractFixture implements FixtureInterface { /** * @var ContainerInterface @@ -61,12 +59,4 @@ public function load(ObjectManager $manager) $manager->persist($user); $manager->flush(); } - - /** - * {@inheritdoc} - */ - public function getOrder() - { - return 0; - } } diff --git a/Tests/Test/WebTestCaseTest.php b/Tests/Test/WebTestCaseTest.php index a7af6238..d5e27cdd 100644 --- a/Tests/Test/WebTestCaseTest.php +++ b/Tests/Test/WebTestCaseTest.php @@ -218,6 +218,15 @@ public function testLoadFixtures() $em = $this->client->getContainer() ->get('doctrine.orm.entity_manager'); + $users = $em->getRepository('LiipFunctionalTestBundle:User') + ->findAll(); + + // There are 2 users. + $this->assertSame( + 2, + count($users) + ); + /** @var \Liip\FunctionalTestBundle\Tests\App\Entity\User $user */ $user = $em->getRepository('LiipFunctionalTestBundle:User') ->findOneBy(array( @@ -234,6 +243,33 @@ public function testLoadFixtures() ); } + /** + * Load fixture which has a dependency. + */ + public function testLoadDependentFixtures() + { + $fixtures = $this->loadFixtures(array( + 'Liip\FunctionalTestBundle\Tests\App\DataFixtures\ORM\LoadDependentUserData', + )); + + $this->assertInstanceOf( + 'Doctrine\Common\DataFixtures\Executor\ORMExecutor', + $fixtures + ); + + $em = $this->client->getContainer() + ->get('doctrine.orm.entity_manager'); + + $users = $em->getRepository('LiipFunctionalTestBundle:User') + ->findAll(); + + // The two files with fixtures have been loaded, there are 4 users. + $this->assertSame( + 4, + count($users) + ); + } + /** * Use nelmio/alice. */