From 624b98c7f943dc52d49f09258b5c05ba761389bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lu=C3=ADs=20Cobucci?= Date: Thu, 6 Jun 2019 17:06:41 +0200 Subject: [PATCH 1/2] Simplify mock setup in test --- .../Migrations/Tests/Version/ExecutorTest.php | 76 +++++++++---------- 1 file changed, 36 insertions(+), 40 deletions(-) diff --git a/tests/Doctrine/Migrations/Tests/Version/ExecutorTest.php b/tests/Doctrine/Migrations/Tests/Version/ExecutorTest.php index ff26d3840a..c34188d630 100644 --- a/tests/Doctrine/Migrations/Tests/Version/ExecutorTest.php +++ b/tests/Doctrine/Migrations/Tests/Version/ExecutorTest.php @@ -62,29 +62,6 @@ public function testAddSql() : void public function testExecuteUp() : void { - $platform = $this->createMock(AbstractPlatform::class); - - $this->connection->expects(self::once()) - ->method('getDatabasePlatform') - ->willReturn($platform); - - $stopwatchEvent = $this->createMock(StopwatchEvent::class); - - $this->stopwatch->expects(self::any()) - ->method('start') - ->willReturn($stopwatchEvent); - - $stopwatchEvent->expects(self::any()) - ->method('stop'); - - $stopwatchEvent->expects(self::any()) - ->method('getDuration') - ->willReturn(100); - - $stopwatchEvent->expects(self::any()) - ->method('getMemory') - ->willReturn(100); - $this->outputWriter->expects(self::at(0)) ->method('write') ->with("\n ++ migrating 001\n"); @@ -132,23 +109,6 @@ public function testExecuteUp() : void public function testExecuteDown() : void { - $stopwatchEvent = $this->createMock(StopwatchEvent::class); - - $this->stopwatch->expects(self::any()) - ->method('start') - ->willReturn($stopwatchEvent); - - $stopwatchEvent->expects(self::any()) - ->method('stop'); - - $stopwatchEvent->expects(self::any()) - ->method('getDuration') - ->willReturn(100); - - $stopwatchEvent->expects(self::any()) - ->method('getMemory') - ->willReturn(100); - $this->outputWriter->expects(self::at(0)) ->method('write') ->with("\n -- reverting 001\n"); @@ -216,6 +176,10 @@ protected function setUp() : void ->method('getConnection') ->willReturn($this->connection); + $this->connection->expects(self::any()) + ->method('getDatabasePlatform') + ->willReturn($this->createMock(AbstractPlatform::class)); + $this->version = new Version( $this->configuration, '001', @@ -224,6 +188,23 @@ protected function setUp() : void ); $this->migration = new VersionExecutorTestMigration($this->version); + + $stopwatchEvent = $this->createMock(StopwatchEvent::class); + + $this->stopwatch->expects(self::any()) + ->method('start') + ->willReturn($stopwatchEvent); + + $stopwatchEvent->expects(self::any()) + ->method('stop'); + + $stopwatchEvent->expects(self::any()) + ->method('getDuration') + ->willReturn(100); + + $stopwatchEvent->expects(self::any()) + ->method('getMemory') + ->willReturn(100); } } @@ -241,6 +222,21 @@ class VersionExecutorTestMigration extends AbstractMigration /** @var bool */ public $postDownExecuted = false; + /** @var string */ + private $description; + + public function __construct(Version $version, string $description = '') + { + parent::__construct($version); + + $this->description = $description; + } + + public function getDescription() : string + { + return $this->description; + } + public function preUp(Schema $fromSchema) : void { $this->preUpExecuted = true; From acc550c649156ad9346613fe0e164482d132f006 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lu=C3=ADs=20Cobucci?= Date: Thu, 6 Jun 2019 17:12:13 +0200 Subject: [PATCH 2/2] Display description in migration execution This makes the migration descriptions a bit more useful, by appending it to the version number in the log. --- lib/Doctrine/Migrations/Version/Executor.php | 22 +++++++--- .../Migrations/Tests/Version/ExecutorTest.php | 40 +++++++++++++++++++ 2 files changed, 57 insertions(+), 5 deletions(-) diff --git a/lib/Doctrine/Migrations/Version/Executor.php b/lib/Doctrine/Migrations/Version/Executor.php index 4b1fee854b..76d2798e12 100644 --- a/lib/Doctrine/Migrations/Version/Executor.php +++ b/lib/Doctrine/Migrations/Version/Executor.php @@ -197,11 +197,7 @@ private function executeMigration( $migration->{'pre' . ucfirst($direction)}($fromSchema); - if ($direction === Direction::UP) { - $this->outputWriter->write("\n" . sprintf(' ++ migrating %s', $version->getVersion()) . "\n"); - } else { - $this->outputWriter->write("\n" . sprintf(' -- reverting %s', $version->getVersion()) . "\n"); - } + $this->outputWriter->write("\n" . $this->getMigrationHeader($version, $migration, $direction) . "\n"); $version->setState(State::EXEC); @@ -274,6 +270,22 @@ private function executeMigration( return $versionExecutionResult; } + private function getMigrationHeader(Version $version, AbstractMigration $migration, string $direction) : string + { + $versionInfo = $version->getVersion(); + $description = $migration->getDescription(); + + if ($description !== '') { + $versionInfo .= ' (' . $description . ')'; + } + + if ($direction === Direction::UP) { + return sprintf(' ++ migrating %s', $versionInfo); + } + + return sprintf(' -- reverting %s', $versionInfo); + } + private function skipMigration( SkipMigration $e, Version $version, diff --git a/tests/Doctrine/Migrations/Tests/Version/ExecutorTest.php b/tests/Doctrine/Migrations/Tests/Version/ExecutorTest.php index c34188d630..de4fd93969 100644 --- a/tests/Doctrine/Migrations/Tests/Version/ExecutorTest.php +++ b/tests/Doctrine/Migrations/Tests/Version/ExecutorTest.php @@ -107,6 +107,26 @@ public function testExecuteUp() : void self::assertFalse($this->migration->postDownExecuted); } + /** + * @test + */ + public function executeUpShouldAppendDescriptionWhenItIsNotEmpty() : void + { + $this->outputWriter->expects(self::at(0)) + ->method('write') + ->with("\n ++ migrating 001 (testing)\n"); + + $migratorConfiguration = (new MigratorConfiguration()) + ->setTimeAllQueries(true); + + $this->versionExecutor->execute( + $this->version, + new VersionExecutorTestMigration($this->version, 'testing'), + Direction::UP, + $migratorConfiguration + ); + } + public function testExecuteDown() : void { $this->outputWriter->expects(self::at(0)) @@ -154,6 +174,26 @@ public function testExecuteDown() : void self::assertTrue($this->migration->postDownExecuted); } + /** + * @test + */ + public function executeDownShouldAppendDescriptionWhenItIsNotEmpty() : void + { + $this->outputWriter->expects(self::at(0)) + ->method('write') + ->with("\n -- reverting 001 (testing)\n"); + + $migratorConfiguration = (new MigratorConfiguration()) + ->setTimeAllQueries(true); + + $this->versionExecutor->execute( + $this->version, + new VersionExecutorTestMigration($this->version, 'testing'), + Direction::DOWN, + $migratorConfiguration + ); + } + protected function setUp() : void { $this->configuration = $this->createMock(Configuration::class);