|
6 | 6 |
|
7 | 7 | use Api\App\Attribute\ResourceDeprecation; |
8 | 8 | use Api\App\Exception\ConflictException; |
| 9 | +use Api\App\Exception\ExpiredException; |
9 | 10 | use Api\App\Middleware\DeprecationMiddleware; |
10 | 11 | use Fig\Http\Message\RequestMethodInterface; |
| 12 | +use Fig\Http\Message\StatusCodeInterface; |
11 | 13 | use Laminas\Diactoros\Response\EmptyResponse; |
12 | 14 | use Laminas\Stratigility\MiddlewarePipe; |
13 | 15 | use Mezzio\Middleware\LazyLoadingMiddleware; |
@@ -272,6 +274,100 @@ public function handle(ServerRequestInterface $request): ResponseInterface |
272 | 274 | $this->assertSame('2038-01-01', $response->getHeader('sunset')[0]); |
273 | 275 | } |
274 | 276 |
|
| 277 | + /** |
| 278 | + * @throws Exception |
| 279 | + * @throws ReflectionException |
| 280 | + */ |
| 281 | + public function testPastSunsetThrowsExpiredExceptionWithoutRunningHandler(): void |
| 282 | + { |
| 283 | + $deprecated = new #[ResourceDeprecation(sunset: '2000-01-01', link: 'test-link')] |
| 284 | + class implements RequestHandlerInterface { |
| 285 | + public function handle(ServerRequestInterface $request): ResponseInterface |
| 286 | + { |
| 287 | + return new EmptyResponse(); |
| 288 | + } |
| 289 | + }; |
| 290 | + |
| 291 | + $this->routeRequestTo($deprecated); |
| 292 | + |
| 293 | + $handler = $this->createMock(RequestHandlerInterface::class); |
| 294 | + $handler->expects($this->never())->method('handle'); |
| 295 | + |
| 296 | + $this->expectException(ExpiredException::class); |
| 297 | + |
| 298 | + $this->deprecationMiddleware->process($this->request, $handler); |
| 299 | + } |
| 300 | + |
| 301 | + /** |
| 302 | + * @throws Exception |
| 303 | + * @throws ReflectionException |
| 304 | + */ |
| 305 | + public function testExpiredExceptionCarriesStatusMessageAndLink(): void |
| 306 | + { |
| 307 | + $deprecated = new #[ResourceDeprecation(sunset: '2000-01-01', link: 'test-link')] |
| 308 | + class implements RequestHandlerInterface { |
| 309 | + public function handle(ServerRequestInterface $request): ResponseInterface |
| 310 | + { |
| 311 | + return new EmptyResponse(); |
| 312 | + } |
| 313 | + }; |
| 314 | + |
| 315 | + $this->routeRequestTo($deprecated); |
| 316 | + |
| 317 | + try { |
| 318 | + $this->deprecationMiddleware->process($this->request, $this->handler); |
| 319 | + $this->fail('ExpiredException was not thrown'); |
| 320 | + } catch (ExpiredException $exception) { |
| 321 | + $this->assertSame(StatusCodeInterface::STATUS_GONE, $exception->getStatus()); |
| 322 | + $this->assertSame('This resource is no longer available (sunset on 2000-01-01).', $exception->getDetail()); |
| 323 | + $this->assertSame('test-link', $exception->getType()); |
| 324 | + } |
| 325 | + } |
| 326 | + |
| 327 | + /** |
| 328 | + * @throws Exception |
| 329 | + * @throws ReflectionException |
| 330 | + */ |
| 331 | + public function testExpiredExceptionFallsBackToConfiguredDocumentationUrl(): void |
| 332 | + { |
| 333 | + $deprecated = new #[ResourceDeprecation(sunset: '2000-01-01')] |
| 334 | + class implements RequestHandlerInterface { |
| 335 | + public function handle(ServerRequestInterface $request): ResponseInterface |
| 336 | + { |
| 337 | + return new EmptyResponse(); |
| 338 | + } |
| 339 | + }; |
| 340 | + |
| 341 | + $this->routeRequestTo($deprecated); |
| 342 | + |
| 343 | + try { |
| 344 | + $this->deprecationMiddleware->process($this->request, $this->handler); |
| 345 | + $this->fail('ExpiredException was not thrown'); |
| 346 | + } catch (ExpiredException $exception) { |
| 347 | + $this->assertSame(self::VERSIONING_CONFIG['documentation_url'], $exception->getType()); |
| 348 | + } |
| 349 | + } |
| 350 | + |
| 351 | + /** |
| 352 | + * @throws Exception |
| 353 | + */ |
| 354 | + private function routeRequestTo(RequestHandlerInterface $deprecated): void |
| 355 | + { |
| 356 | + $routeResult = $this->createStub(RouteResult::class); |
| 357 | + $route = $this->createStub(Route::class); |
| 358 | + $lazyLoadingMiddleware = new LazyLoadingMiddleware( |
| 359 | + $this->createStub(MiddlewareContainer::class), |
| 360 | + $deprecated::class, |
| 361 | + ); |
| 362 | + |
| 363 | + $route->method('getMiddleware')->willReturn($lazyLoadingMiddleware); |
| 364 | + $routeResult->method('isFailure')->willReturn(false); |
| 365 | + $routeResult->method('getMatchedRoute')->willReturn($route); |
| 366 | + $this->request->method('getAttribute')->willReturn($routeResult); |
| 367 | + $this->request->method('getMethod')->willReturn(RequestMethodInterface::METHOD_GET); |
| 368 | + $this->handler->method('handle')->willReturn($this->response); |
| 369 | + } |
| 370 | + |
275 | 371 | /** |
276 | 372 | * @param non-empty-string $baseLink |
277 | 373 | * @param array<non-empty-string, mixed> $attribute |
|
0 commit comments