Skip to content

Commit

Permalink
Use isReadonly() method to detect that driver is readonly; add test
Browse files Browse the repository at this point in the history
  • Loading branch information
roxblnfk committed Oct 24, 2024
1 parent 5b9e74e commit b2481dc
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Migrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ private function getDatabases(): iterable
if ($this->dbal instanceof DatabaseManager) {
return array_filter(
$this->dbal->getDatabases(),
fn (DatabaseInterface $db): bool => !$db->getDriver()->getSchemaHandler() instanceof ReadonlyHandler
fn (DatabaseInterface $db): bool => !$db->getDriver()->isReadonly()
);
}
return [];
Expand Down
44 changes: 44 additions & 0 deletions tests/Migrations/Unit/IsolatedMigratorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

namespace Cycle\Migrations\Tests\Unit;

use Cycle\Database\Config\DatabaseConfig;
use Cycle\Database\Database;
use Cycle\Database\DatabaseManager;
use Cycle\Database\Driver\DriverInterface;
use Cycle\Migrations\Config\MigrationConfig;
use Cycle\Migrations\Migrator;
use Cycle\Migrations\RepositoryInterface;
use PHPUnit\Framework\TestCase;

final class IsolatedMigratorTest extends TestCase
{
public function testReadonlyDriversMustNotBeUsed()
{
$dbal = new DatabaseManager(new DatabaseConfig());
$dbal->addDatabase(
new Database(
'foo',
'',
$driver = $this->createMock(DriverInterface::class),
),
);
$driver->expects($this->atLeastOnce())->method('isReadonly')->willReturn(true);
$driver
->expects($this->any())
->method($this->callback(fn($name) => $name !== 'isReadonly'))
->willThrowException(new \RuntimeException('Unexpected method call'));

$repository = $this->createMock(RepositoryInterface::class);

$migrator = new Migrator(
new MigrationConfig([]),
$dbal,
$repository,
);

$migrator->isConfigured();
}
}

0 comments on commit b2481dc

Please sign in to comment.