Tested new method to update short URLs metadata

This commit is contained in:
Alejandro Celaya 2018-01-07 20:00:21 +01:00
parent fac9455a1e
commit 0521227127
4 changed files with 43 additions and 10 deletions

View File

@ -6,7 +6,7 @@ namespace Shlinkio\Shlink\Core\Model;
use Shlinkio\Shlink\Core\Exception\ValidationException; use Shlinkio\Shlink\Core\Exception\ValidationException;
use Shlinkio\Shlink\Core\Validation\ShortUrlMetaInputFilter; use Shlinkio\Shlink\Core\Validation\ShortUrlMetaInputFilter;
final class ShortCodeMeta final class ShortUrlMeta
{ {
/** /**
* @var \DateTime|null * @var \DateTime|null
@ -32,7 +32,7 @@ final class ShortCodeMeta
/** /**
* @param array $data * @param array $data
* @return ShortCodeMeta * @return ShortUrlMeta
* @throws ValidationException * @throws ValidationException
*/ */
public static function createFromRawData(array $data): self public static function createFromRawData(array $data): self
@ -47,7 +47,7 @@ final class ShortCodeMeta
* @param string|\DateTimeInterface|null $validUntil * @param string|\DateTimeInterface|null $validUntil
* @param string|null $customSlug * @param string|null $customSlug
* @param int|null $maxVisits * @param int|null $maxVisits
* @return ShortCodeMeta * @return ShortUrlMeta
* @throws ValidationException * @throws ValidationException
*/ */
public static function createFromParams( public static function createFromParams(
@ -79,7 +79,9 @@ final class ShortCodeMeta
} }
$this->validSince = $inputFilter->getValue(ShortUrlMetaInputFilter::VALID_SINCE); $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 = $inputFilter->getValue(ShortUrlMetaInputFilter::VALID_UNTIL);
$this->validUntil = $this->validUntil !== null ? new \DateTime($this->validUntil) : null;
$this->customSlug = $inputFilter->getValue(ShortUrlMetaInputFilter::CUSTOM_SLUG); $this->customSlug = $inputFilter->getValue(ShortUrlMetaInputFilter::CUSTOM_SLUG);
$this->maxVisits = $inputFilter->getValue(ShortUrlMetaInputFilter::MAX_VISITS); $this->maxVisits = $inputFilter->getValue(ShortUrlMetaInputFilter::MAX_VISITS);
$this->maxVisits = $this->maxVisits !== null ? (int) $this->maxVisits : null; $this->maxVisits = $this->maxVisits !== null ? (int) $this->maxVisits : null;

View File

@ -7,7 +7,7 @@ use Doctrine\ORM;
use Shlinkio\Shlink\Common\Paginator\Adapter\PaginableRepositoryAdapter; use Shlinkio\Shlink\Common\Paginator\Adapter\PaginableRepositoryAdapter;
use Shlinkio\Shlink\Core\Entity\ShortUrl; use Shlinkio\Shlink\Core\Entity\ShortUrl;
use Shlinkio\Shlink\Core\Exception\InvalidShortCodeException; 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\Repository\ShortUrlRepository;
use Shlinkio\Shlink\Core\Util\TagManagerTrait; use Shlinkio\Shlink\Core\Util\TagManagerTrait;
use Zend\Paginator\Paginator; use Zend\Paginator\Paginator;
@ -61,11 +61,11 @@ class ShortUrlService implements ShortUrlServiceInterface
/** /**
* @param string $shortCode * @param string $shortCode
* @param ShortCodeMeta $shortCodeMeta * @param ShortUrlMeta $shortCodeMeta
* @return ShortUrl * @return ShortUrl
* @throws InvalidShortCodeException * @throws InvalidShortCodeException
*/ */
public function updateMetadataByShortCode(string $shortCode, ShortCodeMeta $shortCodeMeta): ShortUrl public function updateMetadataByShortCode(string $shortCode, ShortUrlMeta $shortCodeMeta): ShortUrl
{ {
$shortUrl = $this->findByShortCode($shortCode); $shortUrl = $this->findByShortCode($shortCode);
if ($shortCodeMeta->hasValidSince()) { if ($shortCodeMeta->hasValidSince()) {

View File

@ -5,7 +5,7 @@ namespace Shlinkio\Shlink\Core\Service;
use Shlinkio\Shlink\Core\Entity\ShortUrl; use Shlinkio\Shlink\Core\Entity\ShortUrl;
use Shlinkio\Shlink\Core\Exception\InvalidShortCodeException; use Shlinkio\Shlink\Core\Exception\InvalidShortCodeException;
use Shlinkio\Shlink\Core\Model\ShortCodeMeta; use Shlinkio\Shlink\Core\Model\ShortUrlMeta;
use Zend\Paginator\Paginator; use Zend\Paginator\Paginator;
interface ShortUrlServiceInterface interface ShortUrlServiceInterface
@ -29,9 +29,9 @@ interface ShortUrlServiceInterface
/** /**
* @param string $shortCode * @param string $shortCode
* @param ShortCodeMeta $shortCodeMeta * @param ShortUrlMeta $shortCodeMeta
* @return ShortUrl * @return ShortUrl
* @throws InvalidShortCodeException * @throws InvalidShortCodeException
*/ */
public function updateMetadataByShortCode(string $shortCode, ShortCodeMeta $shortCodeMeta): ShortUrl; public function updateMetadataByShortCode(string $shortCode, ShortUrlMeta $shortCodeMeta): ShortUrl;
} }

View File

@ -10,8 +10,11 @@ use Prophecy\Argument;
use Prophecy\Prophecy\ObjectProphecy; use Prophecy\Prophecy\ObjectProphecy;
use Shlinkio\Shlink\Core\Entity\ShortUrl; use Shlinkio\Shlink\Core\Entity\ShortUrl;
use Shlinkio\Shlink\Core\Entity\Tag; 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\Repository\ShortUrlRepository;
use Shlinkio\Shlink\Core\Service\ShortUrlService; use Shlinkio\Shlink\Core\Service\ShortUrlService;
use Shlinkio\Shlink\Core\Validation\ShortUrlMetaInputFilter;
class ShortUrlServiceTest extends TestCase class ShortUrlServiceTest extends TestCase
{ {
@ -55,7 +58,6 @@ class ShortUrlServiceTest extends TestCase
/** /**
* @test * @test
* @expectedException \Shlinkio\Shlink\Core\Exception\InvalidShortCodeException
*/ */
public function exceptionIsThrownWhenSettingTagsOnInvalidShortcode() public function exceptionIsThrownWhenSettingTagsOnInvalidShortcode()
{ {
@ -65,6 +67,7 @@ class ShortUrlServiceTest extends TestCase
->shouldBeCalledTimes(1); ->shouldBeCalledTimes(1);
$this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal()); $this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal());
$this->expectException(InvalidShortCodeException::class);
$this->service->setTagsByShortCode($shortCode); $this->service->setTagsByShortCode($shortCode);
} }
@ -88,4 +91,32 @@ class ShortUrlServiceTest extends TestCase
$this->service->setTagsByShortCode($shortCode, ['foo', 'bar']); $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();
}
} }