Skip to content

Commit

Permalink
DDC-646 - Bugfix with missing inclusion of Namespace, added test for …
Browse files Browse the repository at this point in the history
…ConvertDoctrine1SchemaCommand
  • Loading branch information
beberlei committed Jun 19, 2010
1 parent 440a255 commit 51e6681
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 10 deletions.
Expand Up @@ -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.
Expand All @@ -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
*/
Expand Down Expand Up @@ -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);

Expand All @@ -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 '<info>%s</info>' does not exist.", $destPath)
Expand All @@ -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);
}
}
Expand Down
1 change: 1 addition & 0 deletions tests/Doctrine/Tests/ORM/Tools/AllTests.php
Expand Up @@ -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;
}
Expand Down
@@ -0,0 +1,23 @@
<?php

namespace Doctrine\Tests\ORM\Tools\Console\Command;

use Doctrine\ORM\Tools\Console\Command\ConvertDoctrine1SchemaCommand;

class ConvertDoctrine1SchemaCommandTest extends \Doctrine\Tests\OrmTestCase
{
public function testExecution()
{
$entityGenerator = $this->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);
}
}

0 comments on commit 51e6681

Please sign in to comment.