Skip to content
Open
Show file tree
Hide file tree
Changes from 4 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
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
57 changes: 48 additions & 9 deletions src/DoctrineDataFixtureModule/Command/ImportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,21 @@ class ImportCommand extends Command
* @var Zend\ServiceManager\ServiceLocatorInterface
*/
protected $serviceLocator;
/**
* ServiceLocatorAwareLoader
* @var DoctrineDataFixtureModule\Loader\ServiceLocatorAwareLoader
*/
protected $loader;

protected $purger;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Missing docblock


const PURGE_MODE_TRUNCATE = 2;

public function __construct(ServiceLocatorInterface $serviceLocator)
{
$this->serviceLocator = $serviceLocator;
$this->loader = new ServiceLocatorAwareLoader($this->serviceLocator);

parent::__construct();
}

Expand All @@ -72,24 +81,44 @@ 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->purger->setPurgeMode(self::PURGE_MODE_TRUNCATE);
}

$executor = new ORMExecutor($this->em, $purger);
if ($input->getOption('fixtures') != null) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

!==

$fixtures = $input->getOption('fixtures');
if (is_dir($fixtures)) {
$this->loader->loadFromDirectory($fixtures);
} elseif (file_exists($fixtures)) {
$classes = get_declared_classes();
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Is this class supposed to handle this? Seems to be a case for a context aware fixture loader

include($fixtures);
$newClasses = get_declared_classes();

foreach ($this->paths as $key => $value) {
$loader->loadFromDirectory($value);
$diff = array_diff($newClasses, $classes);
$class = array_pop($diff);
$this->loader->addFixture(new $class);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Classes should be filtered and class_implements should be used here to filter what has to be instantiated

} else {
throw new \RuntimeException('Cannot find File or Directory.');
}
} else {
foreach ($this->paths as $key => $value) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This logic should also be moved to a fixture loader class, and not be in the command itself

$this->loader->loadFromDirectory($value);
}
}
$executor->execute($loader->getFixtures(), $input->getOption('append'));

$executor = new ORMExecutor($this->em, $this->purger);
$executor->execute($this->loader->getFixtures(), $input->getOption('append'));
}

public function setPath($paths)
Expand All @@ -101,4 +130,14 @@ public function setEntityManager($em)
{
$this->em = $em;
}

public function getLoader()
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Is this getter required?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Not sure how i would get the loader for testing otherwise

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

If you inject the loader at __construct instead of building it in the command, you can keep a reference to it

{
return $this->loader;
}

public function setPurger(ORMPurger $purger)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

No need for a setter here, I suppose

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Injecting the purger on instantiation so i could also inject a mock

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Can you do it at __construct?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

suppose that can be done

{
$this->purger = $purger;
}
}
2 changes: 2 additions & 0 deletions src/DoctrineDataFixtureModule/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use Doctrine\ORM\Tools\Console\ConsoleRunner;
use DoctrineDataFixtureModule\Command\ImportCommand;
use DoctrineDataFixtureModule\Service\FixtureFactory;
use Doctrine\Common\DataFixtures\Purger\ORMPurger;

/**
* Base module for Doctrine Data Fixture.
Expand Down Expand Up @@ -74,6 +75,7 @@ public function init(ModuleManager $e)
$importCommand = new ImportCommand($sm);
$importCommand->setEntityManager($em);
$importCommand->setPath($paths);
$importCommand->setORMPurger(new ORMPurger);
ConsoleRunner::addCommands($cli);
$cli->addCommands(array(
$importCommand
Expand Down
129 changes: 129 additions & 0 deletions tests/DoctrineDataFixtureTest/Command/ImportCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
<?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\Command;


use DoctrineDataFixtureModule\Command\ImportCommand;
use Doctrine\ORM\EntityManager;
use Symfony\Component\Console\Tester\CommandTester;

use Zend\ServiceManager\ServiceManager;
use Zend\Mvc\Service\ServiceManagerConfig;
use PHPUnit_Framework_TestCase;
use Doctrine\ORM\Tools\Setup;

/**
* Test Import commands for fixtures
*
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGPL?!

* @link www.doctrine-project.org
* @author Martin Shwalbe <martin.shwalbe@gmail.com>
Copy link
Copy Markdown
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
{
/**
* @dataProvider provider
*/
public function testExecute($option, $value, $assert)
{
$serviceLocator = new ServiceManager(new ServiceManagerConfig());

$command = new ImportCommand($serviceLocator);

$command->setentityManager($this->getMockSqliteEntityManager());
$command->setPurger($this->getMockPurger());
$paths = array(
'DoctrineDataFixture_Test_Paths' => __DIR__ . '/../TestAsset/Fixtures/NoSL',
);
$command->setPath($paths);

$commandTester = new CommandTester($command);
$commandTester->execute(
array(
$option=> $value,
)
);

$loader= $command->getLoader();
$fixtures = $loader->getFixtures();

$this->assertArrayHasKey($assert, $fixtures);
}

public function provider() {
return array(
array('--append', true, 'DoctrineDataFixtureTest\TestAsset\Fixtures\NoSL\FixtureA'),
array('--fixtures', __DIR__ . '/../TestAsset/Fixtures/NoSL', 'DoctrineDataFixtureTest\TestAsset\Fixtures\NoSL\FixtureA'),
array('--fixtures', __DIR__ . '/../TestAsset/Fixtures/HasSL/FixtureA.php', 'DoctrineDataFixtureTest\TestAsset\Fixtures\HasSL\FixtureA')
);
}

private function getMockFixture($em)
{
return $this->getMock('Doctrine\Common\DataFixtures\FixtureInterface');
}

private function getMockPurger()
{
return $this->getMock('Doctrine\Common\DataFixtures\Purger\ORMPurger');
}

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

protected function getMockEntityManager()
{
$driver = $this->getMock('Doctrine\DBAL\Driver');
$driver->expects($this->once())
->method('getDatabasePlatform')
->will($this->returnValue($this->getMock('Doctrine\DBAL\Platforms\MySqlPlatform')));

$conn = $this->getMock('Doctrine\DBAL\Connection', array(), array(array(), $driver));
$conn->expects($this->once())
->method('getEventManager')
->will($this->returnValue($this->getMock('Doctrine\Common\EventManager')));

$config = $this->getMock('Doctrine\ORM\Configuration');
$config->expects($this->once())
->method('getProxyDir')
->will($this->returnValue('test'));

$config->expects($this->once())
->method('getProxyNamespace')
->will($this->returnValue('Proxies'));

$config->expects($this->once())
->method('getMetadataDriverImpl')
->will($this->returnValue($this->getMock('Doctrine\ORM\Mapping\Driver\DriverChain')));

$em = EntityManager::create($conn, $config);
return $em;
}
}
36 changes: 36 additions & 0 deletions tests/DoctrineDataFixtureTest/TestAsset/Entity/Role.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Doctrine\Tests\Common\DataFixtures\TestEntity;

/**
* @Entity
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The ORM module does not support short annotations

*/
class Role
{
/**
* @Column(type="integer")
* @Id
* @GeneratedValue(strategy="IDENTITY")
*/
private $id;

/**
* @Column(length=50)
*/
private $name;

public function getId()
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Remove setters/getters: make props public instead

{
return $this->id;
}

public function setName($name)
{
$this->name = $name;
}

public function getName()
{
return $this->name;
}
}
76 changes: 76 additions & 0 deletions tests/DoctrineDataFixtureTest/TestAsset/Entity/User.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace DoctrineDataFixtureTest\TestAsset\Entity;

/**
* @Entity
*/
class User
{
/**
* @Column(type="integer")
* @Id
*/
private $id;

/**
* @Column(length=32)
* @Id
*/
private $code;

/**
* @Column(length=32)
*/
private $password;

/**
* @Column(length=255)
*/
private $email;

/**
* @ManyToOne(targetEntity="Role", cascade={"persist"})
*/
private $role;

public function setId($id)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Same as above

{
$this->id = $id;
}

public function setCode($code)
{
$this->code = $code;
}

public function setPassword($password)
{
$this->password = md5($password);
}

public function getPassword()
{
return $this->password;
}

public function setEmail($email)
{
$this->email = $email;
}

public function getEmail()
{
return $this->email;
}

public function setRole(Role $role)
{
$this->role = $role;
}

public function getRole()
{
return $this->role;
}
}