From b99d6624172a00e73c43e6111c06b169a967b9d7 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Thu, 3 May 2018 17:57:43 +0200 Subject: [PATCH] Created SingleStepCreateShortCodeActionTest --- .../SingleStepCreateShortCodeActionTest.php | 123 ++++++++++++++++++ 1 file changed, 123 insertions(+) create mode 100644 module/Rest/test/Action/ShortCode/SingleStepCreateShortCodeActionTest.php diff --git a/module/Rest/test/Action/ShortCode/SingleStepCreateShortCodeActionTest.php b/module/Rest/test/Action/ShortCode/SingleStepCreateShortCodeActionTest.php new file mode 100644 index 00000000..88d722e5 --- /dev/null +++ b/module/Rest/test/Action/ShortCode/SingleStepCreateShortCodeActionTest.php @@ -0,0 +1,123 @@ +urlShortener = $this->prophesize(UrlShortenerInterface::class); + $this->apiKeyService = $this->prophesize(ApiKeyServiceInterface::class); + + $this->action = new SingleStepCreateShortCodeAction( + $this->urlShortener->reveal(), + Translator::factory([]), + $this->apiKeyService->reveal(), + [ + 'schema' => 'http', + 'hostname' => 'foo.com', + ] + ); + } + + /** + * @test + * @dataProvider provideInvalidApiKeys + */ + public function errorResponseIsReturnedIfInvalidApiKeyIsProvided(?ApiKey $apiKey) + { + $request = ServerRequestFactory::fromGlobals()->withQueryParams(['apiKey' => 'abc123']); + $findApiKey = $this->apiKeyService->getByKey('abc123')->willReturn($apiKey); + + /** @var JsonResponse $resp */ + $resp = $this->action->handle($request); + $payload = $resp->getPayload(); + + $this->assertEquals(400, $resp->getStatusCode()); + $this->assertEquals('INVALID_ARGUMENT', $payload['error']); + $this->assertEquals('No API key was provided or it is not valid', $payload['message']); + $findApiKey->shouldHaveBeenCalled(); + } + + public function provideInvalidApiKeys(): array + { + return [ + [null], + [(new ApiKey())->disable()], + ]; + } + + /** + * @test + */ + public function errorResponseIsReturnedIfNoUrlIsProvided() + { + $request = ServerRequestFactory::fromGlobals()->withQueryParams(['apiKey' => 'abc123']); + $findApiKey = $this->apiKeyService->getByKey('abc123')->willReturn(new ApiKey()); + + /** @var JsonResponse $resp */ + $resp = $this->action->handle($request); + $payload = $resp->getPayload(); + + $this->assertEquals(400, $resp->getStatusCode()); + $this->assertEquals('INVALID_ARGUMENT', $payload['error']); + $this->assertEquals('A URL was not provided', $payload['message']); + $findApiKey->shouldHaveBeenCalled(); + } + + /** + * @test + */ + public function properDataIsPassedWhenGeneratingShortCode() + { + $request = ServerRequestFactory::fromGlobals()->withQueryParams([ + 'apiKey' => 'abc123', + 'longUrl' => 'http://foobar.com', + ]); + $findApiKey = $this->apiKeyService->getByKey('abc123')->willReturn(new ApiKey()); + $generateShortCode = $this->urlShortener->urlToShortCode( + Argument::that(function (UriInterface $argument) { + Assert::assertEquals('http://foobar.com', (string) $argument); + return $argument; + }), + [], + null, + null, + null, + null + ); + + $resp = $this->action->handle($request); + + $this->assertEquals(200, $resp->getStatusCode()); + $findApiKey->shouldHaveBeenCalled(); + $generateShortCode->shouldHaveBeenCalled(); + } +}