Skip to content

Commit

Permalink
Merge pull request #38 from h4cc/feature/31
Browse files Browse the repository at this point in the history
Made FixtureSets sortable. closes #31
  • Loading branch information
h4cc committed Oct 31, 2014
2 parents 8228ce5 + 3091151 commit 2cc1a3b
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 19 deletions.
72 changes: 53 additions & 19 deletions Command/LoadSetsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,26 +74,9 @@ protected function execute(InputInterface $input, OutputInterface $output)
$schemaTool->createSchema();
}

// Store all loaded references in this array, so they can be used by other FixtureSets.
$references = array();

foreach ($sets as $file) {
$output->write("Loading file '$file' ... ");

// The file should return a FixtureSetInterface
$set = $this->loadSet($file);

if (!$set || !($set instanceof FixtureSetInterface)) {
throw new \InvalidArgumentException("File '$file' does not return a FixtureSetInterface.");
}

$entities = $manager->load($set, $references);
$fixtureSets = $this->loadFixtureSetsFromFiles($sets);

// Only reusing loaded entities. Internal references are ignored because of intended private state.
$references = array_merge($references, $entities);

$output->writeln("loaded " . count($entities) . " entities ... done.");
}
$this->loadFixtureSets($fixtureSets, $manager, $output);
}

protected function loadSet($file)
Expand Down Expand Up @@ -127,4 +110,55 @@ protected function findSetsByDefaultNaming() {
// Return paths to sets.
return array_keys(iterator_to_array($finder));
}

private function loadFixtureSetsFromFiles($sets)
{
$fixtureSets = array();

foreach ($sets as $file) {

// The file should return a FixtureSetInterface
$set = $this->loadSet($file);

if (!$set || !($set instanceof FixtureSetInterface)) {
throw new \InvalidArgumentException("File '$file' does not return a FixtureSetInterface.");
}

$fixtureSets[$file] = $set;
}

return $this->orderFixtureSets($fixtureSets);
}

private function orderFixtureSets($fixtureSets)
{
uasort($fixtureSets, function(FixtureSetInterface $setA, FixtureSetInterface $setB) {
$a = $setA->getOrder();
$b = $setB->getOrder();
if ($a == $b) {
return 0;
}
return ($a < $b) ? -1 : 1;
});

return $fixtureSets;
}

private function loadFixtureSets($fixtureSets, $manager, $output)
{
// Store all loaded references in this array, so they can be used by other FixtureSets.
$references = array();

foreach ($fixtureSets as $file => $set) {

$output->write("Loading file '$file' ... ");

$entities = $manager->load($set, $references);

// Only reusing loaded entities. Internal references are ignored because of intended private state.
$references = array_merge($references, $entities);

$output->writeln("loaded " . count($entities) . " entities ... done.");
}
}
}
17 changes: 17 additions & 0 deletions Fixtures/FixtureSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public function __construct(array $options = array())
'seed' => 1,
'do_drop' => false,
'do_persist' => true,
'order' => 1,
);
$this->options = array_merge(
$defaultOptions,
Expand Down Expand Up @@ -135,4 +136,20 @@ public function setSeed($seed)
{
$this->options['seed'] = is_null($seed) ? null : (integer)$seed;
}

/**
* @return int
*/
public function getOrder()
{
return $this->options['order'];
}

/**
* @param int $order
*/
public function setOrder($order)
{
$this->options['order'] = is_null($order) ? 1 : (integer)$order;
}
}
5 changes: 5 additions & 0 deletions Fixtures/FixtureSetInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,9 @@ public function getLocale();
* @return int|null
*/
public function getSeed();

/**
* @return int
*/
public function getOrder();
}
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@ $ php app/console h4cc_alice_fixtures:load:sets
```

The order in which the bundles are loaded is defined by the order in which they are defined in `AppKernel`.
A customization to that can be done using the "order" value in your fixture sets.
Default is '1' and lower order values will be taken first.

Example for a Preconfigured FixtureSet:

Expand All @@ -215,6 +217,7 @@ $set = new h4cc\AliceFixturesBundle\Fixtures\FixtureSet(array(
'seed' => 42,
'do_drop' => true,
'do_persist' => true,
'order' => 5
));

$set->addFile(__DIR__.'/Users.yml', 'yaml');
Expand Down
4 changes: 4 additions & 0 deletions Tests/Fixture/FixtureSetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,13 @@ public function testConstructor()
'seed' => 42,
'do_drop' => true,
'do_persist' => false,
'order' => 1337
)
);

$this->assertEquals('de_DE', $set->getLocale());
$this->assertEquals(42, $set->getSeed());
$this->assertEquals(1337, $set->getOrder());
$this->assertEquals(true, $set->getDoDrop());
$this->assertEquals(false, $set->getDoPersist());
$this->assertEquals(array(), $set->getFiles());
Expand All @@ -56,13 +58,15 @@ public function testSetter()

$set->setLocale('de_DE');
$set->setSeed(42);
$set->setOrder(1337);
$set->setDoDrop(true);
$set->setDoPersist(false);
$set->addFile('/foo', 'bar');
$set->addFile('/bob', 'xyz');

$this->assertEquals('de_DE', $set->getLocale());
$this->assertEquals(42, $set->getSeed());
$this->assertEquals(1337, $set->getOrder());
$this->assertEquals(true, $set->getDoDrop());
$this->assertEquals(false, $set->getDoPersist());
$this->assertEquals(
Expand Down

0 comments on commit 2cc1a3b

Please sign in to comment.