From cc1093af178b289accaf1e5d0d1ec7004431ca0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edi=20Modri=C4=87?= Date: Wed, 22 Apr 2020 18:14:36 +0200 Subject: [PATCH 1/2] Use SymfonyStyle to output text in console --- .../Tools/Console/Command/CurrentCommand.php | 4 +- .../Tools/Console/Command/DiffCommand.php | 17 ++++--- .../Tools/Console/Command/DoctrineCommand.php | 29 ++++------- .../Console/Command/DumpSchemaCommand.php | 3 +- .../Tools/Console/Command/ExecuteCommand.php | 8 +-- .../Tools/Console/Command/GenerateCommand.php | 3 +- .../Tools/Console/Command/LatestCommand.php | 4 +- .../Tools/Console/Command/MigrateCommand.php | 51 +++++++++---------- .../Tools/Console/Command/RollupCommand.php | 10 ++-- .../Console/Command/SyncMetadataCommand.php | 2 +- .../Tools/Console/Command/UpToDateCommand.php | 12 +++-- .../Tools/Console/Command/VersionCommand.php | 22 ++++---- 12 files changed, 80 insertions(+), 85 deletions(-) diff --git a/lib/Doctrine/Migrations/Tools/Console/Command/CurrentCommand.php b/lib/Doctrine/Migrations/Tools/Console/Command/CurrentCommand.php index c2b52bfb76..c961346035 100644 --- a/lib/Doctrine/Migrations/Tools/Console/Command/CurrentCommand.php +++ b/lib/Doctrine/Migrations/Tools/Console/Command/CurrentCommand.php @@ -42,8 +42,8 @@ protected function execute(InputInterface $input, OutputInterface $output) : int } } - $output->writeln(sprintf( - '%s%s', + $this->io->text(sprintf( + "%s%s\n", (string) $version, $description !== '' ? ' - ' . $description : '' )); diff --git a/lib/Doctrine/Migrations/Tools/Console/Command/DiffCommand.php b/lib/Doctrine/Migrations/Tools/Console/Command/DiffCommand.php index 84ec1a2f95..eaa2206df8 100644 --- a/lib/Doctrine/Migrations/Tools/Console/Command/DiffCommand.php +++ b/lib/Doctrine/Migrations/Tools/Console/Command/DiffCommand.php @@ -130,7 +130,7 @@ protected function execute( $newMigrations = $statusCalculator->getNewMigrations(); if (! $this->checkNewMigrationsOrExecutedUnavailable($newMigrations, $executedUnavailableMigrations, $input, $output)) { - $output->writeln('Migration cancelled!'); + $this->io->error('Migration cancelled!'); return 3; } @@ -149,7 +149,7 @@ protected function execute( ); } catch (NoChangesDetected $exception) { if ($allowEmptyDiff) { - $output->writeln($exception->getMessage()); + $this->io->error($exception->getMessage()); return 0; } @@ -157,7 +157,7 @@ protected function execute( throw $exception; } - $output->writeln([ + $this->io->text([ sprintf('Generated new migration class to "%s"', $path), '', sprintf( @@ -169,6 +169,7 @@ protected function execute( 'To revert the migration you can use migrations:execute --down \'%s\'', addslashes($fqcn) ), + '', ]); return 0; @@ -185,19 +186,19 @@ private function checkNewMigrationsOrExecutedUnavailable( } if (count($newMigrations) !== 0) { - $output->writeln(sprintf( - 'WARNING! You have %d available migrations to execute.', + $this->io->warning(sprintf( + 'You have %d available migrations to execute.', count($newMigrations) )); } if (count($executedUnavailableMigrations) !== 0) { - $output->writeln(sprintf( - 'WARNING! You have %d previously executed migrations in the database that are not registered migrations.', + $this->io->warning(sprintf( + 'You have %d previously executed migrations in the database that are not registered migrations.', count($executedUnavailableMigrations) )); } - return $this->canExecute('Are you sure you wish to continue? (y/n)', $input, $output); + return $this->canExecute('Are you sure you wish to continue?', $input); } } diff --git a/lib/Doctrine/Migrations/Tools/Console/Command/DoctrineCommand.php b/lib/Doctrine/Migrations/Tools/Console/Command/DoctrineCommand.php index 8b45760543..1f7a38ff83 100644 --- a/lib/Doctrine/Migrations/Tools/Console/Command/DoctrineCommand.php +++ b/lib/Doctrine/Migrations/Tools/Console/Command/DoctrineCommand.php @@ -14,7 +14,8 @@ use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; -use Symfony\Component\Console\Question\ConfirmationQuestion; +use Symfony\Component\Console\Style\StyleInterface; +use Symfony\Component\Console\Style\SymfonyStyle; use function is_string; /** @@ -25,6 +26,9 @@ abstract class DoctrineCommand extends Command /** @var DependencyFactory|null */ private $dependencyFactory; + /** @var StyleInterface */ + protected $io; + public function __construct(?DependencyFactory $dependencyFactory = null, ?string $name = null) { parent::__construct($name); @@ -55,6 +59,8 @@ protected function configure() : void protected function initialize(InputInterface $input, OutputInterface $output) : void { + $this->io = new SymfonyStyle($input, $output); + $configurationParameter = $input->getOption('configuration'); if ($this->dependencyFactory === null) { $configurationLoader = new ConfigurationFileWithFallback( @@ -87,23 +93,8 @@ protected function getDependencyFactory() : DependencyFactory return $this->dependencyFactory; } - protected function askConfirmation( - string $question, - InputInterface $input, - OutputInterface $output - ) : bool { - return $this->getHelper('question')->ask( - $input, - $output, - new ConfirmationQuestion($question) - ); - } - - protected function canExecute( - string $question, - InputInterface $input, - OutputInterface $output - ) : bool { - return ! $input->isInteractive() || $this->askConfirmation($question, $input, $output); + protected function canExecute(string $question, InputInterface $input) : bool + { + return ! $input->isInteractive() || $this->io->confirm($question); } } diff --git a/lib/Doctrine/Migrations/Tools/Console/Command/DumpSchemaCommand.php b/lib/Doctrine/Migrations/Tools/Console/Command/DumpSchemaCommand.php index 9243dd6879..58c3969976 100644 --- a/lib/Doctrine/Migrations/Tools/Console/Command/DumpSchemaCommand.php +++ b/lib/Doctrine/Migrations/Tools/Console/Command/DumpSchemaCommand.php @@ -111,7 +111,7 @@ public function execute( $lineLength ); - $output->writeln([ + $this->io->text([ sprintf('Dumped your schema to a new migration class at "%s"', $path), '', sprintf( @@ -125,6 +125,7 @@ public function execute( ), '', 'To use this as a rollup migration you can use the migrations:rollup command.', + '', ]); return 0; diff --git a/lib/Doctrine/Migrations/Tools/Console/Command/ExecuteCommand.php b/lib/Doctrine/Migrations/Tools/Console/Command/ExecuteCommand.php index 08f7efee24..2d9c472960 100644 --- a/lib/Doctrine/Migrations/Tools/Console/Command/ExecuteCommand.php +++ b/lib/Doctrine/Migrations/Tools/Console/Command/ExecuteCommand.php @@ -123,7 +123,7 @@ protected function execute(InputInterface $input, OutputInterface $output) : int $path = is_string($path) ? $path : getcwd(); if (! is_string($path) || ! is_writable($path)) { - $output->writeln('Path not writeable!'); + $this->io->error('Path not writeable!'); return 1; } @@ -134,10 +134,10 @@ protected function execute(InputInterface $input, OutputInterface $output) : int return 0; } - $question = 'WARNING! You are about to execute a database migration that could result in schema changes and data lost. Are you sure you wish to continue? (y/n)'; + $question = 'WARNING! You are about to execute a database migration that could result in schema changes and data lost. Are you sure you wish to continue?'; - if (! $migratorConfiguration->isDryRun() && ! $this->canExecute($question, $input, $output)) { - $output->writeln('Migration cancelled!'); + if (! $migratorConfiguration->isDryRun() && ! $this->canExecute($question, $input)) { + $this->io->error('Migration cancelled!'); return 1; } diff --git a/lib/Doctrine/Migrations/Tools/Console/Command/GenerateCommand.php b/lib/Doctrine/Migrations/Tools/Console/Command/GenerateCommand.php index 844523b28e..905181483a 100644 --- a/lib/Doctrine/Migrations/Tools/Console/Command/GenerateCommand.php +++ b/lib/Doctrine/Migrations/Tools/Console/Command/GenerateCommand.php @@ -67,7 +67,7 @@ protected function execute(InputInterface $input, OutputInterface $output) : int $path = $migrationGenerator->generateMigration($fqcn); - $output->writeln([ + $this->io->text([ sprintf('Generated new migration class to "%s"', $path), '', sprintf( @@ -79,6 +79,7 @@ protected function execute(InputInterface $input, OutputInterface $output) : int 'To revert the migration you can use migrations:execute --down \'%s\'', $fqcn ), + '', ]); return 0; diff --git a/lib/Doctrine/Migrations/Tools/Console/Command/LatestCommand.php b/lib/Doctrine/Migrations/Tools/Console/Command/LatestCommand.php index 7f905821b8..e082f4a0c0 100644 --- a/lib/Doctrine/Migrations/Tools/Console/Command/LatestCommand.php +++ b/lib/Doctrine/Migrations/Tools/Console/Command/LatestCommand.php @@ -39,8 +39,8 @@ protected function execute(InputInterface $input, OutputInterface $output) : int $description = ''; } - $output->writeln(sprintf( - '%s%s', + $this->io->text(sprintf( + "%s%s\n", $version, $description !== '' ? ' - ' . $description : '' )); diff --git a/lib/Doctrine/Migrations/Tools/Console/Command/MigrateCommand.php b/lib/Doctrine/Migrations/Tools/Console/Command/MigrateCommand.php index 38accee066..8ee568ac3d 100644 --- a/lib/Doctrine/Migrations/Tools/Console/Command/MigrateCommand.php +++ b/lib/Doctrine/Migrations/Tools/Console/Command/MigrateCommand.php @@ -123,7 +123,7 @@ protected function execute(InputInterface $input, OutputInterface $output) : int try { $version = $this->getDependencyFactory()->getVersionAliasResolver()->resolveVersionAlias($versionAlias); } catch (UnknownMigrationVersion|NoMigrationsFoundWithCriteria $e) { - $this->getVersionNameFromAlias($versionAlias, $output); + $this->getVersionNameFromAlias($versionAlias); return 1; } @@ -132,7 +132,7 @@ protected function execute(InputInterface $input, OutputInterface $output) : int $statusCalculator = $this->getDependencyFactory()->getMigrationStatusCalculator(); $executedUnavailableMigrations = $statusCalculator->getExecutedUnavailableMigrations(); - if ($this->checkExecutedUnavailableMigrations($executedUnavailableMigrations, $input, $output) === false) { + if ($this->checkExecutedUnavailableMigrations($executedUnavailableMigrations, $input) === false) { return 3; } @@ -142,13 +142,13 @@ protected function execute(InputInterface $input, OutputInterface $output) : int $plan = $planCalculator->getPlanUntilVersion($version); if (count($plan) === 0 && ! $allowNoMigration) { - $output->writeln('Could not find any migrations to execute.'); + $this->io->warning('Could not find any migrations to execute.'); return 1; } if (count($plan) === 0) { - $this->getVersionNameFromAlias($versionAlias, $output); + $this->getVersionNameFromAlias($versionAlias); return 0; } @@ -161,7 +161,7 @@ protected function execute(InputInterface $input, OutputInterface $output) : int $path = is_string($path) ? $path : getcwd(); if (! is_string($path) || ! is_writable($path)) { - $output->writeln('Path not writeable!'); + $this->io->error('Path not writeable!'); return 1; } @@ -172,10 +172,10 @@ protected function execute(InputInterface $input, OutputInterface $output) : int return 0; } - $question = 'WARNING! You are about to execute a database migration that could result in schema changes and data loss. Are you sure you wish to continue? (y/n)'; + $question = 'WARNING! You are about to execute a database migration that could result in schema changes and data loss. Are you sure you wish to continue?'; - if (! $migratorConfiguration->isDryRun() && ! $this->canExecute($question, $input, $output)) { - $output->writeln('Migration cancelled!'); + if (! $migratorConfiguration->isDryRun() && ! $this->canExecute($question, $input)) { + $this->io->error('Migration cancelled!'); return 3; } @@ -192,23 +192,24 @@ protected function execute(InputInterface $input, OutputInterface $output) : int $migrator->migrate($plan, $migratorConfiguration); + $this->io->newLine(); + return 0; } private function checkExecutedUnavailableMigrations( ExecutedMigrationsSet $executedUnavailableMigrations, - InputInterface $input, - OutputInterface $output + InputInterface $input ) : bool { if (count($executedUnavailableMigrations) !== 0) { - $output->writeln(sprintf( - 'WARNING! You have %s previously executed migrations in the database that are not registered migrations.', + $this->io->warning(sprintf( + 'You have %s previously executed migrations in the database that are not registered migrations.', count($executedUnavailableMigrations) )); foreach ($executedUnavailableMigrations->getItems() as $executedUnavailableMigration) { - $output->writeln(sprintf( - ' >> %s (%s)', + $this->io->text(sprintf( + '>> %s (%s)', $executedUnavailableMigration->getExecutedAt() !== null ? $executedUnavailableMigration->getExecutedAt()->format('Y-m-d H:i:s') : null, @@ -216,10 +217,10 @@ private function checkExecutedUnavailableMigrations( )); } - $question = 'Are you sure you wish to continue? (y/n)'; + $question = 'Are you sure you wish to continue?'; - if (! $this->canExecute($question, $input, $output)) { - $output->writeln('Migration cancelled!'); + if (! $this->canExecute($question, $input)) { + $this->io->error('Migration cancelled!'); return false; } @@ -228,30 +229,28 @@ private function checkExecutedUnavailableMigrations( return true; } - private function getVersionNameFromAlias( - string $versionAlias, - OutputInterface $output - ) : void { + private function getVersionNameFromAlias(string $versionAlias) : void + { if ($versionAlias === 'first') { - $output->writeln('Already at first version.'); + $this->io->error('Already at first version.'); return; } if ($versionAlias === 'next' || $versionAlias === 'latest') { - $output->writeln('Already at latest version.'); + $this->io->error('Already at latest version.'); return; } if (substr($versionAlias, 0, 7) === 'current') { - $output->writeln('The delta couldn\'t be reached.'); + $this->io->error('The delta couldn\'t be reached.'); return; } - $output->writeln(sprintf( - 'Unknown version: %s', + $this->io->error(sprintf( + 'Unknown version: %s', OutputFormatter::escape($versionAlias) )); } diff --git a/lib/Doctrine/Migrations/Tools/Console/Command/RollupCommand.php b/lib/Doctrine/Migrations/Tools/Console/Command/RollupCommand.php index 47cb8a1c4d..fa114a1d11 100644 --- a/lib/Doctrine/Migrations/Tools/Console/Command/RollupCommand.php +++ b/lib/Doctrine/Migrations/Tools/Console/Command/RollupCommand.php @@ -37,10 +37,10 @@ protected function configure() : void protected function execute(InputInterface $input, OutputInterface $output) : int { - $question = 'WARNING! You are about to execute a database migration that could result in schema changes and data loss. Are you sure you wish to continue? (y/n)'; + $question = 'WARNING! You are about to execute a database migration that could result in schema changes and data loss. Are you sure you wish to continue?'; - if (! $this->canExecute($question, $input, $output)) { - $output->writeln('Migration cancelled!'); + if (! $this->canExecute($question, $input)) { + $this->io->error('Migration cancelled!'); return 3; } @@ -48,8 +48,8 @@ protected function execute(InputInterface $input, OutputInterface $output) : int $this->getDependencyFactory()->getMetadataStorage()->ensureInitialized(); $version = $this->getDependencyFactory()->getRollup()->rollup(); - $output->writeln(sprintf( - 'Rolled up migrations to version %s', + $this->io->success(sprintf( + 'Rolled up migrations to version %s', (string) $version )); diff --git a/lib/Doctrine/Migrations/Tools/Console/Command/SyncMetadataCommand.php b/lib/Doctrine/Migrations/Tools/Console/Command/SyncMetadataCommand.php index e1ba193f74..c4c4d2de6c 100644 --- a/lib/Doctrine/Migrations/Tools/Console/Command/SyncMetadataCommand.php +++ b/lib/Doctrine/Migrations/Tools/Console/Command/SyncMetadataCommand.php @@ -33,7 +33,7 @@ public function execute( ) : int { $this->getDependencyFactory()->getMetadataStorage()->ensureInitialized(); - $output->writeln('Metadata storage synchronized'); + $this->io->success('Metadata storage synchronized'); return 0; } diff --git a/lib/Doctrine/Migrations/Tools/Console/Command/UpToDateCommand.php b/lib/Doctrine/Migrations/Tools/Console/Command/UpToDateCommand.php index f10370a375..67bd195a6a 100644 --- a/lib/Doctrine/Migrations/Tools/Console/Command/UpToDateCommand.php +++ b/lib/Doctrine/Migrations/Tools/Console/Command/UpToDateCommand.php @@ -55,15 +55,15 @@ protected function execute(InputInterface $input, OutputInterface $output) : int $executedUnavailableMigrationsCount = count($executedUnavailableMigrations); if ($newMigrationsCount === 0 && $executedUnavailableMigrationsCount === 0) { - $output->writeln('Up-to-date! No migrations to execute.'); + $this->io->success('Up-to-date! No migrations to execute.'); return 0; } $exitCode = 0; if ($newMigrationsCount > 0) { - $output->writeln(sprintf( - 'Out-of-date! %u migration%s available to execute.', + $this->io->error(sprintf( + 'Out-of-date! %u migration%s available to execute.', $newMigrationsCount, $newMigrationsCount > 1 ? 's are' : ' is' )); @@ -71,8 +71,8 @@ protected function execute(InputInterface $input, OutputInterface $output) : int } if ($executedUnavailableMigrationsCount > 0) { - $output->writeln(sprintf( - 'You have %1$u previously executed migration%3$s in the database that %2$s registered migration%3$s.', + $this->io->error(sprintf( + 'You have %1$u previously executed migration%3$s in the database that %2$s registered migration%3$s.', $executedUnavailableMigrationsCount, $executedUnavailableMigrationsCount > 1 ? 'are not' : 'is not a', $executedUnavailableMigrationsCount > 1 ? 's' : '' @@ -85,6 +85,8 @@ protected function execute(InputInterface $input, OutputInterface $output) : int if ($input->getOption('list-migrations')) { $versions = $this->getSortedVersions($newMigrations, $executedUnavailableMigrations); $this->getDependencyFactory()->getMigrationStatusInfosHelper()->listVersions($versions, $output); + + $this->io->newLine(); } return $exitCode; diff --git a/lib/Doctrine/Migrations/Tools/Console/Command/VersionCommand.php b/lib/Doctrine/Migrations/Tools/Console/Command/VersionCommand.php index aee9973ec7..dbf35c03eb 100644 --- a/lib/Doctrine/Migrations/Tools/Console/Command/VersionCommand.php +++ b/lib/Doctrine/Migrations/Tools/Console/Command/VersionCommand.php @@ -111,14 +111,14 @@ protected function execute(InputInterface $input, OutputInterface $output) : int $this->markMigrated = $input->getOption('add'); if ($input->isInteractive()) { - $question = 'WARNING! You are about to add, delete or synchronize migration versions from the version table that could result in data lost. Are you sure you wish to continue? (y/n)'; + $question = 'WARNING! You are about to add, delete or synchronize migration versions from the version table that could result in data lost. Are you sure you wish to continue?'; - $confirmation = $this->askConfirmation($question, $input, $output); + $confirmation = $this->io->confirm($question); if ($confirmation) { $this->markVersions($input, $output); } else { - $output->writeln('Migration cancelled!'); + $this->io->error('Migration cancelled!'); } } else { $this->markVersions($input, $output); @@ -204,15 +204,15 @@ private function mark(InputInterface $input, OutputInterface $output, Version $v $question = 'WARNING! You are about to delete a migration version from the version table that has no corresponding migration file.' . - 'Do you want to delete this migration from the migrations table? (y/n)'; + 'Do you want to delete this migration from the migrations table?'; - $confirmation = $this->askConfirmation($question, $input, $output); + $confirmation = $this->io->confirm($question); if ($confirmation) { $migrationResult = new ExecutionResult($version, Direction::DOWN); $storage->complete($migrationResult); - $output->writeln(sprintf( - '%s deleted from the version table.', + $this->io->text(sprintf( + "%s deleted from the version table.\n", (string) $version )); @@ -246,16 +246,16 @@ private function mark(InputInterface $input, OutputInterface $output, Version $v $migrationResult = new ExecutionResult($version, Direction::UP); $storage->complete($migrationResult); - $output->writeln(sprintf( - '%s added to the version table.', + $this->io->text(sprintf( + "%s added to the version table.\n", (string) $version )); } else { $migrationResult = new ExecutionResult($version, Direction::DOWN); $storage->complete($migrationResult); - $output->writeln(sprintf( - '%s deleted from the version table.', + $this->io->text(sprintf( + "%s deleted from the version table.\n", (string) $version )); } From 362ca53bbbd89036a3f12fc89a8bf4a09f2ce0b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edi=20Modri=C4=87?= Date: Thu, 23 Apr 2020 12:33:52 +0200 Subject: [PATCH 2/2] Update tests for SymfonyStyle usage in commands --- .travis.yml | 2 + .../Tools/Console/Command/DiffCommandTest.php | 35 ++++------- .../Console/Command/DumpSchemaCommandTest.php | 3 +- .../Console/Command/ExecuteCommandTest.php | 24 ++------ .../Console/Command/GenerateCommandTest.php | 3 +- .../Console/Command/MigrateCommandTest.php | 59 +++++++------------ .../Console/Command/RollupCommandTest.php | 21 ++----- .../Command/SyncMetadataCommandTest.php | 19 +++--- .../Console/Command/UpToDateCommandTest.php | 6 +- 9 files changed, 63 insertions(+), 109 deletions(-) diff --git a/.travis.yml b/.travis.yml index 298eca5039..bc2923dba3 100644 --- a/.travis.yml +++ b/.travis.yml @@ -13,6 +13,8 @@ cache: before_install: - mv ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/xdebug.ini{,.disabled} || echo "xdebug not available" + - stty cols 120 + - export COLUMNS=120 install: - rm composer.lock diff --git a/tests/Doctrine/Migrations/Tests/Tools/Console/Command/DiffCommandTest.php b/tests/Doctrine/Migrations/Tests/Tools/Console/Command/DiffCommandTest.php index 70c74c36bc..8f4e46787c 100644 --- a/tests/Doctrine/Migrations/Tests/Tools/Console/Command/DiffCommandTest.php +++ b/tests/Doctrine/Migrations/Tests/Tools/Console/Command/DiffCommandTest.php @@ -18,9 +18,8 @@ use Doctrine\Migrations\Version\Version; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; -use Symfony\Component\Console\Helper\HelperSet; -use Symfony\Component\Console\Helper\QuestionHelper; use Symfony\Component\Console\Tester\CommandTester; +use function array_map; use function explode; use function sys_get_temp_dir; use function trim; @@ -42,9 +41,6 @@ final class DiffCommandTest extends TestCase /** @var MockObject|DependencyFactory */ private $dependencyFactory; - /** @var MockObject|QuestionHelper */ - private $questions; - /** @var CommandTester */ private $diffCommandTester; @@ -88,7 +84,7 @@ public function testExecute() : void 'To run just this migration for testing purposes, you can use migrations:execute --up \'FooNs\\\\Version1234\'', '', 'To revert the migration you can use migrations:execute --down \'FooNs\\\\Version1234\'', - ], explode("\n", trim($output))); + ], array_map('trim', explode("\n", trim($output)))); } public function testAvailableMigrationsCancel() : void @@ -103,19 +99,16 @@ public function testAvailableMigrationsCancel() : void ->method('getExecutedUnavailableMigrations') ->willReturn(new ExecutedMigrationsSet([])); - $this->questions->expects(self::once()) - ->method('ask') - ->willReturn(false); + $this->diffCommandTester->setInputs(['no']); $this->migrationDiffGenerator->expects(self::never())->method('generate'); $statusCode = $this->diffCommandTester->execute([]); $output = $this->diffCommandTester->getDisplay(true); - self::assertSame([ - 'WARNING! You have 1 available migrations to execute.', - 'Migration cancelled!', - ], explode("\n", trim($output))); + + self::assertStringContainsString('[WARNING] You have 1 available migrations to execute.', $output); + self::assertStringContainsString('[ERROR] Migration cancelled!', $output); self::assertSame(3, $statusCode); } @@ -133,20 +126,17 @@ public function testExecutedUnavailableMigrationsCancel() : void ->method('getExecutedUnavailableMigrations') ->willReturn(new ExecutedMigrationsSet([$e1])); - $this->questions->expects(self::once()) - ->method('ask') - ->willReturn(false); + $this->diffCommandTester->setInputs(['no']); $this->migrationDiffGenerator->expects(self::never())->method('generate'); $statusCode = $this->diffCommandTester->execute([]); $output = $this->diffCommandTester->getDisplay(true); - self::assertSame([ - 'WARNING! You have 1 available migrations to execute.', - 'WARNING! You have 1 previously executed migrations in the database that are not registered migrations.', - 'Migration cancelled!', - ], explode("\n", trim($output))); + + self::assertStringContainsString('[WARNING] You have 1 available migrations to execute.', $output); + self::assertStringContainsString('[WARNING] You have 1 previously executed migrations in the database that are not registered migrations.', $output); + self::assertStringContainsString('[ERROR] Migration cancelled!', $output); self::assertSame(3, $statusCode); } @@ -179,8 +169,5 @@ protected function setUp() : void $this->diffCommand = new DiffCommand($this->dependencyFactory); $this->diffCommandTester = new CommandTester($this->diffCommand); - - $this->questions = $this->createStub(QuestionHelper::class); - $this->diffCommand->setHelperSet(new HelperSet(['question' => $this->questions])); } } diff --git a/tests/Doctrine/Migrations/Tests/Tools/Console/Command/DumpSchemaCommandTest.php b/tests/Doctrine/Migrations/Tests/Tools/Console/Command/DumpSchemaCommandTest.php index d25ba44d79..1765117e0c 100644 --- a/tests/Doctrine/Migrations/Tests/Tools/Console/Command/DumpSchemaCommandTest.php +++ b/tests/Doctrine/Migrations/Tests/Tools/Console/Command/DumpSchemaCommandTest.php @@ -18,6 +18,7 @@ use PHPUnit\Framework\TestCase; use RuntimeException; use Symfony\Component\Console\Tester\CommandTester; +use function array_map; use function explode; use function sys_get_temp_dir; use function trim; @@ -84,7 +85,7 @@ public function testExecute() : void '', 'To use this as a rollup migration you can use the migrations:rollup command.', ], - explode("\n", trim($output)) + array_map('trim', explode("\n", trim($output))) ); } diff --git a/tests/Doctrine/Migrations/Tests/Tools/Console/Command/ExecuteCommandTest.php b/tests/Doctrine/Migrations/Tests/Tools/Console/Command/ExecuteCommandTest.php index bf04fecd12..79c2ee3156 100644 --- a/tests/Doctrine/Migrations/Tests/Tools/Console/Command/ExecuteCommandTest.php +++ b/tests/Doctrine/Migrations/Tests/Tools/Console/Command/ExecuteCommandTest.php @@ -21,8 +21,6 @@ use Doctrine\Migrations\Version\MigrationPlanCalculator; use Doctrine\Migrations\Version\Version; use PHPUnit\Framework\MockObject\MockObject; -use Symfony\Component\Console\Helper\HelperSet; -use Symfony\Component\Console\Helper\QuestionHelper; use Symfony\Component\Console\Tester\CommandTester; use function getcwd; use function sys_get_temp_dir; @@ -48,9 +46,6 @@ class ExecuteCommandTest extends MigrationTestCase /** @var MigrationPlanCalculator|MockObject */ private $planCalculator; - /** @var MockObject|QuestionHelper */ - private $questions; - /** * @param mixed $arg * @@ -93,9 +88,7 @@ public function getWriteSqlValues() : array public function testExecute() : void { - $this->questions->expects(self::once()) - ->method('ask') - ->willReturn(true); + $this->executeCommandTester->setInputs(['yes']); $this->migrator ->expects(self::once()) @@ -112,7 +105,7 @@ public function testExecute() : void ]); self::assertSame(0, $this->executeCommandTester->getStatusCode()); - self::assertSame('[notice] Executing 1 up', trim($this->executeCommandTester->getDisplay(true))); + self::assertStringContainsString('[notice] Executing 1 up', trim($this->executeCommandTester->getDisplay(true))); } public function testExecuteMultiple() : void @@ -127,9 +120,7 @@ public function testExecuteMultiple() : void ->with([new Version('1'), new Version('2')]) ->willReturn($pl); - $this->questions->expects(self::once()) - ->method('ask') - ->willReturn(true); + $this->executeCommandTester->setInputs(['yes']); $this->migrator ->expects(self::once()) @@ -146,14 +137,12 @@ public function testExecuteMultiple() : void ]); self::assertSame(0, $this->executeCommandTester->getStatusCode()); - self::assertSame('[notice] Executing 1, 2 up', trim($this->executeCommandTester->getDisplay(true))); + self::assertStringContainsString('[notice] Executing 1, 2 up', trim($this->executeCommandTester->getDisplay(true))); } public function testExecuteCancel() : void { - $this->questions->expects(self::once()) - ->method('ask') - ->willReturn(false); + $this->executeCommandTester->setInputs(['no']); $this->migrator ->expects(self::never()) @@ -201,9 +190,6 @@ protected function setUp() : void $this->executeCommand = new ExecuteCommand($this->dependencyFactory); - $this->questions = $this->createMock(QuestionHelper::class); - $this->executeCommand->setHelperSet(new HelperSet(['question' => $this->questions])); - $this->executeCommandTester = new CommandTester($this->executeCommand); } } diff --git a/tests/Doctrine/Migrations/Tests/Tools/Console/Command/GenerateCommandTest.php b/tests/Doctrine/Migrations/Tests/Tools/Console/Command/GenerateCommandTest.php index f821fca448..fbd37c5e59 100644 --- a/tests/Doctrine/Migrations/Tests/Tools/Console/Command/GenerateCommandTest.php +++ b/tests/Doctrine/Migrations/Tests/Tools/Console/Command/GenerateCommandTest.php @@ -12,6 +12,7 @@ use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use Symfony\Component\Console\Tester\CommandTester; +use function array_map; use function explode; use function sys_get_temp_dir; use function trim; @@ -49,7 +50,7 @@ public function testExecute() : void 'To run just this migration for testing purposes, you can use migrations:execute --up \'FooNs\Version1234\'', '', 'To revert the migration you can use migrations:execute --down \'FooNs\Version1234\'', - ], explode("\n", trim($output))); + ], array_map('trim', explode("\n", trim($output)))); } protected function setUp() : void diff --git a/tests/Doctrine/Migrations/Tests/Tools/Console/Command/MigrateCommandTest.php b/tests/Doctrine/Migrations/Tests/Tools/Console/Command/MigrateCommandTest.php index 08cf8505d4..a260e8675b 100644 --- a/tests/Doctrine/Migrations/Tests/Tools/Console/Command/MigrateCommandTest.php +++ b/tests/Doctrine/Migrations/Tests/Tools/Console/Command/MigrateCommandTest.php @@ -28,7 +28,6 @@ use PHPUnit\Framework\MockObject\MockObject; use Symfony\Component\Console\Helper\HelperSet; use Symfony\Component\Console\Helper\QuestionHelper; -use Symfony\Component\Console\Question\ConfirmationQuestion; use Symfony\Component\Console\Tester\CommandTester; use function getcwd; use function strpos; @@ -142,16 +141,15 @@ public function testExecutedUnavailableMigrationsCancel() : void $migrator->expects(self::never()) ->method('migrate'); - $this->questions->expects(self::at(0)) - ->method('ask') - ->willReturnCallback(static function ($input, $output, $question) : bool { - self::assertEquals(new ConfirmationQuestion('Are you sure you wish to continue? (y/n)'), $question); - - return false; - }); + $this->migrateCommandTester->setInputs(['no']); $this->migrateCommandTester->execute(['version' => 'prev']); + $output = $this->migrateCommandTester->getDisplay(true); + + self::assertStringContainsString('Are you sure you wish to continue?', $output); + self::assertStringContainsString('[ERROR] Migration cancelled!', $output); + self::assertSame(3, $this->migrateCommandTester->getStatusCode()); } @@ -199,9 +197,7 @@ public function testExecuteMigrate() : void $migrator = $this->createMock(DbalMigrator::class); $this->dependencyFactory->setService(Migrator::class, $migrator); - $this->questions->expects(self::once()) - ->method('ask') - ->willReturn(true); + $this->migrateCommandTester->setInputs(['yes']); $migrator->expects(self::once()) ->method('migrate') @@ -215,7 +211,7 @@ public function testExecuteMigrate() : void $this->migrateCommandTester->execute([]); self::assertSame(0, $this->migrateCommandTester->getStatusCode()); - self::assertSame('[notice] Migrating up to A', trim($this->migrateCommandTester->getDisplay(true))); + self::assertStringContainsString('[notice] Migrating up to A', trim($this->migrateCommandTester->getDisplay(true))); } public function testExecuteMigrateDown() : void @@ -232,9 +228,7 @@ public function testExecuteMigrateDown() : void $migrator = $this->createMock(DbalMigrator::class); $this->dependencyFactory->setService(Migrator::class, $migrator); - $this->questions->expects(self::once()) - ->method('ask') - ->willReturn(true); + $this->migrateCommandTester->setInputs(['yes']); $migrator->expects(self::once()) ->method('migrate') @@ -248,7 +242,7 @@ public function testExecuteMigrateDown() : void $this->migrateCommandTester->execute(['version' => 'prev']); self::assertSame(0, $this->migrateCommandTester->getStatusCode()); - self::assertSame('[notice] Migrating down to A', trim($this->migrateCommandTester->getDisplay(true))); + self::assertStringContainsString('[notice] Migrating down to A', trim($this->migrateCommandTester->getDisplay(true))); } public function testExecuteMigrateAllOrNothing() : void @@ -285,23 +279,15 @@ public function testExecuteMigrateCancelExecutedUnavailableMigrations() : void $migrator->expects(self::never()) ->method('migrate'); - $this->questions->expects(self::at(0)) - ->method('ask') - ->willReturnCallback(static function ($input, $output, $question) : bool { - self::assertEquals(new ConfirmationQuestion('Are you sure you wish to continue? (y/n)'), $question); + $this->migrateCommandTester->setInputs(['yes', 'no']); - return true; - }); + $this->migrateCommandTester->execute(['version' => 'latest']); - $this->questions->expects(self::at(1)) - ->method('ask') - ->willReturnCallback(static function ($input, $output, $question) : bool { - self::assertEquals(new ConfirmationQuestion('WARNING! You are about to execute a database migration that could result in schema changes and data loss. Are you sure you wish to continue? (y/n)'), $question); + $output = $this->migrateCommandTester->getDisplay(true); - return false; - }); - - $this->migrateCommandTester->execute(['version' => 'latest']); + self::assertStringContainsString('[WARNING] You have 1 previously executed migrations in the database that are not registered migrations.', $output); + self::assertStringContainsString('WARNING! You are about to execute a database migration that could result in schema changes and data loss. Are you sure you wish to continue?', $output); + self::assertStringContainsString('[ERROR] Migration cancelled!', $output); self::assertSame(3, $this->migrateCommandTester->getStatusCode()); } @@ -314,16 +300,15 @@ public function testExecuteMigrateCancel() : void $migrator->expects(self::never()) ->method('migrate'); - $this->questions->expects(self::once()) - ->method('ask') - ->willReturnCallback(static function ($input, $output, $question) : bool { - self::assertEquals(new ConfirmationQuestion('WARNING! You are about to execute a database migration that could result in schema changes and data loss. Are you sure you wish to continue? (y/n)'), $question); - - return false; - }); + $this->migrateCommandTester->setInputs(['no']); $this->migrateCommandTester->execute(['version' => 'latest']); + $output = $this->migrateCommandTester->getDisplay(true); + + self::assertStringContainsString('WARNING! You are about to execute a database migration that could result in schema changes and data loss. Are you sure you wish to continue?', $output); + self::assertStringContainsString('[ERROR] Migration cancelled!', $output); + self::assertSame(3, $this->migrateCommandTester->getStatusCode()); } diff --git a/tests/Doctrine/Migrations/Tests/Tools/Console/Command/RollupCommandTest.php b/tests/Doctrine/Migrations/Tests/Tools/Console/Command/RollupCommandTest.php index 475e1a5944..dd2ba88099 100644 --- a/tests/Doctrine/Migrations/Tests/Tools/Console/Command/RollupCommandTest.php +++ b/tests/Doctrine/Migrations/Tests/Tools/Console/Command/RollupCommandTest.php @@ -10,8 +10,6 @@ use Doctrine\Migrations\Version\Version; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; -use Symfony\Component\Console\Helper\HelperSet; -use Symfony\Component\Console\Helper\QuestionHelper; use Symfony\Component\Console\Tester\CommandTester; use function trim; @@ -43,17 +41,12 @@ public function testExecute() : void $this->rollupCommandTest->execute([], ['interactive' => false]); $output = $this->rollupCommandTest->getDisplay(true); - self::assertSame('Rolled up migrations to version 1234', trim($output)); + self::assertSame('[OK] Rolled up migrations to version 1234', trim($output)); } public function testExecutionContinuesWhenAnsweringYes() : void { - $questions = $this->createMock(QuestionHelper::class); - $questions->expects(self::once()) - ->method('ask') - ->willReturn(true); - - $this->rollupCommand->setHelperSet(new HelperSet(['question' => $questions])); + $this->rollupCommandTest->setInputs(['yes']); $this->dependencyFactory ->expects(self::once()) @@ -67,15 +60,12 @@ public function testExecutionContinuesWhenAnsweringYes() : void $this->rollupCommandTest->execute([]); $output = $this->rollupCommandTest->getDisplay(true); - self::assertSame('Rolled up migrations to version 1234', trim($output)); + self::assertStringContainsString('[OK] Rolled up migrations to version 1234', trim($output)); } public function testExecutionStoppedWhenAnsweringNo() : void { - $questions = $this->createMock(QuestionHelper::class); - $questions->expects(self::once()) - ->method('ask') - ->willReturn(false); + $this->rollupCommandTest->setInputs(['no']); $this->dependencyFactory ->expects(self::never()) @@ -84,13 +74,12 @@ public function testExecutionStoppedWhenAnsweringNo() : void $this->rollup->expects(self::never()) ->method('rollup'); - $this->rollupCommand->setHelperSet(new HelperSet(['question' => $questions])); $exitCode = $this->rollupCommandTest->execute([]); self::assertSame(3, $exitCode); $output = $this->rollupCommandTest->getDisplay(true); - self::assertSame('Migration cancelled!', trim($output)); + self::assertStringContainsString('[ERROR] Migration cancelled!', trim($output)); } protected function setUp() : void diff --git a/tests/Doctrine/Migrations/Tests/Tools/Console/Command/SyncMetadataCommandTest.php b/tests/Doctrine/Migrations/Tests/Tools/Console/Command/SyncMetadataCommandTest.php index 946343026b..73ca70c57b 100644 --- a/tests/Doctrine/Migrations/Tests/Tools/Console/Command/SyncMetadataCommandTest.php +++ b/tests/Doctrine/Migrations/Tests/Tools/Console/Command/SyncMetadataCommandTest.php @@ -9,8 +9,7 @@ use Doctrine\Migrations\Tools\Console\Command\SyncMetadataCommand; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; -use Symfony\Component\Console\Input\InputInterface; -use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Console\Tester\CommandTester; final class SyncMetadataCommandTest extends TestCase { @@ -23,19 +22,19 @@ final class SyncMetadataCommandTest extends TestCase /** @var SyncMetadataCommand */ private $storageCommand; + /** @var CommandTester */ + private $storageCommandTester; + public function testExecute() : void { - $input = $this->createMock(InputInterface::class); - $output = $this->createMock(OutputInterface::class); - $this->storage->expects(self::once()) ->method('ensureInitialized'); - $output->expects(self::once()) - ->method('writeln') - ->with('Metadata storage synchronized'); + $this->storageCommandTester->execute([]); - $this->storageCommand->execute($input, $output); + $output = $this->storageCommandTester->getDisplay(true); + + self::assertStringContainsString('[OK] Metadata storage synchronized', $output); } protected function setUp() : void @@ -49,5 +48,7 @@ protected function setUp() : void ->willReturn($this->storage); $this->storageCommand = new SyncMetadataCommand($this->dependencyFactory); + + $this->storageCommandTester = new CommandTester($this->storageCommand); } } diff --git a/tests/Doctrine/Migrations/Tests/Tools/Console/Command/UpToDateCommandTest.php b/tests/Doctrine/Migrations/Tests/Tools/Console/Command/UpToDateCommandTest.php index c8b55cbf7f..272d2161d0 100644 --- a/tests/Doctrine/Migrations/Tests/Tools/Console/Command/UpToDateCommandTest.php +++ b/tests/Doctrine/Migrations/Tests/Tools/Console/Command/UpToDateCommandTest.php @@ -121,8 +121,10 @@ public function testMigrationList() : void self::assertSame( [ - 'Out-of-date! 1 migration is available to execute.', - 'You have 1 previously executed migration in the database that is not a registered migration.', + '[ERROR] Out-of-date! 1 migration is available to execute.', + '', + '[ERROR] You have 1 previously executed migration in the database that is not a registered migration.', + '', '+-----------+-------------------------+---------------------+----------------+-------------+', '| Migration Versions | |', '+-----------+-------------------------+---------------------+----------------+-------------+',