Skip to content

Commit

Permalink
Tests: Add DependentFixtureInterface test
Browse files Browse the repository at this point in the history
Ignore code coverage for unreachable code
  • Loading branch information
alexislefebvre committed Feb 11, 2016
1 parent 1562ff8 commit 0b65790
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 11 deletions.
6 changes: 6 additions & 0 deletions Test/WebTestCase.php
Expand Up @@ -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 */
Expand Down Expand Up @@ -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);
Expand Down
64 changes: 64 additions & 0 deletions Tests/App/DataFixtures/ORM/LoadDependentUserData.php
@@ -0,0 +1,64 @@
<?php

/*
* This file is part of the Liip/FunctionalTestBundle
*
* (c) Lukas Kahwe Smith <smith@pooteeweet.org>
*
* 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',
);
}
}
12 changes: 1 addition & 11 deletions Tests/App/DataFixtures/ORM/LoadUserData.php
Expand Up @@ -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
Expand Down Expand Up @@ -61,12 +59,4 @@ public function load(ObjectManager $manager)
$manager->persist($user);
$manager->flush();
}

/**
* {@inheritdoc}
*/
public function getOrder()
{
return 0;
}
}
36 changes: 36 additions & 0 deletions Tests/Test/WebTestCaseTest.php
Expand Up @@ -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(
Expand All @@ -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.
*/
Expand Down

0 comments on commit 0b65790

Please sign in to comment.