Skip to content

Commit

Permalink
Merge pull request #441 from stollr/order_fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
greg0ire authored Apr 20, 2023
2 parents e6b97f5 + 141ad13 commit 4af35da
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ private function getUnsequencedClasses(array $sequences, ?iterable $classes = nu
}

foreach ($classes as $class) {
if ($sequences[$class] !== -1) {
if (! isset($sequences[$class]) || $sequences[$class] !== -1) {
continue;
}

Expand Down
41 changes: 41 additions & 0 deletions tests/Common/DataFixtures/DependentFixtureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@
use Doctrine\Common\DataFixtures\Loader;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use Doctrine\Persistence\ObjectManager;
use Exception;
use InvalidArgumentException;
use RuntimeException;

use function array_search;
use function array_shift;
use function restore_error_handler;
use function set_error_handler;

/**
* Test Fixture ordering by dependencies.
Expand Down Expand Up @@ -154,6 +157,29 @@ public function testInCaseGetFixturesReturnsDifferentResultsEachTime(): void
$this->assertInstanceOf(BaseParentFixture1::class, array_shift($orderedFixtures));
$this->assertInstanceOf(DependentFixture1::class, array_shift($orderedFixtures));
}

public function testOrderFixturesDependingOnOrderedFixture(): void
{
set_error_handler(static function (int $errno, string $errstr): never {
throw new Exception($errstr, $errno);
});

try {
$loader = new Loader();
$loader->addFixture(new DependingOnOrderedFixture());
$loader->addFixture(new OrderedByNumberFixture1());
$loader->addFixture(new OrderedByNumberFixture2());

$orderedFixtures = $loader->getFixtures();

$this->assertCount(3, $orderedFixtures);
$this->assertInstanceOf(OrderedByNumberFixture1::class, array_shift($orderedFixtures));
$this->assertInstanceOf(OrderedByNumberFixture2::class, array_shift($orderedFixtures));
$this->assertInstanceOf(DependingOnOrderedFixture::class, array_shift($orderedFixtures));
} finally {
restore_error_handler();
}
}
}

class DependentFixture1 implements FixtureInterface, DependentFixtureInterface
Expand Down Expand Up @@ -368,6 +394,21 @@ public function getDependencies(): array
}
}

class DependingOnOrderedFixture implements FixtureInterface, DependentFixtureInterface
{
public function load(ObjectManager $manager): void
{
}

/**
* {@inheritDoc}
*/
public function getDependencies(): array
{
return [OrderedByNumberFixture2::class];
}
}

class FixtureImplementingBothOrderingInterfaces implements FixtureInterface, OrderedFixtureInterface, DependentFixtureInterface
{
public function load(ObjectManager $manager): void
Expand Down

0 comments on commit 4af35da

Please sign in to comment.