From ebeaa3c64a721ae2e71172b38660fddf4187cfa8 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sat, 30 Jul 2016 20:02:48 +0200 Subject: [PATCH] Created RedirectActionTest --- module/Core/src/Action/RedirectAction.php | 17 ++- .../Core/test/Action/RedirectActionTest.php | 104 ++++++++++++++++++ 2 files changed, 118 insertions(+), 3 deletions(-) create mode 100644 module/Core/test/Action/RedirectActionTest.php diff --git a/module/Core/src/Action/RedirectAction.php b/module/Core/src/Action/RedirectAction.php index 122f7f29..031aa0fe 100644 --- a/module/Core/src/Action/RedirectAction.php +++ b/module/Core/src/Action/RedirectAction.php @@ -70,10 +70,10 @@ class RedirectAction implements MiddlewareInterface // If provided shortCode does not belong to a valid long URL, dispatch next middleware, which will trigger // a not-found error if (! isset($longUrl)) { - return $out($request, $response->withStatus(404), 'Not found'); + return $this->notFoundResponse($request, $response, $out); } - // Track visit to this shortcode + // Track visit to this short code $this->visitTracker->track($shortCode); // Return a redirect response to the long URL. @@ -81,7 +81,18 @@ class RedirectAction implements MiddlewareInterface return new RedirectResponse($longUrl); } catch (\Exception $e) { // In case of error, dispatch 404 error - return $out($request, $response); + return $this->notFoundResponse($request, $response, $out); } } + + /** + * @param Request $request + * @param Response $response + * @param callable $out + * @return Response + */ + protected function notFoundResponse(Request $request, Response $response, callable $out) + { + return $out($request, $response->withStatus(404), 'Not Found'); + } } diff --git a/module/Core/test/Action/RedirectActionTest.php b/module/Core/test/Action/RedirectActionTest.php new file mode 100644 index 00000000..434189fc --- /dev/null +++ b/module/Core/test/Action/RedirectActionTest.php @@ -0,0 +1,104 @@ +urlShortener = $this->prophesize(UrlShortener::class); + $visitTracker = $this->prophesize(VisitsTracker::class); + $visitTracker->track(Argument::any()); + $this->action = new RedirectAction($this->urlShortener->reveal(), $visitTracker->reveal()); + } + + /** + * @test + */ + public function redirectionIsPerformedToLongUrl() + { + $shortCode = 'abc123'; + $expectedUrl = 'http://domain.com/foo/bar'; + $this->urlShortener->shortCodeToUrl($shortCode)->willReturn($expectedUrl) + ->shouldBeCalledTimes(1); + + $request = ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode); + $response = $this->action->__invoke($request, new Response()); + + $this->assertInstanceOf(Response\RedirectResponse::class, $response); + $this->assertEquals(302, $response->getStatusCode()); + $this->assertTrue($response->hasHeader('Location')); + $this->assertEquals($expectedUrl, $response->getHeaderLine('Location')); + } + + /** + * @test + */ + public function nextErrorMiddlewareIsInvokedIfLongUrlIsNotFound() + { + $shortCode = 'abc123'; + $this->urlShortener->shortCodeToUrl($shortCode)->willReturn(null) + ->shouldBeCalledTimes(1); + + $request = ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode); + $originalResponse = new Response(); + $test = $this; + $this->action->__invoke($request, $originalResponse, function ( + ServerRequestInterface $req, + ResponseInterface $resp, + $error + ) use ( + $test, + $request + ) { + $test->assertSame($request, $req); + $test->assertEquals(404, $resp->getStatusCode()); + $test->assertEquals('Not Found', $error); + }); + } + + /** + * @test + */ + public function nextErrorMiddlewareIsInvokedIfAnExceptionIsThrown() + { + $shortCode = 'abc123'; + $this->urlShortener->shortCodeToUrl($shortCode)->willThrow(\Exception::class) + ->shouldBeCalledTimes(1); + + $request = ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode); + $originalResponse = new Response(); + $test = $this; + $this->action->__invoke($request, $originalResponse, function ( + ServerRequestInterface $req, + ResponseInterface $resp, + $error + ) use ( + $test, + $request + ) { + $test->assertSame($request, $req); + $test->assertEquals(404, $resp->getStatusCode()); + $test->assertEquals('Not Found', $error); + }); + } +}