diff --git a/src/Command/RollbackCommand.php b/src/Command/RollbackCommand.php index c34eea71..8739b7d2 100644 --- a/src/Command/RollbackCommand.php +++ b/src/Command/RollbackCommand.php @@ -231,16 +231,12 @@ protected function getTargetFromDate(string|bool $date): string ]; /** @var string $date */ - if (!isset($dateStrlenToAppend[strlen($date)])) { - throw new InvalidArgumentException('Invalid date. Format is YYYY[MM[DD[HH[II[SS]]]]].'); - } $dateLength = strlen($date); if (!isset($dateStrlenToAppend[$dateLength])) { throw new InvalidArgumentException('Invalid date. Format is YYYY[MM[DD[HH[II[SS]]]]].'); } $target = $date . $dateStrlenToAppend[$dateLength]; $dateTime = DateTime::createFromFormat('YmdHis', $target); - if ($dateTime === false) { throw new InvalidArgumentException('Invalid date. Format is YYYY[MM[DD[HH[II[SS]]]]].'); } diff --git a/tests/TestCase/Command/MigrateCommandTest.php b/tests/TestCase/Command/MigrateCommandTest.php index 4da80375..efee4f14 100644 --- a/tests/TestCase/Command/MigrateCommandTest.php +++ b/tests/TestCase/Command/MigrateCommandTest.php @@ -7,6 +7,8 @@ use Cake\Core\Configure; use Cake\Core\Exception\MissingPluginException; use Cake\Database\Exception\DatabaseException; +use Cake\Event\EventInterface; +use Cake\Event\EventManager; use Cake\TestSuite\TestCase; class MigrateCommandTest extends TestCase @@ -291,4 +293,41 @@ public function testMigrateWithNoLock() $this->assertOutputNotContains('Dumping'); $this->assertFileDoesNotExist($migrationPath . DS . 'schema-dump-test.lock'); } + + public function testEventsFired(): void + { + /** @var array $fired */ + $fired = []; + EventManager::instance()->on('Migration.beforeMigrate', function (EventInterface $event) use (&$fired): void { + $fired[] = $event->getName(); + }); + EventManager::instance()->on('Migration.afterMigrate', function (EventInterface $event) use (&$fired): void { + $fired[] = $event->getName(); + }); + $this->exec('migrations migrate -c test --no-lock'); + $this->assertExitSuccess(); + $this->assertSame(['Migration.beforeMigrate', 'Migration.afterMigrate'], $fired); + } + + public function testBeforeMigrateEventAbort(): void + { + /** @var array $fired */ + $fired = []; + EventManager::instance()->on('Migration.beforeMigrate', function (EventInterface $event) use (&$fired): void { + $fired[] = $event->getName(); + $event->stopPropagation(); + $event->setResult(0); + }); + EventManager::instance()->on('Migration.afterMigrate', function (EventInterface $event) use (&$fired): void { + $fired[] = $event->getName(); + }); + $this->exec('migrations migrate -c test --no-lock'); + $this->assertExitError(); + + // Only one event was fired + $this->assertSame(['Migration.beforeMigrate'], $fired); + + $table = $this->fetchTable('Phinxlog'); + $this->assertEquals(0, $table->find()->count()); + } } diff --git a/tests/TestCase/Command/RollbackCommandTest.php b/tests/TestCase/Command/RollbackCommandTest.php index 590a4f57..3c45bbc3 100644 --- a/tests/TestCase/Command/RollbackCommandTest.php +++ b/tests/TestCase/Command/RollbackCommandTest.php @@ -7,6 +7,8 @@ use Cake\Console\TestSuite\StubConsoleOutput; use Cake\Core\Configure; use Cake\Database\Exception\DatabaseException; +use Cake\Event\EventInterface; +use Cake\Event\EventManager; use Cake\TestSuite\TestCase; use InvalidArgumentException; @@ -231,4 +233,41 @@ public function testFakeOption(): void $dumpFile = $migrationPath . DS . 'schema-dump-test.lock'; $this->assertFileDoesNotExist($dumpFile); } + + public function testEventsFired(): void + { + /** @var array $fired */ + $fired = []; + EventManager::instance()->on('Migration.beforeRollback', function (EventInterface $event) use (&$fired): void { + $fired[] = $event->getName(); + }); + EventManager::instance()->on('Migration.afterRollback', function (EventInterface $event) use (&$fired): void { + $fired[] = $event->getName(); + }); + $this->exec('migrations rollback -c test --no-lock'); + $this->assertExitSuccess(); + $this->assertSame(['Migration.beforeRollback', 'Migration.afterRollback'], $fired); + } + + public function testBeforeMigrateEventAbort(): void + { + /** @var array $fired */ + $fired = []; + EventManager::instance()->on('Migration.beforeRollback', function (EventInterface $event) use (&$fired): void { + $fired[] = $event->getName(); + $event->stopPropagation(); + $event->setResult(0); + }); + EventManager::instance()->on('Migration.afterRollback', function (EventInterface $event) use (&$fired): void { + $fired[] = $event->getName(); + }); + $this->exec('migrations rollback -c test --no-lock'); + $this->assertExitError(); + + // Only one event was fired + $this->assertSame(['Migration.beforeRollback'], $fired); + + $table = $this->fetchTable('Phinxlog'); + $this->assertEquals(0, $table->find()->count()); + } }