Skip to content

Commit

Permalink
Merge pull request #2358 from acelaya-forks/feature/phpunit-12
Browse files Browse the repository at this point in the history
Update to PHPUnit 12
  • Loading branch information
acelaya authored Feb 13, 2025
2 parents 48f910a + 65c0103 commit 81e07bf
Show file tree
Hide file tree
Showing 16 changed files with 62 additions and 101 deletions.
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,16 @@
"require-dev": {
"devizzent/cebe-php-openapi": "^1.1.2",
"devster/ubench": "^2.1",
"phpstan/phpstan": "^2.0",
"phpstan/phpstan": "^2.1",
"phpstan/phpstan-doctrine": "^2.0",
"phpstan/phpstan-phpunit": "^2.0",
"phpstan/phpstan-symfony": "^2.0",
"phpunit/php-code-coverage": "^11.0",
"phpunit/phpcov": "^10.0",
"phpunit/phpunit": "^11.5",
"phpunit/php-code-coverage": "^12.0",
"phpunit/phpcov": "^11.0",
"phpunit/phpunit": "^12.0",
"roave/security-advisories": "dev-master",
"shlinkio/php-coding-standard": "~2.4.0",
"shlinkio/shlink-test-utils": "^4.2",
"shlinkio/shlink-test-utils": "^4.3.1",
"symfony/var-dumper": "^7.2",
"veewee/composer-run-parallel": "^1.4"
},
Expand Down
3 changes: 1 addition & 2 deletions module/CLI/test/Command/Api/GenerateKeyCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use Shlinkio\Shlink\Rest\Entity\ApiKey;
use Shlinkio\Shlink\Rest\Service\ApiKeyServiceInterface;
use ShlinkioTest\Shlink\CLI\Util\CliTestUtils;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Tester\CommandTester;

class GenerateKeyCommandTest extends TestCase
Expand All @@ -27,7 +26,7 @@ protected function setUp(): void
{
$this->apiKeyService = $this->createMock(ApiKeyServiceInterface::class);
$roleResolver = $this->createMock(RoleResolverInterface::class);
$roleResolver->method('determineRoles')->with($this->isInstanceOf(InputInterface::class))->willReturn([]);
$roleResolver->method('determineRoles')->willReturn([]);

$command = new GenerateKeyCommand($this->apiKeyService, $roleResolver);
$this->commandTester = CliTestUtils::testerForCommand($command);
Expand Down
8 changes: 4 additions & 4 deletions module/CLI/test/Command/Db/CreateDatabaseCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ protected function setUp(): void
{
$locker = $this->createMock(LockFactory::class);
$lock = $this->createMock(SharedLockInterface::class);
$lock->method('acquire')->withAnyParameters()->willReturn(true);
$locker->method('createLock')->withAnyParameters()->willReturn($lock);
$lock->method('acquire')->willReturn(true);
$locker->method('createLock')->willReturn($lock);

$phpExecutableFinder = $this->createMock(PhpExecutableFinder::class);
$phpExecutableFinder->method('find')->with($this->isFalse())->willReturn('/usr/local/bin/php');
$phpExecutableFinder->method('find')->willReturn('/usr/local/bin/php');

$this->processHelper = $this->createMock(ProcessRunnerInterface::class);
$this->schemaManager = $this->createMock(AbstractSchemaManager::class);
Expand All @@ -60,7 +60,7 @@ protected function setUp(): void
$em->method('getMetadataFactory')->willReturn($this->metadataFactory);

$noDbNameConn = $this->createMock(Connection::class);
$noDbNameConn->method('createSchemaManager')->withAnyParameters()->willReturn($this->schemaManager);
$noDbNameConn->method('createSchemaManager')->willReturn($this->schemaManager);

$command = new CreateDatabaseCommand($locker, $this->processHelper, $phpExecutableFinder, $em, $noDbNameConn);
$this->commandTester = CliTestUtils::testerForCommand($command);
Expand Down
6 changes: 3 additions & 3 deletions module/CLI/test/Command/Db/MigrateDatabaseCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ protected function setUp(): void
{
$locker = $this->createMock(LockFactory::class);
$lock = $this->createMock(SharedLockInterface::class);
$lock->method('acquire')->withAnyParameters()->willReturn(true);
$locker->method('createLock')->withAnyParameters()->willReturn($lock);
$lock->method('acquire')->willReturn(true);
$locker->method('createLock')->willReturn($lock);

$phpExecutableFinder = $this->createMock(PhpExecutableFinder::class);
$phpExecutableFinder->method('find')->with($this->isFalse())->willReturn('/usr/local/bin/php');
$phpExecutableFinder->method('find')->willReturn('/usr/local/bin/php');

$this->processHelper = $this->createMock(ProcessRunnerInterface::class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function providingNonUniqueSlugOutputsError(): void
$this->urlShortener->expects($this->once())->method('shorten')->withAnyParameters()->willThrowException(
NonUniqueSlugException::fromSlug('my-slug'),
);
$this->stringifier->method('stringify')->with($this->isInstanceOf(ShortUrl::class))->willReturn('');
$this->stringifier->method('stringify')->willReturn('');

$this->commandTester->execute(['longUrl' => 'http://domain.com/invalid', '--custom-slug' => 'my-slug']);
$output = $this->commandTester->getDisplay();
Expand Down Expand Up @@ -112,7 +112,7 @@ public function properlyProcessesProvidedDomain(array $input, string|null $expec
return true;
}),
)->willReturn(UrlShorteningResult::withoutErrorOnEventDispatching(ShortUrl::createFake()));
$this->stringifier->method('stringify')->with($this->isInstanceOf(ShortUrl::class))->willReturn('');
$this->stringifier->method('stringify')->willReturn('');

$input['longUrl'] = 'http://domain.com/foo/bar';
$this->commandTester->execute($input);
Expand All @@ -139,7 +139,7 @@ public function urlValidationHasExpectedValueBasedOnProvidedFlags(
return true;
}),
)->willReturn(UrlShorteningResult::withoutErrorOnEventDispatching($shortUrl));
$this->stringifier->method('stringify')->with($this->isInstanceOf(ShortUrl::class))->willReturn('');
$this->stringifier->method('stringify')->willReturn('');

$options['longUrl'] = 'http://domain.com/foo/bar';
$this->commandTester->execute($options);
Expand Down
30 changes: 15 additions & 15 deletions module/CLI/test/Command/Visit/LocateVisitsCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ protected function setUp(): void

$locker = $this->createMock(Lock\LockFactory::class);
$this->lock = $this->createMock(Lock\SharedLockInterface::class);
$locker->method('createLock')->with($this->isString(), 600.0, false)->willReturn($this->lock);
$locker->method('createLock')->willReturn($this->lock);

$command = new LocateVisitsCommand($this->visitService, $this->visitToLocation, $locker);

Expand All @@ -67,7 +67,7 @@ public function expectedSetOfVisitsIsProcessedBasedOnArgs(
$location = VisitLocation::fromGeolocation(Location::empty());
$mockMethodBehavior = $this->invokeHelperMethods($visit, $location);

$this->lock->method('acquire')->with($this->isFalse())->willReturn(true);
$this->lock->method('acquire')->willReturn(true);
$this->visitService->expects($this->exactly($expectedUnlocatedCalls))
->method('locateUnlocatedVisits')
->withAnyParameters()
Expand All @@ -83,7 +83,7 @@ public function expectedSetOfVisitsIsProcessedBasedOnArgs(
$this->visitToLocation->expects(
$this->exactly($expectedUnlocatedCalls + $expectedEmptyCalls + $expectedAllCalls),
)->method('resolveVisitLocation')->withAnyParameters()->willReturn(Location::emptyInstance());
$this->downloadDbCommand->method('run')->withAnyParameters()->willReturn(ExitCode::EXIT_SUCCESS);
$this->downloadDbCommand->method('run')->willReturn(ExitCode::EXIT_SUCCESS);

$this->commandTester->setInputs(['y']);
$this->commandTester->execute($args);
Expand All @@ -108,15 +108,15 @@ public static function provideArgs(): iterable
public function localhostAndEmptyAddressesAreIgnored(IpCannotBeLocatedException $e, string $message): void
{
$visit = Visit::forValidShortUrl(ShortUrl::createFake(), Visitor::empty());
$location = VisitLocation::fromGeolocation(Location::emptyInstance());
$location = VisitLocation::fromGeolocation(Location::empty());

$this->lock->method('acquire')->with($this->isFalse())->willReturn(true);
$this->lock->method('acquire')->willReturn(true);
$this->visitService->expects($this->once())
->method('locateUnlocatedVisits')
->withAnyParameters()
->willReturnCallback($this->invokeHelperMethods($visit, $location));
$this->visitToLocation->expects($this->once())->method('resolveVisitLocation')->willThrowException($e);
$this->downloadDbCommand->method('run')->withAnyParameters()->willReturn(ExitCode::EXIT_SUCCESS);
$this->downloadDbCommand->method('run')->willReturn(ExitCode::EXIT_SUCCESS);

$this->commandTester->execute([], ['verbosity' => OutputInterface::VERBOSITY_VERBOSE]);

Expand All @@ -137,15 +137,15 @@ public function errorWhileLocatingIpIsDisplayed(): void
$visit = Visit::forValidShortUrl(ShortUrl::createFake(), Visitor::fromParams(remoteAddress: '1.2.3.4'));
$location = VisitLocation::fromGeolocation(Location::emptyInstance());

$this->lock->method('acquire')->with($this->isFalse())->willReturn(true);
$this->lock->method('acquire')->willReturn(true);
$this->visitService->expects($this->once())
->method('locateUnlocatedVisits')
->withAnyParameters()
->willReturnCallback($this->invokeHelperMethods($visit, $location));
$this->visitToLocation->expects($this->once())->method('resolveVisitLocation')->willThrowException(
IpCannotBeLocatedException::forError(WrongIpException::fromIpAddress('1.2.3.4')),
);
$this->downloadDbCommand->method('run')->withAnyParameters()->willReturn(ExitCode::EXIT_SUCCESS);
$this->downloadDbCommand->method('run')->willReturn(ExitCode::EXIT_SUCCESS);

$this->commandTester->execute([], ['verbosity' => OutputInterface::VERBOSITY_VERBOSE]);

Expand All @@ -165,11 +165,11 @@ private function invokeHelperMethods(Visit $visit, VisitLocation $location): cal
#[Test]
public function noActionIsPerformedIfLockIsAcquired(): void
{
$this->lock->method('acquire')->with($this->isFalse())->willReturn(false);
$this->lock->method('acquire')->willReturn(false);

$this->visitService->expects($this->never())->method('locateUnlocatedVisits');
$this->visitToLocation->expects($this->never())->method('resolveVisitLocation');
$this->downloadDbCommand->method('run')->withAnyParameters()->willReturn(ExitCode::EXIT_SUCCESS);
$this->downloadDbCommand->method('run')->willReturn(ExitCode::EXIT_SUCCESS);

$this->commandTester->execute([], ['verbosity' => OutputInterface::VERBOSITY_VERBOSE]);
$output = $this->commandTester->getDisplay();
Expand All @@ -183,8 +183,8 @@ public function noActionIsPerformedIfLockIsAcquired(): void
#[Test]
public function showsProperMessageWhenGeoLiteUpdateFails(): void
{
$this->lock->method('acquire')->with($this->isFalse())->willReturn(true);
$this->downloadDbCommand->method('run')->withAnyParameters()->willReturn(ExitCode::EXIT_FAILURE);
$this->lock->method('acquire')->willReturn(true);
$this->downloadDbCommand->method('run')->willReturn(ExitCode::EXIT_FAILURE);
$this->visitService->expects($this->never())->method('locateUnlocatedVisits');

$this->commandTester->execute([]);
Expand All @@ -196,8 +196,8 @@ public function showsProperMessageWhenGeoLiteUpdateFails(): void
#[Test]
public function providingAllFlagOnItsOwnDisplaysNotice(): void
{
$this->lock->method('acquire')->with($this->isFalse())->willReturn(true);
$this->downloadDbCommand->method('run')->withAnyParameters()->willReturn(ExitCode::EXIT_SUCCESS);
$this->lock->method('acquire')->willReturn(true);
$this->downloadDbCommand->method('run')->willReturn(ExitCode::EXIT_SUCCESS);

$this->commandTester->execute(['--all' => true]);
$output = $this->commandTester->getDisplay();
Expand All @@ -208,7 +208,7 @@ public function providingAllFlagOnItsOwnDisplaysNotice(): void
#[Test, DataProvider('provideAbortInputs')]
public function processingAllCancelsCommandIfUserDoesNotActivelyAgreeToConfirmation(array $inputs): void
{
$this->downloadDbCommand->method('run')->withAnyParameters()->willReturn(ExitCode::EXIT_SUCCESS);
$this->downloadDbCommand->method('run')->willReturn(ExitCode::EXIT_SUCCESS);

$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Execution aborted');
Expand Down
3 changes: 0 additions & 3 deletions module/CLI/test/Util/CliTestUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,8 @@ public static function createCommandMock(string $name): MockObject & Command
$command = $generator->testDouble(
Command::class,
mockObject: true,
markAsMockObject: true,
callOriginalConstructor: false,
callOriginalClone: false,
cloneArguments: false,
allowMockingUnknownTypes: false,
);
$command->method('getName')->willReturn($name);
$command->method('isEnabled')->willReturn(true);
Expand Down
4 changes: 2 additions & 2 deletions module/CLI/test/Util/ProcessRunnerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ protected function setUp(): void
$this->helper = $this->createMock(ProcessHelper::class);
$this->formatter = $this->createMock(DebugFormatterHelper::class);
$helperSet = $this->createMock(HelperSet::class);
$helperSet->method('get')->with('debug_formatter')->willReturn($this->formatter);
$this->helper->method('getHelperSet')->with()->willReturn($helperSet);
$helperSet->method('get')->willReturn($this->formatter);
$this->helper->method('getHelperSet')->willReturn($helperSet);
$this->process = $this->createMock(Process::class);
$this->output = $this->createMock(OutputInterface::class);

Expand Down
20 changes: 5 additions & 15 deletions module/Core/test/Action/QrCodeActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,7 @@ public function imageIsReturnedWithExpectedContentTypeBasedOnProvidedFormat(
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);

Expand Down Expand Up @@ -109,9 +107,7 @@ public function imageIsReturnedWithExpectedSize(
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);
Expand Down Expand Up @@ -199,9 +195,7 @@ public function imageCanRemoveExtraMarginWhenBlockRoundIsDisabled(
->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);
Expand Down Expand Up @@ -242,9 +236,7 @@ public function properColorsAreUsed(string|null $queryColor, string|null $option
->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(
Expand Down Expand Up @@ -306,9 +298,7 @@ public function logoIsAddedToQrCodeIfOptionIsDefined(array $query, string $expec
->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);
Expand Down
2 changes: 1 addition & 1 deletion module/Core/test/Action/RedirectActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ protected function setUp(): void
$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,
Expand Down
4 changes: 2 additions & 2 deletions module/Core/test/Geolocation/GeolocationDbUpdaterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ protected function setUp(): void
$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);
Expand Down Expand Up @@ -291,7 +291,7 @@ public static function provideTrackingOptions(): iterable
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);
}
Expand Down
Loading

0 comments on commit 81e07bf

Please sign in to comment.