Skip to content

Commit

Permalink
Add test case.
Browse files Browse the repository at this point in the history
  • Loading branch information
dereuromark committed Dec 2, 2024
1 parent 35bad47 commit 721089d
Show file tree
Hide file tree
Showing 2 changed files with 174 additions and 0 deletions.
148 changes: 148 additions & 0 deletions tests/TestCase/Middleware/PendingMigrationsMiddlewareTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
<?php
declare(strict_types=1);

/**
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link https://cakephp.org CakePHP(tm) Project
* @license https://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Migrations\Test\TestCase\Middleware;

use Cake\Console\ConsoleIo;
use Cake\Core\Exception\CakeException;
use Cake\Datasource\ConnectionManager;
use Cake\Http\Response;
use Cake\Http\ServerRequest;
use Cake\TestSuite\TestCase;
use Migrations\Config\Config;
use Migrations\Middleware\PendingMigrationsMiddleware;
use Migrations\Migration\Manager;
use TestApp\Http\TestRequestHandler;

class PendingMigrationsMiddlewareTest extends TestCase
{
/**
* Setup method
*
* @return void
*/
public function setUp(): void
{
parent::setUp();
}

public function tearDown(): void
{
parent::tearDown();
ConnectionManager::drop('custom');
ConnectionManager::drop('default');
}

/**
* @return void
*/
public function testAppMigrationsFail(): void
{
$middleware = new PendingMigrationsMiddleware();

$request = new ServerRequest();
$handler = new TestRequestHandler(function ($req) {
return new Response();
});

$this->expectException(CakeException::class);
$this->expectExceptionCode(503);
$this->expectExceptionMessage('Pending migrations need to be run for app:');

$middleware->process($request, $handler);
}

/**
* @doesNotPerformAssertions
* @return void
*/
public function testAppMigrationsSuccess(): void
{
$middleware = new PendingMigrationsMiddleware();

$config = [
'paths' => [
'migrations' => ROOT . DS . 'config' . DS . 'Migrations' . DS,
],
'environment' => [
'connection' => 'default',
'migration_table' => 'phinxlog',
],
];
$config = new Config($config);
$manager = new Manager($config, new ConsoleIo());
$manager->migrate(null, true);

$request = new ServerRequest();
$handler = new TestRequestHandler(function ($req) {
return new Response();
});
$middleware->process($request, $handler);
}

/**
* @return void
*/
public function testAppAndPluginsMigrationsFail(): void
{
$this->loadPlugins(['Migrator']);

$middleware = new PendingMigrationsMiddleware([
'plugins' => true,
]);

$request = new ServerRequest();
$handler = new TestRequestHandler(function ($req) {
return new Response();
});

$this->expectException(CakeException::class);
$this->expectExceptionCode(503);
$this->expectExceptionMessage('Pending migrations need to be run for Migrator:');

$middleware->process($request, $handler);
}

/**
* @doesNotPerformAssertions
* @return void
*/
public function testAppAndPluginsMigrationsSuccess(): void
{
$this->loadPlugins(['Migrator']);

$middleware = new PendingMigrationsMiddleware([
'plugins' => true,
]);

$config = [
'paths' => [
'migrations' => ROOT . DS . 'Plugin' . DS . 'Migrator' . DS . 'config' . DS . 'Migrations' . DS,
],
'environment' => [
'connection' => 'default',
'migration_table' => 'migrator_phinxlog',
],
];
$config = new Config($config);
$manager = new Manager($config, new ConsoleIo());
$manager->migrate(null, true);

$request = new ServerRequest();
$handler = new TestRequestHandler(function ($req) {
return new Response();
});

$middleware->process($request, $handler);
}
}
26 changes: 26 additions & 0 deletions tests/test_app/App/Http/TestRequestHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
declare(strict_types=1);

namespace TestApp\Http;

use Cake\Http\Response;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;

class TestRequestHandler implements RequestHandlerInterface
{
public $callable;

public function __construct(?callable $callable = null)
{
$this->callable = $callable ?: function ($request) {
return new Response();
};
}

public function handle(ServerRequestInterface $request): ResponseInterface
{
return ($this->callable)($request);
}
}

0 comments on commit 721089d

Please sign in to comment.