diff --git a/lib/Doctrine/ORM/Tools/Console/Command/ClearCache/CollectionRegionCommand.php b/lib/Doctrine/ORM/Tools/Console/Command/ClearCache/CollectionRegionCommand.php index f12bfa5602..33f003a19a 100644 --- a/lib/Doctrine/ORM/Tools/Console/Command/ClearCache/CollectionRegionCommand.php +++ b/lib/Doctrine/ORM/Tools/Console/Command/ClearCache/CollectionRegionCommand.php @@ -19,13 +19,14 @@ namespace Doctrine\ORM\Tools\Console\Command\ClearCache; +use Doctrine\ORM\Cache; +use Doctrine\ORM\Cache\Region\DefaultRegion; use Symfony\Component\Console\Command\Command; -use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; -use Doctrine\ORM\Cache\Region\DefaultRegion; -use Doctrine\ORM\Cache; +use Symfony\Component\Console\Style\SymfonyStyle; /** * Command to clear a collection cache region. @@ -40,17 +41,14 @@ class CollectionRegionCommand extends Command */ protected function configure() { - $this - ->setName('orm:clear-cache:region:collection') - ->setDescription('Clear a second-level cache collection region.') - ->addArgument('owner-class', InputArgument::OPTIONAL, 'The owner entity name.') - ->addArgument('association', InputArgument::OPTIONAL, 'The association collection name.') - ->addArgument('owner-id', InputArgument::OPTIONAL, 'The owner identifier.') - ->addOption('all', null, InputOption::VALUE_NONE, 'If defined, all entity regions will be deleted/invalidated.') - ->addOption('flush', null, InputOption::VALUE_NONE, 'If defined, all cache entries will be flushed.'); - - - $this->setHelp(<<setName('orm:clear-cache:region:collection') + ->setDescription('Clear a second-level cache collection region') + ->addArgument('owner-class', InputArgument::OPTIONAL, 'The owner entity name.') + ->addArgument('association', InputArgument::OPTIONAL, 'The association collection name.') + ->addArgument('owner-id', InputArgument::OPTIONAL, 'The owner identifier.') + ->addOption('all', null, InputOption::VALUE_NONE, 'If defined, all entity regions will be deleted/invalidated.') + ->addOption('flush', null, InputOption::VALUE_NONE, 'If defined, all cache entries will be flushed.') + ->setHelp(<<%command.name% command is meant to clear a second-level cache collection regions for an associated Entity Manager. It is possible to delete/invalidate all collection region, a specific collection region or flushes the cache provider. @@ -74,7 +72,7 @@ protected function configure() Finally, be aware that if --flush option is passed, not all cache providers are able to flush entries, because of a limitation of its execution nature. EOT - ); + ); } /** @@ -82,6 +80,8 @@ protected function configure() */ protected function execute(InputInterface $input, OutputInterface $output) { + $ui = new SymfonyStyle($input, $output); + $em = $this->getHelper('em')->getEntityManager(); $ownerClass = $input->getArgument('owner-class'); $assoc = $input->getArgument('association'); @@ -92,7 +92,7 @@ protected function execute(InputInterface $input, OutputInterface $output) throw new \InvalidArgumentException('No second-level cache is configured on the given EntityManager.'); } - if ( (! $ownerClass || ! $assoc) && ! $input->getOption('all')) { + if (( ! $ownerClass || ! $assoc) && ! $input->getOption('all')) { throw new \InvalidArgumentException('Missing arguments "--owner-class" "--association"'); } @@ -108,13 +108,19 @@ protected function execute(InputInterface $input, OutputInterface $output) $collectionRegion->getCache()->flushAll(); - $output->writeln(sprintf('Flushing cache provider configured for "%s#%s"', $ownerClass, $assoc)); + $ui->comment( + sprintf( + 'Flushing cache provider configured for "%s#%s"', + $ownerClass, + $assoc + ) + ); return; } if ($input->getOption('all')) { - $output->writeln('Clearing all second-level cache collection regions'); + $ui->comment('Clearing all second-level cache collection regions'); $cache->evictEntityRegions(); @@ -122,13 +128,20 @@ protected function execute(InputInterface $input, OutputInterface $output) } if ($ownerId) { - $output->writeln(sprintf('Clearing second-level cache entry for collection "%s#%s" owner entity identified by "%s"', $ownerClass, $assoc, $ownerId)); + $ui->comment( + sprintf( + 'Clearing second-level cache entry for collection "%s#%s" owner entity identified by "%s"', + $ownerClass, + $assoc, + $ownerId + ) + ); $cache->evictCollection($ownerClass, $assoc, $ownerId); return; } - $output->writeln(sprintf('Clearing second-level cache for collection "%s#%s"', $ownerClass, $assoc)); + $ui->comment(sprintf('Clearing second-level cache for collection "%s#%s"', $ownerClass, $assoc)); $cache->evictCollectionRegion($ownerClass, $assoc); } } diff --git a/lib/Doctrine/ORM/Tools/Console/Command/ClearCache/EntityRegionCommand.php b/lib/Doctrine/ORM/Tools/Console/Command/ClearCache/EntityRegionCommand.php index 0258b7de22..67a73093fc 100644 --- a/lib/Doctrine/ORM/Tools/Console/Command/ClearCache/EntityRegionCommand.php +++ b/lib/Doctrine/ORM/Tools/Console/Command/ClearCache/EntityRegionCommand.php @@ -19,13 +19,14 @@ namespace Doctrine\ORM\Tools\Console\Command\ClearCache; +use Doctrine\ORM\Cache; +use Doctrine\ORM\Cache\Region\DefaultRegion; use Symfony\Component\Console\Command\Command; -use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; -use Doctrine\ORM\Cache\Region\DefaultRegion; -use Doctrine\ORM\Cache; +use Symfony\Component\Console\Style\SymfonyStyle; /** * Command to clear a entity cache region. @@ -40,16 +41,13 @@ class EntityRegionCommand extends Command */ protected function configure() { - $this - ->setName('orm:clear-cache:region:entity') - ->setDescription('Clear a second-level cache entity region.') - ->addArgument('entity-class', InputArgument::OPTIONAL, 'The entity name.') - ->addArgument('entity-id', InputArgument::OPTIONAL, 'The entity identifier.') - ->addOption('all', null, InputOption::VALUE_NONE, 'If defined, all entity regions will be deleted/invalidated.') - ->addOption('flush', null, InputOption::VALUE_NONE, 'If defined, all cache entries will be flushed.'); - - - $this->setHelp(<<setName('orm:clear-cache:region:entity') + ->setDescription('Clear a second-level cache entity region') + ->addArgument('entity-class', InputArgument::OPTIONAL, 'The entity name.') + ->addArgument('entity-id', InputArgument::OPTIONAL, 'The entity identifier.') + ->addOption('all', null, InputOption::VALUE_NONE, 'If defined, all entity regions will be deleted/invalidated.') + ->addOption('flush', null, InputOption::VALUE_NONE, 'If defined, all cache entries will be flushed.') + ->setHelp(<<%command.name% command is meant to clear a second-level cache entity region for an associated Entity Manager. It is possible to delete/invalidate all entity region, a specific entity region or flushes the cache provider. @@ -73,7 +71,7 @@ protected function configure() Finally, be aware that if --flush option is passed, not all cache providers are able to flush entries, because of a limitation of its execution nature. EOT - ); + ); } /** @@ -81,6 +79,8 @@ protected function configure() */ protected function execute(InputInterface $input, OutputInterface $output) { + $ui = new SymfonyStyle($input, $output); + $em = $this->getHelper('em')->getEntityManager(); $entityClass = $input->getArgument('entity-class'); $entityId = $input->getArgument('entity-id'); @@ -106,13 +106,13 @@ protected function execute(InputInterface $input, OutputInterface $output) $entityRegion->getCache()->flushAll(); - $output->writeln(sprintf('Flushing cache provider configured for entity named "%s"', $entityClass)); + $ui->comment(sprintf('Flushing cache provider configured for entity named "%s"', $entityClass)); return; } if ($input->getOption('all')) { - $output->writeln('Clearing all second-level cache entity regions'); + $ui->comment('Clearing all second-level cache entity regions'); $cache->evictEntityRegions(); @@ -120,13 +120,19 @@ protected function execute(InputInterface $input, OutputInterface $output) } if ($entityId) { - $output->writeln(sprintf('Clearing second-level cache entry for entity "%s" identified by "%s"', $entityClass, $entityId)); + $ui->comment( + sprintf( + 'Clearing second-level cache entry for entity "%s" identified by "%s"', + $entityClass, + $entityId + ) + ); $cache->evictEntity($entityClass, $entityId); return; } - $output->writeln(sprintf('Clearing second-level cache for entity "%s"', $entityClass)); + $ui->comment(sprintf('Clearing second-level cache for entity "%s"', $entityClass)); $cache->evictEntityRegion($entityClass); } } diff --git a/lib/Doctrine/ORM/Tools/Console/Command/ClearCache/MetadataCommand.php b/lib/Doctrine/ORM/Tools/Console/Command/ClearCache/MetadataCommand.php index 5bc0c859d6..e6531f2221 100644 --- a/lib/Doctrine/ORM/Tools/Console/Command/ClearCache/MetadataCommand.php +++ b/lib/Doctrine/ORM/Tools/Console/Command/ClearCache/MetadataCommand.php @@ -19,12 +19,13 @@ namespace Doctrine\ORM\Tools\Console\Command\ClearCache; +use Doctrine\Common\Cache\ApcCache; +use Doctrine\Common\Cache\XcacheCache; use Symfony\Component\Console\Command\Command; -use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; -use Doctrine\Common\Cache\ApcCache; -use Doctrine\Common\Cache\XcacheCache; +use Symfony\Component\Console\Style\SymfonyStyle; /** * Command to clear the metadata cache of the various cache drivers. @@ -43,19 +44,10 @@ class MetadataCommand extends Command */ protected function configure() { - $this - ->setName('orm:clear-cache:metadata') - ->setDescription('Clear all metadata cache of the various cache drivers.') - ->setDefinition( - [ - new InputOption( - 'flush', null, InputOption::VALUE_NONE, - 'If defined, cache entries will be flushed instead of deleted/invalidated.' - ) - ] - ); - - $this->setHelp(<<setName('orm:clear-cache:metadata') + ->setDescription('Clear all metadata cache of the various cache drivers') + ->addOption('flush', null, InputOption::VALUE_NONE, 'If defined, cache entries will be flushed instead of deleted/invalidated.') + ->setHelp(<<%command.name% command is meant to clear the metadata cache of associated Entity Manager. It is possible to invalidate all cache entries at once - called delete -, or flushes the cache provider instance completely. @@ -72,7 +64,7 @@ protected function configure() Finally, be aware that if --flush option is passed, not all cache providers are able to flush entries, because of a limitation of its execution nature. EOT - ); + ); } /** @@ -80,6 +72,8 @@ protected function configure() */ protected function execute(InputInterface $input, OutputInterface $output) { + $ui = new SymfonyStyle($input, $output); + $em = $this->getHelper('em')->getEntityManager(); $cacheDriver = $em->getConfiguration()->getMetadataCacheImpl(); @@ -95,8 +89,7 @@ protected function execute(InputInterface $input, OutputInterface $output) throw new \LogicException("Cannot clear XCache Cache from Console, its shared in the Webserver memory and not accessible from the CLI."); } - - $output->writeln('Clearing ALL Metadata cache entries'); + $ui->comment('Clearing all Metadata cache entries'); $result = $cacheDriver->deleteAll(); $message = ($result) ? 'Successfully deleted cache entries.' : 'No cache entries were deleted.'; @@ -106,6 +99,14 @@ protected function execute(InputInterface $input, OutputInterface $output) $message = ($result) ? 'Successfully flushed cache entries.' : $message; } - $output->writeln($message); + if ( ! $result) { + $ui->error($message); + + return 1; + } + + $ui->success($message); + + return 0; } } diff --git a/lib/Doctrine/ORM/Tools/Console/Command/ClearCache/QueryCommand.php b/lib/Doctrine/ORM/Tools/Console/Command/ClearCache/QueryCommand.php index 5828d07716..95bec91de8 100644 --- a/lib/Doctrine/ORM/Tools/Console/Command/ClearCache/QueryCommand.php +++ b/lib/Doctrine/ORM/Tools/Console/Command/ClearCache/QueryCommand.php @@ -19,12 +19,13 @@ namespace Doctrine\ORM\Tools\Console\Command\ClearCache; +use Doctrine\Common\Cache\ApcCache; +use Doctrine\Common\Cache\XcacheCache; use Symfony\Component\Console\Command\Command; -use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; -use Doctrine\Common\Cache\ApcCache; -use Doctrine\Common\Cache\XcacheCache; +use Symfony\Component\Console\Style\SymfonyStyle; /** * Command to clear the query cache of the various cache drivers. @@ -43,19 +44,10 @@ class QueryCommand extends Command */ protected function configure() { - $this - ->setName('orm:clear-cache:query') - ->setDescription('Clear all query cache of the various cache drivers.') - ->setDefinition( - [ - new InputOption( - 'flush', null, InputOption::VALUE_NONE, - 'If defined, cache entries will be flushed instead of deleted/invalidated.' - ) - ] - ); - - $this->setHelp(<<setName('orm:clear-cache:query') + ->setDescription('Clear all query cache of the various cache drivers') + ->addOption('flush', null, InputOption::VALUE_NONE, 'If defined, cache entries will be flushed instead of deleted/invalidated.') + ->setHelp(<<%command.name% command is meant to clear the query cache of associated Entity Manager. It is possible to invalidate all cache entries at once - called delete -, or flushes the cache provider instance completely. @@ -72,7 +64,7 @@ protected function configure() Finally, be aware that if --flush option is passed, not all cache providers are able to flush entries, because of a limitation of its execution nature. EOT - ); + ); } /** @@ -80,6 +72,8 @@ protected function configure() */ protected function execute(InputInterface $input, OutputInterface $output) { + $ui = new SymfonyStyle($input, $output); + $em = $this->getHelper('em')->getEntityManager(); $cacheDriver = $em->getConfiguration()->getQueryCacheImpl(); @@ -94,7 +88,7 @@ protected function execute(InputInterface $input, OutputInterface $output) throw new \LogicException("Cannot clear XCache Cache from Console, its shared in the Webserver memory and not accessible from the CLI."); } - $output->write('Clearing ALL Query cache entries' . PHP_EOL); + $ui->comment('Clearing all Query cache entries'); $result = $cacheDriver->deleteAll(); $message = ($result) ? 'Successfully deleted cache entries.' : 'No cache entries were deleted.'; @@ -104,6 +98,14 @@ protected function execute(InputInterface $input, OutputInterface $output) $message = ($result) ? 'Successfully flushed cache entries.' : $message; } - $output->write($message . PHP_EOL); + if ( ! $result) { + $ui->error($message); + + return 1; + } + + $ui->success($message); + + return 0; } } diff --git a/lib/Doctrine/ORM/Tools/Console/Command/ClearCache/QueryRegionCommand.php b/lib/Doctrine/ORM/Tools/Console/Command/ClearCache/QueryRegionCommand.php index 0637c7a4a0..f1578bc641 100644 --- a/lib/Doctrine/ORM/Tools/Console/Command/ClearCache/QueryRegionCommand.php +++ b/lib/Doctrine/ORM/Tools/Console/Command/ClearCache/QueryRegionCommand.php @@ -19,13 +19,14 @@ namespace Doctrine\ORM\Tools\Console\Command\ClearCache; +use Doctrine\ORM\Cache; +use Doctrine\ORM\Cache\Region\DefaultRegion; use Symfony\Component\Console\Command\Command; -use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; -use Doctrine\ORM\Cache\Region\DefaultRegion; -use Doctrine\ORM\Cache; +use Symfony\Component\Console\Style\SymfonyStyle; /** * Command to clear a query cache region. @@ -40,15 +41,12 @@ class QueryRegionCommand extends Command */ protected function configure() { - $this - ->setName('orm:clear-cache:region:query') - ->setDescription('Clear a second-level cache query region.') - ->addArgument('region-name', InputArgument::OPTIONAL, 'The query region to clear.') - ->addOption('all', null, InputOption::VALUE_NONE, 'If defined, all query regions will be deleted/invalidated.') - ->addOption('flush', null, InputOption::VALUE_NONE, 'If defined, all cache entries will be flushed.'); - - - $this->setHelp(<<setName('orm:clear-cache:region:query') + ->setDescription('Clear a second-level cache query region') + ->addArgument('region-name', InputArgument::OPTIONAL, 'The query region to clear.') + ->addOption('all', null, InputOption::VALUE_NONE, 'If defined, all query regions will be deleted/invalidated.') + ->addOption('flush', null, InputOption::VALUE_NONE, 'If defined, all cache entries will be flushed.') + ->setHelp(<<%command.name% command is meant to clear a second-level cache query region for an associated Entity Manager. It is possible to delete/invalidate all query region, a specific query region or flushes the cache provider. @@ -72,7 +70,7 @@ protected function configure() Finally, be aware that if --flush option is passed, not all cache providers are able to flush entries, because of a limitation of its execution nature. EOT - ); + ); } /** @@ -80,6 +78,8 @@ protected function configure() */ protected function execute(InputInterface $input, OutputInterface $output) { + $ui = new SymfonyStyle($input, $output); + $em = $this->getHelper('em')->getEntityManager(); $name = $input->getArgument('region-name'); $cache = $em->getCache(); @@ -105,20 +105,25 @@ protected function execute(InputInterface $input, OutputInterface $output) $queryRegion->getCache()->flushAll(); - $output->writeln(sprintf('Flushing cache provider configured for second-level cache query region named "%s"', $name)); + $ui->comment( + sprintf( + 'Flushing cache provider configured for second-level cache query region named "%s"', + $name + ) + ); return; } if ($input->getOption('all')) { - $output->writeln('Clearing all second-level cache query regions'); + $ui->comment('Clearing all second-level cache query regions'); $cache->evictQueryRegions(); return; } - $output->writeln(sprintf('Clearing second-level cache query region named "%s"', $name)); + $ui->comment(sprintf('Clearing second-level cache query region named "%s"', $name)); $cache->evictQueryRegion($name); } } diff --git a/lib/Doctrine/ORM/Tools/Console/Command/ClearCache/ResultCommand.php b/lib/Doctrine/ORM/Tools/Console/Command/ClearCache/ResultCommand.php index 55b89efa8e..42ad79c502 100644 --- a/lib/Doctrine/ORM/Tools/Console/Command/ClearCache/ResultCommand.php +++ b/lib/Doctrine/ORM/Tools/Console/Command/ClearCache/ResultCommand.php @@ -19,12 +19,13 @@ namespace Doctrine\ORM\Tools\Console\Command\ClearCache; +use Doctrine\Common\Cache\ApcCache; +use Doctrine\Common\Cache\XcacheCache; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; -use Doctrine\Common\Cache\ApcCache; -use Doctrine\Common\Cache\XcacheCache; +use Symfony\Component\Console\Style\SymfonyStyle; /** * Command to clear the result cache of the various cache drivers. @@ -43,19 +44,10 @@ class ResultCommand extends Command */ protected function configure() { - $this - ->setName('orm:clear-cache:result') - ->setDescription('Clear all result cache of the various cache drivers.') - ->setDefinition( - [ - new InputOption( - 'flush', null, InputOption::VALUE_NONE, - 'If defined, cache entries will be flushed instead of deleted/invalidated.' - ) - ] - ); - - $this->setHelp(<<setName('orm:clear-cache:result') + ->setDescription('Clear all result cache of the various cache drivers') + ->addOption('flush', null, InputOption::VALUE_NONE, 'If defined, cache entries will be flushed instead of deleted/invalidated.') + ->setHelp(<<%command.name% command is meant to clear the result cache of associated Entity Manager. It is possible to invalidate all cache entries at once - called delete -, or flushes the cache provider instance completely. @@ -72,7 +64,7 @@ protected function configure() Finally, be aware that if --flush option is passed, not all cache providers are able to flush entries, because of a limitation of its execution nature. EOT - ); + ); } /** @@ -80,6 +72,8 @@ protected function configure() */ protected function execute(InputInterface $input, OutputInterface $output) { + $ui = new SymfonyStyle($input, $output); + $em = $this->getHelper('em')->getEntityManager(); $cacheDriver = $em->getConfiguration()->getResultCacheImpl(); @@ -95,7 +89,7 @@ protected function execute(InputInterface $input, OutputInterface $output) throw new \LogicException("Cannot clear XCache Cache from Console, its shared in the Webserver memory and not accessible from the CLI."); } - $output->writeln('Clearing ALL Result cache entries'); + $ui->comment('Clearing all Result cache entries'); $result = $cacheDriver->deleteAll(); $message = ($result) ? 'Successfully deleted cache entries.' : 'No cache entries were deleted.'; @@ -105,6 +99,14 @@ protected function execute(InputInterface $input, OutputInterface $output) $message = ($result) ? 'Successfully flushed cache entries.' : $message; } - $output->writeln($message); + if ( ! $result) { + $ui->error($message); + + return 1; + } + + $ui->success($message); + + return 0; } } diff --git a/lib/Doctrine/ORM/Tools/Console/Command/ConvertDoctrine1SchemaCommand.php b/lib/Doctrine/ORM/Tools/Console/Command/ConvertDoctrine1SchemaCommand.php index 6c81de940d..b6b43bf8e0 100644 --- a/lib/Doctrine/ORM/Tools/Console/Command/ConvertDoctrine1SchemaCommand.php +++ b/lib/Doctrine/ORM/Tools/Console/Command/ConvertDoctrine1SchemaCommand.php @@ -19,14 +19,14 @@ namespace Doctrine\ORM\Tools\Console\Command; -use Symfony\Component\Console\Input\InputArgument; -use Symfony\Component\Console\Input\InputOption; -use Doctrine\ORM\Tools\Export\ClassMetadataExporter; use Doctrine\ORM\Tools\ConvertDoctrine1Schema; use Doctrine\ORM\Tools\EntityGenerator; -use Symfony\Component\Console\Output\OutputInterface; -use Symfony\Component\Console\Input\InputInterface; +use Doctrine\ORM\Tools\Export\ClassMetadataExporter; use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Output\OutputInterface; /** * Command to convert a Doctrine 1 schema to a Doctrine 2 mapping file. @@ -99,41 +99,16 @@ public function setMetadataExporter(ClassMetadataExporter $metadataExporter) */ protected function configure() { - $this - ->setName('orm:convert-d1-schema') - ->setAliases(['orm:convert:d1-schema']) - ->setDescription('Converts Doctrine 1.X schema into a Doctrine 2.X schema.') - ->setDefinition( - [ - new InputArgument( - 'from-path', InputArgument::REQUIRED, 'The path of Doctrine 1.X schema information.' - ), - new InputArgument( - 'to-type', InputArgument::REQUIRED, 'The destination Doctrine 2.X mapping type.' - ), - new InputArgument( - 'dest-path', InputArgument::REQUIRED, - 'The path to generate your Doctrine 2.X mapping information.' - ), - new InputOption( - 'from', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, - 'Optional paths of Doctrine 1.X schema information.', - [] - ), - new InputOption( - 'extend', null, InputOption::VALUE_OPTIONAL, - 'Defines a base class to be extended by generated entity classes.' - ), - new InputOption( - 'num-spaces', null, InputOption::VALUE_OPTIONAL, - 'Defines the number of indentation spaces', 4 - ) - ] - ) - ->setHelp(<<setName('orm:convert-d1-schema') + ->setAliases(['orm:convert:d1-schema']) + ->setDescription('Converts Doctrine 1.x schema into a Doctrine 2.x schema') + ->addArgument('from-path', InputArgument::REQUIRED, 'The path of Doctrine 1.X schema information.') + ->addArgument('to-type', InputArgument::REQUIRED, 'The destination Doctrine 2.X mapping type.') + ->addArgument('dest-path', InputArgument::REQUIRED, 'The path to generate your Doctrine 2.X mapping information.') + ->addOption('from', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Optional paths of Doctrine 1.X schema information.', []) + ->addOption('extend', null, InputOption::VALUE_OPTIONAL, 'Defines a base class to be extended by generated entity classes.') + ->addOption('num-spaces', null, InputOption::VALUE_OPTIONAL, 'Defines the number of indentation spaces', 4) + ->setHelp('Converts Doctrine 1.x schema into a Doctrine 2.x schema.'); } /** diff --git a/lib/Doctrine/ORM/Tools/Console/Command/ConvertMappingCommand.php b/lib/Doctrine/ORM/Tools/Console/Command/ConvertMappingCommand.php index 798dbe7285..f675664114 100644 --- a/lib/Doctrine/ORM/Tools/Console/Command/ConvertMappingCommand.php +++ b/lib/Doctrine/ORM/Tools/Console/Command/ConvertMappingCommand.php @@ -19,16 +19,17 @@ namespace Doctrine\ORM\Tools\Console\Command; -use Symfony\Component\Console\Input\InputArgument; -use Symfony\Component\Console\Input\InputOption; +use Doctrine\ORM\Mapping\Driver\DatabaseDriver; use Doctrine\ORM\Tools\Console\MetadataFilter; -use Doctrine\ORM\Tools\Export\ClassMetadataExporter; -use Doctrine\ORM\Tools\EntityGenerator; use Doctrine\ORM\Tools\DisconnectedClassMetadataFactory; -use Doctrine\ORM\Mapping\Driver\DatabaseDriver; -use Symfony\Component\Console\Output\OutputInterface; -use Symfony\Component\Console\Input\InputInterface; +use Doctrine\ORM\Tools\EntityGenerator; +use Doctrine\ORM\Tools\Export\ClassMetadataExporter; use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Console\Style\SymfonyStyle; /** * Command to convert your mapping information between the various formats. @@ -47,45 +48,18 @@ class ConvertMappingCommand extends Command */ protected function configure() { - $this - ->setName('orm:convert-mapping') - ->setAliases(['orm:convert:mapping']) - ->setDescription('Convert mapping information between supported formats.') - ->setDefinition( - [ - new InputOption( - 'filter', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, - 'A string pattern used to match entities that should be processed.' - ), - new InputArgument( - 'to-type', InputArgument::REQUIRED, 'The mapping type to be converted.' - ), - new InputArgument( - 'dest-path', InputArgument::REQUIRED, - 'The path to generate your entities classes.' - ), - new InputOption( - 'force', 'f', InputOption::VALUE_NONE, - 'Force to overwrite existing mapping files.' - ), - new InputOption( - 'from-database', null, null, 'Whether or not to convert mapping information from existing database.' - ), - new InputOption( - 'extend', null, InputOption::VALUE_OPTIONAL, - 'Defines a base class to be extended by generated entity classes.' - ), - new InputOption( - 'num-spaces', null, InputOption::VALUE_OPTIONAL, - 'Defines the number of indentation spaces', 4 - ), - new InputOption( - 'namespace', null, InputOption::VALUE_OPTIONAL, - 'Defines a namespace for the generated entity classes, if converted from database.' - ), - ] - ) - ->setHelp(<<setName('orm:convert-mapping') + ->setAliases(['orm:convert:mapping']) + ->setDescription('Convert mapping information between supported formats') + ->addArgument('to-type', InputArgument::REQUIRED, 'The mapping type to be converted.') + ->addArgument('dest-path', InputArgument::REQUIRED, 'The path to generate your entities classes.') + ->addOption('filter', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'A string pattern used to match entities that should be processed.') + ->addOption('force', 'f', InputOption::VALUE_NONE, 'Force to overwrite existing mapping files.') + ->addOption('from-database', null, null, 'Whether or not to convert mapping information from existing database.') + ->addOption('extend', null, InputOption::VALUE_OPTIONAL, 'Defines a base class to be extended by generated entity classes.') + ->addOption('num-spaces', null, InputOption::VALUE_OPTIONAL, 'Defines the number of indentation spaces', 4) + ->addOption('namespace', null, InputOption::VALUE_OPTIONAL, 'Defines a namespace for the generated entity classes, if converted from database.') + ->setHelp(<<one-time command. It should not be necessary for @@ -108,7 +82,7 @@ protected function configure() \$config->setFilterSchemaAssetsExpression(\$regexp); EOT - ); + ); } /** @@ -116,6 +90,8 @@ protected function configure() */ protected function execute(InputInterface $input, OutputInterface $output) { + $ui = new SymfonyStyle($input, $output); + $em = $this->getHelper('em')->getEntityManager(); if ($input->getOption('from-database') === true) { @@ -171,20 +147,26 @@ protected function execute(InputInterface $input, OutputInterface $output) } } - if (count($metadata)) { - foreach ($metadata as $class) { - $output->writeln(sprintf('Processing entity "%s"', $class->name)); - } - - $exporter->setMetadata($metadata); - $exporter->export(); + if (empty($metadata)) { + $ui->success('No Metadata Classes to process.'); + return; + } - $output->writeln(PHP_EOL . sprintf( - 'Exporting "%s" mapping information to "%s"', $toType, $destPath - )); - } else { - $output->writeln('No Metadata Classes to process.'); + foreach ($metadata as $class) { + $ui->text(sprintf('Processing entity "%s"', $class->name)); } + + $exporter->setMetadata($metadata); + $exporter->export(); + + $ui->newLine(); + $ui->text( + sprintf( + 'Exporting "%s" mapping information to "%s"', + $toType, + $destPath + ) + ); } /** diff --git a/lib/Doctrine/ORM/Tools/Console/Command/EnsureProductionSettingsCommand.php b/lib/Doctrine/ORM/Tools/Console/Command/EnsureProductionSettingsCommand.php index 180254f9ce..eb0ff17567 100644 --- a/lib/Doctrine/ORM/Tools/Console/Command/EnsureProductionSettingsCommand.php +++ b/lib/Doctrine/ORM/Tools/Console/Command/EnsureProductionSettingsCommand.php @@ -20,9 +20,10 @@ namespace Doctrine\ORM\Tools\Console\Command; use Symfony\Component\Console\Command\Command; -use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Console\Style\SymfonyStyle; use Throwable; /** @@ -43,21 +44,10 @@ class EnsureProductionSettingsCommand extends Command */ protected function configure() { - $this - ->setName('orm:ensure-production-settings') - ->setDescription('Verify that Doctrine is properly configured for a production environment.') - ->setDefinition( - [ - new InputOption( - 'complete', null, InputOption::VALUE_NONE, - 'Flag to also inspect database connection existence.' - ) - ] - ) - ->setHelp(<<setName('orm:ensure-production-settings') + ->setDescription('Verify that Doctrine is properly configured for a production environment') + ->addOption('complete', null, InputOption::VALUE_NONE, 'Flag to also inspect database connection existence.') + ->setHelp('Verify that Doctrine is properly configured for a production environment.'); } /** @@ -65,6 +55,8 @@ protected function configure() */ protected function execute(InputInterface $input, OutputInterface $output) { + $ui = new SymfonyStyle($input, $output); + $em = $this->getHelper('em')->getEntityManager(); try { @@ -74,11 +66,13 @@ protected function execute(InputInterface $input, OutputInterface $output) $em->getConnection()->connect(); } } catch (Throwable $e) { - $output->writeln('' . $e->getMessage() . ''); + $ui->error($e->getMessage()); return 1; } - $output->writeln('Environment is correctly configured for production.'); + $ui->success('Environment is correctly configured for production.'); + + return 0; } } diff --git a/lib/Doctrine/ORM/Tools/Console/Command/GenerateEntitiesCommand.php b/lib/Doctrine/ORM/Tools/Console/Command/GenerateEntitiesCommand.php index 080bfc97ac..063cb244ce 100644 --- a/lib/Doctrine/ORM/Tools/Console/Command/GenerateEntitiesCommand.php +++ b/lib/Doctrine/ORM/Tools/Console/Command/GenerateEntitiesCommand.php @@ -19,14 +19,15 @@ namespace Doctrine\ORM\Tools\Console\Command; -use Symfony\Component\Console\Input\InputArgument; -use Symfony\Component\Console\Input\InputOption; use Doctrine\ORM\Tools\Console\MetadataFilter; -use Doctrine\ORM\Tools\EntityGenerator; use Doctrine\ORM\Tools\DisconnectedClassMetadataFactory; -use Symfony\Component\Console\Output\OutputInterface; -use Symfony\Component\Console\Input\InputInterface; +use Doctrine\ORM\Tools\EntityGenerator; use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Console\Style\SymfonyStyle; /** * Command to generate entity classes and method stubs from your mapping information. @@ -45,50 +46,19 @@ class GenerateEntitiesCommand extends Command */ protected function configure() { - $this - ->setName('orm:generate-entities') - ->setAliases(['orm:generate:entities']) - ->setDescription('Generate entity classes and method stubs from your mapping information.') - ->setDefinition( - [ - new InputOption( - 'filter', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, - 'A string pattern used to match entities that should be processed.' - ), - new InputArgument( - 'dest-path', InputArgument::REQUIRED, 'The path to generate your entity classes.' - ), - new InputOption( - 'generate-annotations', null, InputOption::VALUE_OPTIONAL, - 'Flag to define if generator should generate annotation metadata on entities.', false - ), - new InputOption( - 'generate-methods', null, InputOption::VALUE_OPTIONAL, - 'Flag to define if generator should generate stub methods on entities.', true - ), - new InputOption( - 'regenerate-entities', null, InputOption::VALUE_OPTIONAL, - 'Flag to define if generator should regenerate entity if it exists.', false - ), - new InputOption( - 'update-entities', null, InputOption::VALUE_OPTIONAL, - 'Flag to define if generator should only update entity if it exists.', true - ), - new InputOption( - 'extend', null, InputOption::VALUE_REQUIRED, - 'Defines a base class to be extended by generated entity classes.' - ), - new InputOption( - 'num-spaces', null, InputOption::VALUE_REQUIRED, - 'Defines the number of indentation spaces', 4 - ), - new InputOption( - 'no-backup', null, InputOption::VALUE_NONE, - 'Flag to define if generator should avoid backuping existing entity file if it exists.' - ) - ] - ) - ->setHelp(<<setName('orm:generate-entities') + ->setAliases(['orm:generate:entities']) + ->setDescription('Generate entity classes and method stubs from your mapping information') + ->addArgument('dest-path', InputArgument::REQUIRED, 'The path to generate your entity classes.') + ->addOption('filter', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'A string pattern used to match entities that should be processed.') + ->addOption('generate-annotations', null, InputOption::VALUE_OPTIONAL, 'Flag to define if generator should generate annotation metadata on entities.', false) + ->addOption('generate-methods', null, InputOption::VALUE_OPTIONAL, 'Flag to define if generator should generate stub methods on entities.', true) + ->addOption('regenerate-entities', null, InputOption::VALUE_OPTIONAL, 'Flag to define if generator should regenerate entity if it exists.', false) + ->addOption('update-entities', null, InputOption::VALUE_OPTIONAL, 'Flag to define if generator should only update entity if it exists.', true) + ->addOption('extend', null, InputOption::VALUE_REQUIRED, 'Defines a base class to be extended by generated entity classes.') + ->addOption('num-spaces', null, InputOption::VALUE_REQUIRED, 'Defines the number of indentation spaces', 4) + ->addOption('no-backup', null, InputOption::VALUE_NONE, 'Flag to define if generator should avoid backuping existing entity file if it exists.') + ->setHelp(<<--update-entities or --regenerate-entities flags your existing @@ -108,7 +78,7 @@ protected function configure() class is supposed to extend which. You have to adjust the entity code manually for inheritance to work! EOT - ); + ); } /** @@ -116,6 +86,8 @@ class is supposed to extend which. You have to adjust the entity */ protected function execute(InputInterface $input, OutputInterface $output) { + $ui = new SymfonyStyle($input, $output); + $em = $this->getHelper('em')->getEntityManager(); $cmf = new DisconnectedClassMetadataFactory(); @@ -138,34 +110,33 @@ protected function execute(InputInterface $input, OutputInterface $output) ); } - if (count($metadatas)) { - // Create EntityGenerator - $entityGenerator = new EntityGenerator(); - - $entityGenerator->setGenerateAnnotations($input->getOption('generate-annotations')); - $entityGenerator->setGenerateStubMethods($input->getOption('generate-methods')); - $entityGenerator->setRegenerateEntityIfExists($input->getOption('regenerate-entities')); - $entityGenerator->setUpdateEntityIfExists($input->getOption('update-entities')); - $entityGenerator->setNumSpaces($input->getOption('num-spaces')); - $entityGenerator->setBackupExisting(!$input->getOption('no-backup')); - - if (($extend = $input->getOption('extend')) !== null) { - $entityGenerator->setClassToExtend($extend); - } - - foreach ($metadatas as $metadata) { - $output->writeln( - sprintf('Processing entity "%s"', $metadata->name) - ); - } - - // Generating Entities - $entityGenerator->generate($metadatas, $destPath); - - // Outputting information message - $output->writeln(PHP_EOL . sprintf('Entity classes generated to "%s"', $destPath)); - } else { - $output->writeln('No Metadata Classes to process.'); + if (empty($metadatas)) { + $ui->success('No Metadata Classes to process.'); + return; + } + + $entityGenerator = new EntityGenerator(); + + $entityGenerator->setGenerateAnnotations($input->getOption('generate-annotations')); + $entityGenerator->setGenerateStubMethods($input->getOption('generate-methods')); + $entityGenerator->setRegenerateEntityIfExists($input->getOption('regenerate-entities')); + $entityGenerator->setUpdateEntityIfExists($input->getOption('update-entities')); + $entityGenerator->setNumSpaces($input->getOption('num-spaces')); + $entityGenerator->setBackupExisting(!$input->getOption('no-backup')); + + if (($extend = $input->getOption('extend')) !== null) { + $entityGenerator->setClassToExtend($extend); } + + foreach ($metadatas as $metadata) { + $ui->text(sprintf('Processing entity "%s"', $metadata->name)); + } + + // Generating Entities + $entityGenerator->generate($metadatas, $destPath); + + // Outputting information message + $ui->newLine(); + $ui->success(sprintf('Entity classes generated to "%s"', $destPath)); } } diff --git a/lib/Doctrine/ORM/Tools/Console/Command/GenerateProxiesCommand.php b/lib/Doctrine/ORM/Tools/Console/Command/GenerateProxiesCommand.php index 9475bca71b..c792b0067a 100644 --- a/lib/Doctrine/ORM/Tools/Console/Command/GenerateProxiesCommand.php +++ b/lib/Doctrine/ORM/Tools/Console/Command/GenerateProxiesCommand.php @@ -19,12 +19,14 @@ namespace Doctrine\ORM\Tools\Console\Command; +use Doctrine\ORM\EntityManagerInterface; +use Doctrine\ORM\Tools\Console\MetadataFilter; +use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; -use Doctrine\ORM\Tools\Console\MetadataFilter; use Symfony\Component\Console\Output\OutputInterface; -use Symfony\Component\Console\Input\InputInterface; -use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Style\SymfonyStyle; /** * Command to (re)generate the proxy classes used by doctrine. @@ -43,26 +45,12 @@ class GenerateProxiesCommand extends Command */ protected function configure() { - $this - ->setName('orm:generate-proxies') - ->setAliases(['orm:generate:proxies']) - ->setDescription('Generates proxy classes for entity classes.') - ->setDefinition( - [ - new InputOption( - 'filter', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, - 'A string pattern used to match entities that should be processed.' - ), - new InputArgument( - 'dest-path', InputArgument::OPTIONAL, - 'The path to generate your proxy classes. If none is provided, it will attempt to grab from configuration.' - ), - ] - ) - ->setHelp(<<setName('orm:generate-proxies') + ->setAliases(['orm:generate:proxies']) + ->setDescription('Generates proxy classes for entity classes') + ->addArgument('dest-path', InputArgument::OPTIONAL, 'The path to generate your proxy classes. If none is provided, it will attempt to grab from configuration.') + ->addOption('filter', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'A string pattern used to match entities that should be processed.') + ->setHelp('Generates proxy classes for entity classes.'); } /** @@ -70,6 +58,9 @@ protected function configure() */ protected function execute(InputInterface $input, OutputInterface $output) { + $ui = new SymfonyStyle($input, $output); + + /** @var EntityManagerInterface $em */ $em = $this->getHelper('em')->getEntityManager(); $metadatas = $em->getMetadataFactory()->getAllMetadata(); @@ -98,20 +89,20 @@ protected function execute(InputInterface $input, OutputInterface $output) ); } - if ( count($metadatas)) { - foreach ($metadatas as $metadata) { - $output->writeln( - sprintf('Processing entity "%s"', $metadata->name) - ); - } - - // Generating Proxies - $em->getProxyFactory()->generateProxyClasses($metadatas, $destPath); + if (empty($metadatas)) { + $ui->success('No Metadata Classes to process.'); + return; + } - // Outputting information message - $output->writeln(PHP_EOL . sprintf('Proxy classes generated to "%s"', $destPath)); - } else { - $output->writeln('No Metadata Classes to process.'); + foreach ($metadatas as $metadata) { + $ui->text(sprintf('Processing entity "%s"', $metadata->name)); } + + // Generating Proxies + $em->getProxyFactory()->generateProxyClasses($metadatas, $destPath); + + // Outputting information message + $ui->newLine(); + $ui->text(sprintf('Proxy classes generated to "%s"', $destPath)); } } diff --git a/lib/Doctrine/ORM/Tools/Console/Command/GenerateRepositoriesCommand.php b/lib/Doctrine/ORM/Tools/Console/Command/GenerateRepositoriesCommand.php index 1da7a85c14..787fb8bc50 100644 --- a/lib/Doctrine/ORM/Tools/Console/Command/GenerateRepositoriesCommand.php +++ b/lib/Doctrine/ORM/Tools/Console/Command/GenerateRepositoriesCommand.php @@ -19,13 +19,14 @@ namespace Doctrine\ORM\Tools\Console\Command; -use Symfony\Component\Console\Input\InputArgument; -use Symfony\Component\Console\Input\InputOption; use Doctrine\ORM\Tools\Console\MetadataFilter; use Doctrine\ORM\Tools\EntityRepositoryGenerator; -use Symfony\Component\Console\Output\OutputInterface; -use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Console\Style\SymfonyStyle; /** * Command to generate repository classes for mapping information. @@ -44,25 +45,12 @@ class GenerateRepositoriesCommand extends Command */ protected function configure() { - $this - ->setName('orm:generate-repositories') - ->setAliases(['orm:generate:repositories']) - ->setDescription('Generate repository classes from your mapping information.') - ->setDefinition( - [ - new InputOption( - 'filter', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, - 'A string pattern used to match entities that should be processed.' - ), - new InputArgument( - 'dest-path', InputArgument::REQUIRED, 'The path to generate your repository classes.' - ) - ] - ) - ->setHelp(<<setName('orm:generate-repositories') + ->setAliases(['orm:generate:repositories']) + ->setDescription('Generate repository classes from your mapping information') + ->addArgument('dest-path', InputArgument::REQUIRED, 'The path to generate your repository classes.') + ->addOption('filter', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'A string pattern used to match entities that should be processed.') + ->setHelp('Generate repository classes from your mapping information.'); } /** @@ -70,6 +58,8 @@ protected function configure() */ protected function execute(InputInterface $input, OutputInterface $output) { + $ui = new SymfonyStyle($input, $output); + $em = $this->getHelper('em')->getEntityManager(); $metadatas = $em->getMetadataFactory()->getAllMetadata(); @@ -92,32 +82,33 @@ protected function execute(InputInterface $input, OutputInterface $output) ); } - if (count($metadatas)) { - $numRepositories = 0; - $generator = new EntityRepositoryGenerator(); + if (empty($metadatas)) { + $ui->success('No Metadata Classes to process.'); + return; + } - $generator->setDefaultRepositoryName($repositoryName); + $numRepositories = 0; + $generator = new EntityRepositoryGenerator(); - foreach ($metadatas as $metadata) { - if ($metadata->customRepositoryClassName) { - $output->writeln( - sprintf('Processing repository "%s"', $metadata->customRepositoryClassName) - ); + $generator->setDefaultRepositoryName($repositoryName); - $generator->writeEntityRepositoryClass($metadata->customRepositoryClassName, $destPath); + foreach ($metadatas as $metadata) { + if ($metadata->customRepositoryClassName) { + $ui->text(sprintf('Processing repository "%s"', $metadata->customRepositoryClassName)); - $numRepositories++; - } - } + $generator->writeEntityRepositoryClass($metadata->customRepositoryClassName, $destPath); - if ($numRepositories) { - // Outputting information message - $output->writeln(PHP_EOL . sprintf('Repository classes generated to "%s"', $destPath)); - } else { - $output->writeln('No Repository classes were found to be processed.'); + ++$numRepositories; } - } else { - $output->writeln('No Metadata Classes to process.'); } + + if ($numRepositories === 0) { + $ui->text('No Repository classes were found to be processed.'); + return; + } + + // Outputting information message + $ui->newLine(); + $ui->text(sprintf('Repository classes generated to "%s"', $destPath)); } } diff --git a/lib/Doctrine/ORM/Tools/Console/Command/InfoCommand.php b/lib/Doctrine/ORM/Tools/Console/Command/InfoCommand.php index 560d18718b..89f6c31857 100644 --- a/lib/Doctrine/ORM/Tools/Console/Command/InfoCommand.php +++ b/lib/Doctrine/ORM/Tools/Console/Command/InfoCommand.php @@ -20,9 +20,10 @@ namespace Doctrine\ORM\Tools\Console\Command; use Doctrine\ORM\Mapping\MappingException; +use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; -use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Style\SymfonyStyle; /** * Show information about mapped entities. @@ -38,15 +39,14 @@ class InfoCommand extends Command */ protected function configure() { - $this - ->setName('orm:info') - ->setDescription('Show basic information about all mapped entities') - ->setHelp(<<setName('orm:info') + ->setDescription('Show basic information about all mapped entities') + ->setHelp(<<%command.name% shows basic information about which entities exist and possibly if their mapping information contains errors or not. EOT - ); + ); } /** @@ -54,6 +54,8 @@ protected function configure() */ protected function execute(InputInterface $input, OutputInterface $output) { + $ui = new SymfonyStyle($input, $output); + /* @var $entityManager \Doctrine\ORM\EntityManager */ $entityManager = $this->getHelper('em')->getEntityManager(); @@ -61,25 +63,34 @@ protected function execute(InputInterface $input, OutputInterface $output) ->getMetadataDriverImpl() ->getAllClassNames(); - if (!$entityClassNames) { - throw new \Exception( - 'You do not have any mapped Doctrine ORM entities according to the current configuration. '. - 'If you have entities or mapping files you should check your mapping configuration for errors.' + if ( ! $entityClassNames) { + $ui->caution( + [ + 'You do not have any mapped Doctrine ORM entities according to the current configuration.', + 'If you have entities or mapping files you should check your mapping configuration for errors.' + ] ); + + return 1; } - $output->writeln(sprintf("Found %d mapped entities:", count($entityClassNames))); + $ui->text(sprintf("Found %d mapped entities:", count($entityClassNames))); + $ui->newLine(); $failure = false; foreach ($entityClassNames as $entityClassName) { try { $entityManager->getClassMetadata($entityClassName); - $output->writeln(sprintf("[OK] %s", $entityClassName)); + $ui->text(sprintf("[OK] %s", $entityClassName)); } catch (MappingException $e) { - $output->writeln("[FAIL] ".$entityClassName); - $output->writeln(sprintf("%s", $e->getMessage())); - $output->writeln(''); + $ui->text( + [ + sprintf("[FAIL] %s", $entityClassName), + sprintf("%s", $e->getMessage()), + '' + ] + ); $failure = true; } diff --git a/lib/Doctrine/ORM/Tools/Console/Command/MappingDescribeCommand.php b/lib/Doctrine/ORM/Tools/Console/Command/MappingDescribeCommand.php index 6f2761e0dc..355246731f 100644 --- a/lib/Doctrine/ORM/Tools/Console/Command/MappingDescribeCommand.php +++ b/lib/Doctrine/ORM/Tools/Console/Command/MappingDescribeCommand.php @@ -22,10 +22,10 @@ use Doctrine\Common\Persistence\Mapping\MappingException; use Doctrine\ORM\EntityManagerInterface; use Symfony\Component\Console\Command\Command; -use Symfony\Component\Console\Helper\Table; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Console\Style\SymfonyStyle; /** * Show information about mapped entities. @@ -41,11 +41,10 @@ final class MappingDescribeCommand extends Command */ protected function configure() { - $this - ->setName('orm:mapping:describe') - ->addArgument('entityName', InputArgument::REQUIRED, 'Full or partial name of entity') - ->setDescription('Display information about mapped objects') - ->setHelp(<<setName('orm:mapping:describe') + ->addArgument('entityName', InputArgument::REQUIRED, 'Full or partial name of entity') + ->setDescription('Display information about mapped objects') + ->setHelp(<<%command.full_name% My\Namespace\Entity\MyEntity @@ -54,7 +53,7 @@ protected function configure() %command.full_name% MyEntity EOT - ); + ); } /** @@ -62,10 +61,12 @@ protected function configure() */ protected function execute(InputInterface $input, OutputInterface $output) { + $ui = new SymfonyStyle($input, $output); + /* @var $entityManager \Doctrine\ORM\EntityManagerInterface */ $entityManager = $this->getHelper('em')->getEntityManager(); - $this->displayEntity($input->getArgument('entityName'), $entityManager, $output); + $this->displayEntity($input->getArgument('entityName'), $entityManager, $ui); return 0; } @@ -75,18 +76,14 @@ protected function execute(InputInterface $input, OutputInterface $output) * * @param string $entityName Full or partial entity class name * @param EntityManagerInterface $entityManager - * @param OutputInterface $output + * @param SymfonyStyle $ui */ - private function displayEntity($entityName, EntityManagerInterface $entityManager, OutputInterface $output) + private function displayEntity($entityName, EntityManagerInterface $entityManager, SymfonyStyle $ui) { - $table = new Table($output); - - $table->setHeaders(['Field', 'Value']); - $metadata = $this->getClassMetadata($entityName, $entityManager); - array_map( - [$table, 'addRow'], + $ui->table( + ['Field', 'Value'], array_merge( [ $this->formatField('Name', $metadata->name), @@ -125,8 +122,6 @@ private function displayEntity($entityName, EntityManagerInterface $entityManage $this->formatMappings($metadata->fieldMappings) ) ); - - $table->render(); } /** @@ -138,10 +133,9 @@ private function displayEntity($entityName, EntityManagerInterface $entityManage */ private function getMappedEntities(EntityManagerInterface $entityManager) { - $entityClassNames = $entityManager - ->getConfiguration() - ->getMetadataDriverImpl() - ->getAllClassNames(); + $entityClassNames = $entityManager->getConfiguration() + ->getMetadataDriverImpl() + ->getAllClassNames(); if ( ! $entityClassNames) { throw new \InvalidArgumentException( @@ -219,11 +213,7 @@ private function formatValue($value) } if (is_array($value)) { - if (defined('JSON_UNESCAPED_UNICODE') && defined('JSON_UNESCAPED_SLASHES')) { - return json_encode($value, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); - } - - return json_encode($value); + return json_encode($value, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); } if (is_object($value)) { @@ -285,14 +275,6 @@ private function formatMappings(array $propertyMappings) */ private function formatEntityListeners(array $entityListeners) { - return $this->formatField( - 'Entity listeners', - array_map( - function ($entityListener) { - return get_class($entityListener); - }, - $entityListeners - ) - ); + return $this->formatField('Entity listeners', array_map('get_class', $entityListeners)); } } diff --git a/lib/Doctrine/ORM/Tools/Console/Command/RunDqlCommand.php b/lib/Doctrine/ORM/Tools/Console/Command/RunDqlCommand.php index b246850a9c..4e6b69bb42 100644 --- a/lib/Doctrine/ORM/Tools/Console/Command/RunDqlCommand.php +++ b/lib/Doctrine/ORM/Tools/Console/Command/RunDqlCommand.php @@ -19,12 +19,13 @@ namespace Doctrine\ORM\Tools\Console\Command; +use Doctrine\Common\Util\Debug; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; -use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; -use Doctrine\Common\Util\Debug; +use Symfony\Component\Console\Style\SymfonyStyle; /** * Command to execute DQL queries in a given EntityManager. @@ -43,39 +44,15 @@ class RunDqlCommand extends Command */ protected function configure() { - $this - ->setName('orm:run-dql') - ->setDescription('Executes arbitrary DQL directly from the command line.') - ->setDefinition( - [ - new InputArgument('dql', InputArgument::REQUIRED, 'The DQL to execute.'), - new InputOption( - 'hydrate', null, InputOption::VALUE_REQUIRED, - 'Hydration mode of result set. Should be either: object, array, scalar or single-scalar.', - 'object' - ), - new InputOption( - 'first-result', null, InputOption::VALUE_REQUIRED, - 'The first result in the result set.' - ), - new InputOption( - 'max-result', null, InputOption::VALUE_REQUIRED, - 'The maximum number of results in the result set.' - ), - new InputOption( - 'depth', null, InputOption::VALUE_REQUIRED, - 'Dumping depth of Entity graph.', 7 - ), - new InputOption( - 'show-sql', null, InputOption::VALUE_NONE, - 'Dump generated SQL instead of executing query' - ) - ] - ) - ->setHelp(<<setName('orm:run-dql') + ->setDescription('Executes arbitrary DQL directly from the command line') + ->addArgument('dql', InputArgument::REQUIRED, 'The DQL to execute.') + ->addOption('hydrate', null, InputOption::VALUE_REQUIRED, 'Hydration mode of result set. Should be either: object, array, scalar or single-scalar.', 'object') + ->addOption('first-result', null, InputOption::VALUE_REQUIRED, 'The first result in the result set.') + ->addOption('max-result', null, InputOption::VALUE_REQUIRED, 'The maximum number of results in the result set.') + ->addOption('depth', null, InputOption::VALUE_REQUIRED, 'Dumping depth of Entity graph.', 7) + ->addOption('show-sql', null, InputOption::VALUE_NONE, 'Dump generated SQL instead of executing query') + ->setHelp('Executes arbitrary DQL directly from the command line.'); } /** @@ -83,17 +60,19 @@ protected function configure() */ protected function execute(InputInterface $input, OutputInterface $output) { + $ui = new SymfonyStyle($input, $output); + /* @var $em \Doctrine\ORM\EntityManagerInterface */ $em = $this->getHelper('em')->getEntityManager(); if (($dql = $input->getArgument('dql')) === null) { - throw new \RuntimeException("Argument 'DQL' is required in order to execute this command correctly."); + throw new \RuntimeException("Argument 'dql' is required in order to execute this command correctly."); } $depth = $input->getOption('depth'); if ( ! is_numeric($depth)) { - throw new \LogicException("Option 'depth' must contains an integer value"); + throw new \LogicException("Option 'depth' must contain an integer value"); } $hydrationModeName = $input->getOption('hydrate'); @@ -109,7 +88,7 @@ protected function execute(InputInterface $input, OutputInterface $output) if (($firstResult = $input->getOption('first-result')) !== null) { if ( ! is_numeric($firstResult)) { - throw new \LogicException("Option 'first-result' must contains an integer value"); + throw new \LogicException("Option 'first-result' must contain an integer value"); } $query->setFirstResult((int) $firstResult); @@ -117,19 +96,19 @@ protected function execute(InputInterface $input, OutputInterface $output) if (($maxResult = $input->getOption('max-result')) !== null) { if ( ! is_numeric($maxResult)) { - throw new \LogicException("Option 'max-result' must contains an integer value"); + throw new \LogicException("Option 'max-result' must contain an integer value"); } $query->setMaxResults((int) $maxResult); } if ($input->getOption('show-sql')) { - $output->writeln(Debug::dump($query->getSQL(), 2, true, false)); + $ui->text($query->getSQL()); return; } $resultSet = $query->execute([], constant($hydrationMode)); - $output->writeln(Debug::dump($resultSet, $input->getOption('depth'), true, false)); + $ui->text(Debug::dump($resultSet, $input->getOption('depth'), true, false)); } } diff --git a/lib/Doctrine/ORM/Tools/Console/Command/SchemaTool/AbstractCommand.php b/lib/Doctrine/ORM/Tools/Console/Command/SchemaTool/AbstractCommand.php index dc7e7ec7e8..21907d094a 100644 --- a/lib/Doctrine/ORM/Tools/Console/Command/SchemaTool/AbstractCommand.php +++ b/lib/Doctrine/ORM/Tools/Console/Command/SchemaTool/AbstractCommand.php @@ -19,10 +19,11 @@ namespace Doctrine\ORM\Tools\Console\Command\SchemaTool; +use Doctrine\ORM\Tools\SchemaTool; +use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; -use Symfony\Component\Console\Command\Command; -use Doctrine\ORM\Tools\SchemaTool; +use Symfony\Component\Console\Style\SymfonyStyle; /** * Base class for CreateCommand, DropCommand and UpdateCommand. @@ -44,13 +45,15 @@ abstract class AbstractCommand extends Command * * @return null|int Null or 0 if everything went fine, or an error code. */ - abstract protected function executeSchemaCommand(InputInterface $input, OutputInterface $output, SchemaTool $schemaTool, array $metadatas); + abstract protected function executeSchemaCommand(InputInterface $input, OutputInterface $output, SchemaTool $schemaTool, array $metadatas, SymfonyStyle $ui); /** * {@inheritdoc} */ protected function execute(InputInterface $input, OutputInterface $output) { + $ui = new SymfonyStyle($input, $output); + $emHelper = $this->getHelper('em'); /* @var $em \Doctrine\ORM\EntityManager */ @@ -58,15 +61,12 @@ protected function execute(InputInterface $input, OutputInterface $output) $metadatas = $em->getMetadataFactory()->getAllMetadata(); - if ( ! empty($metadatas)) { - // Create SchemaTool - $tool = new SchemaTool($em); - - return $this->executeSchemaCommand($input, $output, $tool, $metadatas); - } else { - $output->writeln('No Metadata Classes to process.'); + if (empty($metadatas)) { + $ui->success('No Metadata Classes to process.'); return 0; } + + return $this->executeSchemaCommand($input, $output, new SchemaTool($em), $metadatas, $ui); } } diff --git a/lib/Doctrine/ORM/Tools/Console/Command/SchemaTool/CreateCommand.php b/lib/Doctrine/ORM/Tools/Console/Command/SchemaTool/CreateCommand.php index 73fbcbabbc..0c2137417b 100644 --- a/lib/Doctrine/ORM/Tools/Console/Command/SchemaTool/CreateCommand.php +++ b/lib/Doctrine/ORM/Tools/Console/Command/SchemaTool/CreateCommand.php @@ -19,10 +19,11 @@ namespace Doctrine\ORM\Tools\Console\Command\SchemaTool; -use Symfony\Component\Console\Input\InputOption; +use Doctrine\ORM\Tools\SchemaTool; use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; -use Doctrine\ORM\Tools\SchemaTool; +use Symfony\Component\Console\Style\SymfonyStyle; /** * Command to create the database schema for a set of classes based on their mappings. @@ -41,20 +42,10 @@ class CreateCommand extends AbstractCommand */ protected function configure() { - $this - ->setName('orm:schema-tool:create') - ->setDescription( - 'Processes the schema and either create it directly on EntityManager Storage Connection or generate the SQL output.' - ) - ->setDefinition( - [ - new InputOption( - 'dump-sql', null, InputOption::VALUE_NONE, - 'Instead of trying to apply generated SQLs into EntityManager Storage Connection, output them.' - ) - ] - ) - ->setHelp(<<setName('orm:schema-tool:create') + ->setDescription('Processes the schema and either create it directly on EntityManager Storage Connection or generate the SQL output') + ->addOption('dump-sql', null, InputOption::VALUE_NONE, 'Instead of trying to apply generated SQLs into EntityManager Storage Connection, output them.') + ->setHelp(<<Hint: If you have a database with tables that should not be managed @@ -63,25 +54,37 @@ protected function configure() \$config->setFilterSchemaAssetsExpression(\$regexp); EOT - ); + ); } /** * {@inheritdoc} */ - protected function executeSchemaCommand(InputInterface $input, OutputInterface $output, SchemaTool $schemaTool, array $metadatas) + protected function executeSchemaCommand(InputInterface $input, OutputInterface $output, SchemaTool $schemaTool, array $metadatas, SymfonyStyle $ui) { - if ($input->getOption('dump-sql')) { + $dumpSql = true === $input->getOption('dump-sql'); + + if ($dumpSql) { $sqls = $schemaTool->getCreateSchemaSql($metadatas); - $output->writeln(implode(';' . PHP_EOL, $sqls) . ';'); - } else { - $output->writeln('ATTENTION: This operation should not be executed in a production environment.' . PHP_EOL); + $ui->text('The following SQL statements will be executed:'); + $ui->newLine(); - $output->writeln('Creating database schema...'); - $schemaTool->createSchema($metadatas); - $output->writeln('Database schema created successfully!'); + foreach ($sqls as $sql) { + $ui->text(sprintf(' %s;', $sql)); + } + + return 0; } + $ui->caution('This operation should not be executed in a production environment!'); + + $ui->text('Creating database schema...'); + $ui->newLine(); + + $schemaTool->createSchema($metadatas); + + $ui->success('Database schema created successfully!'); + return 0; } } diff --git a/lib/Doctrine/ORM/Tools/Console/Command/SchemaTool/DropCommand.php b/lib/Doctrine/ORM/Tools/Console/Command/SchemaTool/DropCommand.php index e283470391..3d77fc8fd8 100644 --- a/lib/Doctrine/ORM/Tools/Console/Command/SchemaTool/DropCommand.php +++ b/lib/Doctrine/ORM/Tools/Console/Command/SchemaTool/DropCommand.php @@ -19,10 +19,11 @@ namespace Doctrine\ORM\Tools\Console\Command\SchemaTool; -use Symfony\Component\Console\Input\InputOption; +use Doctrine\ORM\Tools\SchemaTool; use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; -use Doctrine\ORM\Tools\SchemaTool; +use Symfony\Component\Console\Style\SymfonyStyle; /** * Command to drop the database schema for a set of classes based on their mappings. @@ -41,28 +42,12 @@ class DropCommand extends AbstractCommand */ protected function configure() { - $this - ->setName('orm:schema-tool:drop') - ->setDescription( - 'Drop the complete database schema of EntityManager Storage Connection or generate the corresponding SQL output.' - ) - ->setDefinition( - [ - new InputOption( - 'dump-sql', null, InputOption::VALUE_NONE, - 'Instead of trying to apply generated SQLs into EntityManager Storage Connection, output them.' - ), - new InputOption( - 'force', 'f', InputOption::VALUE_NONE, - "Don't ask for the deletion of the database, but force the operation to run." - ), - new InputOption( - 'full-database', null, InputOption::VALUE_NONE, - 'Instead of using the Class Metadata to detect the database table schema, drop ALL assets that the database contains.' - ), - ] - ) - ->setHelp(<<setName('orm:schema-tool:drop') + ->setDescription('Drop the complete database schema of EntityManager Storage Connection or generate the corresponding SQL output') + ->addOption('dump-sql', null, InputOption::VALUE_NONE, 'Instead of trying to apply generated SQLs into EntityManager Storage Connection, output them.') + ->addOption('force', 'f', InputOption::VALUE_NONE, "Don't ask for the deletion of the database, but force the operation to run.") + ->addOption('full-database', null, InputOption::VALUE_NONE, 'Instead of using the Class Metadata to detect the database table schema, drop ALL assets that the database contains.') + ->setHelp(<<setFilterSchemaAssetsExpression(\$regexp); EOT - ); + ); } /** * {@inheritdoc} */ - protected function executeSchemaCommand(InputInterface $input, OutputInterface $output, SchemaTool $schemaTool, array $metadatas) + protected function executeSchemaCommand(InputInterface $input, OutputInterface $output, SchemaTool $schemaTool, array $metadatas, SymfonyStyle $ui) { $isFullDatabaseDrop = $input->getOption('full-database'); + $dumpSql = true === $input->getOption('dump-sql'); + $force = true === $input->getOption('force'); - if ($input->getOption('dump-sql')) { + if ($dumpSql) { if ($isFullDatabaseDrop) { $sqls = $schemaTool->getDropDatabaseSQL(); } else { $sqls = $schemaTool->getDropSchemaSQL($metadatas); } - $output->writeln(implode(';' . PHP_EOL, $sqls)); + $ui->text('The following SQL statements will be executed:'); + $ui->newLine(); + + foreach ($sqls as $sql) { + $ui->text(sprintf(' %s;', $sql)); + } return 0; } - if ($input->getOption('force')) { - $output->writeln('Dropping database schema...'); + if ($force) { + $ui->text('Dropping database schema...'); + $ui->newLine(); if ($isFullDatabaseDrop) { $schemaTool->dropDatabase(); @@ -102,12 +95,12 @@ protected function executeSchemaCommand(InputInterface $input, OutputInterface $ $schemaTool->dropSchema($metadatas); } - $output->writeln('Database schema dropped successfully!'); + $ui->success('Database schema dropped successfully!'); return 0; } - $output->writeln('ATTENTION: This operation should not be executed in a production environment.' . PHP_EOL); + $ui->caution('This operation should not be executed in a production environment!'); if ($isFullDatabaseDrop) { $sqls = $schemaTool->getDropDatabaseSQL(); @@ -115,18 +108,23 @@ protected function executeSchemaCommand(InputInterface $input, OutputInterface $ $sqls = $schemaTool->getDropSchemaSQL($metadatas); } - if (count($sqls)) { - $output->writeln(sprintf('The Schema-Tool would execute "%s" queries to update the database.', count($sqls))); - $output->writeln('Please run the operation by passing one - or both - of the following options:'); + if (empty($sqls)) { + $ui->success('Nothing to drop. The database is empty!'); - $output->writeln(sprintf(' %s --force to execute the command', $this->getName())); - $output->writeln(sprintf(' %s --dump-sql to dump the SQL statements to the screen', $this->getName())); - - return 1; + return 0; } - $output->writeln('Nothing to drop. The database is empty!'); + $ui->text( + [ + sprintf('The Schema-Tool would execute "%s" queries to update the database.', count($sqls)), + '', + 'Please run the operation by passing one - or both - of the following options:', + '', + sprintf(' %s --force to execute the command', $this->getName()), + sprintf(' %s --dump-sql to dump the SQL statements to the screen', $this->getName()), + ] + ); - return 0; + return 1; } } diff --git a/lib/Doctrine/ORM/Tools/Console/Command/SchemaTool/UpdateCommand.php b/lib/Doctrine/ORM/Tools/Console/Command/SchemaTool/UpdateCommand.php index 6ba2e41f8e..c9059775b1 100644 --- a/lib/Doctrine/ORM/Tools/Console/Command/SchemaTool/UpdateCommand.php +++ b/lib/Doctrine/ORM/Tools/Console/Command/SchemaTool/UpdateCommand.php @@ -19,10 +19,11 @@ namespace Doctrine\ORM\Tools\Console\Command\SchemaTool; -use Symfony\Component\Console\Input\InputOption; +use Doctrine\ORM\Tools\SchemaTool; use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; -use Doctrine\ORM\Tools\SchemaTool; +use Symfony\Component\Console\Style\SymfonyStyle; /** * Command to generate the SQL needed to update the database schema to match @@ -48,30 +49,12 @@ class UpdateCommand extends AbstractCommand */ protected function configure() { - $this - ->setName($this->name) - ->setDescription( - 'Executes (or dumps) the SQL needed to update the database schema to match the current mapping metadata.' - ) - ->setDefinition( - [ - new InputOption( - 'complete', null, InputOption::VALUE_NONE, - 'If defined, all assets of the database which are not relevant to the current metadata will be dropped.' - ), - - new InputOption( - 'dump-sql', null, InputOption::VALUE_NONE, - 'Dumps the generated SQL statements to the screen (does not execute them).' - ), - new InputOption( - 'force', 'f', InputOption::VALUE_NONE, - 'Causes the generated SQL statements to be physically executed against your database.' - ), - ] - ); - - $this->setHelp(<<setName($this->name) + ->setDescription('Executes (or dumps) the SQL needed to update the database schema to match the current mapping metadata') + ->addOption('complete', null, InputOption::VALUE_NONE, 'If defined, all assets of the database which are not relevant to the current metadata will be dropped.') + ->addOption('dump-sql', null, InputOption::VALUE_NONE, 'Dumps the generated SQL statements to the screen (does not execute them).') + ->addOption('force', 'f', InputOption::VALUE_NONE, 'Causes the generated SQL statements to be physically executed against your database.') + ->setHelp(<<%command.name% command generates the SQL needed to synchronize the database schema with the current mapping metadata of the default entity manager. @@ -101,21 +84,21 @@ protected function configure() \$config->setFilterSchemaAssetsExpression(\$regexp); EOT - ); + ); } /** * {@inheritdoc} */ - protected function executeSchemaCommand(InputInterface $input, OutputInterface $output, SchemaTool $schemaTool, array $metadatas) + protected function executeSchemaCommand(InputInterface $input, OutputInterface $output, SchemaTool $schemaTool, array $metadatas, SymfonyStyle $ui) { // Defining if update is complete or not (--complete not defined means $saveMode = true) $saveMode = ! $input->getOption('complete'); $sqls = $schemaTool->getUpdateSchemaSql($metadatas, $saveMode); - if (0 === count($sqls)) { - $output->writeln('Nothing to update - your database is already in sync with the current entity metadata.'); + if (empty($sqls)) { + $ui->success('Nothing to update - your database is already in sync with the current entity metadata.'); return 0; } @@ -124,35 +107,52 @@ protected function executeSchemaCommand(InputInterface $input, OutputInterface $ $force = true === $input->getOption('force'); if ($dumpSql) { - $output->writeln(implode(';' . PHP_EOL, $sqls) . ';'); + $ui->text('The following SQL statements will be executed:'); + $ui->newLine(); + + foreach ($sqls as $sql) { + $ui->text(sprintf(' %s;', $sql)); + } } if ($force) { if ($dumpSql) { - $output->writeln(''); + $ui->newLine(); } - $output->writeln('Updating database schema...'); + $ui->text('Updating database schema...'); + $ui->newLine(); + $schemaTool->updateSchema($metadatas, $saveMode); $pluralization = (1 === count($sqls)) ? 'query was' : 'queries were'; - $output->writeln(sprintf('Database schema updated successfully! "%s" %s executed', count($sqls), $pluralization)); + $ui->text(sprintf(' %s %s executed', count($sqls), $pluralization)); + $ui->success('Database schema updated successfully!'); } if ($dumpSql || $force) { return 0; } - $output->writeln('ATTENTION: This operation should not be executed in a production environment.'); - $output->writeln(' Use the incremental update to detect changes during development and use'); - $output->writeln(' the SQL DDL provided to manually update your database in production.'); - $output->writeln(''); - - $output->writeln(sprintf('The Schema-Tool would execute "%s" queries to update the database.', count($sqls))); - $output->writeln('Please run the operation by passing one - or both - of the following options:'); + $ui->caution( + [ + 'This operation should not be executed in a production environment!', + '', + 'Use the incremental update to detect changes during development and use', + 'the SQL DDL provided to manually update your database in production.', + ] + ); - $output->writeln(sprintf(' %s --force to execute the command', $this->getName())); - $output->writeln(sprintf(' %s --dump-sql to dump the SQL statements to the screen', $this->getName())); + $ui->text( + [ + sprintf('The Schema-Tool would execute "%s" queries to update the database.', count($sqls)), + '', + 'Please run the operation by passing one - or both - of the following options:', + '', + sprintf(' %s --force to execute the command', $this->getName()), + sprintf(' %s --dump-sql to dump the SQL statements to the screen', $this->getName()), + ] + ); return 1; } diff --git a/lib/Doctrine/ORM/Tools/Console/Command/ValidateSchemaCommand.php b/lib/Doctrine/ORM/Tools/Console/Command/ValidateSchemaCommand.php index 57b16fc1c4..343d3b53db 100644 --- a/lib/Doctrine/ORM/Tools/Console/Command/ValidateSchemaCommand.php +++ b/lib/Doctrine/ORM/Tools/Console/Command/ValidateSchemaCommand.php @@ -19,11 +19,12 @@ namespace Doctrine\ORM\Tools\Console\Command; +use Doctrine\ORM\Tools\SchemaValidator; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; -use Doctrine\ORM\Tools\SchemaValidator; +use Symfony\Component\Console\Style\SymfonyStyle; /** * Command to validate that the current mapping is valid. @@ -43,26 +44,11 @@ class ValidateSchemaCommand extends Command */ protected function configure() { - $this - ->setName('orm:validate-schema') - ->setDescription('Validate the mapping files.') - ->addOption( - 'skip-mapping', - null, - InputOption::VALUE_NONE, - 'Skip the mapping validation check' - ) - ->addOption( - 'skip-sync', - null, - InputOption::VALUE_NONE, - 'Skip checking if the mapping is in sync with the database' - ) - ->setHelp( - <<setName('orm:validate-schema') + ->setDescription('Validate the mapping files') + ->addOption('skip-mapping', null, InputOption::VALUE_NONE, 'Skip the mapping validation check') + ->addOption('skip-sync', null, InputOption::VALUE_NONE, 'Skip checking if the mapping is in sync with the database') + ->setHelp('Validate that the mapping files are correct and in sync with the database.'); } /** @@ -70,35 +56,43 @@ protected function configure() */ protected function execute(InputInterface $input, OutputInterface $output) { + $ui = new SymfonyStyle($input, $output); + $em = $this->getHelper('em')->getEntityManager(); $validator = new SchemaValidator($em); $exit = 0; + $ui->section('Mapping'); + if ($input->getOption('skip-mapping')) { - $output->writeln('[Mapping] Skipped mapping check.'); + $ui->text('[SKIPPED] The mapping was not checked.'); } elseif ($errors = $validator->validateMapping()) { foreach ($errors as $className => $errorMessages) { - $output->writeln("[Mapping] FAIL - The entity-class '" . $className . "' mapping is invalid:"); - - foreach ($errorMessages as $errorMessage) { - $output->writeln('* ' . $errorMessage); - } + $ui->text( + sprintf( + '[FAIL] The entity-class %s mapping is invalid:', + $className + ) + ); - $output->writeln(''); + $ui->listing($errorMessages); + $ui->newLine(); } ++$exit; } else { - $output->writeln('[Mapping] OK - The mapping files are correct.'); + $ui->success('The mapping files are correct.'); } + $ui->section('Database'); + if ($input->getOption('skip-sync')) { - $output->writeln('[Database] SKIPPED - The database was not checked for synchronicity.'); - } elseif (!$validator->schemaInSyncWithMetadata()) { - $output->writeln('[Database] FAIL - The database schema is not in sync with the current mapping file.'); + $ui->text('[SKIPPED] The database was not checked for synchronicity.'); + } elseif ( ! $validator->schemaInSyncWithMetadata()) { + $ui->error('The database schema is not in sync with the current mapping file.'); $exit += 2; } else { - $output->writeln('[Database] OK - The database schema is in sync with the mapping files.'); + $ui->success('The database schema is in sync with the mapping files.'); } return $exit; diff --git a/phpunit.xml.dist b/phpunit.xml.dist index c6bd898824..be88f60e7e 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -19,6 +19,10 @@ bootstrap="./tests/Doctrine/Tests/TestInit.php" > + + + + ./tests/Doctrine/Tests/ORM diff --git a/tests/Doctrine/Tests/ORM/Tools/Console/Command/ClearCacheCollectionRegionCommandTest.php b/tests/Doctrine/Tests/ORM/Tools/Console/Command/ClearCacheCollectionRegionCommandTest.php index 5ac2f97527..f8c17b318b 100644 --- a/tests/Doctrine/Tests/ORM/Tools/Console/Command/ClearCacheCollectionRegionCommandTest.php +++ b/tests/Doctrine/Tests/ORM/Tools/Console/Command/ClearCacheCollectionRegionCommandTest.php @@ -30,76 +30,90 @@ protected function setUp() $this->enableSecondLevelCache(); parent::setUp(); - $this->application = new Application(); - $this->command = new CollectionRegionCommand(); - - $this->application->setHelperSet(new HelperSet( - [ - 'em' => new EntityManagerHelper($this->_em) - ] - )); + $this->command = new CollectionRegionCommand(); + $this->application = new Application(); + $this->application->setHelperSet(new HelperSet(['em' => new EntityManagerHelper($this->_em)])); $this->application->add($this->command); } public function testClearAllRegion() { - $command = $this->application->find('orm:clear-cache:region:collection'); - $tester = new CommandTester($command); + $command = $this->application->find('orm:clear-cache:region:collection'); + $tester = new CommandTester($command); + $tester->execute( [ - 'command' => $command->getName(), - '--all' => true, - ], ['decorated' => false] + 'command' => $command->getName(), + '--all' => true, + ], + ['decorated' => false] ); - $this->assertEquals('Clearing all second-level cache collection regions' . PHP_EOL, $tester->getDisplay()); + self::assertContains(' // Clearing all second-level cache collection regions', $tester->getDisplay()); } public function testClearByOwnerEntityClassName() { - $command = $this->application->find('orm:clear-cache:region:collection'); - $tester = new CommandTester($command); + $command = $this->application->find('orm:clear-cache:region:collection'); + $tester = new CommandTester($command); + $tester->execute( [ - 'command' => $command->getName(), - 'owner-class' => State::class, - 'association' => 'cities', - ], ['decorated' => false] + 'command' => $command->getName(), + 'owner-class' => State::class, + 'association' => 'cities', + ], + ['decorated' => false] ); - $this->assertEquals('Clearing second-level cache for collection "Doctrine\Tests\Models\Cache\State#cities"' . PHP_EOL, $tester->getDisplay()); + self::assertContains( + ' // Clearing second-level cache for collection "Doctrine\Tests\Models\Cache\State#cities"', + $tester->getDisplay() + ); } public function testClearCacheEntryName() { - $command = $this->application->find('orm:clear-cache:region:collection'); - $tester = new CommandTester($command); + $command = $this->application->find('orm:clear-cache:region:collection'); + $tester = new CommandTester($command); + $tester->execute( [ - 'command' => $command->getName(), - 'owner-class' => State::class, - 'association' => 'cities', - 'owner-id' => 1, - ], ['decorated' => false] + 'command' => $command->getName(), + 'owner-class' => State::class, + 'association' => 'cities', + 'owner-id' => 1, + ], + ['decorated' => false] + ); + + self::assertContains( + ' // Clearing second-level cache entry for collection "Doctrine\Tests\Models\Cache\State#cities" owner', + $tester->getDisplay() ); - $this->assertEquals('Clearing second-level cache entry for collection "Doctrine\Tests\Models\Cache\State#cities" owner entity identified by "1"' . PHP_EOL, $tester->getDisplay()); + self::assertContains(' // entity identified by "1"', $tester->getDisplay()); } public function testFlushRegionName() { - $command = $this->application->find('orm:clear-cache:region:collection'); - $tester = new CommandTester($command); + $command = $this->application->find('orm:clear-cache:region:collection'); + $tester = new CommandTester($command); + $tester->execute( [ - 'command' => $command->getName(), - 'owner-class' => State::class, - 'association' => 'cities', - '--flush' => true, - ], ['decorated' => false] + 'command' => $command->getName(), + 'owner-class' => State::class, + 'association' => 'cities', + '--flush' => true, + ], + ['decorated' => false] ); - $this->assertEquals('Flushing cache provider configured for "Doctrine\Tests\Models\Cache\State#cities"' . PHP_EOL, $tester->getDisplay()); + self::assertContains( + ' // Flushing cache provider configured for "Doctrine\Tests\Models\Cache\State#cities"', + $tester->getDisplay() + ); } } diff --git a/tests/Doctrine/Tests/ORM/Tools/Console/Command/ClearCacheEntityRegionCommandTest.php b/tests/Doctrine/Tests/ORM/Tools/Console/Command/ClearCacheEntityRegionCommandTest.php index 3546a3116d..b1d8d386b2 100644 --- a/tests/Doctrine/Tests/ORM/Tools/Console/Command/ClearCacheEntityRegionCommandTest.php +++ b/tests/Doctrine/Tests/ORM/Tools/Console/Command/ClearCacheEntityRegionCommandTest.php @@ -30,73 +30,87 @@ protected function setUp() $this->enableSecondLevelCache(); parent::setUp(); - $this->application = new Application(); - $this->command = new EntityRegionCommand(); - - $this->application->setHelperSet(new HelperSet( - [ - 'em' => new EntityManagerHelper($this->_em) - ] - )); + $this->command = new EntityRegionCommand(); + $this->application = new Application(); + $this->application->setHelperSet(new HelperSet(['em' => new EntityManagerHelper($this->_em)])); $this->application->add($this->command); } public function testClearAllRegion() { - $command = $this->application->find('orm:clear-cache:region:entity'); - $tester = new CommandTester($command); + $command = $this->application->find('orm:clear-cache:region:entity'); + $tester = new CommandTester($command); + $tester->execute( [ - 'command' => $command->getName(), - '--all' => true, - ], ['decorated' => false] + 'command' => $command->getName(), + '--all' => true, + ], + ['decorated' => false] ); - $this->assertEquals('Clearing all second-level cache entity regions' . PHP_EOL, $tester->getDisplay()); + self::assertContains(' // Clearing all second-level cache entity regions', $tester->getDisplay()); } public function testClearByEntityClassName() { - $command = $this->application->find('orm:clear-cache:region:entity'); - $tester = new CommandTester($command); + $command = $this->application->find('orm:clear-cache:region:entity'); + $tester = new CommandTester($command); + $tester->execute( [ - 'command' => $command->getName(), - 'entity-class' => Country::class, - ], ['decorated' => false] + 'command' => $command->getName(), + 'entity-class' => Country::class, + ], + ['decorated' => false] ); - $this->assertEquals('Clearing second-level cache for entity "Doctrine\Tests\Models\Cache\Country"' . PHP_EOL, $tester->getDisplay()); + self::assertContains( + ' // Clearing second-level cache for entity "Doctrine\Tests\Models\Cache\Country"', + $tester->getDisplay() + ); } public function testClearCacheEntryName() { - $command = $this->application->find('orm:clear-cache:region:entity'); - $tester = new CommandTester($command); + $command = $this->application->find('orm:clear-cache:region:entity'); + $tester = new CommandTester($command); + $tester->execute( [ - 'command' => $command->getName(), - 'entity-class' => Country::class, - 'entity-id' => 1, - ], ['decorated' => false] + 'command' => $command->getName(), + 'entity-class' => Country::class, + 'entity-id' => 1, + ], + ['decorated' => false] + ); + + self::assertContains( + ' // Clearing second-level cache entry for entity "Doctrine\Tests\Models\Cache\Country" identified by', + $tester->getDisplay() ); - $this->assertEquals('Clearing second-level cache entry for entity "Doctrine\Tests\Models\Cache\Country" identified by "1"' . PHP_EOL, $tester->getDisplay()); + self::assertContains(' // "1"', $tester->getDisplay()); } public function testFlushRegionName() { - $command = $this->application->find('orm:clear-cache:region:entity'); - $tester = new CommandTester($command); + $command = $this->application->find('orm:clear-cache:region:entity'); + $tester = new CommandTester($command); + $tester->execute( [ - 'command' => $command->getName(), - 'entity-class' => Country::class, - '--flush' => true, - ], ['decorated' => false] + 'command' => $command->getName(), + 'entity-class' => Country::class, + '--flush' => true, + ], + ['decorated' => false] ); - $this->assertEquals('Flushing cache provider configured for entity named "Doctrine\Tests\Models\Cache\Country"' . PHP_EOL, $tester->getDisplay()); + self::assertContains( + ' // Flushing cache provider configured for entity named "Doctrine\Tests\Models\Cache\Country"', + $tester->getDisplay() + ); } } diff --git a/tests/Doctrine/Tests/ORM/Tools/Console/Command/ClearCacheQueryRegionCommandTest.php b/tests/Doctrine/Tests/ORM/Tools/Console/Command/ClearCacheQueryRegionCommandTest.php index 0d296bf553..1d761260cb 100644 --- a/tests/Doctrine/Tests/ORM/Tools/Console/Command/ClearCacheQueryRegionCommandTest.php +++ b/tests/Doctrine/Tests/ORM/Tools/Console/Command/ClearCacheQueryRegionCommandTest.php @@ -4,10 +4,10 @@ use Doctrine\ORM\Tools\Console\Command\ClearCache\QueryRegionCommand; use Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper; -use Symfony\Component\Console\Tester\CommandTester; -use Symfony\Component\Console\Helper\HelperSet; -use Symfony\Component\Console\Application; use Doctrine\Tests\OrmFunctionalTestCase; +use Symfony\Component\Console\Application; +use Symfony\Component\Console\Helper\HelperSet; +use Symfony\Component\Console\Tester\CommandTester; /** * @group DDC-2183 @@ -29,72 +29,84 @@ protected function setUp() $this->enableSecondLevelCache(); parent::setUp(); - $this->application = new Application(); - $this->command = new QueryRegionCommand(); - - $this->application->setHelperSet(new HelperSet( - [ - 'em' => new EntityManagerHelper($this->_em) - ] - )); + $this->command = new QueryRegionCommand(); + $this->application = new Application(); + $this->application->setHelperSet(new HelperSet(['em' => new EntityManagerHelper($this->_em)])); $this->application->add($this->command); } public function testClearAllRegion() { - $command = $this->application->find('orm:clear-cache:region:query'); - $tester = new CommandTester($command); + $command = $this->application->find('orm:clear-cache:region:query'); + $tester = new CommandTester($command); + $tester->execute( [ - 'command' => $command->getName(), - '--all' => true, - ], ['decorated' => false] + 'command' => $command->getName(), + '--all' => true, + ], + ['decorated' => false] ); - $this->assertEquals('Clearing all second-level cache query regions' . PHP_EOL, $tester->getDisplay()); + self::assertContains(' // Clearing all second-level cache query regions', $tester->getDisplay()); } public function testClearDefaultRegionName() { - $command = $this->application->find('orm:clear-cache:region:query'); - $tester = new CommandTester($command); + $command = $this->application->find('orm:clear-cache:region:query'); + $tester = new CommandTester($command); + $tester->execute( [ - 'command' => $command->getName(), - 'region-name' => null, - ], ['decorated' => false] + 'command' => $command->getName(), + 'region-name' => null, + ], + ['decorated' => false] ); - $this->assertEquals('Clearing second-level cache query region named "query_cache_region"' . PHP_EOL, $tester->getDisplay()); + self::assertContains( + ' // Clearing second-level cache query region named "query_cache_region"', + $tester->getDisplay() + ); } public function testClearByRegionName() { - $command = $this->application->find('orm:clear-cache:region:query'); - $tester = new CommandTester($command); + $command = $this->application->find('orm:clear-cache:region:query'); + $tester = new CommandTester($command); + $tester->execute( [ - 'command' => $command->getName(), - 'region-name' => 'my_region', - ], ['decorated' => false] + 'command' => $command->getName(), + 'region-name' => 'my_region', + ], + ['decorated' => false] ); - $this->assertEquals('Clearing second-level cache query region named "my_region"' . PHP_EOL, $tester->getDisplay()); + self::assertContains( + ' // Clearing second-level cache query region named "my_region"', + $tester->getDisplay() + ); } public function testFlushRegionName() { - $command = $this->application->find('orm:clear-cache:region:query'); - $tester = new CommandTester($command); + $command = $this->application->find('orm:clear-cache:region:query'); + $tester = new CommandTester($command); + $tester->execute( [ - 'command' => $command->getName(), - 'region-name' => 'my_region', - '--flush' => true, - ], ['decorated' => false] + 'command' => $command->getName(), + 'region-name' => 'my_region', + '--flush' => true, + ], + ['decorated' => false] ); - $this->assertEquals('Flushing cache provider configured for second-level cache query region named "my_region"' . PHP_EOL, $tester->getDisplay()); + self::assertContains( + ' // Flushing cache provider configured for second-level cache query region named "my_region"', + $tester->getDisplay() + ); } } diff --git a/tests/Doctrine/Tests/ORM/Tools/Console/Command/GenerateRepositoriesCommandTest.php b/tests/Doctrine/Tests/ORM/Tools/Console/Command/GenerateRepositoriesCommandTest.php index 45bdfa3862..74fbc38b22 100644 --- a/tests/Doctrine/Tests/ORM/Tools/Console/Command/GenerateRepositoriesCommandTest.php +++ b/tests/Doctrine/Tests/ORM/Tools/Console/Command/GenerateRepositoriesCommandTest.php @@ -2,18 +2,18 @@ namespace Doctrine\Tests\ORM\Tools\Console\Command; +use Doctrine\Common\Persistence\Mapping\ClassMetadataFactory; +use Doctrine\ORM\Configuration; +use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityRepository; use Doctrine\ORM\Tools\Console\Command\GenerateRepositoriesCommand; use Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper; use Doctrine\Tests\Models\DDC3231\DDC3231EntityRepository; -use Symfony\Component\Console\Tester\CommandTester; -use Symfony\Component\Console\Helper\HelperSet; -use Symfony\Component\Console\Application; use Doctrine\Tests\OrmFunctionalTestCase; +use Symfony\Component\Console\Application; +use Symfony\Component\Console\Helper\HelperSet; +use Symfony\Component\Console\Tester\CommandTester; -/** - * GenerateRepositoriesCommandTest - */ class GenerateRepositoriesCommandTest extends OrmFunctionalTestCase { /** @@ -34,25 +34,12 @@ protected function setUp() \mkdir($this->path); - $metadataDriver = $this->_em->getConfiguration()->getMetadataDriverImpl(); - - $metadataDriver->addPaths( - [ - __DIR__ . '/../../../../Models/DDC3231/' - ] - ); + $metadataDriver->addPaths([__DIR__ . '/../../../../Models/DDC3231/']); $this->application = new Application(); - - $this->application->setHelperSet(new HelperSet( - [ - 'em' => new EntityManagerHelper($this->_em) - ] - )); - + $this->application->setHelperSet(new HelperSet(['em' => new EntityManagerHelper($this->_em)])); $this->application->add(new GenerateRepositoriesCommand()); - } /** @@ -88,20 +75,20 @@ public function testGenerateRepositories() $cname = 'Doctrine\Tests\Models\DDC3231\DDC3231User1Repository'; $fname = str_replace('\\', DIRECTORY_SEPARATOR, $cname) . '.php'; - $this->assertFileExists($this->path . DIRECTORY_SEPARATOR . $fname); - $this->assertFileExists($this->path . DIRECTORY_SEPARATOR . 'DDC3231User1NoNamespaceRepository.php'); + self::assertFileExists($this->path . DIRECTORY_SEPARATOR . $fname); + self::assertFileExists($this->path . DIRECTORY_SEPARATOR . 'DDC3231User1NoNamespaceRepository.php'); require $this->path . DIRECTORY_SEPARATOR . $fname; require $this->path . DIRECTORY_SEPARATOR . 'DDC3231User1NoNamespaceRepository.php'; - $this->assertTrue(class_exists($cname)); - $this->assertTrue(class_exists('DDC3231User1NoNamespaceRepository')); + self::assertTrue(class_exists($cname)); + self::assertTrue(class_exists('DDC3231User1NoNamespaceRepository')); $repo1 = new \ReflectionClass($cname); $repo2 = new \ReflectionClass('DDC3231User1NoNamespaceRepository'); - $this->assertSame(EntityRepository::class, $repo1->getParentClass()->getName()); - $this->assertSame(EntityRepository::class, $repo2->getParentClass()->getName()); + self::assertSame(EntityRepository::class, $repo1->getParentClass()->getName()); + self::assertSame(EntityRepository::class, $repo2->getParentClass()->getName()); } public function testGenerateRepositoriesCustomDefaultRepository() @@ -111,20 +98,20 @@ public function testGenerateRepositoriesCustomDefaultRepository() $cname = 'Doctrine\Tests\Models\DDC3231\DDC3231User2Repository'; $fname = str_replace('\\', DIRECTORY_SEPARATOR, $cname) . '.php'; - $this->assertFileExists($this->path . DIRECTORY_SEPARATOR . $fname); - $this->assertFileExists($this->path . DIRECTORY_SEPARATOR . 'DDC3231User2NoNamespaceRepository.php'); + self::assertFileExists($this->path . DIRECTORY_SEPARATOR . $fname); + self::assertFileExists($this->path . DIRECTORY_SEPARATOR . 'DDC3231User2NoNamespaceRepository.php'); require $this->path . DIRECTORY_SEPARATOR . $fname; require $this->path . DIRECTORY_SEPARATOR . 'DDC3231User2NoNamespaceRepository.php'; - $this->assertTrue(class_exists($cname)); - $this->assertTrue(class_exists('DDC3231User2NoNamespaceRepository')); + self::assertTrue(class_exists($cname)); + self::assertTrue(class_exists('DDC3231User2NoNamespaceRepository')); $repo1 = new \ReflectionClass($cname); $repo2 = new \ReflectionClass('DDC3231User2NoNamespaceRepository'); - $this->assertSame(DDC3231EntityRepository::class, $repo1->getParentClass()->getName()); - $this->assertSame(DDC3231EntityRepository::class, $repo2->getParentClass()->getName()); + self::assertSame(DDC3231EntityRepository::class, $repo1->getParentClass()->getName()); + self::assertSame(DDC3231EntityRepository::class, $repo2->getParentClass()->getName()); } /** @@ -137,15 +124,50 @@ private function generateRepositories($filter, $defaultRepository = null) $this->_em->getConfiguration()->setDefaultRepositoryClassName($defaultRepository); } - $command = $this->application->find('orm:generate-repositories'); - $tester = new CommandTester($command); + $command = $this->application->find('orm:generate-repositories'); + $tester = new CommandTester($command); + $tester->execute( [ - 'command' => $command->getName(), - 'dest-path' => $this->path, - '--filter' => $filter, + 'command' => $command->getName(), + 'dest-path' => $this->path, + '--filter' => $filter, ] ); } + public function testNoMetadataClassesToProcess() : void + { + $configuration = $this->createMock(Configuration::class); + $metadataFactory = $this->createMock(ClassMetadataFactory::class); + $em = $this->createMock(EntityManagerInterface::class); + + $configuration->method('getDefaultRepositoryClassName') + ->willReturn('fooRepository'); + + $metadataFactory->method('getAllMetadata') + ->willReturn([]); + + $em->method('getMetadataFactory') + ->willReturn($metadataFactory); + + $em->method('getConfiguration') + ->willReturn($configuration); + + $application = new Application(); + $application->setHelperSet(new HelperSet(['em' => new EntityManagerHelper($em)])); + $application->add(new GenerateRepositoriesCommand()); + + $command = $application->find('orm:generate-repositories'); + $tester = new CommandTester($command); + + $tester->execute( + [ + 'command' => $command->getName(), + 'dest-path' => $this->path, + ] + ); + + self::assertContains('[OK] No Metadata Classes to process.', $tester->getDisplay()); + } } diff --git a/tests/Doctrine/Tests/ORM/Tools/Console/Command/InfoCommandTest.php b/tests/Doctrine/Tests/ORM/Tools/Console/Command/InfoCommandTest.php index a27fb90d79..59d5d9532a 100644 --- a/tests/Doctrine/Tests/ORM/Tools/Console/Command/InfoCommandTest.php +++ b/tests/Doctrine/Tests/ORM/Tools/Console/Command/InfoCommandTest.php @@ -2,6 +2,10 @@ namespace Doctrine\Tests\ORM\Tools\Console\Command; +use Doctrine\Common\Persistence\Mapping\Driver\MappingDriver; +use Doctrine\ORM\Configuration; +use Doctrine\ORM\EntityManagerInterface; +use Doctrine\ORM\Mapping\MappingException; use Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper; use Doctrine\Tests\Models\Cache\AttractionInfo; use Doctrine\Tests\Models\Cache\City; @@ -33,23 +37,86 @@ protected function setUp() parent::setUp(); $this->application = new Application(); - $command = new InfoCommand(); - $this->application->setHelperSet( - new HelperSet(['em' => new EntityManagerHelper($this->_em)]) - ); - - $this->application->add($command); + $this->application->setHelperSet(new HelperSet(['em' => new EntityManagerHelper($this->_em)])); + $this->application->add(new InfoCommand()); - $this->command = $this->application->find('orm:info'); - $this->tester = new CommandTester($command); + $this->command = $this->application->find('orm:info'); + $this->tester = new CommandTester($this->command); } public function testListAllClasses() { $this->tester->execute(['command' => $this->command->getName()]); - $this->assertContains(AttractionInfo::class, $this->tester->getDisplay()); - $this->assertContains(City::class, $this->tester->getDisplay()); + self::assertContains(AttractionInfo::class, $this->tester->getDisplay()); + self::assertContains(City::class, $this->tester->getDisplay()); + } + + public function testEmptyEntityClassNames() : void + { + $mappingDriver = $this->createMock(MappingDriver::class); + $configuration = $this->createMock(Configuration::class); + $em = $this->createMock(EntityManagerInterface::class); + + $mappingDriver->method('getAllClassNames') + ->willReturn([]); + + $configuration->method('getMetadataDriverImpl') + ->willReturn($mappingDriver); + + $em->method('getConfiguration') + ->willReturn($configuration); + + $application = new Application(); + $application->setHelperSet(new HelperSet(['em' => new EntityManagerHelper($em)])); + $application->add(new InfoCommand()); + + $command = $application->find('orm:info'); + $tester = new CommandTester($command); + + $tester->execute(['command' => $command->getName()]); + + self::assertContains( + ' ! [CAUTION] You do not have any mapped Doctrine ORM entities according to the current configuration', + $tester->getDisplay() + ); + + self::assertContains( + ' ! If you have entities or mapping files you should check your mapping configuration for errors.', + $tester->getDisplay() + ); + } + + public function testInvalidEntityClassMetadata() : void + { + $mappingDriver = $this->createMock(MappingDriver::class); + $configuration = $this->createMock(Configuration::class); + $em = $this->createMock(EntityManagerInterface::class); + + $mappingDriver->method('getAllClassNames') + ->willReturn(['InvalidEntity']); + + $configuration->method('getMetadataDriverImpl') + ->willReturn($mappingDriver); + + $em->method('getConfiguration') + ->willReturn($configuration); + + $em->method('getClassMetadata') + ->with('InvalidEntity') + ->willThrowException(new MappingException('exception message')); + + $application = new Application(); + $application->setHelperSet(new HelperSet(['em' => new EntityManagerHelper($em)])); + $application->add(new InfoCommand()); + + $command = $application->find('orm:info'); + $tester = new CommandTester($command); + + $tester->execute(['command' => $command->getName()]); + + self::assertContains('[FAIL] InvalidEntity', $tester->getDisplay()); + self::assertContains('exception message', $tester->getDisplay()); } } diff --git a/tests/Doctrine/Tests/ORM/Tools/Console/Command/MappingDescribeCommandTest.php b/tests/Doctrine/Tests/ORM/Tools/Console/Command/MappingDescribeCommandTest.php index 437f521e3d..f73456b47a 100644 --- a/tests/Doctrine/Tests/ORM/Tools/Console/Command/MappingDescribeCommandTest.php +++ b/tests/Doctrine/Tests/ORM/Tools/Console/Command/MappingDescribeCommandTest.php @@ -37,32 +37,26 @@ protected function setUp() parent::setUp(); $this->application = new Application(); - $command = new MappingDescribeCommand(); - - $this->application->setHelperSet(new HelperSet( - [ - 'em' => new EntityManagerHelper($this->_em) - ] - )); - - $this->application->add($command); + $this->application->setHelperSet(new HelperSet(['em' => new EntityManagerHelper($this->_em)])); + $this->application->add(new MappingDescribeCommand()); $this->command = $this->application->find('orm:mapping:describe'); - $this->tester = new CommandTester($command); + $this->tester = new CommandTester($this->command); } public function testShowSpecificFuzzySingle() { $this->tester->execute( [ - 'command' => $this->command->getName(), - 'entityName' => 'AttractionInfo', + 'command' => $this->command->getName(), + 'entityName' => 'AttractionInfo', ] ); $display = $this->tester->getDisplay(); - $this->assertContains(AttractionInfo::class, $display); - $this->assertContains('Root entity name', $display); + + self::assertContains(AttractionInfo::class, $display); + self::assertContains('Root entity name', $display); } /** @@ -73,8 +67,8 @@ public function testShowSpecificFuzzyAmbiguous() { $this->tester->execute( [ - 'command' => $this->command->getName(), - 'entityName' => 'Attraction', + 'command' => $this->command->getName(), + 'entityName' => 'Attraction', ] ); } @@ -87,8 +81,8 @@ public function testShowSpecificNotFound() { $this->tester->execute( [ - 'command' => $this->command->getName(), - 'entityName' => 'AttractionFooBar' + 'command' => $this->command->getName(), + 'entityName' => 'AttractionFooBar', ] ); } diff --git a/tests/Doctrine/Tests/ORM/Tools/Console/Command/RunDqlCommandTest.php b/tests/Doctrine/Tests/ORM/Tools/Console/Command/RunDqlCommandTest.php index e2e68bd7e2..552c678739 100644 --- a/tests/Doctrine/Tests/ORM/Tools/Console/Command/RunDqlCommandTest.php +++ b/tests/Doctrine/Tests/ORM/Tools/Console/Command/RunDqlCommandTest.php @@ -38,15 +38,10 @@ protected function setUp() parent::setUp(); - $this->application = new Application(); - $this->command = new RunDqlCommand(); - - $this->application->setHelperSet(new HelperSet( - [ - 'em' => new EntityManagerHelper($this->_em) - ] - )); + $this->command = new RunDqlCommand(); + $this->application = new Application(); + $this->application->setHelperSet(new HelperSet(['em' => new EntityManagerHelper($this->_em)])); $this->application->add($this->command); $this->tester = new CommandTester($this->command); @@ -54,7 +49,7 @@ protected function setUp() public function testCommandName() { - $this->assertSame($this->command, $this->application->get('orm:run-dql')); + self::assertSame($this->command, $this->application->get('orm:run-dql')); } public function testWillRunQuery() @@ -62,17 +57,17 @@ public function testWillRunQuery() $this->_em->persist(new DateTimeModel()); $this->_em->flush(); - $this->assertSame( + self::assertSame( 0, $this->tester->execute( [ - 'command' => $this->command->getName(), - 'dql' => 'SELECT e FROM ' . DateTimeModel::class . ' e', + 'command' => $this->command->getName(), + 'dql' => 'SELECT e FROM ' . DateTimeModel::class . ' e', ] ) ); - $this->assertContains(DateTimeModel::class, $this->tester->getDisplay()); + self::assertContains(DateTimeModel::class, $this->tester->getDisplay()); } public function testWillShowQuery() @@ -80,17 +75,17 @@ public function testWillShowQuery() $this->_em->persist(new DateTimeModel()); $this->_em->flush(); - $this->assertSame( + self::assertSame( 0, $this->tester->execute( [ - 'command' => $this->command->getName(), - 'dql' => 'SELECT e FROM ' . DateTimeModel::class . ' e', - '--show-sql' => 'true' + 'command' => $this->command->getName(), + 'dql' => 'SELECT e FROM ' . DateTimeModel::class . ' e', + '--show-sql' => 'true', ] ) ); - $this->assertStringMatchesFormat('%Astring%sSELECT %a', $this->tester->getDisplay()); + self::assertStringMatchesFormat('SELECT %a', trim($this->tester->getDisplay())); } } diff --git a/tests/travis/mariadb.travis.xml b/tests/travis/mariadb.travis.xml index 16024c4c6c..d4a3fef40c 100644 --- a/tests/travis/mariadb.travis.xml +++ b/tests/travis/mariadb.travis.xml @@ -19,6 +19,8 @@ + + diff --git a/tests/travis/mysql.travis.xml b/tests/travis/mysql.travis.xml index 16024c4c6c..d4a3fef40c 100644 --- a/tests/travis/mysql.travis.xml +++ b/tests/travis/mysql.travis.xml @@ -19,6 +19,8 @@ + + diff --git a/tests/travis/pgsql.travis.xml b/tests/travis/pgsql.travis.xml index 6ad430af32..d429084476 100644 --- a/tests/travis/pgsql.travis.xml +++ b/tests/travis/pgsql.travis.xml @@ -22,6 +22,8 @@ + + diff --git a/tests/travis/sqlite.travis.xml b/tests/travis/sqlite.travis.xml index 7894dfd2e0..b783a34ffa 100644 --- a/tests/travis/sqlite.travis.xml +++ b/tests/travis/sqlite.travis.xml @@ -6,6 +6,10 @@ failOnRisky="true" bootstrap="../Doctrine/Tests/TestInit.php" > + + + + ./../Doctrine/Tests/ORM