Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(loader): don't create fixture dependency if already added #291

Open
wants to merge 1 commit into
base: 1.5.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/Doctrine/Common/DataFixtures/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public function addFixture(FixtureInterface $fixture)
} elseif ($fixture instanceof DependentFixtureInterface) {
$this->orderFixturesByDependencies = true;
foreach($fixture->getDependencies() as $class) {
if (class_exists($class)) {
if (!isset($this->fixtures[$fixtureClass]) && class_exists($class)) {
$this->addFixture($this->createFixture($class));
}
}
Expand Down
42 changes: 42 additions & 0 deletions tests/Doctrine/Tests/Common/DataFixtures/LoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

namespace Doctrine\Tests\Common\DataFixtures;

use Doctrine\Common\DataFixtures\DependentFixtureInterface;
use Doctrine\Common\DataFixtures\FixtureInterface;
use Doctrine\Common\DataFixtures\Loader;
use Doctrine\Common\DataFixtures\SharedFixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
use TestFixtures\MyFixture1;
use TestFixtures\NotAFixture;

Expand Down Expand Up @@ -58,4 +60,44 @@ public function testGetFixture()

$this->assertInstanceOf(MyFixture1::class, $fixture);
}

public function testAddFixtureDoesNotCreateFixtureDependencyIfAlreadyAdded()
{
$loader = new Loader();

$a = new AlreadyAddedFixture();

$loader->addFixture($a);

$b = new DependentOnAlreadyAddedFixture();

$loader->addFixture($b);

$this->assertEquals(1, AlreadyAddedFixture::$called, 'Should only have called the constructor once');
}
}

class AlreadyAddedFixture implements FixtureInterface
{
static $called = 0;

public function __construct()
{
static::$called += 1;
}

public function load(ObjectManager $manager)
{}
}

class DependentOnAlreadyAddedFixture implements FixtureInterface, DependentFixtureInterface
{
public function load(ObjectManager $manager)
{}

public function getDependencies()
{
return [AlreadyAddedFixture::class];
}
}