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\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;

View File

@ -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()) {

View File

@ -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;
}

View File

@ -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();
}
}