-
Notifications
You must be signed in to change notification settings - Fork 70
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 4 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 |
|---|---|---|
|
|
@@ -51,12 +51,21 @@ class ImportCommand extends Command | |
| * @var Zend\ServiceManager\ServiceLocatorInterface | ||
| */ | ||
| protected $serviceLocator; | ||
| /** | ||
| * ServiceLocatorAwareLoader | ||
| * @var DoctrineDataFixtureModule\Loader\ServiceLocatorAwareLoader | ||
| */ | ||
| protected $loader; | ||
|
|
||
| protected $purger; | ||
|
|
||
| const PURGE_MODE_TRUNCATE = 2; | ||
|
|
||
| public function __construct(ServiceLocatorInterface $serviceLocator) | ||
| { | ||
| $this->serviceLocator = $serviceLocator; | ||
| $this->loader = new ServiceLocatorAwareLoader($this->serviceLocator); | ||
|
|
||
| parent::__construct(); | ||
| } | ||
|
|
||
|
|
@@ -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) { | ||
|
Collaborator
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.
|
||
| $fixtures = $input->getOption('fixtures'); | ||
| if (is_dir($fixtures)) { | ||
| $this->loader->loadFromDirectory($fixtures); | ||
| } elseif (file_exists($fixtures)) { | ||
| $classes = get_declared_classes(); | ||
|
Collaborator
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. 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); | ||
|
Collaborator
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. Classes should be filtered and |
||
| } else { | ||
| throw new \RuntimeException('Cannot find File or Directory.'); | ||
| } | ||
| } else { | ||
| foreach ($this->paths as $key => $value) { | ||
|
Collaborator
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. 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) | ||
|
|
@@ -101,4 +130,14 @@ public function setEntityManager($em) | |
| { | ||
| $this->em = $em; | ||
| } | ||
|
|
||
| public function getLoader() | ||
|
Collaborator
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. Is this getter required?
Owner
Author
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. Not sure how i would get the loader for testing otherwise
Collaborator
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. If you inject the loader at |
||
| { | ||
| return $this->loader; | ||
| } | ||
|
|
||
| public function setPurger(ORMPurger $purger) | ||
|
Collaborator
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. No need for a setter here, I suppose
Owner
Author
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. Injecting the purger on instantiation so i could also inject a mock
Collaborator
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. Can you do it at
Owner
Author
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. suppose that can be done |
||
| { | ||
| $this->purger = $purger; | ||
| } | ||
| } | ||
| 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 | ||
|
Collaborator
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. LGPL?! |
||
| * @link www.doctrine-project.org | ||
| * @author Martin Shwalbe <martin.shwalbe@gmail.com> | ||
|
Collaborator
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 | ||
| { | ||
| /** | ||
| * @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; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| <?php | ||
|
|
||
| namespace Doctrine\Tests\Common\DataFixtures\TestEntity; | ||
|
|
||
| /** | ||
| * @Entity | ||
|
Collaborator
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. 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() | ||
|
Collaborator
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. Remove setters/getters: make props public instead |
||
| { | ||
| return $this->id; | ||
| } | ||
|
|
||
| public function setName($name) | ||
| { | ||
| $this->name = $name; | ||
| } | ||
|
|
||
| public function getName() | ||
| { | ||
| return $this->name; | ||
| } | ||
| } | ||
| 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) | ||
|
Collaborator
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. 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; | ||
| } | ||
| } | ||
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.
Missing docblock