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

Clean up output from pending migrations middleware. #797

Merged
merged 1 commit into from
Jan 3, 2025
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
27 changes: 22 additions & 5 deletions src/Middleware/PendingMigrationsMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
namespace Migrations\Middleware;

use Cake\Console\ConsoleIo;
use Cake\Console\TestSuite\StubConsoleInput;
use Cake\Console\TestSuite\StubConsoleOutput;
use Cake\Core\Configure;
use Cake\Core\Exception\CakeException;
use Cake\Core\InstanceConfigTrait;
Expand Down Expand Up @@ -107,8 +109,7 @@ protected function checkAppMigrations(): bool
$database = $connection->config()['database'];
$this->_config['environment']['database'] = $database;

$config = new Config($this->_config);
$manager = new Manager($config, new ConsoleIo());
$manager = $this->getManager($this->_config);

$migrations = $manager->getMigrations();
foreach ($migrations as $migration) {
Expand Down Expand Up @@ -144,9 +145,7 @@ protected function checkPluginMigrations(string $plugin): bool
$table = Util::tableName($plugin);

$config['environment']['migration_table'] = $table;

$managerConfig = new Config($config);
$manager = new Manager($managerConfig, new ConsoleIo());
$manager = $this->getManager($config);

$migrations = $manager->getMigrations();
foreach ($migrations as $migration) {
Expand All @@ -166,4 +165,22 @@ protected function isSkipped(ServerRequestInterface $request): bool
{
return (bool)Hash::get($request->getQueryParams(), static::SKIP_QUERY_KEY);
}

/**
* Create a manager instance with stubbed console io
*
* @param array $config Configuration data
* @return \Migrations\Migration\Manager
*/
protected function getManager(array $config): Manager
{
$managerConfig = new Config($config);
$io = new ConsoleIo(
new StubConsoleOutput(),
new StubConsoleOutput(),
new StubConsoleInput([]),
);

return new Manager($managerConfig, $io);
}
}
23 changes: 19 additions & 4 deletions tests/TestCase/Middleware/PendingMigrationsMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
namespace Migrations\Test\TestCase\Middleware;

use Cake\Console\ConsoleIo;
use Cake\Console\TestSuite\StubConsoleInput;
use Cake\Console\TestSuite\StubConsoleOutput;
use Cake\Core\Exception\CakeException;
use Cake\Datasource\ConnectionManager;
use Cake\Http\Response;
Expand All @@ -26,6 +28,10 @@

class PendingMigrationsMiddlewareTest extends TestCase
{
private string $db;

private ConsoleIo $io;

/**
* Setup method
*
Expand All @@ -34,6 +40,15 @@ class PendingMigrationsMiddlewareTest extends TestCase
public function setUp(): void
{
parent::setUp();
$connection = ConnectionManager::get('test');

$config = $connection->config();
$this->db = $config['database'];
$this->io = new ConsoleIo(
new StubConsoleOutput(),
new StubConsoleOutput(),
new StubConsoleInput([])
);
}

public function tearDown(): void
Expand Down Expand Up @@ -75,13 +90,13 @@ public function testAppMigrationsSuccess(): void
'migrations' => ROOT . DS . 'config' . DS . 'Migrations' . DS,
],
'environment' => [
'database' => 'cakephp_test',
'database' => $this->db,
'connection' => 'default',
'migration_table' => 'phinxlog',
],
];
$config = new Config($config);
$manager = new Manager($config, new ConsoleIo());
$manager = new Manager($config, $this->io);
$manager->migrate(null, true);

$request = new ServerRequest();
Expand Down Expand Up @@ -132,12 +147,12 @@ public function testAppAndPluginsMigrationsSuccess(): void
],
'environment' => [
'connection' => 'default',
'database' => 'cakephp_test',
'database' => $this->db,
'migration_table' => 'migrator_phinxlog',
],
];
$config = new Config($config);
$manager = new Manager($config, new ConsoleIo());
$manager = new Manager($config, $this->io);
$manager->migrate(null, true);

$request = new ServerRequest();
Expand Down
Loading