urlShortener = $this->prophesize(UrlShortener::class); $this->action = new CreateShortcodeAction($this->urlShortener->reveal(), Translator::factory([]), [ 'schema' => 'http', 'hostname' => 'foo.com', ]); } /** * @test */ public function missingLongUrlParamReturnsError() { $response = $this->action->process( ServerRequestFactory::fromGlobals(), TestUtils::createDelegateMock()->reveal() ); $this->assertEquals(400, $response->getStatusCode()); } /** * @test */ public function properShortcodeConversionReturnsData() { $this->urlShortener->urlToShortCode(Argument::type(Uri::class), Argument::type('array'), null, null) ->willReturn('abc123') ->shouldBeCalledTimes(1); $request = ServerRequestFactory::fromGlobals()->withParsedBody([ 'longUrl' => 'http://www.domain.com/foo/bar', ]); $response = $this->action->process($request, TestUtils::createDelegateMock()->reveal()); $this->assertEquals(200, $response->getStatusCode()); $this->assertTrue(strpos($response->getBody()->getContents(), 'http://foo.com/abc123') > 0); } /** * @test */ public function anInvalidUrlReturnsError() { $this->urlShortener->urlToShortCode(Argument::type(Uri::class), Argument::type('array'), null, null) ->willThrow(InvalidUrlException::class) ->shouldBeCalledTimes(1); $request = ServerRequestFactory::fromGlobals()->withParsedBody([ 'longUrl' => 'http://www.domain.com/foo/bar', ]); $response = $this->action->process($request, TestUtils::createDelegateMock()->reveal()); $this->assertEquals(400, $response->getStatusCode()); $this->assertTrue(strpos($response->getBody()->getContents(), RestUtils::INVALID_URL_ERROR) > 0); } /** * @test */ public function aGenericExceptionWillReturnError() { $this->urlShortener->urlToShortCode(Argument::type(Uri::class), Argument::type('array'), null, null) ->willThrow(\Exception::class) ->shouldBeCalledTimes(1); $request = ServerRequestFactory::fromGlobals()->withParsedBody([ 'longUrl' => 'http://www.domain.com/foo/bar', ]); $response = $this->action->process($request, TestUtils::createDelegateMock()->reveal()); $this->assertEquals(500, $response->getStatusCode()); $this->assertTrue(strpos($response->getBody()->getContents(), RestUtils::UNKNOWN_ERROR) > 0); } }