Skip to content

Commit

Permalink
Merge pull request #25 from adamlundrigan/feature/service-locator-aware
Browse files Browse the repository at this point in the history
Make fixture loader service locator aware
  • Loading branch information
Hounddog committed Jan 24, 2014
2 parents 6beeb4f + 1fc987e commit 1a3571c
Show file tree
Hide file tree
Showing 6 changed files with 203 additions and 3 deletions.
17 changes: 15 additions & 2 deletions src/DoctrineDataFixtureModule/Command/ImportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@
use Symfony\Component\Console\Input\InputOption;
use Doctrine\ORM\Tools\SchemaTool;
use Doctrine\DBAL\Migrations\Configuration\Configuration;
use Doctrine\Common\DataFixtures\Loader;
use Doctrine\Common\DataFixtures\Executor\ORMExecutor;
use Doctrine\Common\DataFixtures\Purger\ORMPurger;
use DoctrineDataFixtureModule\Loader\ServiceLocatorAwareLoader;
use Zend\ServiceManager\ServiceLocatorInterface;

/**
* Command for generate migration classes by comparing your current database schema
Expand All @@ -44,8 +45,20 @@ class ImportCommand extends Command
protected $paths;

protected $em;

/**
* Service Locator instance
* @var Zend\ServiceManager\ServiceLocatorInterface
*/
protected $serviceLocator;

const PURGE_MODE_TRUNCATE = 2;

public function __construct(ServiceLocatorInterface $serviceLocator)
{
$this->serviceLocator = $serviceLocator;
parent::__construct();
}

protected function configure()
{
Expand All @@ -64,7 +77,7 @@ protected function configure()

public function execute(InputInterface $input, OutputInterface $output)
{
$loader = new Loader();
$loader = new ServiceLocatorAwareLoader($this->serviceLocator);
$purger = new ORMPurger();

if ($input->getOption('purge-with-truncate')) {
Expand Down
58 changes: 58 additions & 0 deletions src/DoctrineDataFixtureModule/Loader/ServiceLocatorAwareLoader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* 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
* <http://www.doctrine-project.org>.
*/
namespace DoctrineDataFixtureModule\Loader;

use Doctrine\Common\DataFixtures\Loader as BaseLoader;
use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Doctrine\Common\DataFixtures\FixtureInterface;

/**
* 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
* @link www.doctrine-project.org
* @author Adam Lundrigan <adam@lundrigan.ca>
*/
class ServiceLocatorAwareLoader extends BaseLoader
{
/**
* @var ServiceLocatorInterface
*/
protected $serviceLocator;

public function __construct(ServiceLocatorInterface $serviceLocator)
{
$this->serviceLocator = $serviceLocator;
}

/**
* Add a fixture object instance to the loader.
*
* @param FixtureInterface $fixture
*/
public function addFixture(FixtureInterface $fixture)
{
if ($fixture instanceof ServiceLocatorAwareInterface) {
$fixture->setServiceLocator($this->serviceLocator);
}
parent::addFixture($fixture);
}
}
2 changes: 1 addition & 1 deletion src/DoctrineDataFixtureModule/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function init(ModuleManager $e)
$em = $sm->get('doctrine.entitymanager.orm_default');
$paths = $sm->get('doctrine.configuration.fixtures');

$importCommand = new ImportCommand();
$importCommand = new ImportCommand($sm);
$importCommand->setEntityManager($em);
$importCommand->setPath($paths);
ConsoleRunner::addCommands($cli);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* 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
* <http://www.doctrine-project.org>.
*/
namespace DoctrineDataFixtureTest\Loader;

use Doctrine\Common\DataFixtures\Loader;
use Zend\ServiceManager\ServiceManager;
use Zend\Mvc\Service\ServiceManagerConfig;
use DoctrineDataFixtureModule\Loader\ServiceLocatorAwareLoader;

/**
* Test Service Locator-aware fixture loader
*
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link www.doctrine-project.org
* @author Adam Lundrigan <adam@lundrigan.ca>
*/
class ServiceLocatorAwareLoaderTest extends \PHPUnit_Framework_TestCase
{

/**
* Ensures that ServiceLocatorAwareLoader does not affect loading of
* fixtures that are not SL-aware
*/
public function testLoadingFixtureWhichIsNotServiceLocatorAware()
{
$fixtureClassName = 'DoctrineDataFixtureTest\TestAsset\Fixtures\NoSL\FixtureA';
$serviceLocator = new ServiceManager(new ServiceManagerConfig());

$loader = new ServiceLocatorAwareLoader($serviceLocator);
$loader->loadFromDirectory(__DIR__ . '/../TestAsset/Fixtures/NoSL');
$fixtures = $loader->getFixtures();

$this->assertArrayHasKey($fixtureClassName, $fixtures);
$fixture = $fixtures[$fixtureClassName];
$this->assertInstanceOf('Doctrine\Common\DataFixtures\FixtureInterface', $fixture);
$this->assertNotInstanceOf('Zend\ServiceManager\ServiceLocatorAwareInterface', $fixture);
}

/**
* Ensures that the Service Locator instance passed into the ServiceLocatorAwareLoader
* actually makes it to the SL-aware fixtures loaded
*/
public function testLoadingFixtureWhichIsServiceLocatorAware()
{
$fixtureClassName = 'DoctrineDataFixtureTest\TestAsset\Fixtures\HasSL\FixtureA';
$serviceLocator = new ServiceManager(new ServiceManagerConfig());

$loader = new ServiceLocatorAwareLoader($serviceLocator);
$loader->loadFromDirectory(__DIR__ . '/../TestAsset/Fixtures/HasSL');
$fixtures = $loader->getFixtures();

$this->assertArrayHasKey($fixtureClassName, $fixtures);
$fixture = $fixtures[$fixtureClassName];
$this->assertInstanceOf('Doctrine\Common\DataFixtures\FixtureInterface', $fixture);
$this->assertInstanceOf('Zend\ServiceManager\ServiceLocatorAwareInterface', $fixture);
$this->assertSame($serviceLocator, $fixture->getServiceLocator());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
namespace DoctrineDataFixtureTest\TestAsset\Fixtures\HasSL;

use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\Common\DataFixtures\FixtureInterface;
use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

class FixtureA implements FixtureInterface, ServiceLocatorAwareInterface
{
/**
* @var ServiceLocatorInterface
*/
protected $serviceLocator = null;


public function load(ObjectManager $manager)
{
}

/**
* Set service locator
*
* @param ServiceLocatorInterface $serviceLocator
* @return mixed
*/
public function setServiceLocator(ServiceLocatorInterface $serviceLocator)
{
$this->serviceLocator = $serviceLocator;

return $this;
}

/**
* Get service locator
*
* @return ServiceLocatorInterface
*/
public function getServiceLocator()
{
return $this->serviceLocator;
}
}
12 changes: 12 additions & 0 deletions tests/DoctrineDataFixtureTest/TestAsset/Fixtures/NoSL/FixtureA.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
namespace DoctrineDataFixtureTest\TestAsset\Fixtures\NoSL;

use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\Common\DataFixtures\FixtureInterface;

class FixtureA implements FixtureInterface
{
public function load(ObjectManager $manager)
{
}
}

0 comments on commit 1a3571c

Please sign in to comment.