Skip to content
Open
Show file tree
Hide file tree
Changes from 8 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
156 changes: 156 additions & 0 deletions clover.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
<?xml version="1.0" encoding="UTF-8"?>
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should not be committed

<coverage generated="1391258417">
<project timestamp="1391258417">
<package name="DoctrineDataFixtureModule\Command">
<file name="/home/martin/Work/github/DoctrineDataFixtureModule/src/DoctrineDataFixtureModule/Command/ImportCommand.php">
<class name="ImportCommand" namespace="DoctrineDataFixtureModule\Command">
<metrics methods="3" coveredmethods="3" conditionals="0" coveredconditionals="0" statements="29" coveredstatements="27" elements="32" coveredelements="30"/>
</class>
<line num="0" type="stmt" count="1"/>
<line num="59" type="method" name="__construct" crap="1" count="1"/>
<line num="65" type="stmt" count="1"/>
<line num="66" type="stmt" count="1"/>
<line num="67" type="stmt" count="1"/>
<line num="68" type="stmt" count="1"/>
<line num="70" type="stmt" count="1"/>
<line num="71" type="stmt" count="1"/>
<line num="73" type="method" name="configure" crap="1" count="1"/>
<line num="75" type="stmt" count="1"/>
<line num="77" type="stmt" count="1"/>
<line num="78" type="stmt" count="1"/>
<line num="79" type="stmt" count="1"/>
<line num="83" type="stmt" count="1"/>
<line num="84" type="stmt" count="1"/>
<line num="85" type="stmt" count="1"/>
<line num="86" type="stmt" count="1"/>
<line num="87" type="stmt" count="1"/>
<line num="88" type="stmt" count="1"/>
<line num="89" type="stmt" count="1"/>
<line num="91" type="stmt" count="1"/>
<line num="92" type="stmt" count="1"/>
<line num="94" type="method" name="execute" crap="3.07" count="1"/>
<line num="96" type="stmt" count="1"/>
<line num="97" type="stmt" count="1"/>
<line num="98" type="stmt" count="1"/>
<line num="100" type="stmt" count="1"/>
<line num="101" type="stmt" count="0"/>
<line num="102" type="stmt" count="0"/>
<line num="103" type="stmt" count="1"/>
<line num="106" type="stmt" count="1"/>
<line num="109" type="stmt" count="1"/>
<line num="110" type="stmt" count="1"/>
<line num="112" type="stmt" count="1"/>
<metrics loc="111" ncloc="81" classes="1" methods="3" coveredmethods="2" conditionals="0" coveredconditionals="0" statements="29" coveredstatements="27" elements="32" coveredelements="29"/>
</file>
</package>
<package name="DoctrineDataFixtureModule\Loader">
<file name="/home/martin/Work/github/DoctrineDataFixtureModule/src/DoctrineDataFixtureModule/Loader/ServiceLocatorAwareLoader.php">
<class name="ServiceLocatorAwareLoader" namespace="DoctrineDataFixtureModule\Loader">
<metrics methods="4" coveredmethods="4" conditionals="0" coveredconditionals="0" statements="23" coveredstatements="22" elements="27" coveredelements="26"/>
</class>
<line num="41" type="method" name="__construct" crap="1" count="6"/>
<line num="43" type="stmt" count="6"/>
<line num="44" type="stmt" count="6"/>
<line num="51" type="method" name="addFixture" crap="2" count="5"/>
<line num="53" type="stmt" count="5"/>
<line num="54" type="stmt" count="3"/>
<line num="55" type="stmt" count="3"/>
<line num="56" type="stmt" count="5"/>
<line num="57" type="stmt" count="5"/>
<line num="59" type="method" name="loadPath" crap="3.01" count="2"/>
<line num="61" type="stmt" count="2"/>
<line num="62" type="stmt" count="1"/>
<line num="63" type="stmt" count="2"/>
<line num="64" type="stmt" count="1"/>
<line num="65" type="stmt" count="1"/>
<line num="66" type="stmt" count="1"/>
<line num="68" type="stmt" count="1"/>
<line num="69" type="stmt" count="1"/>
<line num="70" type="stmt" count="1"/>
<line num="71" type="stmt" count="1"/>
<line num="72" type="stmt" count="0"/>
<line num="74" type="stmt" count="2"/>
<line num="76" type="method" name="loadPaths" crap="2" count="1"/>
<line num="78" type="stmt" count="1"/>
<line num="79" type="stmt" count="1"/>
<line num="80" type="stmt" count="1"/>
<line num="81" type="stmt" count="1"/>
<line num="83" type="stmt" count="1"/>
<line num="87" type="stmt" count="2"/>
<metrics loc="82" ncloc="49" classes="1" methods="4" coveredmethods="3" conditionals="0" coveredconditionals="0" statements="23" coveredstatements="22" elements="27" coveredelements="25"/>
</file>
</package>
<package name="DoctrineDataFixtureModule">
<file name="/home/martin/Work/github/DoctrineDataFixtureModule/src/DoctrineDataFixtureModule/Module.php">
<class name="Module" namespace="DoctrineDataFixtureModule">
<metrics methods="5" coveredmethods="0" conditionals="0" coveredconditionals="0" statements="54" coveredstatements="0" elements="59" coveredelements="0"/>
</class>
<line num="18" type="stmt" count="0"/>
<line num="47" type="method" name="getAutoloaderConfig" crap="2" count="0"/>
<line num="48" type="stmt" count="0"/>
<line num="49" type="stmt" count="0"/>
<line num="50" type="stmt" count="0"/>
<line num="51" type="stmt" count="0"/>
<line num="52" type="stmt" count="0"/>
<line num="53" type="stmt" count="0"/>
<line num="54" type="stmt" count="0"/>
<line num="55" type="stmt" count="0"/>
<line num="56" type="stmt" count="0"/>
<line num="61" type="method" name="init" crap="2" count="0"/>
<line num="62" type="stmt" count="0"/>
<line num="63" type="stmt" count="0"/>
<line num="66" type="method" name="anonymous function" crap="2" count="0"/>
<line num="67" type="stmt" count="0"/>
<line num="68" type="stmt" count="0"/>
<line num="70" type="stmt" count="0"/>
<line num="71" type="stmt" count="0"/>
<line num="72" type="stmt" count="0"/>
<line num="73" type="stmt" count="0"/>
<line num="74" type="stmt" count="0"/>
<line num="76" type="stmt" count="0"/>
<line num="77" type="stmt" count="0"/>
<line num="78" type="stmt" count="0"/>
<line num="79" type="stmt" count="0"/>
<line num="80" type="stmt" count="0"/>
<line num="81" type="stmt" count="0"/>
<line num="82" type="stmt" count="0"/>
<line num="87" type="method" name="getConfig" crap="2" count="0"/>
<line num="88" type="stmt" count="0"/>
<line num="89" type="stmt" count="0"/>
<line num="90" type="stmt" count="0"/>
<line num="95" type="method" name="getServiceConfig" crap="2" count="0"/>
<line num="96" type="stmt" count="0"/>
<line num="97" type="stmt" count="0"/>
<line num="98" type="stmt" count="0"/>
<line num="99" type="stmt" count="0"/>
<line num="100" type="stmt" count="0"/>
<line num="101" type="stmt" count="0"/>
<line num="102" type="stmt" count="0"/>
<metrics loc="103" ncloc="63" classes="1" methods="5" coveredmethods="0" conditionals="0" coveredconditionals="0" statements="41" coveredstatements="0" elements="46" coveredelements="0"/>
</file>
</package>
<package name="DoctrineDataFixtureModule\Service">
<file name="/home/martin/Work/github/DoctrineDataFixtureModule/src/DoctrineDataFixtureModule/Service/FixtureFactory.php">
<class name="FixtureFactory" namespace="DoctrineDataFixtureModule\Service">
<metrics methods="2" coveredmethods="0" conditionals="0" coveredconditionals="0" statements="13" coveredstatements="0" elements="15" coveredelements="0"/>
</class>
<line num="18" type="stmt" count="0"/>
<line num="35" type="method" name="createService" crap="2" count="0"/>
<line num="36" type="stmt" count="0"/>
<line num="38" type="stmt" count="0"/>
<line num="40" type="stmt" count="0"/>
<line num="41" type="stmt" count="0"/>
<line num="52" type="method" name="getOptions" crap="6" count="0"/>
<line num="53" type="stmt" count="0"/>
<line num="54" type="stmt" count="0"/>
<line num="55" type="stmt" count="0"/>
<line num="56" type="stmt" count="0"/>
<line num="57" type="stmt" count="0"/>
<line num="59" type="stmt" count="0"/>
<line num="60" type="stmt" count="0"/>
<metrics loc="61" ncloc="27" classes="1" methods="2" coveredmethods="0" conditionals="0" coveredconditionals="0" statements="14" coveredstatements="0" elements="16" coveredelements="0"/>
</file>
</package>
<metrics files="4" loc="357" ncloc="220" classes="4" methods="14" coveredmethods="5" conditionals="0" coveredconditionals="0" statements="107" coveredstatements="49" elements="121" coveredelements="54"/>
</project>
</coverage>
57 changes: 32 additions & 25 deletions src/DoctrineDataFixtureModule/Command/ImportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
use Doctrine\Common\DataFixtures\Executor\ORMExecutor;
use Doctrine\Common\DataFixtures\Purger\ORMPurger;
use DoctrineDataFixtureModule\Loader\ServiceLocatorAwareLoader;
use Zend\ServiceManager\ServiceLocatorInterface;
use Doctrine\ORM\EntityManager;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you use the EntityManagerInterface? (since ORM 2.4)

Copy link
Owner Author

Choose a reason for hiding this comment

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

I think we should rather create another pr to upgrade it for ORM 2.4

Copy link
Collaborator

Choose a reason for hiding this comment

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

Not a big deal, do it here.


/**
* Command for generate migration classes by comparing your current database schema
Expand All @@ -47,16 +47,26 @@ class ImportCommand extends Command
protected $em;

/**
* Service Locator instance
* @var Zend\ServiceManager\ServiceLocatorInterface
* ServiceLocatorAwareLoader
* @var DoctrineDataFixtureModule\Loader\ServiceLocatorAwareLoader
*/
protected $serviceLocator;
protected $loader;

protected $purger;
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


const PURGE_MODE_TRUNCATE = 2;

public function __construct(ServiceLocatorInterface $serviceLocator)
{
$this->serviceLocator = $serviceLocator;
public function __construct(
ServiceLocatorAwareLoader $loader,
ORMPurger $purger,
EntityManager $em,
array $paths = null
Copy link
Collaborator

Choose a reason for hiding this comment

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

=null?

) {
$this->loader = $loader;
$this->purger = $purger;
$this->em = $em;
$this->paths = $paths;

parent::__construct();
}

Expand All @@ -72,33 +82,30 @@ 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);

foreach ($this->paths as $key => $value) {
$loader->loadFromDirectory($value);
if ($input->getOption('fixtures') != null) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

!==

$this->loader->loadPath($input->getOption('fixtures'));
} else {
$this->loader->loadPaths($this->paths);
}
$executor->execute($loader->getFixtures(), $input->getOption('append'));
}

public function setPath($paths)
{
$this->paths=$paths;
}
$executor = new ORMExecutor($this->em, $this->purger);

public function setEntityManager($em)
{
$this->em = $em;

$executor->execute($this->loader->getFixtures(), $input->getOption('append'));
}
}
24 changes: 24 additions & 0 deletions src/DoctrineDataFixtureModule/Loader/ServiceLocatorAwareLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,28 @@ public function addFixture(FixtureInterface $fixture)
}
parent::addFixture($fixture);
}

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);
} elseif (file_exists($path)) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

No need for the else keyword, as you are returning early

$classes = get_declared_classes();
include($path);
$newClasses = get_declared_classes();

$diff = array_diff($newClasses, $classes);
$class = array_pop($diff);
$this->addFixture(new $class);
} else {
Copy link
Collaborator

Choose a reason for hiding this comment

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

This } else { is not needed if you return early

throw new \RuntimeException('Cannot find File or Directory.');
}
}

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);
}
}
}
7 changes: 4 additions & 3 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,10 +72,9 @@ 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);
$importCommand = new ImportCommand($loader, new ORMPurger, $em, $paths);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Just instantiate this at line 80

ConsoleRunner::addCommands($cli);
$cli->addCommands(array(
$importCommand
Expand Down
Loading