diff --git a/module/Core/test/Action/QrCodeActionTest.php b/module/Core/test/Action/QrCodeActionTest.php new file mode 100644 index 00000000..d4924724 --- /dev/null +++ b/module/Core/test/Action/QrCodeActionTest.php @@ -0,0 +1,93 @@ +prophesize(RouterInterface::class); + $router->generateUri(Argument::cetera())->willReturn('/foo/bar'); + + $this->urlShortener = $this->prophesize(UrlShortener::class); + + $this->action = new QrCodeAction($router->reveal(), $this->urlShortener->reveal()); + } + + /** + * @test + */ + public function aNonexistentShortCodeWillReturnNotFoundResponse() + { + $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 anInvalidShortCodeWillReturnNotFoundResponse() + { + $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 aCorrectRequestReturnsTheQrCodeResponse() + { + $shortCode = 'abc123'; + $this->urlShortener->shortCodeToUrl($shortCode)->willReturn(new ShortUrl())->shouldBeCalledTimes(1); + + $resp = $this->action->__invoke( + ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode), + new Response(), + function ($req, $resp) { + return $resp; + } + ); + + $this->assertInstanceOf(QrCodeResponse::class, $resp); + $this->assertEquals(200, $resp->getStatusCode()); + } +} diff --git a/module/Core/test/Middleware/QrCodeCacheMiddlewareTest.php b/module/Core/test/Middleware/QrCodeCacheMiddlewareTest.php new file mode 100644 index 00000000..64aac3e0 --- /dev/null +++ b/module/Core/test/Middleware/QrCodeCacheMiddlewareTest.php @@ -0,0 +1,69 @@ +cache = new ArrayCache(); + $this->middleware = new QrCodeCacheMiddleware($this->cache); + } + + /** + * @test + */ + public function noCachedPathFallbacksToNextMiddleware() + { + $isCalled = false; + $this->middleware->__invoke( + ServerRequestFactory::fromGlobals(), + new Response(), + function ($req, $resp) use (&$isCalled) { + $isCalled = true; + return $resp; + } + ); + $this->assertTrue($isCalled); + } + + /** + * @test + */ + public function cachedPathReturnsCacheContent() + { + $isCalled = false; + $uri = (new Uri())->withPath('/foo'); + $this->cache->save('/foo', ['body' => 'the body', 'content-type' => 'image/png']); + + $resp = $this->middleware->__invoke( + ServerRequestFactory::fromGlobals()->withUri($uri), + new Response(), + function ($req, $resp) use (&$isCalled) { + $isCalled = true; + return $resp; + } + ); + + $this->assertFalse($isCalled); + $resp->getBody()->rewind(); + $this->assertEquals('the body', $resp->getBody()->getContents()); + $this->assertEquals('image/png', $resp->getHeaderLine('Content-Type')); + } +}