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