'http', 'hostname' => 'foo.com', ]; /** @var CreateShortUrlAction */ private $action; /** @var ObjectProphecy */ private $urlShortener; public function setUp(): void { $this->urlShortener = $this->prophesize(UrlShortener::class); $this->action = new CreateShortUrlAction($this->urlShortener->reveal(), self::DOMAIN_CONFIG); } /** @test */ public function missingLongUrlParamReturnsError(): void { $this->expectException(ValidationException::class); $this->action->handle(new ServerRequest()); } /** @test */ public function properShortcodeConversionReturnsData(): void { $shortUrl = new ShortUrl(''); $this->urlShortener->urlToShortCode(Argument::type(Uri::class), Argument::type('array'), Argument::cetera()) ->willReturn($shortUrl) ->shouldBeCalledOnce(); $request = (new ServerRequest())->withParsedBody([ 'longUrl' => 'http://www.domain.com/foo/bar', ]); $response = $this->action->handle($request); $this->assertEquals(200, $response->getStatusCode()); $this->assertTrue(strpos($response->getBody()->getContents(), $shortUrl->toString(self::DOMAIN_CONFIG)) > 0); } /** * @test * @dataProvider provideInvalidDomains */ public function anInvalidDomainReturnsError(string $domain): void { $shortUrl = new ShortUrl(''); $urlToShortCode = $this->urlShortener->urlToShortCode(Argument::cetera())->willReturn($shortUrl); $request = (new ServerRequest())->withParsedBody([ 'longUrl' => 'http://www.domain.com/foo/bar', 'domain' => $domain, ]); $this->expectException(ValidationException::class); $urlToShortCode->shouldNotBeCalled(); $this->action->handle($request); } public function provideInvalidDomains(): iterable { yield ['localhost:80000']; yield ['127.0.0.1']; yield ['???/&%$&']; } }