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(false); $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(true); $this->expectException(ValidationException::class); $findApiKey->shouldBeCalledOnce(); $this->action->handle($request); } /** @test */ public function properDataIsPassedWhenGeneratingShortCode(): void { $request = (new ServerRequest())->withQueryParams([ 'apiKey' => 'abc123', 'longUrl' => 'http://foobar.com', ]); $findApiKey = $this->apiKeyService->check('abc123')->willReturn(true); $generateShortCode = $this->urlShortener->urlToShortCode( Argument::that(function (string $argument): string { Assert::assertEquals('http://foobar.com', $argument); return $argument; }), [], ShortUrlMeta::createEmpty(), )->willReturn(new ShortUrl('')); $resp = $this->action->handle($request); self::assertEquals(200, $resp->getStatusCode()); $findApiKey->shouldHaveBeenCalled(); $generateShortCode->shouldHaveBeenCalled(); } }