urlShortener = $this->prophesize(UrlShortenerInterface::class); $this->apiKeyService = $this->prophesize(ApiKeyServiceInterface::class); $this->action = new SingleStepCreateShortUrlAction( $this->urlShortener->reveal(), $this->apiKeyService->reveal(), [ 'schema' => 'http', 'hostname' => 'foo.com', ], ); } /** @test */ public function errorResponseIsReturnedIfInvalidApiKeyIsProvided(): void { $request = (new ServerRequest())->withQueryParams(['apiKey' => 'abc123']); $findApiKey = $this->apiKeyService->check('abc123')->willReturn(new ApiKeyCheckResult()); $this->expectException(ValidationException::class); $findApiKey->shouldBeCalledOnce(); $this->action->handle($request); } /** @test */ public function errorResponseIsReturnedIfNoUrlIsProvided(): void { $request = (new ServerRequest())->withQueryParams(['apiKey' => 'abc123']); $findApiKey = $this->apiKeyService->check('abc123')->willReturn(new ApiKeyCheckResult(new ApiKey())); $this->expectException(ValidationException::class); $findApiKey->shouldBeCalledOnce(); $this->action->handle($request); } /** @test */ public function properDataIsPassedWhenGeneratingShortCode(): void { $apiKey = new ApiKey(); $key = $apiKey->toString(); $request = (new ServerRequest())->withQueryParams([ 'apiKey' => $key, 'longUrl' => 'http://foobar.com', ]); $findApiKey = $this->apiKeyService->check($key)->willReturn(new ApiKeyCheckResult($apiKey)); $generateShortCode = $this->urlShortener->shorten( Argument::that(function (string $argument): string { Assert::assertEquals('http://foobar.com', $argument); return $argument; }), [], ShortUrlMeta::fromRawData(['apiKey' => $key]), )->willReturn(new ShortUrl('')); $resp = $this->action->handle($request); self::assertEquals(200, $resp->getStatusCode()); $findApiKey->shouldHaveBeenCalled(); $generateShortCode->shouldHaveBeenCalled(); } }