| @@ 83-98 (lines=16) @@ | ||
| 80 | /** |
|
| 81 | * @test |
|
| 82 | */ |
|
| 83 | public function invalidShortCodeExceptionFallsBackToNextMiddleware() |
|
| 84 | { |
|
| 85 | $shortCode = 'abc123'; |
|
| 86 | $this->urlShortener->shortCodeToUrl($shortCode)->willThrow(InvalidShortCodeException::class) |
|
| 87 | ->shouldBeCalledTimes(1); |
|
| 88 | $delegate = $this->prophesize(RequestHandlerInterface::class); |
|
| 89 | /** @var MethodProphecy $process */ |
|
| 90 | $process = $delegate->handle(Argument::any())->willReturn(new Response()); |
|
| 91 | ||
| 92 | $this->action->process( |
|
| 93 | ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode), |
|
| 94 | $delegate->reveal() |
|
| 95 | ); |
|
| 96 | ||
| 97 | $process->shouldHaveBeenCalledTimes(1); |
|
| 98 | } |
|
| 99 | } |
|
| 100 | ||
| @@ 44-58 (lines=15) @@ | ||
| 41 | /** |
|
| 42 | * @test |
|
| 43 | */ |
|
| 44 | public function aNotFoundShortCodeWillDelegateIntoNextMiddleware() |
|
| 45 | { |
|
| 46 | $shortCode = 'abc123'; |
|
| 47 | $this->urlShortener->shortCodeToUrl($shortCode)->willThrow(EntityDoesNotExistException::class) |
|
| 48 | ->shouldBeCalledTimes(1); |
|
| 49 | $delegate = $this->prophesize(RequestHandlerInterface::class); |
|
| 50 | $process = $delegate->handle(Argument::any())->willReturn(new Response()); |
|
| 51 | ||
| 52 | $this->action->process( |
|
| 53 | ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode), |
|
| 54 | $delegate->reveal() |
|
| 55 | ); |
|
| 56 | ||
| 57 | $process->shouldHaveBeenCalledTimes(1); |
|
| 58 | } |
|
| 59 | ||
| 60 | /** |
|
| 61 | * @test |
|
| @@ 63-78 (lines=16) @@ | ||
| 60 | /** |
|
| 61 | * @test |
|
| 62 | */ |
|
| 63 | public function anInvalidShortCodeWillReturnNotFoundResponse() |
|
| 64 | { |
|
| 65 | $shortCode = 'abc123'; |
|
| 66 | $this->urlShortener->shortCodeToUrl($shortCode)->willThrow(InvalidShortCodeException::class) |
|
| 67 | ->shouldBeCalledTimes(1); |
|
| 68 | $delegate = $this->prophesize(RequestHandlerInterface::class); |
|
| 69 | /** @var MethodProphecy $process */ |
|
| 70 | $process = $delegate->handle(Argument::any())->willReturn(new Response()); |
|
| 71 | ||
| 72 | $this->action->process( |
|
| 73 | ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode), |
|
| 74 | $delegate->reveal() |
|
| 75 | ); |
|
| 76 | ||
| 77 | $process->shouldHaveBeenCalledTimes(1); |
|
| 78 | } |
|
| 79 | ||
| 80 | /** |
|
| 81 | * @test |
|
| @@ 36-46 (lines=11) @@ | ||
| 33 | /** |
|
| 34 | * @test |
|
| 35 | */ |
|
| 36 | public function incorrectShortCodeReturnsError() |
|
| 37 | { |
|
| 38 | $shortCode = 'abc123'; |
|
| 39 | $this->urlShortener->shortCodeToUrl($shortCode)->willThrow(EntityDoesNotExistException::class) |
|
| 40 | ->shouldBeCalledTimes(1); |
|
| 41 | ||
| 42 | $request = ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode); |
|
| 43 | $response = $this->action->handle($request); |
|
| 44 | $this->assertEquals(404, $response->getStatusCode()); |
|
| 45 | $this->assertTrue(strpos($response->getBody()->getContents(), RestUtils::INVALID_ARGUMENT_ERROR) > 0); |
|
| 46 | } |
|
| 47 | ||
| 48 | /** |
|
| 49 | * @test |
|
| @@ 66-76 (lines=11) @@ | ||
| 63 | /** |
|
| 64 | * @test |
|
| 65 | */ |
|
| 66 | public function invalidShortCodeExceptionReturnsError() |
|
| 67 | { |
|
| 68 | $shortCode = 'abc123'; |
|
| 69 | $this->urlShortener->shortCodeToUrl($shortCode)->willThrow(InvalidShortCodeException::class) |
|
| 70 | ->shouldBeCalledTimes(1); |
|
| 71 | ||
| 72 | $request = ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode); |
|
| 73 | $response = $this->action->handle($request); |
|
| 74 | $this->assertEquals(400, $response->getStatusCode()); |
|
| 75 | $this->assertTrue(strpos($response->getBody()->getContents(), RestUtils::INVALID_SHORTCODE_ERROR) > 0); |
|
| 76 | } |
|
| 77 | ||
| 78 | /** |
|
| 79 | * @test |
|
| @@ 81-91 (lines=11) @@ | ||
| 78 | /** |
|
| 79 | * @test |
|
| 80 | */ |
|
| 81 | public function unexpectedExceptionWillReturnError() |
|
| 82 | { |
|
| 83 | $shortCode = 'abc123'; |
|
| 84 | $this->urlShortener->shortCodeToUrl($shortCode)->willThrow(\Exception::class) |
|
| 85 | ->shouldBeCalledTimes(1); |
|
| 86 | ||
| 87 | $request = ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode); |
|
| 88 | $response = $this->action->handle($request); |
|
| 89 | $this->assertEquals(500, $response->getStatusCode()); |
|
| 90 | $this->assertTrue(strpos($response->getBody()->getContents(), RestUtils::UNKNOWN_ERROR) > 0); |
|
| 91 | } |
|
| 92 | } |
|
| 93 | ||