Create ShortUrlVisitsDeleterTest

This commit is contained in:
Alejandro Celaya 2023-05-14 12:32:54 +02:00
parent ffc0555c7c
commit 69ff7de481
3 changed files with 59 additions and 0 deletions

View File

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\Core\ShortUrl;
use Shlinkio\Shlink\Core\Exception\ShortUrlNotFoundException;
use Shlinkio\Shlink\Core\Model\BulkDeleteResult;
use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlIdentifier;
use Shlinkio\Shlink\Core\Visit\Repository\VisitDeleterRepositoryInterface;
@ -17,6 +18,9 @@ class ShortUrlVisitsDeleter implements ShortUrlVisitsDeleterInterface
) {
}
/**
* @throws ShortUrlNotFoundException
*/
public function deleteShortUrlVisits(ShortUrlIdentifier $identifier, ?ApiKey $apiKey): BulkDeleteResult
{
$this->resolver->resolveShortUrl($identifier, $apiKey);

View File

@ -4,11 +4,15 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\Core\ShortUrl;
use Shlinkio\Shlink\Core\Exception\ShortUrlNotFoundException;
use Shlinkio\Shlink\Core\Model\BulkDeleteResult;
use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlIdentifier;
use Shlinkio\Shlink\Rest\Entity\ApiKey;
interface ShortUrlVisitsDeleterInterface
{
/**
* @throws ShortUrlNotFoundException
*/
public function deleteShortUrlVisits(ShortUrlIdentifier $identifier, ?ApiKey $apiKey): BulkDeleteResult;
}

View File

@ -0,0 +1,51 @@
<?php
declare(strict_types=1);
namespace ShlinkioTest\Shlink\Core\ShortUrl;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlIdentifier;
use Shlinkio\Shlink\Core\ShortUrl\ShortUrlResolverInterface;
use Shlinkio\Shlink\Core\ShortUrl\ShortUrlVisitsDeleter;
use Shlinkio\Shlink\Core\Visit\Repository\VisitDeleterRepositoryInterface;
class ShortUrlVisitsDeleterTest extends TestCase
{
private ShortUrlVisitsDeleter $deleter;
private MockObject & VisitDeleterRepositoryInterface $repository;
private MockObject & ShortUrlResolverInterface $resolver;
protected function setUp(): void
{
$this->repository = $this->createMock(VisitDeleterRepositoryInterface::class);
$this->resolver = $this->createMock(ShortUrlResolverInterface::class);
$this->deleter = new ShortUrlVisitsDeleter($this->repository, $this->resolver);
}
#[Test, DataProvider('provideVisitsCounts')]
public function returnsDeletedVisitsFromRepo(int $visitsCount): void
{
$identifier = ShortUrlIdentifier::fromShortCodeAndDomain('');
$this->resolver->expects($this->once())->method('resolveShortUrl')->with($identifier, null);
$this->repository->expects($this->once())->method('deleteShortUrlVisits')->with($identifier, null)->willReturn(
$visitsCount,
);
$result = $this->deleter->deleteShortUrlVisits($identifier, null);
self::assertEquals($visitsCount, $result->affectedItems);
}
public static function provideVisitsCounts(): iterable
{
yield '45' => [45];
yield '5000' => [5000];
yield '0' => [0];
}
}