From 4615bbaaf706487a5c6be9268b304812ee19906b Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Thu, 18 Aug 2016 17:23:40 +0200 Subject: [PATCH] CreatedPreviewActionTest --- module/Core/test/Action/PreviewActionTest.php | 114 ++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 module/Core/test/Action/PreviewActionTest.php diff --git a/module/Core/test/Action/PreviewActionTest.php b/module/Core/test/Action/PreviewActionTest.php new file mode 100644 index 00000000..1ef49307 --- /dev/null +++ b/module/Core/test/Action/PreviewActionTest.php @@ -0,0 +1,114 @@ +previewGenerator = $this->prophesize(PreviewGenerator::class); + $this->urlShortener = $this->prophesize(UrlShortener::class); + $this->action = new PreviewAction($this->previewGenerator->reveal(), $this->urlShortener->reveal()); + } + + /** + * @test + */ + public function invalidShortCodeFallbacksToNextMiddlewareWithStatusNotFound() + { + $shortCode = 'abc123'; + $this->urlShortener->shortCodeToUrl($shortCode)->willReturn(null)->shouldBeCalledTimes(1); + + $resp = $this->action->__invoke( + ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode), + new Response(), + function ($req, $resp) { + return $resp; + } + ); + + $this->assertEquals(404, $resp->getStatusCode()); + } + + /** + * @test + */ + public function correctShortCodeReturnsImageResponse() + { + $shortCode = 'abc123'; + $url = 'foobar.com'; + $path = __FILE__; + $this->urlShortener->shortCodeToUrl($shortCode)->willReturn($url)->shouldBeCalledTimes(1); + $this->previewGenerator->generatePreview($url)->willReturn($path)->shouldBeCalledTimes(1); + + $resp = $this->action->__invoke( + ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode), + new Response() + ); + + $this->assertEquals(filesize($path), $resp->getHeaderLine('Content-length')); + $this->assertEquals((new \finfo(FILEINFO_MIME))->file($path), $resp->getHeaderLine('Content-type')); + } + + /** + * @test + */ + public function invalidShortcodeExceptionReturnsNotFound() + { + $shortCode = 'abc123'; + $this->urlShortener->shortCodeToUrl($shortCode)->willThrow(InvalidShortCodeException::class) + ->shouldBeCalledTimes(1); + + $resp = $this->action->__invoke( + ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode), + new Response(), + function ($req, $resp) { + return $resp; + } + ); + + $this->assertEquals(404, $resp->getStatusCode()); + } + + /** + * @test + */ + public function previewExceptionReturnsNotFound() + { + $shortCode = 'abc123'; + $this->urlShortener->shortCodeToUrl($shortCode)->willThrow(PreviewGenerationException::class) + ->shouldBeCalledTimes(1); + + $resp = $this->action->__invoke( + ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode), + new Response(), + function ($req, $resp) { + return $resp; + } + ); + + $this->assertEquals(500, $resp->getStatusCode()); + } +}