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 (UriInterface $argument) { Assert::assertEquals('http://foobar.com', (string) $argument); return $argument; }), [], ShortUrlMeta::createEmpty() )->willReturn(new ShortUrl('')); $resp = $this->action->handle($request); $this->assertEquals(200, $resp->getStatusCode()); $findApiKey->shouldHaveBeenCalled(); $generateShortCode->shouldHaveBeenCalled(); } }