From 04e0a192add9ae5504fca0ec9be2be685a169716 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sun, 31 Jul 2016 15:58:18 +0200 Subject: [PATCH] Created CreateShortcodeActionTest --- .../test/Action/CreateShortcodeActionTest.php | 92 +++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 module/Rest/test/Action/CreateShortcodeActionTest.php diff --git a/module/Rest/test/Action/CreateShortcodeActionTest.php b/module/Rest/test/Action/CreateShortcodeActionTest.php new file mode 100644 index 00000000..dcc56bf6 --- /dev/null +++ b/module/Rest/test/Action/CreateShortcodeActionTest.php @@ -0,0 +1,92 @@ +urlShortener = $this->prophesize(UrlShortener::class); + $this->action = new CreateShortcodeAction($this->urlShortener->reveal(), Translator::factory([]), [ + 'schema' => 'http', + 'hostname' => 'foo.com', + ]); + } + + /** + * @test + */ + public function missingLongUrlParamReturnsError() + { + $response = $this->action->__invoke(ServerRequestFactory::fromGlobals(), new Response()); + $this->assertEquals(400, $response->getStatusCode()); + } + + /** + * @test + */ + public function properShortcodeConversionReturnsData() + { + $this->urlShortener->urlToShortCode(Argument::type(Uri::class))->willReturn('abc123') + ->shouldBeCalledTimes(1); + + $request = ServerRequestFactory::fromGlobals()->withParsedBody([ + 'longUrl' => 'http://www.domain.com/foo/bar', + ]); + $response = $this->action->__invoke($request, new Response()); + $this->assertEquals(200, $response->getStatusCode()); + $this->assertTrue(strpos($response->getBody()->getContents(), 'http://foo.com/abc123') > 0); + } + + /** + * @test + */ + public function anInvalidUrlReturnsError() + { + $this->urlShortener->urlToShortCode(Argument::type(Uri::class))->willThrow(InvalidUrlException::class) + ->shouldBeCalledTimes(1); + + $request = ServerRequestFactory::fromGlobals()->withParsedBody([ + 'longUrl' => 'http://www.domain.com/foo/bar', + ]); + $response = $this->action->__invoke($request, new Response()); + $this->assertEquals(400, $response->getStatusCode()); + $this->assertTrue(strpos($response->getBody()->getContents(), RestUtils::INVALID_URL_ERROR) > 0); + } + + /** + * @test + */ + public function aGenericExceptionWillReturnError() + { + $this->urlShortener->urlToShortCode(Argument::type(Uri::class))->willThrow(\Exception::class) + ->shouldBeCalledTimes(1); + + $request = ServerRequestFactory::fromGlobals()->withParsedBody([ + 'longUrl' => 'http://www.domain.com/foo/bar', + ]); + $response = $this->action->__invoke($request, new Response()); + $this->assertEquals(500, $response->getStatusCode()); + $this->assertTrue(strpos($response->getBody()->getContents(), RestUtils::UNKNOWN_ERROR) > 0); + } +}