Skip to content

Commit

Permalink
Merge pull request #12 from h4cc/dev-issue11
Browse files Browse the repository at this point in the history
Fix for Issue 11
  • Loading branch information
h4cc committed Feb 4, 2014
2 parents bd4f02d + 6d1a688 commit 1181677
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 7 deletions.
43 changes: 36 additions & 7 deletions Fixtures/FixtureManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,28 +146,57 @@ public function load(FixtureSet $set)
}
}

// Load files
// Objects are the loaded entities without "local".
$objects = array();
// References contain, _all_ objects loaded.
$references = array();

// Load each file
foreach ($set->getFiles() as $file) {
// Use seed before each loading, so results will be more predictable.
$this->initSeedFromSet($set);

$loader = $loaders[$file['type']];

$loader->setReferences($references);
$loader->load($file['path']);
$newObjects = $loader->load($file['path']);
$references = $loader->getReferences();
$this->logDebug("Loaded file '" . $file['path'] . "'.");

$this->logDebug("Loaded ".count($newObjects)." file '" . $file['path'] . "'.");
$objects = array_merge($objects, $newObjects);
}

// Need to remove the "local" objects from references table.
// This can be skipped, when this change has been released:
// https://github.com/nelmio/alice/commit/d3bdb0d0e67e0b00d6e0b4df6b99a89bc7db882a
$objects = $this->removeLocalReferences($references, $objects);

if ($set->getDoPersist()) {
$this->persist($references, $set->getDoDrop());
$this->logDebug("Persisted " . count($references) . " loaded objects.");
$this->persist($objects, $set->getDoDrop());
$this->logDebug("Persisted " . count($objects) . " loaded objects.");
}

// Detach entities
$this->orm->detach($references);
$this->orm->detach($objects);

return $objects;
}

return $references;
/**
* Helper for a "intersect" of loaded objects.
*
* @param $base
* @param $extra
* @return mixed
*/
private function removeLocalReferences($base, $extra) {
$intersect = $base;
foreach ($base as $key => $value){
if (!in_array($value, $extra)){
unset($intersect[$key]);
}
}
return $intersect;
}

/**
Expand Down
17 changes: 17 additions & 0 deletions Tests/Fixture/FixtureManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,4 +182,21 @@ public function testProviders()

$this->manager->loadFiles(array(__DIR__ . '/../testdata/part_1.yml'));
}

/**
* Issue 11 https://github.com/h4cc/AliceFixturesBundle/issues/11
* Load objects, but do not return the "local" ones, because these can not be persisted by doctrine.
*/
public function testLocalEntities()
{
// We need a real YAML Loader for this.
$this->factoryMock->expects($this->any())->method('getLoader')
->with('yaml', 'en_EN')->will($this->returnValue(new Yaml()));

$objects = $this->manager->loadFiles(array(__DIR__ . '/../testdata/local_date.yml'));

// The result should only contain the "group", not the "date".
$this->assertEquals(array('group'), array_keys($objects));
$this->assertEquals("2000-01-01 00:00:00", $objects['group']->getCreationDate()->format('Y-m-d H:i:s'));
}
}
8 changes: 8 additions & 0 deletions Tests/testdata/local_date.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

\DateTime (local):
date:
__construct: ["2000-01-01 00:00:00"]

h4cc\AliceFixturesBundle\Tests\testdata\Group:
group:
creationDate: @date

0 comments on commit 1181677

Please sign in to comment.