From eba9ffb6704cb704fe27f11a892a3759e1e49616 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Gonz=C3=A1lez?= Date: Fri, 25 Feb 2022 17:07:26 +0000 Subject: [PATCH] fix tests and port code from the CakePHP core --- composer.json | 2 +- phpunit.xml.dist | 33 ++++++++ .../Middleware/CachedRoutingMiddleware.php | 81 ++++++++++++++++++ tests/App/Application.php | 51 ++++++++++++ tests/App/TestRequestHandler.php | 28 +++++++ tests/App/UnserializableMiddleware.php | 34 ++++++++ .../CachedRoutingMiddlewareTest.php | 82 +++++++++++++++++++ tests/bootstrap.php | 60 ++++++++++++++ 8 files changed, 370 insertions(+), 1 deletion(-) create mode 100644 phpunit.xml.dist create mode 100644 src/Routing/Middleware/CachedRoutingMiddleware.php create mode 100644 tests/App/Application.php create mode 100644 tests/App/TestRequestHandler.php create mode 100644 tests/App/UnserializableMiddleware.php create mode 100644 tests/TestCase/Routing/Middleware/CachedRoutingMiddlewareTest.php create mode 100644 tests/bootstrap.php diff --git a/composer.json b/composer.json index 9941f60..2759f1b 100644 --- a/composer.json +++ b/composer.json @@ -28,7 +28,7 @@ "prefer-stable": true, "require": { "php": ">=7.4", - "cakephp/cakephp": "^4.3" + "cakephp/cakephp": "4.next-dev" }, "require-dev": { "phpunit/phpunit": "^9.5", diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 0000000..7783742 --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,33 @@ + + + + + + + + + + + + ./tests/TestCase + + + + + + + + + + + + ./src + + + + diff --git a/src/Routing/Middleware/CachedRoutingMiddleware.php b/src/Routing/Middleware/CachedRoutingMiddleware.php new file mode 100644 index 0000000..3ed66ce --- /dev/null +++ b/src/Routing/Middleware/CachedRoutingMiddleware.php @@ -0,0 +1,81 @@ +cacheConfig = $cacheConfig; + } + + /** + * Check if route cache is enabled and use the configured Cache to 'remember' the route collection + * + * @return \Cake\Routing\RouteCollection + */ + protected function buildRouteCollection(): RouteCollection + { + if (Cache::enabled() && $this->cacheConfig !== null) { + try { + return Cache::remember(static::ROUTE_COLLECTION_CACHE_KEY, function () { + return $this->prepareRouteCollection(); + }, $this->cacheConfig); + } catch (\InvalidArgumentException $e) { + throw $e; + } catch (\Exception $e) { + throw new FailedRouteCacheException( + 'Unable to cache route collection. Cached routes must be serializable. Check for route-specific + middleware or other unserializable settings in your routes. The original exception message can + show what type of object failed to serialize.', + null, + $e + ); + } + } + + return $this->prepareRouteCollection(); + } +} \ No newline at end of file diff --git a/tests/App/Application.php b/tests/App/Application.php new file mode 100644 index 0000000..1d34587 --- /dev/null +++ b/tests/App/Application.php @@ -0,0 +1,51 @@ +connect('/articles', ['controller' => 'Articles', 'action' => 'index']); + } + + public function bootstrap(): void + { + } + + public function middleware(MiddlewareQueue $middlewareQueue): MiddlewareQueue + { + return $middlewareQueue; + } + + public function handle(ServerRequestInterface $request): ResponseInterface + { + return new Response(); + } +} \ No newline at end of file diff --git a/tests/App/TestRequestHandler.php b/tests/App/TestRequestHandler.php new file mode 100644 index 0000000..e36d137 --- /dev/null +++ b/tests/App/TestRequestHandler.php @@ -0,0 +1,28 @@ +app = $app; + } + + public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface + { + return $request; + } +} \ No newline at end of file diff --git a/tests/TestCase/Routing/Middleware/CachedRoutingMiddlewareTest.php b/tests/TestCase/Routing/Middleware/CachedRoutingMiddlewareTest.php new file mode 100644 index 0000000..cdd27e1 --- /dev/null +++ b/tests/TestCase/Routing/Middleware/CachedRoutingMiddlewareTest.php @@ -0,0 +1,82 @@ + 'File', + 'path' => CACHE, + ]); + $request = ServerRequestFactory::fromGlobals(['REQUEST_URI' => '/articles']); + $middleware = new CachedRoutingMiddleware(new Application(), $cacheConfigName); + $middleware->process($request, new TestRequestHandler()); + + $routeCollection = Cache::read('routeCollection', $cacheConfigName); + $this->assertInstanceOf(RouteCollection::class, $routeCollection); + } + + public function testFailedRouteCache(): void + { + 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 CachedRoutingMiddleware($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()); + } +} \ No newline at end of file diff --git a/tests/bootstrap.php b/tests/bootstrap.php new file mode 100644 index 0000000..a31cfd8 --- /dev/null +++ b/tests/bootstrap.php @@ -0,0 +1,60 @@ +