├── .gitignore
├── .travis.yml
├── LICENSE
├── Module.php
├── README.md
├── composer.json
├── config
└── module.config.php
├── phpunit.xml
├── src
└── DoctrineDataFixtureModule
│ ├── Command
│ └── ImportCommand.php
│ ├── Loader
│ └── ServiceLocatorAwareLoader.php
│ ├── Module.php
│ └── Service
│ └── FixtureFactory.php
└── tests
├── Bootstrap.php
└── DoctrineDataFixtureTest
├── Loader
└── ServiceLocatorAwareLoaderTest.php
└── TestAsset
└── Fixtures
├── HasSL
└── FixtureA.php
└── NoSL
└── FixtureA.php
/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 | /vendor
3 | composer.lock
4 | composer.phar
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: php
2 | php:
3 | - 5.4
4 | - 5.5
5 |
6 | before_script:
7 | - composer self-update
8 | - composer update --prefer-source; composer install --dev --prefer-source;
9 |
10 | script:
11 | - ./vendor/bin/phpunit --coverage-clover ./build/logs/clover.xml
12 | - ./vendor/bin/phpcs --standard=PSR2 ./src/ ./tests/
13 |
14 | after_script:
15 | - php vendor/bin/coveralls -v
16 |
17 | notifications:
18 | irc: "irc.freenode.org#zftalk.modules"
19 | email: false
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Copyright (c) 2013, Martin Shwalbe
2 | All rights reserved.
3 |
4 | Redistribution and use in source and binary forms, with or without modification,
5 | are permitted provided that the following conditions are met:
6 |
7 | Redistributions of source code must retain the above copyright notice, this
8 | list of conditions and the following disclaimer.
9 |
10 | Redistributions in binary form must reproduce the above copyright notice, this
11 | list of conditions and the following disclaimer in the documentation and/or
12 | other materials provided with the distribution.
13 |
14 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
15 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
18 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
21 | ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 |
--------------------------------------------------------------------------------
/Module.php:
--------------------------------------------------------------------------------
1 | array(
40 | 'fixture' => array(
41 | 'ModuleName_fixture' => __DIR__ . '/../src/ModuleName/Fixture',
42 | )
43 | )
44 | );
45 | ```
46 |
47 | ## Usage
48 |
49 | #### Command Line
50 | Access the Doctrine command line as following
51 |
52 | ##Import
53 | ```sh
54 | ./vendor/bin/doctrine-module data-fixture:import
55 | ```
56 |
--------------------------------------------------------------------------------
/composer.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "hounddog/doctrine-data-fixture-module",
3 | "description": "Zend Framework 2 Module that provides Doctrine Data-Fixture functionality",
4 | "type": "library",
5 | "license": "MIT",
6 | "keywords": [
7 | "doctrine",
8 | "data-fixture",
9 | "module",
10 | "zf2"
11 | ],
12 | "homepage": "http://www.doctrine-project.org/",
13 | "authors": [
14 | {
15 | "name": "Martin Shwalbe",
16 | "email": "martin.shwalbe@gmail.com"
17 | }
18 | ],
19 | "require": {
20 | "php": ">=5.3.3",
21 | "zendframework/zend-modulemanager": "2.*",
22 | "zendframework/zend-eventmanager": "2.*",
23 | "zendframework/zend-servicemanager": "2.*",
24 | "doctrine/data-fixtures": "1.1.*",
25 | "doctrine/doctrine-orm-module": "~0.7"
26 | },
27 | "require-dev": {
28 | "phpunit/phpunit" : "3.7.*",
29 | "satooshi/php-coveralls": ">=0.6.0",
30 | "squizlabs/php_codesniffer": "1.4.*"
31 | },
32 | "autoload": {
33 | "psr-0": {
34 | "DoctrineDataFixtureModule": "src/"
35 | }
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/config/module.config.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
15 |
16 | ./tests/DoctrineDataFixtureTest
17 |
18 |
19 |
20 | ./src
21 |
22 |
23 |
24 |
28 |
29 |
--------------------------------------------------------------------------------
/src/DoctrineDataFixtureModule/Command/ImportCommand.php:
--------------------------------------------------------------------------------
1 | .
18 | */
19 |
20 | namespace DoctrineDataFixtureModule\Command;
21 |
22 | use Symfony\Component\Console\Command\Command;
23 | use Symfony\Component\Console\Input\InputInterface;
24 | use Symfony\Component\Console\Output\OutputInterface;
25 | use Symfony\Component\Console\Input\InputArgument;
26 | use Symfony\Component\Console\Input\InputOption;
27 | use Doctrine\ORM\Tools\SchemaTool;
28 | use Doctrine\DBAL\Migrations\Configuration\Configuration;
29 | use Doctrine\Common\DataFixtures\Executor\ORMExecutor;
30 | use Doctrine\Common\DataFixtures\Purger\ORMPurger;
31 | use DoctrineDataFixtureModule\Loader\ServiceLocatorAwareLoader;
32 | use Zend\ServiceManager\ServiceLocatorInterface;
33 |
34 | /**
35 | * Command for generate migration classes by comparing your current database schema
36 | * to your mapping information.
37 | *
38 | * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
39 | * @link www.doctrine-project.org
40 | * @since 2.0
41 | * @author Jonathan Wage
42 | */
43 | class ImportCommand extends Command
44 | {
45 | protected $paths;
46 |
47 | protected $em;
48 |
49 | /**
50 | * Service Locator instance
51 | * @var Zend\ServiceManager\ServiceLocatorInterface
52 | */
53 | protected $serviceLocator;
54 |
55 | const PURGE_MODE_TRUNCATE = 2;
56 |
57 | public function __construct(ServiceLocatorInterface $serviceLocator)
58 | {
59 | $this->serviceLocator = $serviceLocator;
60 | parent::__construct();
61 | }
62 |
63 | protected function configure()
64 | {
65 | parent::configure();
66 |
67 | $this->setName('data-fixture:import')
68 | ->setDescription('Import Data Fixtures')
69 | ->setHelp(
70 | <<addOption('append', null, InputOption::VALUE_NONE, 'Append data to existing data.')
75 | ->addOption('purge-with-truncate', null, InputOption::VALUE_NONE, 'Truncate tables before inserting data');
76 | }
77 |
78 | public function execute(InputInterface $input, OutputInterface $output)
79 | {
80 | $loader = new ServiceLocatorAwareLoader($this->serviceLocator);
81 | $purger = new ORMPurger();
82 |
83 | if ($input->getOption('purge-with-truncate')) {
84 | $purger->setPurgeMode(self::PURGE_MODE_TRUNCATE);
85 | }
86 |
87 | $executor = new ORMExecutor($this->em, $purger);
88 |
89 | foreach ($this->paths as $key => $value) {
90 | $loader->loadFromDirectory($value);
91 | }
92 | $executor->execute($loader->getFixtures(), $input->getOption('append'));
93 | }
94 |
95 | public function setPath($paths)
96 | {
97 | $this->paths=$paths;
98 | }
99 |
100 | public function setEntityManager($em)
101 | {
102 | $this->em = $em;
103 | }
104 | }
105 |
--------------------------------------------------------------------------------
/src/DoctrineDataFixtureModule/Loader/ServiceLocatorAwareLoader.php:
--------------------------------------------------------------------------------
1 | .
18 | */
19 | namespace DoctrineDataFixtureModule\Loader;
20 |
21 | use Doctrine\Common\DataFixtures\Loader as BaseLoader;
22 | use Zend\ServiceManager\ServiceLocatorAwareInterface;
23 | use Zend\ServiceManager\ServiceLocatorInterface;
24 | use Doctrine\Common\DataFixtures\FixtureInterface;
25 |
26 | /**
27 | * Doctrine fixture loader which is ZF2 Service Locator-aware
28 | * Will inject the service locator instance into all SL-aware fixtures on add
29 | *
30 | * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
31 | * @link www.doctrine-project.org
32 | * @author Adam Lundrigan
33 | */
34 | class ServiceLocatorAwareLoader extends BaseLoader
35 | {
36 | /**
37 | * @var ServiceLocatorInterface
38 | */
39 | protected $serviceLocator;
40 |
41 | public function __construct(ServiceLocatorInterface $serviceLocator)
42 | {
43 | $this->serviceLocator = $serviceLocator;
44 | }
45 |
46 | /**
47 | * Add a fixture object instance to the loader.
48 | *
49 | * @param FixtureInterface $fixture
50 | */
51 | public function addFixture(FixtureInterface $fixture)
52 | {
53 | if ($fixture instanceof ServiceLocatorAwareInterface) {
54 | $fixture->setServiceLocator($this->serviceLocator);
55 | }
56 | parent::addFixture($fixture);
57 | }
58 | }
59 |
--------------------------------------------------------------------------------
/src/DoctrineDataFixtureModule/Module.php:
--------------------------------------------------------------------------------
1 | .
18 | */
19 |
20 | namespace DoctrineDataFixtureModule;
21 |
22 | use Zend\ModuleManager\Feature\AutoloaderProviderInterface;
23 | use Zend\ModuleManager\Feature\ServiceProviderInterface;
24 | use Zend\ModuleManager\Feature\ConfigProviderInterface;
25 | use Zend\EventManager\EventInterface;
26 | use Zend\ModuleManager\ModuleManager;
27 | use Doctrine\ORM\Tools\Console\ConsoleRunner;
28 | use DoctrineDataFixtureModule\Command\ImportCommand;
29 | use DoctrineDataFixtureModule\Service\FixtureFactory;
30 |
31 | /**
32 | * Base module for Doctrine Data Fixture.
33 | *
34 | * @license MIT
35 | * @link www.doctrine-project.org
36 | * @author Martin Shwalbe
37 | */
38 | class Module implements
39 | AutoloaderProviderInterface,
40 | ServiceProviderInterface,
41 | ConfigProviderInterface
42 | {
43 | /**
44 | * {@inheritDoc}
45 | */
46 | public function getAutoloaderConfig()
47 | {
48 | return array(
49 | 'Zend\Loader\StandardAutoloader' => array(
50 | 'namespaces' => array(
51 | __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__
52 | ),
53 | ),
54 | );
55 | }
56 |
57 | /**
58 | * {@inheritDoc}
59 | */
60 | public function init(ModuleManager $e)
61 | {
62 | $events = $e->getEventManager()->getSharedManager();
63 |
64 | // Attach to helper set event and load the entity manager helper.
65 | $events->attach('doctrine', 'loadCli.post', function (EventInterface $e) {
66 | /* @var $cli \Symfony\Component\Console\Application */
67 | $cli = $e->getTarget();
68 |
69 | /* @var $sm ServiceLocatorInterface */
70 | $sm = $e->getParam('ServiceManager');
71 | $em = $cli->getHelperSet()->get('em')->getEntityManager();
72 | $paths = $sm->get('doctrine.configuration.fixtures');
73 |
74 | $importCommand = new ImportCommand($sm);
75 | $importCommand->setEntityManager($em);
76 | $importCommand->setPath($paths);
77 | ConsoleRunner::addCommands($cli);
78 | $cli->addCommands(array(
79 | $importCommand
80 | ));
81 | });
82 | }
83 |
84 | /**
85 | * {@inheritDoc}
86 | */
87 | public function getConfig()
88 | {
89 | return include __DIR__ . '/../../config/module.config.php';
90 | }
91 |
92 | /**
93 | * {@inheritDoc}
94 | */
95 | public function getServiceConfig()
96 | {
97 | return array(
98 | 'factories' => array(
99 | 'doctrine.configuration.fixtures' => new FixtureFactory('fixtures_default'),
100 | ),
101 | );
102 | }
103 | }
104 |
--------------------------------------------------------------------------------
/src/DoctrineDataFixtureModule/Service/FixtureFactory.php:
--------------------------------------------------------------------------------
1 | .
18 | */
19 |
20 | namespace DoctrineDataFixtureModule\Service;
21 |
22 |
23 | use Zend\ServiceManager\FactoryInterface;
24 | use Zend\ServiceManager\ServiceLocatorInterface;
25 |
26 | /**
27 | * Factory for Fixtures
28 | *
29 | * @license MIT
30 | * @link www.doctrine-project.org
31 | * @author Martin Shwalbe
32 | */
33 | class FixtureFactory implements FactoryInterface
34 | {
35 | public function createService(ServiceLocatorInterface $sl)
36 | {
37 | /** @var $options \DoctrineORMModule\Options\DBALConnection */
38 | $options = $this->getOptions($sl, 'fixtures');
39 |
40 | return $options;
41 | }
42 |
43 | /**
44 | * Gets options from configuration based on name.
45 | *
46 | * @param ServiceLocatorInterface $sl
47 | * @param string $key
48 | * @param null|string $name
49 | * @return \Zend\Stdlib\AbstractOptions
50 | * @throws \RuntimeException
51 | */
52 | public function getOptions(ServiceLocatorInterface $sl, $key)
53 | {
54 | $options = $sl->get('config');
55 | if (!isset($options['doctrine']['fixture'])) {
56 | return array();
57 | }
58 |
59 | return $options['doctrine']['fixture'];
60 | }
61 | }
62 |
--------------------------------------------------------------------------------
/tests/Bootstrap.php:
--------------------------------------------------------------------------------
1 | add('DoctrineDataFixtureTest\\', __DIR__);
21 |
22 | unset($files, $file, $loader);
23 |
--------------------------------------------------------------------------------
/tests/DoctrineDataFixtureTest/Loader/ServiceLocatorAwareLoaderTest.php:
--------------------------------------------------------------------------------
1 | .
18 | */
19 | namespace DoctrineDataFixtureTest\Loader;
20 |
21 | use Doctrine\Common\DataFixtures\Loader;
22 | use Zend\ServiceManager\ServiceManager;
23 | use Zend\Mvc\Service\ServiceManagerConfig;
24 | use DoctrineDataFixtureModule\Loader\ServiceLocatorAwareLoader;
25 |
26 | /**
27 | * Test Service Locator-aware fixture loader
28 | *
29 | * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
30 | * @link www.doctrine-project.org
31 | * @author Adam Lundrigan
32 | */
33 | class ServiceLocatorAwareLoaderTest extends \PHPUnit_Framework_TestCase
34 | {
35 |
36 | /**
37 | * Ensures that ServiceLocatorAwareLoader does not affect loading of
38 | * fixtures that are not SL-aware
39 | */
40 | public function testLoadingFixtureWhichIsNotServiceLocatorAware()
41 | {
42 | $fixtureClassName = 'DoctrineDataFixtureTest\TestAsset\Fixtures\NoSL\FixtureA';
43 | $serviceLocator = new ServiceManager(new ServiceManagerConfig());
44 |
45 | $loader = new ServiceLocatorAwareLoader($serviceLocator);
46 | $loader->loadFromDirectory(__DIR__ . '/../TestAsset/Fixtures/NoSL');
47 | $fixtures = $loader->getFixtures();
48 |
49 | $this->assertArrayHasKey($fixtureClassName, $fixtures);
50 | $fixture = $fixtures[$fixtureClassName];
51 | $this->assertInstanceOf('Doctrine\Common\DataFixtures\FixtureInterface', $fixture);
52 | $this->assertNotInstanceOf('Zend\ServiceManager\ServiceLocatorAwareInterface', $fixture);
53 | }
54 |
55 | /**
56 | * Ensures that the Service Locator instance passed into the ServiceLocatorAwareLoader
57 | * actually makes it to the SL-aware fixtures loaded
58 | */
59 | public function testLoadingFixtureWhichIsServiceLocatorAware()
60 | {
61 | $fixtureClassName = 'DoctrineDataFixtureTest\TestAsset\Fixtures\HasSL\FixtureA';
62 | $serviceLocator = new ServiceManager(new ServiceManagerConfig());
63 |
64 | $loader = new ServiceLocatorAwareLoader($serviceLocator);
65 | $loader->loadFromDirectory(__DIR__ . '/../TestAsset/Fixtures/HasSL');
66 | $fixtures = $loader->getFixtures();
67 |
68 | $this->assertArrayHasKey($fixtureClassName, $fixtures);
69 | $fixture = $fixtures[$fixtureClassName];
70 | $this->assertInstanceOf('Doctrine\Common\DataFixtures\FixtureInterface', $fixture);
71 | $this->assertInstanceOf('Zend\ServiceManager\ServiceLocatorAwareInterface', $fixture);
72 | $this->assertSame($serviceLocator, $fixture->getServiceLocator());
73 | }
74 | }
75 |
--------------------------------------------------------------------------------
/tests/DoctrineDataFixtureTest/TestAsset/Fixtures/HasSL/FixtureA.php:
--------------------------------------------------------------------------------
1 | serviceLocator = $serviceLocator;
30 |
31 | return $this;
32 | }
33 |
34 | /**
35 | * Get service locator
36 | *
37 | * @return ServiceLocatorInterface
38 | */
39 | public function getServiceLocator()
40 | {
41 | return $this->serviceLocator;
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/tests/DoctrineDataFixtureTest/TestAsset/Fixtures/NoSL/FixtureA.php:
--------------------------------------------------------------------------------
1 |