Skip to content
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
5 changes: 1 addition & 4 deletions .github/workflows/analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ jobs:
with:
php-version: ${{ matrix.php-version }}
coverage: none
env:
fail-fast: true
COMPOSER_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Install composer dependencies
uses: ramsey/composer-install@v3
Expand All @@ -35,7 +32,7 @@ jobs:
run: vendor/bin/phpcs

- name: Psalm
run: vendor/bin/psalm --stats --output-format=github
run: vendor/bin/psalm --stats

- name: PHPStan
run: vendor/bin/phpstan analyse
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
fail-fast: false
matrix:
php-version:
- "8.3"
- "8.4"
steps:
- name: Checkout
uses: actions/checkout@v4
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ jobs:
php-version:
- "8.3"
- "8.4"
- "8.5"

steps:
- name: Checkout
Expand Down
7 changes: 3 additions & 4 deletions tests/workflow/ArgumentsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public function testUpWithLimit(): void
$data = $this->command->fetchApplied();
self::assertCount(3, $data);

// check order
$names = array_keys($data);
self::assertEquals('202501021025_account_email.sql', $names[0]);
self::assertEquals('202501021024_account_create.sql', $names[1]);
Expand Down Expand Up @@ -105,8 +106,6 @@ public function testWithDb(): void
public function testWithUnknownDb(): void
{
$this->migrator->init();
$data = $this->command->fetchApplied();
self::assertEmpty($data);

$this->expectException(ConfigurationException::class);
$this->migrator->up(new InputArgs(dbName: 'sqlite/unknown'));
Expand Down Expand Up @@ -151,11 +150,11 @@ public function testDownWithLatestVersion(): void

usleep(10_000);

$this->migrator->up($args);
$this->migrator->up($args); // +1
$data = $this->command->fetchApplied();
self::assertCount(2, $data);

$this->migrator->up($args);
$this->migrator->up($args); // +1
$data = $this->command->fetchApplied();
self::assertCount(3, $data);

Expand Down
12 changes: 12 additions & 0 deletions tests/workflow/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Throwable;
use PDO;
use PDOException;
use PHPUnit\Framework\Attributes\Depends;
use PHPUnit\Framework\TestCase;
use kuaukutsu\poc\migration\driver\DriverType;
use kuaukutsu\poc\migration\internal\command\Args;
Expand Down Expand Up @@ -44,19 +45,22 @@ public function testInit(): void
/**
* @throws Throwable
*/
#[Depends('testInit')]
public function testUp(): void
{
$this->execInitialization();

$this->execUp('table1');

$data = $this->command->fetchApplied();
self::assertCount(1, $data);
self::assertNotEmpty($data['test-table1']);
}

/**
* @throws Throwable
*/
#[Depends('testInit')]
public function testDown(): void
{
$this->execInitialization();
Expand All @@ -82,6 +86,7 @@ public function testDown(): void
/**
* @throws Throwable
*/
#[Depends('testInit')]
public function testFetchLimit(): void
{
$this->execInitialization();
Expand All @@ -100,6 +105,8 @@ public function testFetchLimit(): void
new Args(limit: 2)
);
self::assertCount(2, $data);

// sort order
$names = array_keys($data);
self::assertEquals('test-table3', $names[0]);
self::assertEquals('test-table2', $names[1]);
Expand All @@ -108,6 +115,7 @@ public function testFetchLimit(): void
/**
* @throws Throwable
*/
#[Depends('testInit')]
public function testFetchVersion(): void
{
$this->execInitialization();
Expand Down Expand Up @@ -140,6 +148,7 @@ public function testFetchVersion(): void
/**
* @throws Throwable
*/
#[Depends('testInit')]
public function testUpDryRun(): void
{
$this->execInitialization();
Expand Down Expand Up @@ -170,6 +179,7 @@ public function testUpDryRun(): void
/**
* @throws Throwable
*/
#[Depends('testInit')]
public function testDownDryRun(): void
{
$this->execInitialization();
Expand Down Expand Up @@ -200,6 +210,7 @@ public function testDownDryRun(): void
/**
* @throws Throwable
*/
#[Depends('testInit')]
public function testUpRollbackTransaction(): void
{
$this->execInitialization();
Expand All @@ -222,6 +233,7 @@ public function testUpRollbackTransaction(): void
/**
* @throws Throwable
*/
#[Depends('testInit')]
public function testDownRollbackTransaction(): void
{
$this->execInitialization();
Expand Down
20 changes: 17 additions & 3 deletions tests/workflow/EventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public function testMigrationFilesystemNotice(): void
);
}

public function testMigrationDoesNotContainFiles(): void
public function testMigrationUpDoesNotContainFiles(): void
{
$eventSubscriber = new TestSubscriber();
$migrator = MigratorFactory::makeFromEvent(
Expand All @@ -183,18 +183,32 @@ public function testMigrationDoesNotContainFiles(): void
);

$migrator->init();

$migrator->up();

// migration completed in the previous step
$migrator->up();
self::assertStringContainsString(
'does not contain migration files',
$eventSubscriber->get(Event::FilesystemNotice)
);
}

$eventSubscriber->clear();
public function testMigrationDownDoesNotContainFiles(): void
{
$eventSubscriber = new TestSubscriber();
$migrator = MigratorFactory::makeFromEvent(
new PdoDriver(
dsn: 'sqlite::memory:',
),
[
$eventSubscriber,
],
dirname(__DIR__) . '/migration/sqlite/memory'
);

$migrator->init();
$migrator->down();

// migration completed in the previous step
$migrator->down();
self::assertStringContainsString(
Expand Down
13 changes: 13 additions & 0 deletions tests/workflow/MigrationFailTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PHPUnit\Framework\TestCase;
use kuaukutsu\poc\migration\driver\PdoDriver;
use kuaukutsu\poc\migration\exception\ActionException;
use kuaukutsu\poc\migration\exception\ConfigurationException;
use kuaukutsu\poc\migration\internal\command\CommandInterface;
use kuaukutsu\poc\migration\internal\command\Params;
use kuaukutsu\poc\migration\tests\MigratorFactory;
Expand Down Expand Up @@ -79,4 +80,16 @@ public function testUpExactlyAllException(): void

$this->migrator->up(new InputArgs(exactlyAll: true));
}

public function testMigrationFixtureException(): void
{
$this->migrator->init();

$this->expectException(ConfigurationException::class);
$this->expectExceptionMessageMatches(
'/^the directory .+ does not exist.$/i'
);

$this->migrator->fixture();
}
}
35 changes: 12 additions & 23 deletions tests/workflow/MigrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,33 +48,31 @@ public function testInit(): void
public function testUp(): void
{
$this->migrator->init();
$data = $this->command->fetchApplied();
self::assertEmpty($data);

$this->migrator->up();
$data = $this->command->fetchApplied();
self::assertCount(3, $data);
$countMigration = count($data);
self::assertGreaterThanOrEqual(3, $countMigration);

// sort order
$names = array_keys($data);
self::assertEquals('202501021025_account_email.sql', $names[0]);
self::assertEquals('202501021024_account_create.sql', $names[1]);
self::assertEquals('202501011024_entity_create.sql', $names[2]);

$this->migrator->up();
$data = $this->command->fetchApplied();
self::assertCount(3, $data);
self::assertCount($countMigration, $data);
}

#[Depends('testInit')]
public function testDown(): void
{
$this->migrator->init();
$data = $this->command->fetchApplied();
self::assertEmpty($data);

$this->migrator->up();
$data = $this->command->fetchApplied();
self::assertCount(3, $data);
self::assertGreaterThanOrEqual(3, $data);

$this->migrator->down();
$data = $this->command->fetchApplied();
Expand All @@ -85,12 +83,10 @@ public function testDown(): void
public function testRedo(): void
{
$this->migrator->init();
$data = $this->command->fetchApplied();
self::assertEmpty($data);

$this->migrator->up();
$data = $this->command->fetchApplied();
self::assertCount(3, $data);
self::assertGreaterThanOrEqual(3, $data);

$version = (int)current($data);
self::assertGreaterThan(0, $version);
Expand All @@ -99,7 +95,7 @@ public function testRedo(): void

$this->migrator->redo();
$data = $this->command->fetchApplied();
self::assertCount(3, $data);
self::assertGreaterThanOrEqual(3, $data);

$versionNew = (int)current($data);
self::assertGreaterThan(0, $versionNew);
Expand All @@ -118,24 +114,19 @@ public function testInitializationException(): void

public function testConnectionException(): void
{
$driver = new PdoDriver(
dsn: 'mysql:host=mysql;dbname=main',
);
$driver = new PdoDriver(dsn: 'mysql:host=mysql;dbname=main');
$migrator = MigratorFactory::makeFromEvent($driver);

$this->expectException(ConnectionException::class);
$this->expectExceptionMessageMatches('~PDO_MYSQL:\w+~');

$migrator = MigratorFactory::makeFromDriver($driver);
$migrator->init();
}

public function testActionException(): void
{
$migrator = MigratorFactory::makeFromEvent(
new PdoDriver(
dsn: 'sqlite::memory:',
)
);
$driver = new PdoDriver(dsn: 'sqlite::memory:');
$migrator = MigratorFactory::makeFromEvent($driver);

$this->expectException(ActionException::class);
$this->expectExceptionMessage(
Expand All @@ -146,15 +137,13 @@ public function testActionException(): void
$migrator->up();
}

public function testActionExceptionDryRun(): void
public function testActionNotExceptionDryRun(): void
{
$driver = new PdoDriver(dsn: 'sqlite::memory:');
$migrator = MigratorFactory::makeFromEvent($driver);
$command = $driver->makeCommand(new Params(table: 'migration'));

$migrator->init();
$data = $command->fetchApplied();
self::assertEmpty($data);

$migrator->up(new InputArgs(dryRun: true));
$data = $command->fetchApplied();
Expand Down