EditShortUrlActionTest.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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\PhpUnit\ProphecyTrait;
  8. use Prophecy\Prophecy\ObjectProphecy;
  9. use Shlinkio\Shlink\Core\Entity\ShortUrl;
  10. use Shlinkio\Shlink\Core\Exception\ValidationException;
  11. use Shlinkio\Shlink\Core\Service\ShortUrlServiceInterface;
  12. use Shlinkio\Shlink\Rest\Action\ShortUrl\EditShortUrlAction;
  13. use Shlinkio\Shlink\Rest\Entity\ApiKey;
  14. class EditShortUrlActionTest extends TestCase
  15. {
  16. use ProphecyTrait;
  17. private EditShortUrlAction $action;
  18. private ObjectProphecy $shortUrlService;
  19. public function setUp(): void
  20. {
  21. $this->shortUrlService = $this->prophesize(ShortUrlServiceInterface::class);
  22. $this->action = new EditShortUrlAction($this->shortUrlService->reveal());
  23. }
  24. /** @test */
  25. public function invalidDataThrowsError(): void
  26. {
  27. $request = (new ServerRequest())->withParsedBody([
  28. 'maxVisits' => 'invalid',
  29. ]);
  30. $this->expectException(ValidationException::class);
  31. $this->action->handle($request);
  32. }
  33. /** @test */
  34. public function correctShortCodeReturnsSuccess(): void
  35. {
  36. $request = (new ServerRequest())->withAttribute('shortCode', 'abc123')
  37. ->withAttribute(ApiKey::class, new ApiKey())
  38. ->withParsedBody([
  39. 'maxVisits' => 5,
  40. ]);
  41. $updateMeta = $this->shortUrlService->updateMetadataByShortCode(Argument::cetera())->willReturn(
  42. new ShortUrl(''),
  43. );
  44. $resp = $this->action->handle($request);
  45. self::assertEquals(204, $resp->getStatusCode());
  46. $updateMeta->shouldHaveBeenCalled();
  47. }
  48. }