From 85e18a475471d779b0ce972de33c1e8cb4fb60dd Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Mon, 24 Oct 2022 20:11:25 +0200 Subject: [PATCH] Fixed all phpstan inspections on tests --- module/CLI/test/ApiKey/RoleResolverTest.php | 10 ++++++++-- module/CLI/test/Command/Api/ListKeysCommandTest.php | 10 ++++++++-- .../Command/ShortUrl/CreateShortUrlCommandTest.php | 5 ++++- module/CLI/test/GeoLite/GeolocationDbUpdaterTest.php | 2 -- module/Core/test/Action/QrCodeActionTest.php | 7 +++++-- module/Core/test/Domain/DomainServiceTest.php | 7 +++++-- .../Core/test/Visit/Geolocation/VisitLocatorTest.php | 7 ------- .../Adapter/NonOrphanVisitsPaginatorAdapterTest.php | 2 ++ .../Adapter/OrphanVisitsPaginatorAdapterTest.php | 2 ++ module/Rest/test/ApiKey/Model/RoleDefinitionTest.php | 3 ++- 10 files changed, 36 insertions(+), 19 deletions(-) diff --git a/module/CLI/test/ApiKey/RoleResolverTest.php b/module/CLI/test/ApiKey/RoleResolverTest.php index af50e17e..9f2ec2aa 100644 --- a/module/CLI/test/ApiKey/RoleResolverTest.php +++ b/module/CLI/test/ApiKey/RoleResolverTest.php @@ -38,7 +38,7 @@ class RoleResolverTest extends TestCase ): void { $this->domainService->expects($this->exactly($expectedDomainCalls))->method('getOrCreate')->with( 'example.com', - )->willReturn(Domain::withAuthority('example.com')->setId('1')); + )->willReturn($this->domainWithId(Domain::withAuthority('example.com'))); $result = $this->resolver->determineRoles($input); @@ -47,7 +47,7 @@ class RoleResolverTest extends TestCase public function provideRoles(): iterable { - $domain = Domain::withAuthority('example.com')->setId('1'); + $domain = $this->domainWithId(Domain::withAuthority('example.com')); $buildInput = function (array $definition): InputInterface { $input = $this->createStub(InputInterface::class); $input->method('getOption')->willReturnMap( @@ -113,4 +113,10 @@ class RoleResolverTest extends TestCase $this->resolver->determineRoles($input); } + + private function domainWithId(Domain $domain): Domain + { + $domain->setId('1'); + return $domain; + } } diff --git a/module/CLI/test/Command/Api/ListKeysCommandTest.php b/module/CLI/test/Command/Api/ListKeysCommandTest.php index a0e7d833..f4101ec6 100644 --- a/module/CLI/test/Command/Api/ListKeysCommandTest.php +++ b/module/CLI/test/Command/Api/ListKeysCommandTest.php @@ -86,12 +86,12 @@ class ListKeysCommandTest extends TestCase $apiKey1 = ApiKey::create(), $apiKey2 = $this->apiKeyWithRoles([RoleDefinition::forAuthoredShortUrls()]), $apiKey3 = $this->apiKeyWithRoles( - [RoleDefinition::forDomain(Domain::withAuthority('example.com')->setId('1'))], + [RoleDefinition::forDomain($this->domainWithId(Domain::withAuthority('example.com')))], ), $apiKey4 = ApiKey::create(), $apiKey5 = $this->apiKeyWithRoles([ RoleDefinition::forAuthoredShortUrls(), - RoleDefinition::forDomain(Domain::withAuthority('example.com')->setId('1')), + RoleDefinition::forDomain($this->domainWithId(Domain::withAuthority('example.com'))), ]), $apiKey6 = ApiKey::create(), ], @@ -150,4 +150,10 @@ class ListKeysCommandTest extends TestCase return $apiKey; } + + private function domainWithId(Domain $domain): Domain + { + $domain->setId('1'); + return $domain; + } } diff --git a/module/CLI/test/Command/ShortUrl/CreateShortUrlCommandTest.php b/module/CLI/test/Command/ShortUrl/CreateShortUrlCommandTest.php index 16b85290..734089c9 100644 --- a/module/CLI/test/Command/ShortUrl/CreateShortUrlCommandTest.php +++ b/module/CLI/test/Command/ShortUrl/CreateShortUrlCommandTest.php @@ -37,7 +37,10 @@ class CreateShortUrlCommandTest extends TestCase $command = new CreateShortUrlCommand( $this->urlShortener, $this->stringifier, - new UrlShortenerOptions(domain: ['hostname' => self::DEFAULT_DOMAIN], defaultShortCodesLength: 5), + new UrlShortenerOptions( + domain: ['hostname' => self::DEFAULT_DOMAIN, 'schema' => ''], + defaultShortCodesLength: 5, + ), ); $this->commandTester = $this->testerForCommand($command); } diff --git a/module/CLI/test/GeoLite/GeolocationDbUpdaterTest.php b/module/CLI/test/GeoLite/GeolocationDbUpdaterTest.php index e1ebb4c1..8d6188e9 100644 --- a/module/CLI/test/GeoLite/GeolocationDbUpdaterTest.php +++ b/module/CLI/test/GeoLite/GeolocationDbUpdaterTest.php @@ -31,8 +31,6 @@ class GeolocationDbUpdaterTest extends TestCase { $this->dbUpdater = $this->createMock(DbUpdaterInterface::class); $this->geoLiteDbReader = $this->createMock(Reader::class); - $this->trackingOptions = new TrackingOptions(); - $this->lock = $this->createMock(Lock\LockInterface::class); $this->lock->method('acquire')->with($this->isTrue())->willReturn(true); } diff --git a/module/Core/test/Action/QrCodeActionTest.php b/module/Core/test/Action/QrCodeActionTest.php index 497fb009..89c105c0 100644 --- a/module/Core/test/Action/QrCodeActionTest.php +++ b/module/Core/test/Action/QrCodeActionTest.php @@ -115,8 +115,10 @@ class QrCodeActionTest extends TestCase $delegate = $this->createMock(RequestHandlerInterface::class); $resp = $this->action($defaultOptions)->process($req->withAttribute('shortCode', $code), $delegate); - [$size] = getimagesizefromstring($resp->getBody()->__toString()); + $result = getimagesizefromstring($resp->getBody()->__toString()); + self::assertNotFalse($result); + [$size] = $result; self::assertEquals($expectedSize, $size); } @@ -207,8 +209,9 @@ class QrCodeActionTest extends TestCase $resp = $this->action($defaultOptions)->process($req, $delegate); $image = imagecreatefromstring($resp->getBody()->__toString()); - $color = imagecolorat($image, 1, 1); + self::assertNotFalse($image); + $color = imagecolorat($image, 1, 1); self::assertEquals($color, $expectedColor); } diff --git a/module/Core/test/Domain/DomainServiceTest.php b/module/Core/test/Domain/DomainServiceTest.php index 4d4606b0..46d3e567 100644 --- a/module/Core/test/Domain/DomainServiceTest.php +++ b/module/Core/test/Domain/DomainServiceTest.php @@ -48,8 +48,10 @@ class DomainServiceTest extends TestCase { $default = DomainItem::forDefaultDomain('default.com', new EmptyNotFoundRedirectConfig()); $adminApiKey = ApiKey::create(); + $domain = Domain::withAuthority(''); + $domain->setId('123'); $domainSpecificApiKey = ApiKey::fromMeta( - ApiKeyMeta::withRoles(RoleDefinition::forDomain(Domain::withAuthority('')->setId('123'))), + ApiKeyMeta::withRoles(RoleDefinition::forDomain($domain)), ); yield 'empty list without API key' => [[], [$default], null]; @@ -147,7 +149,8 @@ class DomainServiceTest extends TestCase public function getOrCreateThrowsExceptionForApiKeysWithDomainRole(): void { $authority = 'example.com'; - $domain = Domain::withAuthority($authority)->setId('1'); + $domain = Domain::withAuthority($authority); + $domain->setId('1'); $apiKey = ApiKey::fromMeta(ApiKeyMeta::withRoles(RoleDefinition::forDomain($domain))); $repo = $this->createMock(DomainRepositoryInterface::class); $repo->method('findOneByAuthority')->with($authority, $apiKey)->willReturn(null); diff --git a/module/Core/test/Visit/Geolocation/VisitLocatorTest.php b/module/Core/test/Visit/Geolocation/VisitLocatorTest.php index a9ed7aa2..a39940ae 100644 --- a/module/Core/test/Visit/Geolocation/VisitLocatorTest.php +++ b/module/Core/test/Visit/Geolocation/VisitLocatorTest.php @@ -6,7 +6,6 @@ namespace ShlinkioTest\Shlink\Core\Visit\Geolocation; use Doctrine\ORM\EntityManager; use Exception; -use PHPUnit\Framework\Assert; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use Shlinkio\Shlink\Core\Exception\IpCannotBeLocatedException; @@ -19,10 +18,8 @@ use Shlinkio\Shlink\Core\Visit\Model\Visitor; use Shlinkio\Shlink\Core\Visit\Repository\VisitRepositoryInterface; use Shlinkio\Shlink\IpGeolocation\Model\Location; -use function array_shift; use function count; use function floor; -use function func_get_args; use function Functional\map; use function range; use function sprintf; @@ -72,10 +69,6 @@ class VisitLocatorTest extends TestCase public function onVisitLocated(VisitLocation $visitLocation, Visit $visit): void { - $args = func_get_args(); - - Assert::assertInstanceOf(VisitLocation::class, array_shift($args)); - Assert::assertInstanceOf(Visit::class, array_shift($args)); } }); } diff --git a/module/Core/test/Visit/Paginator/Adapter/NonOrphanVisitsPaginatorAdapterTest.php b/module/Core/test/Visit/Paginator/Adapter/NonOrphanVisitsPaginatorAdapterTest.php index 9c4a6e63..ec256eeb 100644 --- a/module/Core/test/Visit/Paginator/Adapter/NonOrphanVisitsPaginatorAdapterTest.php +++ b/module/Core/test/Visit/Paginator/Adapter/NonOrphanVisitsPaginatorAdapterTest.php @@ -45,6 +45,8 @@ class NonOrphanVisitsPaginatorAdapterTest extends TestCase } /** + * @param int<0, max> $limit + * @param int<0, max> $offset * @test * @dataProvider provideLimitAndOffset */ diff --git a/module/Core/test/Visit/Paginator/Adapter/OrphanVisitsPaginatorAdapterTest.php b/module/Core/test/Visit/Paginator/Adapter/OrphanVisitsPaginatorAdapterTest.php index 8981d05f..6b91a20b 100644 --- a/module/Core/test/Visit/Paginator/Adapter/OrphanVisitsPaginatorAdapterTest.php +++ b/module/Core/test/Visit/Paginator/Adapter/OrphanVisitsPaginatorAdapterTest.php @@ -41,6 +41,8 @@ class OrphanVisitsPaginatorAdapterTest extends TestCase } /** + * @param int<0, max> $limit + * @param int<0, max> $offset * @test * @dataProvider provideLimitAndOffset */ diff --git a/module/Rest/test/ApiKey/Model/RoleDefinitionTest.php b/module/Rest/test/ApiKey/Model/RoleDefinitionTest.php index 4198aa9b..ac513959 100644 --- a/module/Rest/test/ApiKey/Model/RoleDefinitionTest.php +++ b/module/Rest/test/ApiKey/Model/RoleDefinitionTest.php @@ -23,7 +23,8 @@ class RoleDefinitionTest extends TestCase /** @test */ public function forDomainCreatesRoleDefinitionAsExpected(): void { - $domain = Domain::withAuthority('foo.com')->setId('123'); + $domain = Domain::withAuthority('foo.com'); + $domain->setId('123'); $definition = RoleDefinition::forDomain($domain); self::assertEquals(Role::DOMAIN_SPECIFIC, $definition->role);