EditShortUrlActionTest.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. declare(strict_types=1);
  3. namespace ShlinkioTest\Shlink\Rest\Action\ShortUrl;
  4. use Laminas\Diactoros\ServerRequest;
  5. use PHPUnit\Framework\TestCase;
  6. use Prophecy\Argument;
  7. use Prophecy\Prophecy\ObjectProphecy;
  8. use Shlinkio\Shlink\Core\Entity\ShortUrl;
  9. use Shlinkio\Shlink\Core\Exception\ValidationException;
  10. use Shlinkio\Shlink\Core\Service\ShortUrlServiceInterface;
  11. use Shlinkio\Shlink\Rest\Action\ShortUrl\EditShortUrlAction;
  12. class EditShortUrlActionTest extends TestCase
  13. {
  14. private EditShortUrlAction $action;
  15. private ObjectProphecy $shortUrlService;
  16. public function setUp(): void
  17. {
  18. $this->shortUrlService = $this->prophesize(ShortUrlServiceInterface::class);
  19. $this->action = new EditShortUrlAction($this->shortUrlService->reveal());
  20. }
  21. /** @test */
  22. public function invalidDataThrowsError(): void
  23. {
  24. $request = (new ServerRequest())->withParsedBody([
  25. 'maxVisits' => 'invalid',
  26. ]);
  27. $this->expectException(ValidationException::class);
  28. $this->action->handle($request);
  29. }
  30. /** @test */
  31. public function correctShortCodeReturnsSuccess(): void
  32. {
  33. $request = (new ServerRequest())->withAttribute('shortCode', 'abc123')
  34. ->withParsedBody([
  35. 'maxVisits' => 5,
  36. ]);
  37. $updateMeta = $this->shortUrlService->updateMetadataByShortCode(Argument::cetera())->willReturn(
  38. new ShortUrl(''),
  39. );
  40. $resp = $this->action->handle($request);
  41. $this->assertEquals(204, $resp->getStatusCode());
  42. $updateMeta->shouldHaveBeenCalled();
  43. }
  44. }