service = $this->prophesize(DeleteShortUrlServiceInterface::class); $this->action = new DeleteShortUrlAction($this->service->reveal()); } /** * @test */ public function emptyResponseIsReturnedIfProperlyDeleted() { $deleteByShortCode = $this->service->deleteByShortCode(Argument::any())->will(function () { }); $resp = $this->action->handle(ServerRequestFactory::fromGlobals()); $this->assertEquals(204, $resp->getStatusCode()); $deleteByShortCode->shouldHaveBeenCalledOnce(); } /** * @test * @dataProvider provideExceptions */ public function returnsErrorResponseInCaseOfException(Throwable $e, string $error, int $statusCode) { $deleteByShortCode = $this->service->deleteByShortCode(Argument::any())->willThrow($e); /** @var JsonResponse $resp */ $resp = $this->action->handle(ServerRequestFactory::fromGlobals()); $payload = $resp->getPayload(); $this->assertEquals($statusCode, $resp->getStatusCode()); $this->assertEquals($error, $payload['error']); $deleteByShortCode->shouldHaveBeenCalledOnce(); } public function provideExceptions(): array { return [ [new Exception\InvalidShortCodeException(), RestUtils::INVALID_SHORTCODE_ERROR, 404], [new Exception\DeleteShortUrlException(5), RestUtils::INVALID_SHORTCODE_DELETION_ERROR, 400], ]; } }