Skip to content

Commit

Permalink
Merge pull request #797 from cakephp/pending-middleware-cleanup
Browse files Browse the repository at this point in the history
Clean up output from pending migrations middleware.
  • Loading branch information
markstory authored Jan 3, 2025
2 parents 231c14d + ea2e992 commit 59b672f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 9 deletions.
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

0 comments on commit 59b672f

Please sign in to comment.