-
Notifications
You must be signed in to change notification settings - Fork 71
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
base: master
Are you sure you want to change the base?
Single fixture #24
Changes from all commits
e197a00
a80eb61
a6a3695
165d81e
f8d5008
5162302
5c65fda
e5d764f
faca963
05cdc06
3712c89
98b5d6c
20c3046
20d74a7
f7af721
866d6ea
6a830d3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
/build | ||
/vendor | ||
composer.lock | ||
composer.phar | ||
composer.phar | ||
clover.xml |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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>. | ||
*/ | ||
|
||
|
@@ -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 | ||
* @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(); | ||
} | ||
|
||
|
@@ -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')); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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]> | ||
*/ | ||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing docblock |
||
{ | ||
foreach ($paths as $key => $value) { | ||
$this->loadFromDirectory($value); | ||
} | ||
} | ||
} |
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]> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing |
||
*/ | ||
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); | ||
} | ||
} |
There was a problem hiding this comment.
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 :|
There was a problem hiding this comment.
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.