-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
35bad47
commit 721089d
Showing
2 changed files
with
174 additions
and
0 deletions.
There are no files selected for viewing
148 changes: 148 additions & 0 deletions
148
tests/TestCase/Middleware/PendingMigrationsMiddlewareTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |