Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expand test coverage for migration events. #702

Merged
merged 2 commits into from
Mar 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
}
}
Loading