From ea2e99232cedea1e400a926437eb03cb1a2149cf Mon Sep 17 00:00:00 2001 From: Mark Story Date: Thu, 2 Jan 2025 23:30:25 -0500 Subject: [PATCH] Clean up output from pending migrations middleware. --- .../PendingMigrationsMiddleware.php | 27 +++++++++++++++---- .../PendingMigrationsMiddlewareTest.php | 23 +++++++++++++--- 2 files changed, 41 insertions(+), 9 deletions(-) diff --git a/src/Middleware/PendingMigrationsMiddleware.php b/src/Middleware/PendingMigrationsMiddleware.php index abdcbc00..9c140a71 100644 --- a/src/Middleware/PendingMigrationsMiddleware.php +++ b/src/Middleware/PendingMigrationsMiddleware.php @@ -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; @@ -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) { @@ -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) { @@ -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); + } } diff --git a/tests/TestCase/Middleware/PendingMigrationsMiddlewareTest.php b/tests/TestCase/Middleware/PendingMigrationsMiddlewareTest.php index b5e67ee7..171298cc 100644 --- a/tests/TestCase/Middleware/PendingMigrationsMiddlewareTest.php +++ b/tests/TestCase/Middleware/PendingMigrationsMiddlewareTest.php @@ -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; @@ -26,6 +28,10 @@ class PendingMigrationsMiddlewareTest extends TestCase { + private string $db; + + private ConsoleIo $io; + /** * Setup method * @@ -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 @@ -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(); @@ -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();