|
|
@@ -16,24 +16,19 @@ declare(strict_types=1);
|
|
|
*/
|
|
|
namespace Cake\Test\TestCase\Routing\Middleware;
|
|
|
|
|
|
-use Cake\Cache\Cache;
|
|
|
-use Cake\Cache\InvalidArgumentException as CacheInvalidArgumentException;
|
|
|
use Cake\Core\Configure;
|
|
|
use Cake\Core\HttpApplicationInterface;
|
|
|
use Cake\Http\ServerRequestFactory;
|
|
|
-use Cake\Routing\Exception\FailedRouteCacheException;
|
|
|
use Cake\Routing\Exception\MissingRouteException;
|
|
|
use Cake\Routing\Middleware\RoutingMiddleware;
|
|
|
use Cake\Routing\Route\Route;
|
|
|
use Cake\Routing\RouteBuilder;
|
|
|
-use Cake\Routing\RouteCollection;
|
|
|
use Cake\Routing\Router;
|
|
|
use Cake\TestSuite\TestCase;
|
|
|
use Laminas\Diactoros\Response;
|
|
|
use TestApp\Application;
|
|
|
use TestApp\Http\TestRequestHandler;
|
|
|
use TestApp\Middleware\DumbMiddleware;
|
|
|
-use TestApp\Middleware\UnserializableMiddleware;
|
|
|
|
|
|
/**
|
|
|
* Test for RoutingMiddleware
|
|
|
@@ -62,17 +57,6 @@ class RoutingMiddlewareTest extends TestCase
|
|
|
Configure::write('App.base', '');
|
|
|
}
|
|
|
|
|
|
- public function tearDown(): void
|
|
|
- {
|
|
|
- parent::tearDown();
|
|
|
-
|
|
|
- Cache::enable();
|
|
|
- if (in_array('_cake_router_', Cache::configured(), true)) {
|
|
|
- Cache::clear('_cake_router_');
|
|
|
- }
|
|
|
- Cache::drop('_cake_router_');
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* Test redirect responses from redirect routes
|
|
|
*/
|
|
|
@@ -457,125 +441,6 @@ class RoutingMiddlewareTest extends TestCase
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Test we store route collection in cache.
|
|
|
- */
|
|
|
- public function testCacheRoutes(): void
|
|
|
- {
|
|
|
- Configure::write('Error.ignoredDeprecationPaths', [
|
|
|
- 'tests/TestCase/Routing/Middleware/RoutingMiddlewareTest.php',
|
|
|
- ]);
|
|
|
- $cacheConfigName = '_cake_router_';
|
|
|
- Cache::setConfig($cacheConfigName, [
|
|
|
- 'engine' => 'File',
|
|
|
- 'path' => CACHE,
|
|
|
- ]);
|
|
|
- $request = ServerRequestFactory::fromGlobals(['REQUEST_URI' => '/articles']);
|
|
|
- $handler = new TestRequestHandler(function ($req) use ($cacheConfigName) {
|
|
|
- $routeCollection = Cache::read('routeCollection', $cacheConfigName);
|
|
|
- $this->assertInstanceOf(RouteCollection::class, $routeCollection);
|
|
|
-
|
|
|
- return new Response();
|
|
|
- });
|
|
|
- $app = new Application(CONFIG);
|
|
|
- $middleware = new RoutingMiddleware($app, $cacheConfigName);
|
|
|
- $middleware->process($request, $handler);
|
|
|
- Configure::delete('Error.ignoredDeprecationPaths');
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * Test we don't cache routes if cache is disabled.
|
|
|
- */
|
|
|
- public function testCacheNotUsedIfCacheDisabled(): void
|
|
|
- {
|
|
|
- Configure::write('Error.ignoredDeprecationPaths', [
|
|
|
- 'tests/TestCase/Routing/Middleware/RoutingMiddlewareTest.php',
|
|
|
- ]);
|
|
|
- $cacheConfigName = '_cake_router_';
|
|
|
- Cache::drop($cacheConfigName);
|
|
|
- Cache::disable();
|
|
|
- Cache::setConfig($cacheConfigName, [
|
|
|
- 'engine' => 'File',
|
|
|
- 'path' => CACHE,
|
|
|
- ]);
|
|
|
- $request = ServerRequestFactory::fromGlobals(['REQUEST_URI' => '/articles']);
|
|
|
- $handler = new TestRequestHandler(function ($req) use ($cacheConfigName) {
|
|
|
- $routeCollection = Cache::read('routeCollection', $cacheConfigName);
|
|
|
- $this->assertNull($routeCollection);
|
|
|
-
|
|
|
- return new Response();
|
|
|
- });
|
|
|
- $app = new Application(CONFIG);
|
|
|
- $middleware = new RoutingMiddleware($app, $cacheConfigName);
|
|
|
- $middleware->process($request, $handler);
|
|
|
- Configure::delete('Error.ignoredDeprecationPaths');
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * Test cache name is used
|
|
|
- */
|
|
|
- public function testCacheConfigNotFound(): void
|
|
|
- {
|
|
|
- Configure::write('Error.ignoredDeprecationPaths', [
|
|
|
- 'tests/TestCase/Routing/Middleware/RoutingMiddlewareTest.php',
|
|
|
- ]);
|
|
|
- $this->expectException(CacheInvalidArgumentException::class);
|
|
|
- $this->expectExceptionMessage('The "notfound" cache configuration does not exist.');
|
|
|
-
|
|
|
- Cache::setConfig('_cake_router_', [
|
|
|
- 'engine' => 'File',
|
|
|
- 'path' => CACHE,
|
|
|
- ]);
|
|
|
- $request = ServerRequestFactory::fromGlobals(['REQUEST_URI' => '/articles']);
|
|
|
- $app = new Application(CONFIG);
|
|
|
- $middleware = new RoutingMiddleware($app, 'notfound');
|
|
|
- $middleware->process($request, new TestRequestHandler());
|
|
|
- Configure::delete('Error.ignoredDeprecationPaths');
|
|
|
- }
|
|
|
-
|
|
|
- public function testFailedRouteCache(): void
|
|
|
- {
|
|
|
- Cache::setConfig('_cake_router_', [
|
|
|
- 'engine' => 'File',
|
|
|
- 'path' => CACHE,
|
|
|
- ]);
|
|
|
-
|
|
|
- $app = $this->createMock(Application::class);
|
|
|
- $this->expectDeprecation();
|
|
|
- $this->expectDeprecationMessage(
|
|
|
- 'Use of routing cache is deprecated and will be removed in 5.0. ' .
|
|
|
- 'Upgrade to the new `CakeDC/CachedRouting` plugin. ' .
|
|
|
- 'See https://github.com/CakeDC/cakephp-cached-routing'
|
|
|
- );
|
|
|
- new RoutingMiddleware($app, '_cake_router_');
|
|
|
- }
|
|
|
-
|
|
|
- public function testDeprecatedRouteCache(): void
|
|
|
- {
|
|
|
- Configure::write('Error.ignoredDeprecationPaths', [
|
|
|
- 'tests/TestCase/Routing/Middleware/RoutingMiddlewareTest.php',
|
|
|
- ]);
|
|
|
- Cache::setConfig('_cake_router_', [
|
|
|
- 'engine' => 'File',
|
|
|
- 'path' => CACHE,
|
|
|
- ]);
|
|
|
-
|
|
|
- $app = $this->createMock(Application::class);
|
|
|
- $app
|
|
|
- ->method('routes')
|
|
|
- ->will($this->returnCallback(function (RouteBuilder $routes) use ($app) {
|
|
|
- return $routes->registerMiddleware('should fail', new UnserializableMiddleware($app));
|
|
|
- }));
|
|
|
-
|
|
|
- $middleware = new RoutingMiddleware($app, '_cake_router_');
|
|
|
- $request = ServerRequestFactory::fromGlobals(['REQUEST_URI' => '/articles']);
|
|
|
-
|
|
|
- $this->expectException(FailedRouteCacheException::class);
|
|
|
- $this->expectExceptionMessage('Unable to cache route collection.');
|
|
|
- $middleware->process($request, new TestRequestHandler());
|
|
|
- Configure::delete('Error.ignoredDeprecationPaths');
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
* Create a stub application for testing.
|
|
|
*
|
|
|
* @param callable|null $handleCallback Callback for "handle" method.
|