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(); } }