Permalink
Browse files

Method for loading from file added.

  • Loading branch information...
malef committed Oct 12, 2012
1 parent b29bad9 commit 34a1d898c1f1fe685025ff3417bc8b813b2df1d1
Showing with 75 additions and 26 deletions.
  1. +4 −0 README.md
  2. +51 −25 lib/Doctrine/Common/DataFixtures/Loader.php
  3. +20 −1 tests/Doctrine/Tests/Common/DataFixtures/LoaderTest.php
View
@@ -34,6 +34,10 @@ You can load a set of fixtures from a directory as well:
$loader->loadFromDirectory('/path/to/MyDataFixtures');
+Or you can load a set of fixtures from a file:
+
+ $loader->loadFromFile('/path/to/MyDataFixtures/MyFixture1.php');
+
You can get the added fixtures using the getFixtures() method:
$fixtures = $loader->getFixtures();
@@ -68,43 +68,35 @@ class Loader
* Find fixtures classes in a given directory and load them.
*
* @param string $dir Directory to find fixture classes in.
- * @return array $fixtures Array of loaded fixture object instances
+ * @return array $fixtures Array of loaded fixture object instances.
*/
public function loadFromDirectory($dir)
{
if (!is_dir($dir)) {
throw new \InvalidArgumentException(sprintf('"%s" does not exist', $dir));
}
- $fixtures = array();
- $includedFiles = array();
-
$iterator = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($dir),
\RecursiveIteratorIterator::LEAVES_ONLY
);
+ return $this->loadFromIterator($iterator);
+ }
- foreach ($iterator as $file) {
- if (($fileName = $file->getBasename($this->fileExtension)) == $file->getBasename()) {
- continue;
- }
- $sourceFile = realpath($file->getPathName());
- require_once $sourceFile;
- $includedFiles[] = $sourceFile;
- }
- $declared = get_declared_classes();
-
- foreach ($declared as $className) {
- $reflClass = new \ReflectionClass($className);
- $sourceFile = $reflClass->getFileName();
-
- if (in_array($sourceFile, $includedFiles) && ! $this->isTransient($className)) {
- $fixture = new $className;
- $fixtures[] = $fixture;
- $this->addFixture($fixture);
- }
+ /**
+ * Find fixtures classes in a given file and load them.
+ *
+ * @param string $fileName File to find fixture classes in.
+ * @return array $fixtures Array of loaded fixture object instances.
+ */
+ public function loadFromFile($fileName)
+ {
+ if (!is_readable($fileName)) {
+ throw new \InvalidArgumentException(sprintf('"%s" does not exist or is not readable', $fileName));
}
- return $fixtures;
+
+ $iterator = new \ArrayIterator(array(new \SplFileInfo($fileName)));
+ return $this->loadFromIterator($iterator);
}
/**
@@ -328,5 +320,39 @@ private function getUnsequencedClasses($sequences, $classes = null)
}
return $unsequencedClasses;
- }
+ }
+
+ /**
+ * Load fixtures from files contained in iterator.
+ *
+ * @param \Iterator $iterator Iterator over files from which fixtures should be loaded.
+ * @return array $fixtures Array of loaded fixture object instances.
+ */
+ private function loadFromIterator(\Iterator $iterator)
+ {
+ $includedFiles = array();
+ foreach ($iterator as $file) {
+ if (($fileName = $file->getBasename($this->fileExtension)) == $file->getBasename()) {
+ continue;
+ }
+ $sourceFile = realpath($file->getPathName());
+ require_once $sourceFile;
+ $includedFiles[] = $sourceFile;
+ }
+
+ $fixtures = array();
+ $declared = get_declared_classes();
+ foreach ($declared as $className) {
+ $reflClass = new \ReflectionClass($className);
+ $sourceFile = $reflClass->getFileName();
+
+ if (in_array($sourceFile, $includedFiles) && ! $this->isTransient($className)) {
+ $fixture = new $className;
+ $fixtures[] = $fixture;
+ $this->addFixture($fixture);
+ }
+ }
+
+ return $fixtures;
+ }
}
@@ -30,7 +30,7 @@
*/
class LoaderTest extends BaseTest
{
- public function testLoader()
+ public function testLoadFromDirectory()
{
$loader = new Loader();
$loader->addFixture($this->getMock('Doctrine\Common\DataFixtures\FixtureInterface'), array(), array(), 'Mock1');
@@ -44,4 +44,23 @@ public function testLoader()
$this->assertTrue($loader->isTransient('TestFixtures\NotAFixture'));
$this->assertFalse($loader->isTransient('TestFixtures\MyFixture1'));
}
+
+ public function testLoadFromFile()
+ {
+ $loader = new Loader();
+ $loader->addFixture($this->getMock('Doctrine\Common\DataFixtures\FixtureInterface'), array(), array(), 'Mock1');
+ $loader->addFixture($this->getMock('Doctrine\Common\DataFixtures\FixtureInterface', array(), array(), 'Mock2'));
+ $loader->addFixture($this->getMock('Doctrine\Common\DataFixtures\SharedFixtureInterface', array(), array(), 'Mock3'));
+
+ $this->assertCount(3, $loader->getFixtures());
+
+ $loader->loadFromFile(__DIR__.'/TestFixtures/MyFixture1.php');
+ $this->assertCount(4, $loader->getFixtures());
+ $loader->loadFromFile(__DIR__.'/TestFixtures/NotAFixture.php');
+ $this->assertCount(4, $loader->getFixtures());
+ $loader->loadFromFile(__DIR__.'/TestFixtures/MyFixture2.php');
+ $this->assertCount(5, $loader->getFixtures());
+ $this->assertTrue($loader->isTransient('TestFixtures\NotAFixture'));
+ $this->assertFalse($loader->isTransient('TestFixtures\MyFixture1'));
+ }
}

0 comments on commit 34a1d89

Please sign in to comment.