Skip to content

Commit

Permalink
Merge pull request #702 from cakephp/add-event-coverage
Browse files Browse the repository at this point in the history
Expand test coverage for migration events.
  • Loading branch information
markstory authored Mar 31, 2024
2 parents 567c7f9 + c744ed2 commit a765f73
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 4 deletions.
4 changes: 0 additions & 4 deletions src/Command/RollbackCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]]]]].');
}
Expand Down
39 changes: 39 additions & 0 deletions tests/TestCase/Command/MigrateCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -291,4 +293,41 @@ public function testMigrateWithNoLock()
$this->assertOutputNotContains('Dumping');
$this->assertFileDoesNotExist($migrationPath . DS . 'schema-dump-test.lock');
}

public function testEventsFired(): void
{
/** @var array<int, string> $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<int, string> $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());
}
}
39 changes: 39 additions & 0 deletions tests/TestCase/Command/RollbackCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -231,4 +233,41 @@ public function testFakeOption(): void
$dumpFile = $migrationPath . DS . 'schema-dump-test.lock';
$this->assertFileDoesNotExist($dumpFile);
}

public function testEventsFired(): void
{
/** @var array<int, string> $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<int, string> $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());
}
}

0 comments on commit a765f73

Please sign in to comment.