From 0521227127e3c1c7d692055e35141e745c5163b4 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sun, 7 Jan 2018 20:00:21 +0100 Subject: [PATCH] Tested new method to update short URLs metadata --- .../{ShortCodeMeta.php => ShortUrlMeta.php} | 8 +++-- module/Core/src/Service/ShortUrlService.php | 6 ++-- .../src/Service/ShortUrlServiceInterface.php | 6 ++-- .../Core/test/Service/ShortUrlServiceTest.php | 33 ++++++++++++++++++- 4 files changed, 43 insertions(+), 10 deletions(-) rename module/Core/src/Model/{ShortCodeMeta.php => ShortUrlMeta.php} (92%) diff --git a/module/Core/src/Model/ShortCodeMeta.php b/module/Core/src/Model/ShortUrlMeta.php similarity index 92% rename from module/Core/src/Model/ShortCodeMeta.php rename to module/Core/src/Model/ShortUrlMeta.php index 5bdcdf8a..f79edce6 100644 --- a/module/Core/src/Model/ShortCodeMeta.php +++ b/module/Core/src/Model/ShortUrlMeta.php @@ -6,7 +6,7 @@ namespace Shlinkio\Shlink\Core\Model; use Shlinkio\Shlink\Core\Exception\ValidationException; use Shlinkio\Shlink\Core\Validation\ShortUrlMetaInputFilter; -final class ShortCodeMeta +final class ShortUrlMeta { /** * @var \DateTime|null @@ -32,7 +32,7 @@ final class ShortCodeMeta /** * @param array $data - * @return ShortCodeMeta + * @return ShortUrlMeta * @throws ValidationException */ public static function createFromRawData(array $data): self @@ -47,7 +47,7 @@ final class ShortCodeMeta * @param string|\DateTimeInterface|null $validUntil * @param string|null $customSlug * @param int|null $maxVisits - * @return ShortCodeMeta + * @return ShortUrlMeta * @throws ValidationException */ public static function createFromParams( @@ -79,7 +79,9 @@ final class ShortCodeMeta } $this->validSince = $inputFilter->getValue(ShortUrlMetaInputFilter::VALID_SINCE); + $this->validSince = $this->validSince !== null ? new \DateTime($this->validSince) : null; $this->validUntil = $inputFilter->getValue(ShortUrlMetaInputFilter::VALID_UNTIL); + $this->validUntil = $this->validUntil !== null ? new \DateTime($this->validUntil) : null; $this->customSlug = $inputFilter->getValue(ShortUrlMetaInputFilter::CUSTOM_SLUG); $this->maxVisits = $inputFilter->getValue(ShortUrlMetaInputFilter::MAX_VISITS); $this->maxVisits = $this->maxVisits !== null ? (int) $this->maxVisits : null; diff --git a/module/Core/src/Service/ShortUrlService.php b/module/Core/src/Service/ShortUrlService.php index 261cb961..3a89b828 100644 --- a/module/Core/src/Service/ShortUrlService.php +++ b/module/Core/src/Service/ShortUrlService.php @@ -7,7 +7,7 @@ use Doctrine\ORM; use Shlinkio\Shlink\Common\Paginator\Adapter\PaginableRepositoryAdapter; use Shlinkio\Shlink\Core\Entity\ShortUrl; use Shlinkio\Shlink\Core\Exception\InvalidShortCodeException; -use Shlinkio\Shlink\Core\Model\ShortCodeMeta; +use Shlinkio\Shlink\Core\Model\ShortUrlMeta; use Shlinkio\Shlink\Core\Repository\ShortUrlRepository; use Shlinkio\Shlink\Core\Util\TagManagerTrait; use Zend\Paginator\Paginator; @@ -61,11 +61,11 @@ class ShortUrlService implements ShortUrlServiceInterface /** * @param string $shortCode - * @param ShortCodeMeta $shortCodeMeta + * @param ShortUrlMeta $shortCodeMeta * @return ShortUrl * @throws InvalidShortCodeException */ - public function updateMetadataByShortCode(string $shortCode, ShortCodeMeta $shortCodeMeta): ShortUrl + public function updateMetadataByShortCode(string $shortCode, ShortUrlMeta $shortCodeMeta): ShortUrl { $shortUrl = $this->findByShortCode($shortCode); if ($shortCodeMeta->hasValidSince()) { diff --git a/module/Core/src/Service/ShortUrlServiceInterface.php b/module/Core/src/Service/ShortUrlServiceInterface.php index 6ac3a9b6..2350105b 100644 --- a/module/Core/src/Service/ShortUrlServiceInterface.php +++ b/module/Core/src/Service/ShortUrlServiceInterface.php @@ -5,7 +5,7 @@ namespace Shlinkio\Shlink\Core\Service; use Shlinkio\Shlink\Core\Entity\ShortUrl; use Shlinkio\Shlink\Core\Exception\InvalidShortCodeException; -use Shlinkio\Shlink\Core\Model\ShortCodeMeta; +use Shlinkio\Shlink\Core\Model\ShortUrlMeta; use Zend\Paginator\Paginator; interface ShortUrlServiceInterface @@ -29,9 +29,9 @@ interface ShortUrlServiceInterface /** * @param string $shortCode - * @param ShortCodeMeta $shortCodeMeta + * @param ShortUrlMeta $shortCodeMeta * @return ShortUrl * @throws InvalidShortCodeException */ - public function updateMetadataByShortCode(string $shortCode, ShortCodeMeta $shortCodeMeta): ShortUrl; + public function updateMetadataByShortCode(string $shortCode, ShortUrlMeta $shortCodeMeta): ShortUrl; } diff --git a/module/Core/test/Service/ShortUrlServiceTest.php b/module/Core/test/Service/ShortUrlServiceTest.php index ed98a5bc..9de6d89f 100644 --- a/module/Core/test/Service/ShortUrlServiceTest.php +++ b/module/Core/test/Service/ShortUrlServiceTest.php @@ -10,8 +10,11 @@ use Prophecy\Argument; use Prophecy\Prophecy\ObjectProphecy; use Shlinkio\Shlink\Core\Entity\ShortUrl; use Shlinkio\Shlink\Core\Entity\Tag; +use Shlinkio\Shlink\Core\Exception\InvalidShortCodeException; +use Shlinkio\Shlink\Core\Model\ShortUrlMeta; use Shlinkio\Shlink\Core\Repository\ShortUrlRepository; use Shlinkio\Shlink\Core\Service\ShortUrlService; +use Shlinkio\Shlink\Core\Validation\ShortUrlMetaInputFilter; class ShortUrlServiceTest extends TestCase { @@ -55,7 +58,6 @@ class ShortUrlServiceTest extends TestCase /** * @test - * @expectedException \Shlinkio\Shlink\Core\Exception\InvalidShortCodeException */ public function exceptionIsThrownWhenSettingTagsOnInvalidShortcode() { @@ -65,6 +67,7 @@ class ShortUrlServiceTest extends TestCase ->shouldBeCalledTimes(1); $this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal()); + $this->expectException(InvalidShortCodeException::class); $this->service->setTagsByShortCode($shortCode); } @@ -88,4 +91,32 @@ class ShortUrlServiceTest extends TestCase $this->service->setTagsByShortCode($shortCode, ['foo', 'bar']); } + + /** + * @test + */ + public function updateMetadataByShortCodeUpdatesProvidedData() + { + $shortUrl = new ShortUrl(); + + $repo = $this->prophesize(ShortUrlRepository::class); + $findShortUrl = $repo->findOneBy(['shortCode' => 'abc123'])->willReturn($shortUrl); + $getRepo = $this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal()); + $flush = $this->em->flush($shortUrl)->willReturn(null); + + $result = $this->service->updateMetadataByShortCode('abc123', ShortUrlMeta::createFromParams( + (new \DateTime('2017-01-01 00:00:00'))->format(\DateTime::ATOM), + (new \DateTime('2017-01-05 00:00:00'))->format(\DateTime::ATOM), + null, + 5 + )); + + $this->assertSame($shortUrl, $result); + $this->assertEquals(new \DateTime('2017-01-01 00:00:00'), $shortUrl->getValidSince()); + $this->assertEquals(new \DateTime('2017-01-05 00:00:00'), $shortUrl->getValidUntil()); + $this->assertEquals(5, $shortUrl->getMaxVisits()); + $findShortUrl->shouldHaveBeenCalled(); + $getRepo->shouldHaveBeenCalled(); + $flush->shouldHaveBeenCalled(); + } }