From 3d5e5d5df95a1ed3c0cb02d8700f8966f122f10d Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sun, 31 Jul 2016 16:24:00 +0200 Subject: [PATCH] Created ResolveUrlActionTest --- .../Rest/test/Action/ResolveUrlActionTest.php | 90 +++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 module/Rest/test/Action/ResolveUrlActionTest.php diff --git a/module/Rest/test/Action/ResolveUrlActionTest.php b/module/Rest/test/Action/ResolveUrlActionTest.php new file mode 100644 index 00000000..0bd3bded --- /dev/null +++ b/module/Rest/test/Action/ResolveUrlActionTest.php @@ -0,0 +1,90 @@ +urlShortener = $this->prophesize(UrlShortener::class); + $this->action = new ResolveUrlAction($this->urlShortener->reveal(), Translator::factory([])); + } + + /** + * @test + */ + public function incorrectShortCodeReturnsError() + { + $shortCode = 'abc123'; + $this->urlShortener->shortCodeToUrl($shortCode)->willReturn(null) + ->shouldBeCalledTimes(1); + + $request = ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode); + $response = $this->action->__invoke($request, new Response()); + $this->assertEquals(400, $response->getStatusCode()); + $this->assertTrue(strpos($response->getBody()->getContents(), RestUtils::INVALID_ARGUMENT_ERROR) > 0); + } + + /** + * @test + */ + public function correctShortCodeReturnsSuccess() + { + $shortCode = 'abc123'; + $this->urlShortener->shortCodeToUrl($shortCode)->willReturn('http://domain.com/foo/bar') + ->shouldBeCalledTimes(1); + + $request = ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode); + $response = $this->action->__invoke($request, new Response()); + $this->assertEquals(200, $response->getStatusCode()); + $this->assertTrue(strpos($response->getBody()->getContents(), 'http://domain.com/foo/bar') > 0); + } + + /** + * @test + */ + public function invalidShortCodeExceptionReturnsError() + { + $shortCode = 'abc123'; + $this->urlShortener->shortCodeToUrl($shortCode)->willThrow(InvalidShortCodeException::class) + ->shouldBeCalledTimes(1); + + $request = ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode); + $response = $this->action->__invoke($request, new Response()); + $this->assertEquals(400, $response->getStatusCode()); + $this->assertTrue(strpos($response->getBody()->getContents(), RestUtils::INVALID_SHORTCODE_ERROR) > 0); + } + + /** + * @test + */ + public function unexpectedExceptionWillReturnError() + { + $shortCode = 'abc123'; + $this->urlShortener->shortCodeToUrl($shortCode)->willThrow(\Exception::class) + ->shouldBeCalledTimes(1); + + $request = ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode); + $response = $this->action->__invoke($request, new Response()); + $this->assertEquals(500, $response->getStatusCode()); + $this->assertTrue(strpos($response->getBody()->getContents(), RestUtils::UNKNOWN_ERROR) > 0); + } +}