Skip to content

Single fixture #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/build
/vendor
composer.lock
composer.phar
composer.phar
clover.xml
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
76 changes: 44 additions & 32 deletions src/DoctrineDataFixtureModule/Command/ImportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
* <http://www.doctrine-project.org>.
*/

Expand All @@ -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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to contact all authors of the library if you want to change the license :|

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have the license already set as MIT for the library. Unfortunately it is set differetnly in the files.

* @since 2.0
* @author Jonathan Wage <[email protected]>
* @author Martin Shwalbe <[email protected]>
*/
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();
}

Expand All @@ -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'));
}
}
41 changes: 39 additions & 2 deletions src/DoctrineDataFixtureModule/Loader/ServiceLocatorAwareLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
* <http://www.doctrine-project.org>.
*/

namespace DoctrineDataFixtureModule\Loader;

use Doctrine\Common\DataFixtures\Loader as BaseLoader;
Expand All @@ -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 <[email protected]>
*/
Expand Down Expand Up @@ -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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing docblock

{
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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing docblock

{
foreach ($paths as $key => $value) {
$this->loadFromDirectory($value);
}
}
}
8 changes: 4 additions & 4 deletions src/DoctrineDataFixtureModule/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
));
});
}
Expand Down
115 changes: 115 additions & 0 deletions tests/DoctrineDataFixtureTest/Command/ImportCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?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 MIT license. For more information, see
* <http://www.doctrine-project.org>.
*/

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 <[email protected]>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing @covers annotation

*/
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);
}
}
Loading