diff --git a/lib/Doctrine/ORM/Tools/Console/Command/ConvertDoctrine1SchemaCommand.php b/lib/Doctrine/ORM/Tools/Console/Command/ConvertDoctrine1SchemaCommand.php index a82824a1771..42985393b40 100644 --- a/lib/Doctrine/ORM/Tools/Console/Command/ConvertDoctrine1SchemaCommand.php +++ b/lib/Doctrine/ORM/Tools/Console/Command/ConvertDoctrine1SchemaCommand.php @@ -25,7 +25,8 @@ Symfony\Components\Console\Input\InputOption, Symfony\Components\Console, Doctrine\ORM\Tools\Export\ClassMetadataExporter, - Doctrine\ORM\Tools\ConvertDoctrine1Schema; + Doctrine\ORM\Tools\ConvertDoctrine1Schema, + Doctrine\ORM\Tools\EntityGenerator; /** * Command to convert a Doctrine 1 schema to a Doctrine 2 mapping file. @@ -41,6 +42,56 @@ */ class ConvertDoctrine1SchemaCommand extends Console\Command\Command { + /** + * @var EntityGenerator + */ + private $entityGenerator = null; + + /** + * @var ClassMetadataExporter + */ + private $metadataExporter = null; + + /** + * @return EntityGenerator + */ + public function getEntityGenerator() + { + if ($this->entityGenerator == null) { + $this->entityGenerator = new EntityGenerator(); + } + + return $this->entityGenerator; + } + + /** + * @param EntityGenerator $entityGenerator + */ + public function setEntityGenerator(EntityGenerator $entityGenerator) + { + $this->entityGenerator = $entityGenerator; + } + + /** + * @return ClassMetadataExporter + */ + public function getMetadataExporter() + { + if ($this->metadataExporter == null) { + $this->metadataExporter = new ClassMetadataExporter(); + } + + return $this->metadataExporter; + } + + /** + * @param ClassMetadataExporter $metadataExporter + */ + public function setMetadataExporter(ClassMetadataExporter $metadataExporter) + { + $this->metadataExporter = $metadataExporter; + } + /** * @see Console\Command\Command */ @@ -90,6 +141,27 @@ protected function execute(Console\Input\InputInterface $input, Console\Output\O // Process source directories $fromPaths = array_merge(array($input->getArgument('from-path')), $input->getOption('from')); + // Process destination directory + $destPath = realpath($input->getArgument('dest-path')); + + $toType = $input->getArgument('to-type'); + $extend = $input->getOption('extend'); + $numSpaces = $input->getOption('num-spaces'); + + $this->convertDoctrine1Schema($em, $fromPaths, $destPath, $toType, $numSpaces, $extend, $output); + } + + /** + * @param \Doctrine\ORM\EntityManager $em + * @param array $fromPaths + * @param string $destPath + * @param string $toType + * @param int $numSpaces + * @param string|null $extend + * @param Console\Output\OutputInterface $output + */ + public function convertDoctrine1Schema($em, $fromPaths, $destPath, $toType, $numSpaces, $extend, $output) + { foreach ($fromPaths as &$dirName) { $dirName = realpath($dirName); @@ -104,9 +176,6 @@ protected function execute(Console\Input\InputInterface $input, Console\Output\O } } - // Process destination directory - $destPath = realpath($input->getArgument('dest-path')); - if ( ! file_exists($destPath)) { throw new \InvalidArgumentException( sprintf("Doctrine 2.X mapping destination directory '%s' does not exist.", $destPath) @@ -117,18 +186,16 @@ protected function execute(Console\Input\InputInterface $input, Console\Output\O ); } - $toType = $input->getArgument('to-type'); - - $cme = new ClassMetadataExporter(); + $cme = $this->getMetadataExporter(); $exporter = $cme->getExporter($toType, $destPath); if (strtolower($toType) === 'annotation') { - $entityGenerator = new EntityGenerator(); + $entityGenerator = $this->getEntityGenerator(); $exporter->setEntityGenerator($entityGenerator); - $entityGenerator->setNumSpaces($input->getOption('num-spaces')); + $entityGenerator->setNumSpaces($numSpaces); - if (($extend = $input->getOption('extend')) !== null) { + if ($extend !== null) { $entityGenerator->setClassToExtend($extend); } } diff --git a/tests/Doctrine/Tests/ORM/Tools/AllTests.php b/tests/Doctrine/Tests/ORM/Tools/AllTests.php index faeeb1dd083..3d9fe929c4e 100644 --- a/tests/Doctrine/Tests/ORM/Tools/AllTests.php +++ b/tests/Doctrine/Tests/ORM/Tools/AllTests.php @@ -28,6 +28,7 @@ public static function suite() $suite->addTestSuite('Doctrine\Tests\ORM\Tools\SchemaToolTest'); $suite->addTestSuite('Doctrine\Tests\ORM\Tools\EntityGeneratorTest'); $suite->addTestSuite('Doctrine\Tests\ORM\Tools\SchemaValidatorTest'); + $suite->addTestSuite('Doctrine\Tests\ORM\Tools\Console\Command\ConvertDoctrine1SchemaCommandTest'); return $suite; } diff --git a/tests/Doctrine/Tests/ORM/Tools/Console/Command/ConvertDoctrine1SchemaCommandTest.php b/tests/Doctrine/Tests/ORM/Tools/Console/Command/ConvertDoctrine1SchemaCommandTest.php new file mode 100644 index 00000000000..cf520fc3981 --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Tools/Console/Command/ConvertDoctrine1SchemaCommandTest.php @@ -0,0 +1,23 @@ +getMock('Doctrine\ORM\Tools\EntityGenerator'); + $metadataExporter = $this->getMock('Doctrine\ORM\Tools\Export\ClassMetadataExporter'); + $command = new ConvertDoctrine1SchemaCommand(); + $command->setEntityGenerator($entityGenerator); + + $output = $this->getMock('Symfony\Components\Console\Output\OutputInterface'); + $output->expects($this->once()) + ->method('write') + ->with($this->equalTo('No Metadata Classes to process.' . PHP_EOL)); + + $command->convertDoctrine1Schema($this->_getTestEntityManager(), array(), sys_get_temp_dir(), 'annotation', 4, null, $output); + } +} \ No newline at end of file