diff --git a/.gitignore b/.gitignore index 0ee1ad3..9079acb 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ /build /vendor composer.lock -composer.phar \ No newline at end of file +composer.phar +clover.xml diff --git a/README.md b/README.md index 87e90a6..badb27d 100644 --- a/README.md +++ b/README.md @@ -53,3 +53,11 @@ Access the Doctrine command line as following ```sh ./vendor/bin/doctrine-module data-fixture:import ``` + +##Options + +--append + +--purge-with-truncate + +--fixtures=./path/to/fixtures/or/directory diff --git a/src/DoctrineDataFixtureModule/Command/ImportCommand.php b/src/DoctrineDataFixtureModule/Command/ImportCommand.php index f153d41..cfd339d 100644 --- a/src/DoctrineDataFixtureModule/Command/ImportCommand.php +++ b/src/DoctrineDataFixtureModule/Command/ImportCommand.php @@ -13,7 +13,7 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * This software consists of voluntary contributions made by many individuals - * and is licensed under the LGPL. For more information, see + * and is licensed under the MIT license. For more information, see * . */ @@ -29,34 +29,50 @@ use Doctrine\Common\DataFixtures\Executor\ORMExecutor; use Doctrine\Common\DataFixtures\Purger\ORMPurger; use DoctrineDataFixtureModule\Loader\ServiceLocatorAwareLoader; -use Zend\ServiceManager\ServiceLocatorInterface; +use Doctrine\ORM\EntityManagerInterface; /** - * Command for generate migration classes by comparing your current database schema - * to your mapping information. + * Command to import Fixtures * - * @license http://www.opensource.org/licenses/lgpl-license.php LGPL + * @license MIT * @link www.doctrine-project.org - * @since 2.0 - * @author Jonathan Wage + * @author Martin Shwalbe */ class ImportCommand extends Command { protected $paths; - protected $em; + /** + * EntityManager + * @var Doctrine\ORM\EntityManager + */ + protected $entityManager; /** - * Service Locator instance - * @var Zend\ServiceManager\ServiceLocatorInterface + * ServiceLocatorAwareLoader + * @var DoctrineDataFixtureModule\Loader\ServiceLocatorAwareLoader */ - protected $serviceLocator; + protected $serviceLocatorAwareloader; + + /** + * ORMPurger + * @var Doctrine\Common\DataFixtures\Purger\ORMPurger + */ + protected $ormPurger; const PURGE_MODE_TRUNCATE = 2; - public function __construct(ServiceLocatorInterface $serviceLocator) - { - $this->serviceLocator = $serviceLocator; + public function __construct( + ServiceLocatorAwareLoader $serviceLocatorAwareloader, + ORMPurger $ormPurger, + EntityManagerInterface $entityManager, + array $paths = array() + ) { + $this->serviceLocatorAwareloader = $serviceLocatorAwareloader; + $this->ormPurger = $ormPurger; + $this->entityManager = $entityManager; + $this->paths = $paths; + parent::__construct(); } @@ -72,33 +88,29 @@ protected function configure() EOT ) ->addOption('append', null, InputOption::VALUE_NONE, 'Append data to existing data.') - ->addOption('purge-with-truncate', null, InputOption::VALUE_NONE, 'Truncate tables before inserting data'); + ->addOption('purge-with-truncate', null, InputOption::VALUE_NONE, 'Truncate tables before inserting data') + ->addOption( + 'fixtures', + null, + InputOption::VALUE_REQUIRED, + 'Set path to Fixture Class or Directory to be added' + ); } public function execute(InputInterface $input, OutputInterface $output) { - $loader = new ServiceLocatorAwareLoader($this->serviceLocator); - $purger = new ORMPurger(); - if ($input->getOption('purge-with-truncate')) { - $purger->setPurgeMode(self::PURGE_MODE_TRUNCATE); + $this->ormPurger->setPurgeMode(self::PURGE_MODE_TRUNCATE); } - $executor = new ORMExecutor($this->em, $purger); - - foreach ($this->paths as $key => $value) { - $loader->loadFromDirectory($value); + if ($input->getOption('fixtures') !== null) { + $this->serviceLocatorAwareloader->loadPath($input->getOption('fixtures')); + } else { + $this->serviceLocatorAwareloader->loadPaths($this->paths); } - $executor->execute($loader->getFixtures(), $input->getOption('append')); - } - public function setPath($paths) - { - $this->paths=$paths; - } + $executor = new ORMExecutor($this->entityManager, $this->ormPurger); - public function setEntityManager($em) - { - $this->em = $em; + $executor->execute($this->serviceLocatorAwareloader->getFixtures(), $input->getOption('append')); } } diff --git a/src/DoctrineDataFixtureModule/Loader/ServiceLocatorAwareLoader.php b/src/DoctrineDataFixtureModule/Loader/ServiceLocatorAwareLoader.php index a3d05d1..e193cc7 100644 --- a/src/DoctrineDataFixtureModule/Loader/ServiceLocatorAwareLoader.php +++ b/src/DoctrineDataFixtureModule/Loader/ServiceLocatorAwareLoader.php @@ -13,9 +13,10 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * This software consists of voluntary contributions made by many individuals - * and is licensed under the LGPL. For more information, see + * and is licensed under the MIT license. For more information, see * . */ + namespace DoctrineDataFixtureModule\Loader; use Doctrine\Common\DataFixtures\Loader as BaseLoader; @@ -27,7 +28,7 @@ * Doctrine fixture loader which is ZF2 Service Locator-aware * Will inject the service locator instance into all SL-aware fixtures on add * - * @license http://www.opensource.org/licenses/lgpl-license.php LGPL + * @license MIT * @link www.doctrine-project.org * @author Adam Lundrigan */ @@ -55,4 +56,40 @@ public function addFixture(FixtureInterface $fixture) } parent::addFixture($fixture); } + + /** + * Load Fixtures from directory or Single fixture from path + * @param $path + */ + public function loadPath($path) + { + if (is_dir($path)) { + $this->loadFromDirectory($path); + return $this; + } + + if (file_exists($path)) { + $classes = get_declared_classes(); + include($path); + $newClasses = get_declared_classes(); + + $diff = array_diff($newClasses, $classes); + $class = array_pop($diff); + $this->addFixture(new $class); + return $this; + } + + throw new \RuntimeException('Cannot find File or Directory.'); + } + + /** + * Load Fixtures from directories + * @param $paths + */ + public function loadPaths($paths) + { + foreach ($paths as $key => $value) { + $this->loadFromDirectory($value); + } + } } diff --git a/src/DoctrineDataFixtureModule/Module.php b/src/DoctrineDataFixtureModule/Module.php index a229110..6285e8b 100644 --- a/src/DoctrineDataFixtureModule/Module.php +++ b/src/DoctrineDataFixtureModule/Module.php @@ -26,7 +26,9 @@ use Zend\ModuleManager\ModuleManager; use Doctrine\ORM\Tools\Console\ConsoleRunner; use DoctrineDataFixtureModule\Command\ImportCommand; +use DoctrineDataFixtureModule\Loader\ServiceLocatorAwareLoader; use DoctrineDataFixtureModule\Service\FixtureFactory; +use Doctrine\Common\DataFixtures\Purger\ORMPurger; /** * Base module for Doctrine Data Fixture. @@ -70,13 +72,11 @@ public function init(ModuleManager $e) $sm = $e->getParam('ServiceManager'); $em = $sm->get('doctrine.entitymanager.orm_default'); $paths = $sm->get('doctrine.configuration.fixtures'); + $loader = new ServiceLocatorAwareLoader($sm); - $importCommand = new ImportCommand($sm); - $importCommand->setEntityManager($em); - $importCommand->setPath($paths); ConsoleRunner::addCommands($cli); $cli->addCommands(array( - $importCommand + new ImportCommand($loader, new ORMPurger, $em, $paths) )); }); } diff --git a/tests/DoctrineDataFixtureTest/Command/ImportCommandTest.php b/tests/DoctrineDataFixtureTest/Command/ImportCommandTest.php new file mode 100644 index 0000000..30a2b73 --- /dev/null +++ b/tests/DoctrineDataFixtureTest/Command/ImportCommandTest.php @@ -0,0 +1,115 @@ +. + */ + +namespace DoctrineDataFixtureTest\Command; + + +use DoctrineDataFixtureModule\Command\ImportCommand; +use Doctrine\ORM\EntityManager; +use Symfony\Component\Console\Tester\CommandTester; + +use PHPUnit_Framework_TestCase; +use Doctrine\ORM\Tools\Setup; + +use Zend\ServiceManager\ServiceManager; +use Zend\Mvc\Service\ServiceManagerConfig; + +/** + * Test Import commands for fixtures + * + * @license MIT + * @link www.doctrine-project.org + * @author Martin Shwalbe + */ +class ImportCommandTest extends PHPUnit_Framework_TestCase +{ + /** + * @covers DoctrineDataFixtureModule\Command\ImportCommand::execute + */ + public function testExecutePurgeWithTruncate() + { + $paths = array( + 'DoctrineDataFixture_Test_Paths' => __DIR__ . '/../TestAsset/Fixtures/NoSL', + ); + + $command = new ImportCommand( + $this->getMockServiceLocatorAwareLoader(), + $this->getMockPurger(), + $this->getMockSqliteEntityManager(), + $paths + ); + + $commandTester = new CommandTester($command); + $commandTester->execute( + array( + '--purge-with-truncate' => 'true', + ) + ); + } + + private function getMockFixture() + { + return $this->getMock('Doctrine\Common\DataFixtures\FixtureInterface'); + } + + private function getMockPurger() + { + $purger = $this->getMock('Doctrine\Common\DataFixtures\Purger\ORMPurger'); + + $purger->expects($this->once()) + ->method('setPurgeMode') + ->with($this->equalTo(2)); + + return $purger; + } + + protected function getMockServiceLocatorAwareLoader() + { + $loader = $this->getMock( + 'DoctrineDataFixtureModule\Loader\ServiceLocatorAwareLoader', + array(), + array(new ServiceManager(new ServiceManagerConfig())) + ); + + $loader->expects($this->once()) + ->method('getFixtures') + ->will($this->returnValue( + array($this->getMockFixture()) + )); + + $loader->expects($this->once()) + ->method('loadPaths'); + + return $loader; + } + + /** + * EntityManager mock object together with + * annotation mapping driver and pdo_sqlite + * database in memory + * + * @return EntityManager + */ + protected function getMockSqliteEntityManager() + { + $dbParams = array('driver' => 'pdo_sqlite', 'memory' => true); + $config = Setup::createAnnotationMetadataConfiguration(array(__DIR__ . '/../TestAsset/Entity'), true); + return EntityManager::create($dbParams, $config); + } +} diff --git a/tests/DoctrineDataFixtureTest/Loader/ServiceLocatorAwareLoaderTest.php b/tests/DoctrineDataFixtureTest/Loader/ServiceLocatorAwareLoaderTest.php index 84343c7..7d4ec8d 100644 --- a/tests/DoctrineDataFixtureTest/Loader/ServiceLocatorAwareLoaderTest.php +++ b/tests/DoctrineDataFixtureTest/Loader/ServiceLocatorAwareLoaderTest.php @@ -13,9 +13,10 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * This software consists of voluntary contributions made by many individuals - * and is licensed under the LGPL. For more information, see + * and is licensed under the MIT license. For more information, see * . */ + namespace DoctrineDataFixtureTest\Loader; use Doctrine\Common\DataFixtures\Loader; @@ -26,7 +27,7 @@ /** * Test Service Locator-aware fixture loader * - * @license http://www.opensource.org/licenses/lgpl-license.php LGPL + * @license MIT * @link www.doctrine-project.org * @author Adam Lundrigan */ @@ -36,6 +37,9 @@ class ServiceLocatorAwareLoaderTest extends \PHPUnit_Framework_TestCase /** * Ensures that ServiceLocatorAwareLoader does not affect loading of * fixtures that are not SL-aware + * + * @covers DoctrineDataFixtureModule\Loader\ServiceLocatorAwareLoader::loadFromDirectory + * @covers DoctrineDataFixtureModule\Loader\ServiceLocatorAwareLoader::getFixtures */ public function testLoadingFixtureWhichIsNotServiceLocatorAware() { @@ -55,6 +59,9 @@ public function testLoadingFixtureWhichIsNotServiceLocatorAware() /** * Ensures that the Service Locator instance passed into the ServiceLocatorAwareLoader * actually makes it to the SL-aware fixtures loaded + * + * @covers DoctrineDataFixtureModule\Loader\ServiceLocatorAwareLoader::loadFromDirectory + * @covers DoctrineDataFixtureModule\Loader\ServiceLocatorAwareLoader::getFixtures */ public function testLoadingFixtureWhichIsServiceLocatorAware() { @@ -71,4 +78,58 @@ public function testLoadingFixtureWhichIsServiceLocatorAware() $this->assertInstanceOf('Zend\ServiceManager\ServiceLocatorAwareInterface', $fixture); $this->assertSame($serviceLocator, $fixture->getServiceLocator()); } + + /** + * @covers DoctrineDataFixtureModule\Loader\ServiceLocatorAwareLoader::loadPaths + * @covers DoctrineDataFixtureModule\Loader\ServiceLocatorAwareLoader::getFixtures + */ + public function testLoadingByConfigPaths() + { + $paths = array( + 'DoctrineDataFixture_Test_Paths_NoSL' => __DIR__ . '/../TestAsset/Fixtures/NoSL', + 'DoctrineDataFixture_Test_Paths_HasSL' => __DIR__ . '/../TestAsset/Fixtures/HasSL', + ); + + $serviceLocator = new ServiceManager(new ServiceManagerConfig()); + + $loader = new ServiceLocatorAwareLoader($serviceLocator); + $loader->loadPaths($paths); + $fixtures = $loader->getFixtures(); + + $this->assertArrayHasKey('DoctrineDataFixtureTest\TestAsset\Fixtures\HasSL\FixtureA', $fixtures); + $this->assertArrayHasKey('DoctrineDataFixtureTest\TestAsset\Fixtures\NoSL\FixtureA', $fixtures); + } + + /** + * @covers DoctrineDataFixtureModule\Loader\ServiceLocatorAwareLoader::loadPath + * @covers DoctrineDataFixtureModule\Loader\ServiceLocatorAwareLoader::getFixtures + */ + public function testLoadingByDirectoryPath() + { + $fixturePath = __DIR__ . '/../TestAsset/Fixtures/HasSL'; + $serviceLocator = new ServiceManager(new ServiceManagerConfig()); + + $loader = new ServiceLocatorAwareLoader($serviceLocator); + $loader->loadPath($fixturePath); + $fixtures = $loader->getFixtures(); + + $this->assertArrayHasKey('DoctrineDataFixtureTest\TestAsset\Fixtures\HasSL\FixtureA', $fixtures); + } + + /** + * @covers DoctrineDataFixtureModule\Loader\ServiceLocatorAwareLoader::loadPath + * @covers DoctrineDataFixtureModule\Loader\ServiceLocatorAwareLoader::getFixtures + */ + public function testLoadingByPath() + { + $fixturePath = __DIR__ . '/../TestAsset/Fixtures/FixtureA.php'; + $serviceLocator = new ServiceManager(new ServiceManagerConfig()); + + $loader = new ServiceLocatorAwareLoader($serviceLocator); + $loader->loadPath($fixturePath); + $fixtures = $loader->getFixtures(); + + $this->assertArrayHasKey('DoctrineDataFixtureTest\TestAsset\Fixtures\FixtureA', $fixtures); + + } } diff --git a/tests/DoctrineDataFixtureTest/TestAsset/Entity/Role.php b/tests/DoctrineDataFixtureTest/TestAsset/Entity/Role.php new file mode 100644 index 0000000..d731aec --- /dev/null +++ b/tests/DoctrineDataFixtureTest/TestAsset/Entity/Role.php @@ -0,0 +1,41 @@ +. + */ + +namespace DoctrineDataFixtureTest\TestAsset\Entity; + +use Doctrine\ORM\Mapping as ORM; + +/** + * @ORM\Entity + * @ORM\Table(name="doctrine_data_fixture_role") + */ +class Role +{ + /** + * @ORM\Id + * @ORM\Column(type="integer"); + * @ORM\GeneratedValue(strategy="AUTO") + */ + public $id; + + /** + * @ORM\Column(type="string", length=50) + */ + public $name; +} diff --git a/tests/DoctrineDataFixtureTest/TestAsset/Entity/User.php b/tests/DoctrineDataFixtureTest/TestAsset/Entity/User.php new file mode 100644 index 0000000..f721754 --- /dev/null +++ b/tests/DoctrineDataFixtureTest/TestAsset/Entity/User.php @@ -0,0 +1,57 @@ +. + */ + +namespace DoctrineDataFixtureTest\TestAsset\Entity; + +use Doctrine\ORM\Mapping as ORM; + +/** + * @ORM\Entity + * @ORM\Table(name="doctrine_data_fixture_user") + */ +class User +{ + /** + * @ORM\Id + * @ORM\Column(type="integer"); + * @ORM\GeneratedValue(strategy="AUTO") + */ + public $id; + + /** + * @Column(length=32) + * @Id + */ + public $code; + + /** + * @ORM\Column(type="string", length=32) + */ + public $password; + + /** + * @ORM\Column(type="string", length=255) + */ + public $email; + + /** + * @ORM\ManyToOne(targetEntity="Role", cascade={"persist"}) + */ + public $role; +} diff --git a/tests/DoctrineDataFixtureTest/TestAsset/Fixtures/FixtureA.php b/tests/DoctrineDataFixtureTest/TestAsset/Fixtures/FixtureA.php new file mode 100644 index 0000000..323eca2 --- /dev/null +++ b/tests/DoctrineDataFixtureTest/TestAsset/Fixtures/FixtureA.php @@ -0,0 +1,30 @@ +. + */ + +namespace DoctrineDataFixtureTest\TestAsset\Fixtures; + +use Doctrine\Common\Persistence\ObjectManager; +use Doctrine\Common\DataFixtures\FixtureInterface; + +class FixtureA implements FixtureInterface +{ + public function load(ObjectManager $manager) + { + } +} diff --git a/tests/DoctrineDataFixtureTest/TestAsset/Fixtures/HasSL/FixtureA.php b/tests/DoctrineDataFixtureTest/TestAsset/Fixtures/HasSL/FixtureA.php index c6fd35b..380ea63 100644 --- a/tests/DoctrineDataFixtureTest/TestAsset/Fixtures/HasSL/FixtureA.php +++ b/tests/DoctrineDataFixtureTest/TestAsset/Fixtures/HasSL/FixtureA.php @@ -1,4 +1,22 @@ . + */ + namespace DoctrineDataFixtureTest\TestAsset\Fixtures\HasSL; use Doctrine\Common\Persistence\ObjectManager; diff --git a/tests/DoctrineDataFixtureTest/TestAsset/Fixtures/NoSL/FixtureA.php b/tests/DoctrineDataFixtureTest/TestAsset/Fixtures/NoSL/FixtureA.php index 2313721..9d6d762 100644 --- a/tests/DoctrineDataFixtureTest/TestAsset/Fixtures/NoSL/FixtureA.php +++ b/tests/DoctrineDataFixtureTest/TestAsset/Fixtures/NoSL/FixtureA.php @@ -1,4 +1,22 @@ . + */ + namespace DoctrineDataFixtureTest\TestAsset\Fixtures\NoSL; use Doctrine\Common\Persistence\ObjectManager;