Update to PHPUnit 12

This commit is contained in:
Alejandro Celaya
2025-02-13 10:35:58 +01:00
parent 48f910aaaa
commit 65c01034ff
16 changed files with 62 additions and 101 deletions

View File

@@ -79,9 +79,7 @@ class QrCodeActionTest extends TestCase
string $expectedContentType,
): void {
$code = 'abc123';
$this->urlResolver->method('resolveEnabledShortUrl')->with(
ShortUrlIdentifier::fromShortCodeAndDomain($code, ''),
)->willReturn(ShortUrl::createFake());
$this->urlResolver->method('resolveEnabledShortUrl')->willReturn(ShortUrl::createFake());
$handler = $this->createMock(RequestHandlerInterface::class);
$req = (new ServerRequest())->withAttribute('shortCode', $code)->withQueryParams($query);
@@ -109,9 +107,7 @@ class QrCodeActionTest extends TestCase
int $expectedSize,
): void {
$code = 'abc123';
$this->urlResolver->method('resolveEnabledShortUrl')->with(
ShortUrlIdentifier::fromShortCodeAndDomain($code, ''),
)->willReturn(ShortUrl::createFake());
$this->urlResolver->method('resolveEnabledShortUrl')->willReturn(ShortUrl::createFake());
$handler = $this->createMock(RequestHandlerInterface::class);
$resp = $this->action($defaultOptions)->process($req->withAttribute('shortCode', $code), $handler);
@@ -199,9 +195,7 @@ class QrCodeActionTest extends TestCase
->withQueryParams(['size' => 250, 'roundBlockSize' => $roundBlockSize])
->withAttribute('shortCode', $code);
$this->urlResolver->method('resolveEnabledShortUrl')->with(
ShortUrlIdentifier::fromShortCodeAndDomain($code, ''),
)->willReturn(ShortUrl::withLongUrl('https://shlink.io'));
$this->urlResolver->method('resolveEnabledShortUrl')->willReturn(ShortUrl::withLongUrl('https://shlink.io'));
$handler = $this->createMock(RequestHandlerInterface::class);
$resp = $this->action($defaultOptions)->process($req, $handler);
@@ -242,9 +236,7 @@ class QrCodeActionTest extends TestCase
->withQueryParams(['color' => $queryColor])
->withAttribute('shortCode', $code);
$this->urlResolver->method('resolveEnabledShortUrl')->with(
ShortUrlIdentifier::fromShortCodeAndDomain($code),
)->willReturn(ShortUrl::withLongUrl('https://shlink.io'));
$this->urlResolver->method('resolveEnabledShortUrl')->willReturn(ShortUrl::withLongUrl('https://shlink.io'));
$handler = $this->createMock(RequestHandlerInterface::class);
$resp = $this->action(
@@ -306,9 +298,7 @@ class QrCodeActionTest extends TestCase
->withAttribute('shortCode', $code)
->withQueryParams($query);
$this->urlResolver->method('resolveEnabledShortUrl')->with(
ShortUrlIdentifier::fromShortCodeAndDomain($code),
)->willReturn(ShortUrl::withLongUrl('https://shlink.io'));
$this->urlResolver->method('resolveEnabledShortUrl')->willReturn(ShortUrl::withLongUrl('https://shlink.io'));
$handler = $this->createMock(RequestHandlerInterface::class);
$resp = $this->action(new QrCodeOptions(size: 250, logoUrl: $logoUrl))->process($req, $handler);

View File

@@ -37,7 +37,7 @@ class RedirectActionTest extends TestCase
$this->redirectRespHelper = $this->createMock(RedirectResponseHelperInterface::class);
$redirectBuilder = $this->createMock(ShortUrlRedirectionBuilderInterface::class);
$redirectBuilder->method('buildShortUrlRedirect')->withAnyParameters()->willReturn(self::LONG_URL);
$redirectBuilder->method('buildShortUrlRedirect')->willReturn(self::LONG_URL);
$this->action = new RedirectAction(
$this->urlResolver,

View File

@@ -43,7 +43,7 @@ class GeolocationDbUpdaterTest extends TestCase
$this->dbUpdater = $this->createMock(DbUpdaterInterface::class);
$this->lock = $this->createMock(Lock\SharedLockInterface::class);
$this->lock->method('acquire')->with($this->isTrue())->willReturn(true);
$this->lock->method('acquire')->willReturn(true);
$this->em = $this->createMock(EntityManagerInterface::class);
$this->repo = $this->createMock(EntityRepository::class);
@@ -291,7 +291,7 @@ class GeolocationDbUpdaterTest extends TestCase
private function geolocationDbUpdater(TrackingOptions|null $options = null): GeolocationDbUpdater
{
$locker = $this->createMock(Lock\LockFactory::class);
$locker->method('createLock')->with($this->isString())->willReturn($this->lock);
$locker->method('createLock')->willReturn($this->lock);
return new GeolocationDbUpdater($this->dbUpdater, $locker, $options ?? new TrackingOptions(), $this->em, 3);
}

View File

@@ -104,7 +104,7 @@ class ImportedLinksProcessorTest extends TestCase
];
$expectedCalls = count($urls);
$this->em->method('getRepository')->with(ShortUrl::class)->willReturn($this->repo);
$this->em->method('getRepository')->willReturn($this->repo);
$this->repo->expects($this->exactly($expectedCalls))->method('findOneByImportedUrl')->willReturn(null);
$this->shortCodeHelper->expects($this->exactly($expectedCalls))
->method('ensureShortCodeUniqueness')
@@ -138,7 +138,7 @@ class ImportedLinksProcessorTest extends TestCase
new ImportedShlinkUrl(ImportSource::BITLY, 'https://baz', [], Chronos::now(), null, 'baz', null),
];
$this->em->method('getRepository')->with(ShortUrl::class)->willReturn($this->repo);
$this->em->method('getRepository')->willReturn($this->repo);
$this->repo->expects($this->exactly(3))->method('findOneByImportedUrl')->willReturn(null);
$this->shortCodeHelper->expects($this->exactly(3))->method('ensureShortCodeUniqueness')->willReturn(true);
$this->em->expects($this->exactly(3))->method('persist')->with(
@@ -167,7 +167,7 @@ class ImportedLinksProcessorTest extends TestCase
new ImportedShlinkUrl(ImportSource::BITLY, 'https://baz3', [], Chronos::now(), null, 'baz3', null),
];
$this->em->method('getRepository')->with(ShortUrl::class)->willReturn($this->repo);
$this->em->method('getRepository')->willReturn($this->repo);
$this->repo->expects($this->exactly(count($urls)))->method('findOneByImportedUrl')->willReturnCallback(
fn (ImportedShlinkUrl $url): ShortUrl|null => contains(
$url->longUrl,
@@ -195,7 +195,7 @@ class ImportedLinksProcessorTest extends TestCase
new ImportedShlinkUrl(ImportSource::BITLY, 'https://baz3', [], Chronos::now(), null, 'baz3', 'bar'),
];
$this->em->method('getRepository')->with(ShortUrl::class)->willReturn($this->repo);
$this->em->method('getRepository')->willReturn($this->repo);
$this->repo->expects($this->exactly(count($urls)))->method('findOneByImportedUrl')->willReturn(null);
$this->shortCodeHelper->expects($this->exactly(7))->method('ensureShortCodeUniqueness')->willReturnCallback(
fn ($_, bool $hasCustomSlug) => ! $hasCustomSlug,
@@ -219,7 +219,7 @@ class ImportedLinksProcessorTest extends TestCase
int $amountOfPersistedVisits,
ShortUrl|null $foundShortUrl,
): void {
$this->em->method('getRepository')->with(ShortUrl::class)->willReturn($this->repo);
$this->em->method('getRepository')->willReturn($this->repo);
$this->repo->expects($this->once())->method('findOneByImportedUrl')->willReturn($foundShortUrl);
$this->shortCodeHelper->expects($this->exactly($foundShortUrl === null ? 1 : 0))
->method('ensureShortCodeUniqueness')
@@ -276,7 +276,7 @@ class ImportedLinksProcessorTest extends TestCase
#[Test, DataProvider('provideFoundShortUrls')]
public function visitsArePersistedWithProperShortUrl(ShortUrl $originalShortUrl, ShortUrl|null $foundShortUrl): void
{
$this->em->method('getRepository')->with(ShortUrl::class)->willReturn($this->repo);
$this->em->method('getRepository')->willReturn($this->repo);
$this->repo->expects($this->once())->method('findOneByImportedUrl')->willReturn($originalShortUrl);
if (!$originalShortUrl->getId()) {
$this->em->expects($this->never())->method('find');

View File

@@ -116,7 +116,7 @@ class PersistenceShortUrlRelationResolverTest extends TestCase
{
$repo = $this->createMock(DomainRepository::class);
$repo->expects($this->exactly(3))->method('findOneBy')->with($this->isArray())->willReturn(null);
$this->em->method('getRepository')->with(Domain::class)->willReturn($repo);
$this->em->method('getRepository')->willReturn($repo);
$authority = 'foo.com';
$domain1 = $this->resolver->resolveDomain($authority);
@@ -135,7 +135,7 @@ class PersistenceShortUrlRelationResolverTest extends TestCase
{
$tagRepo = $this->createMock(TagRepository::class);
$tagRepo->expects($this->exactly(6))->method('findOneBy')->with($this->isArray())->willReturn(null);
$this->em->method('getRepository')->with(Tag::class)->willReturn($tagRepo);
$this->em->method('getRepository')->willReturn($tagRepo);
$tags = ['foo', 'bar'];
[$foo1, $bar1] = $this->resolver->resolveTags($tags);

View File

@@ -5,7 +5,7 @@ declare(strict_types=1);
namespace ShlinkioTest\Shlink\Core\ShortUrl;
use Cake\Chronos\Chronos;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use Laminas\ServiceManager\Exception\ServiceNotFoundException;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
@@ -24,7 +24,7 @@ use Shlinkio\Shlink\Core\ShortUrl\UrlShortener;
class UrlShortenerTest extends TestCase
{
private UrlShortener $urlShortener;
private MockObject & EntityManager $em;
private MockObject & EntityManagerInterface $em;
private MockObject & ShortUrlTitleResolutionHelperInterface $titleResolutionHelper;
private MockObject & ShortCodeUniquenessHelperInterface $shortCodeHelper;
private MockObject & EventDispatcherInterface $dispatcher;
@@ -35,12 +35,9 @@ class UrlShortenerTest extends TestCase
$this->titleResolutionHelper = $this->createMock(ShortUrlTitleResolutionHelperInterface::class);
$this->shortCodeHelper = $this->createMock(ShortCodeUniquenessHelperInterface::class);
// FIXME Should use the interface, but it doe snot define wrapInTransaction explicitly
$this->em = $this->createMock(EntityManager::class);
$this->em = $this->createMock(EntityManagerInterface::class);
$this->em->method('persist')->willReturnCallback(fn (ShortUrl $shortUrl) => $shortUrl->setId('10'));
$this->em->method('wrapInTransaction')->with($this->isCallable())->willReturnCallback(
fn (callable $callback) => $callback(),
);
$this->em->method('wrapInTransaction')->willReturnCallback(fn (callable $callback) => $callback());
$this->dispatcher = $this->createMock(EventDispatcherInterface::class);
$this->repo = $this->createMock(ShortUrlRepositoryInterface::class);

View File

@@ -108,14 +108,8 @@ class VisitsStatsHelperTest extends TestCase
range(0, 1),
);
$repo2 = $this->createMock(VisitRepository::class);
$repo2->method('findVisitsByShortCode')->with(
$identifier,
$this->isInstanceOf(VisitsListFiltering::class),
)->willReturn($list);
$repo2->method('countVisitsByShortCode')->with(
$identifier,
$this->isInstanceOf(VisitsCountFiltering::class),
)->willReturn(1);
$repo2->method('findVisitsByShortCode')->willReturn($list);
$repo2->method('countVisitsByShortCode')->willReturn(1);
$this->em->expects($this->exactly(2))->method('getRepository')->willReturnMap([
[ShortUrl::class, $repo],
@@ -168,10 +162,8 @@ class VisitsStatsHelperTest extends TestCase
range(0, 1),
);
$repo2 = $this->createMock(VisitRepository::class);
$repo2->method('findVisitsByTag')->with($tag, $this->isInstanceOf(VisitsListFiltering::class))->willReturn(
$list,
);
$repo2->method('countVisitsByTag')->with($tag, $this->isInstanceOf(VisitsCountFiltering::class))->willReturn(1);
$repo2->method('findVisitsByTag')->willReturn($list);
$repo2->method('countVisitsByTag')->willReturn(1);
$this->em->expects($this->exactly(2))->method('getRepository')->willReturnMap([
[Tag::class, $repo],
@@ -209,14 +201,8 @@ class VisitsStatsHelperTest extends TestCase
range(0, 1),
);
$repo2 = $this->createMock(VisitRepository::class);
$repo2->method('findVisitsByDomain')->with(
$domain,
$this->isInstanceOf(VisitsListFiltering::class),
)->willReturn($list);
$repo2->method('countVisitsByDomain')->with(
$domain,
$this->isInstanceOf(VisitsCountFiltering::class),
)->willReturn(1);
$repo2->method('findVisitsByDomain')->willReturn($list);
$repo2->method('countVisitsByDomain')->willReturn(1);
$this->em->expects($this->exactly(2))->method('getRepository')->willReturnMap([
[Domain::class, $repo],
@@ -239,14 +225,8 @@ class VisitsStatsHelperTest extends TestCase
range(0, 1),
);
$repo2 = $this->createMock(VisitRepository::class);
$repo2->method('findVisitsByDomain')->with(
Domain::DEFAULT_AUTHORITY,
$this->isInstanceOf(VisitsListFiltering::class),
)->willReturn($list);
$repo2->method('countVisitsByDomain')->with(
Domain::DEFAULT_AUTHORITY,
$this->isInstanceOf(VisitsCountFiltering::class),
)->willReturn(1);
$repo2->method('findVisitsByDomain')->willReturn($list);
$repo2->method('countVisitsByDomain')->willReturn(1);
$this->em->expects($this->exactly(2))->method('getRepository')->willReturnMap([
[Domain::class, $repo],