Skip to content

Commit

Permalink
Failing falsy order-dependent fixtures (#963)
Browse files Browse the repository at this point in the history
  • Loading branch information
goetas authored and theofidry committed May 5, 2019
1 parent 19bec34 commit 3d8167a
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@
use Nelmio\Alice\FixtureBuilder\Denormalizer\FixtureBagDenormalizerInterface;
use Nelmio\Alice\FixtureBuilder\Denormalizer\FlagParserInterface;
use Nelmio\Alice\IsAServiceTrait;
use Nelmio\Alice\Throwable\Exception\FixtureBuilder\Denormalizer\UnexpectedValueException;

final class SimpleFixtureBagDenormalizer implements FixtureBagDenormalizerInterface
{
use IsAServiceTrait;

/**
* @var FixtureDenormalizerInterface
*/
Expand Down Expand Up @@ -56,7 +57,12 @@ public function __construct(FixtureDenormalizerInterface $fixtureDenormalizer, F
public function denormalize(array $data): FixtureBag
{
$fixtures = new FixtureBag();
foreach ($data as $fqcnWithFlags => $rawFixtureSet) {

$alreadyRetried = [];
while ($result = array_splice($data, 0, 1)) {
$fqcnWithFlags = key($result);
$rawFixtureSet = current($result);

$flags = $this->flagParser->parse($fqcnWithFlags);
$fqcn = $flags->getKey();

Expand All @@ -69,22 +75,30 @@ public function denormalize(array $data): FixtureBag
)
);
}
try {
foreach ($rawFixtureSet as $reference => $specs) {
if (null === $specs) {
$specs = [];
}

foreach ($rawFixtureSet as $reference => $specs) {
if (null === $specs) {
$specs = [];
$fixtures = $this->fixtureDenormalizer->denormalize(
$fixtures,
$fqcn,
$reference,
$specs ?? [],
$flags
);
}
} catch (UnexpectedValueException $exception) {
if (!isset($alreadyRetried[$fqcnWithFlags])) {
$data[$fqcnWithFlags] = $rawFixtureSet;
$alreadyRetried[$fqcnWithFlags] = true;
} else {
throw $exception;
}

$fixtures = $this->fixtureDenormalizer->denormalize(
$fixtures,
$fqcn,
$reference,
$specs ?? [],
$flags
);
}
}

return $fixtures;
}
}
44 changes: 44 additions & 0 deletions tests/Loader/SimpleFilesLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
use Nelmio\Alice\FilesLoaderInterface;
use Nelmio\Alice\ObjectSetFactory;
use Nelmio\Alice\ParserInterface;
use Nelmio\Alice\User;
use Nelmio\Alice\UserDetail;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use ReflectionClass;
Expand Down Expand Up @@ -104,4 +106,46 @@ public function testLoadFilesAndReturnsAnObjectSet()
$parserProphecy->parse(Argument::cetera())->shouldHaveBeenCalledTimes(3);
$dataLoaderProphecy->loadData(Argument::cetera())->shouldHaveBeenCalledTimes(1);
}

public function testLoadFilesWithDifferentOrder()
{
$file1 = 'dummy1.yml';
$file2 = 'dummy2.yml';

$file1Data = [
User::class => [
'user1' => [
'name' => '<username()>',
],
],
];
$file2Data = [
UserDetail::class => [
'userdetail_{@user*}' => [
'email' => '<email()>',
],
'userdetail_single_{@user1}' => [
'email' => '<email()>',
],
],
];

$parserProphecy = $this->prophesize(ParserInterface::class);
$parserProphecy->parse($file1)->willReturn($file1Data);
$parserProphecy->parse($file2)->willReturn($file2Data);
/** @var ParserInterface $parser */
$parser = $parserProphecy->reveal();

$loader = new SimpleFilesLoader($parser, new IsolatedLoader());

$objects = $loader->loadFiles([$file2, $file1], [], [])->getObjects();
self::assertArrayHasKey('user1', $objects);
self::assertArrayHasKey('userdetail_user1', $objects);
self::assertArrayHasKey('userdetail_single_user1', $objects);

$objects = $loader->loadFiles([$file1, $file2], [], [])->getObjects();
self::assertArrayHasKey('user1', $objects);
self::assertArrayHasKey('userdetail_user1', $objects);
self::assertArrayHasKey('userdetail_single_user1', $objects);
}
}

0 comments on commit 3d8167a

Please sign in to comment.