ShortUrlServiceTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. declare(strict_types=1);
  3. namespace ShlinkioTest\Shlink\Core\Service;
  4. use Cake\Chronos\Chronos;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Doctrine\ORM\EntityRepository;
  7. use PHPUnit\Framework\TestCase;
  8. use Prophecy\Argument;
  9. use Prophecy\Prophecy\ObjectProphecy;
  10. use Shlinkio\Shlink\Core\Entity\ShortUrl;
  11. use Shlinkio\Shlink\Core\Entity\Tag;
  12. use Shlinkio\Shlink\Core\Exception\ShortUrlNotFoundException;
  13. use Shlinkio\Shlink\Core\Model\ShortUrlMeta;
  14. use Shlinkio\Shlink\Core\Repository\ShortUrlRepository;
  15. use Shlinkio\Shlink\Core\Service\ShortUrlService;
  16. use function count;
  17. class ShortUrlServiceTest extends TestCase
  18. {
  19. private ShortUrlService $service;
  20. private ObjectProphecy $em;
  21. public function setUp(): void
  22. {
  23. $this->em = $this->prophesize(EntityManagerInterface::class);
  24. $this->em->persist(Argument::any())->willReturn(null);
  25. $this->em->flush()->willReturn(null);
  26. $this->service = new ShortUrlService($this->em->reveal());
  27. }
  28. /** @test */
  29. public function listedUrlsAreReturnedFromEntityManager(): void
  30. {
  31. $list = [
  32. new ShortUrl(''),
  33. new ShortUrl(''),
  34. new ShortUrl(''),
  35. new ShortUrl(''),
  36. ];
  37. $repo = $this->prophesize(ShortUrlRepository::class);
  38. $repo->findList(Argument::cetera())->willReturn($list)->shouldBeCalledOnce();
  39. $repo->countList(Argument::cetera())->willReturn(count($list))->shouldBeCalledOnce();
  40. $this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal());
  41. $list = $this->service->listShortUrls();
  42. $this->assertEquals(4, $list->getCurrentItemCount());
  43. }
  44. /** @test */
  45. public function exceptionIsThrownWhenSettingTagsOnInvalidShortcode(): void
  46. {
  47. $shortCode = 'abc123';
  48. $repo = $this->prophesize(ShortUrlRepository::class);
  49. $repo->findOneBy(['shortCode' => $shortCode])->willReturn(null)
  50. ->shouldBeCalledOnce();
  51. $this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal());
  52. $this->expectException(ShortUrlNotFoundException::class);
  53. $this->service->setTagsByShortCode($shortCode);
  54. }
  55. /** @test */
  56. public function providedTagsAreGetFromRepoAndSetToTheShortUrl(): void
  57. {
  58. $shortUrl = $this->prophesize(ShortUrl::class);
  59. $shortUrl->setTags(Argument::any())->shouldBeCalledOnce();
  60. $shortCode = 'abc123';
  61. $repo = $this->prophesize(ShortUrlRepository::class);
  62. $repo->findOneBy(['shortCode' => $shortCode])->willReturn($shortUrl->reveal())
  63. ->shouldBeCalledOnce();
  64. $this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal());
  65. $tagRepo = $this->prophesize(EntityRepository::class);
  66. $tagRepo->findOneBy(['name' => 'foo'])->willReturn(new Tag('foo'))->shouldBeCalledOnce();
  67. $tagRepo->findOneBy(['name' => 'bar'])->willReturn(null)->shouldBeCalledOnce();
  68. $this->em->getRepository(Tag::class)->willReturn($tagRepo->reveal());
  69. $this->service->setTagsByShortCode($shortCode, ['foo', 'bar']);
  70. }
  71. /** @test */
  72. public function updateMetadataByShortCodeUpdatesProvidedData(): void
  73. {
  74. $shortUrl = new ShortUrl('');
  75. $repo = $this->prophesize(ShortUrlRepository::class);
  76. $findShortUrl = $repo->findOneBy(['shortCode' => 'abc123'])->willReturn($shortUrl);
  77. $getRepo = $this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal());
  78. $flush = $this->em->flush()->willReturn(null);
  79. $result = $this->service->updateMetadataByShortCode('abc123', ShortUrlMeta::createFromParams(
  80. Chronos::parse('2017-01-01 00:00:00')->toAtomString(),
  81. Chronos::parse('2017-01-05 00:00:00')->toAtomString(),
  82. null,
  83. 5,
  84. ));
  85. $this->assertSame($shortUrl, $result);
  86. $this->assertEquals(Chronos::parse('2017-01-01 00:00:00'), $shortUrl->getValidSince());
  87. $this->assertEquals(Chronos::parse('2017-01-05 00:00:00'), $shortUrl->getValidUntil());
  88. $this->assertEquals(5, $shortUrl->getMaxVisits());
  89. $findShortUrl->shouldHaveBeenCalled();
  90. $getRepo->shouldHaveBeenCalled();
  91. $flush->shouldHaveBeenCalled();
  92. }
  93. }