From 965325aa7c2d02e9e78773889af6cf284c623636 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sun, 18 Jun 2023 10:51:59 +0200 Subject: [PATCH] Replace traits with static classes in CLI unit tests --- .../Command/Api/DisableKeyCommandTest.php | 6 ++--- .../Command/Api/GenerateKeyCommandTest.php | 6 ++--- .../test/Command/Api/ListKeysCommandTest.php | 6 ++--- .../Command/Db/CreateDatabaseCommandTest.php | 6 ++--- .../Command/Db/MigrateDatabaseCommandTest.php | 6 ++--- .../Domain/DomainRedirectsCommandTest.php | 6 ++--- .../Domain/GetDomainVisitsCommandTest.php | 6 ++--- .../Command/Domain/ListDomainsCommandTest.php | 6 ++--- .../ShortUrl/CreateShortUrlCommandTest.php | 6 ++--- .../ShortUrl/DeleteShortUrlCommandTest.php | 6 ++--- .../DeleteShortUrlVisitsCommandTest.php | 6 ++--- .../ShortUrl/GetShortUrlVisitsCommandTest.php | 6 ++--- .../ShortUrl/ListShortUrlsCommandTest.php | 6 ++--- .../ShortUrl/ResolveUrlCommandTest.php | 6 ++--- .../Command/Tag/DeleteTagsCommandTest.php | 6 ++--- .../Command/Tag/GetTagVisitsCommandTest.php | 6 ++--- .../test/Command/Tag/ListTagsCommandTest.php | 6 ++--- .../test/Command/Tag/RenameTagCommandTest.php | 6 ++--- .../Visit/DeleteOrphanVisitsCommandTest.php | 6 ++--- .../Visit/DownloadGeoLiteDbCommandTest.php | 6 ++--- .../Visit/GetNonOrphanVisitsCommandTest.php | 6 ++--- .../Visit/GetOrphanVisitsCommandTest.php | 6 ++--- .../Command/Visit/LocateVisitsCommandTest.php | 8 +++---- .../test/Factory/ApplicationFactoryTest.php | 8 +++---- .../CliTestUtils.php} | 23 +++++++++++++++---- module/Core/test/Util/ApiKeyDataProviders.php | 16 +++++++++++++ 26 files changed, 84 insertions(+), 103 deletions(-) rename module/CLI/test/{CliTestUtilsTrait.php => Util/CliTestUtils.php} (57%) create mode 100644 module/Core/test/Util/ApiKeyDataProviders.php diff --git a/module/CLI/test/Command/Api/DisableKeyCommandTest.php b/module/CLI/test/Command/Api/DisableKeyCommandTest.php index 8a1c64e8..a12cb46f 100644 --- a/module/CLI/test/Command/Api/DisableKeyCommandTest.php +++ b/module/CLI/test/Command/Api/DisableKeyCommandTest.php @@ -10,20 +10,18 @@ use PHPUnit\Framework\TestCase; use Shlinkio\Shlink\CLI\Command\Api\DisableKeyCommand; use Shlinkio\Shlink\Common\Exception\InvalidArgumentException; use Shlinkio\Shlink\Rest\Service\ApiKeyServiceInterface; -use ShlinkioTest\Shlink\CLI\CliTestUtilsTrait; +use ShlinkioTest\Shlink\CLI\Util\CliTestUtils; use Symfony\Component\Console\Tester\CommandTester; class DisableKeyCommandTest extends TestCase { - use CliTestUtilsTrait; - private CommandTester $commandTester; private MockObject & ApiKeyServiceInterface $apiKeyService; protected function setUp(): void { $this->apiKeyService = $this->createMock(ApiKeyServiceInterface::class); - $this->commandTester = $this->testerForCommand(new DisableKeyCommand($this->apiKeyService)); + $this->commandTester = CliTestUtils::testerForCommand(new DisableKeyCommand($this->apiKeyService)); } #[Test] diff --git a/module/CLI/test/Command/Api/GenerateKeyCommandTest.php b/module/CLI/test/Command/Api/GenerateKeyCommandTest.php index b5dbe513..5935242d 100644 --- a/module/CLI/test/Command/Api/GenerateKeyCommandTest.php +++ b/module/CLI/test/Command/Api/GenerateKeyCommandTest.php @@ -12,14 +12,12 @@ use Shlinkio\Shlink\CLI\ApiKey\RoleResolverInterface; use Shlinkio\Shlink\CLI\Command\Api\GenerateKeyCommand; use Shlinkio\Shlink\Rest\Entity\ApiKey; use Shlinkio\Shlink\Rest\Service\ApiKeyServiceInterface; -use ShlinkioTest\Shlink\CLI\CliTestUtilsTrait; +use ShlinkioTest\Shlink\CLI\Util\CliTestUtils; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Tester\CommandTester; class GenerateKeyCommandTest extends TestCase { - use CliTestUtilsTrait; - private CommandTester $commandTester; private MockObject & ApiKeyServiceInterface $apiKeyService; @@ -30,7 +28,7 @@ class GenerateKeyCommandTest extends TestCase $roleResolver->method('determineRoles')->with($this->isInstanceOf(InputInterface::class))->willReturn([]); $command = new GenerateKeyCommand($this->apiKeyService, $roleResolver); - $this->commandTester = $this->testerForCommand($command); + $this->commandTester = CliTestUtils::testerForCommand($command); } #[Test] diff --git a/module/CLI/test/Command/Api/ListKeysCommandTest.php b/module/CLI/test/Command/Api/ListKeysCommandTest.php index e4cdb438..0f3e8cb6 100644 --- a/module/CLI/test/Command/Api/ListKeysCommandTest.php +++ b/module/CLI/test/Command/Api/ListKeysCommandTest.php @@ -15,20 +15,18 @@ use Shlinkio\Shlink\Rest\ApiKey\Model\ApiKeyMeta; use Shlinkio\Shlink\Rest\ApiKey\Model\RoleDefinition; use Shlinkio\Shlink\Rest\Entity\ApiKey; use Shlinkio\Shlink\Rest\Service\ApiKeyServiceInterface; -use ShlinkioTest\Shlink\CLI\CliTestUtilsTrait; +use ShlinkioTest\Shlink\CLI\Util\CliTestUtils; use Symfony\Component\Console\Tester\CommandTester; class ListKeysCommandTest extends TestCase { - use CliTestUtilsTrait; - private CommandTester $commandTester; private MockObject & ApiKeyServiceInterface $apiKeyService; protected function setUp(): void { $this->apiKeyService = $this->createMock(ApiKeyServiceInterface::class); - $this->commandTester = $this->testerForCommand(new ListKeysCommand($this->apiKeyService)); + $this->commandTester = CliTestUtils::testerForCommand(new ListKeysCommand($this->apiKeyService)); } #[Test, DataProvider('provideKeysAndOutputs')] diff --git a/module/CLI/test/Command/Db/CreateDatabaseCommandTest.php b/module/CLI/test/Command/Db/CreateDatabaseCommandTest.php index 420ea91d..cece20db 100644 --- a/module/CLI/test/Command/Db/CreateDatabaseCommandTest.php +++ b/module/CLI/test/Command/Db/CreateDatabaseCommandTest.php @@ -18,7 +18,7 @@ use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use Shlinkio\Shlink\CLI\Command\Db\CreateDatabaseCommand; use Shlinkio\Shlink\CLI\Util\ProcessRunnerInterface; -use ShlinkioTest\Shlink\CLI\CliTestUtilsTrait; +use ShlinkioTest\Shlink\CLI\Util\CliTestUtils; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Tester\CommandTester; use Symfony\Component\Lock\LockFactory; @@ -27,8 +27,6 @@ use Symfony\Component\Process\PhpExecutableFinder; class CreateDatabaseCommandTest extends TestCase { - use CliTestUtilsTrait; - private CommandTester $commandTester; private MockObject & ProcessRunnerInterface $processHelper; private MockObject & Connection $regularConn; @@ -63,7 +61,7 @@ class CreateDatabaseCommandTest extends TestCase $noDbNameConn->method('createSchemaManager')->withAnyParameters()->willReturn($this->schemaManager); $command = new CreateDatabaseCommand($locker, $this->processHelper, $phpExecutableFinder, $em, $noDbNameConn); - $this->commandTester = $this->testerForCommand($command); + $this->commandTester = CliTestUtils::testerForCommand($command); } #[Test] diff --git a/module/CLI/test/Command/Db/MigrateDatabaseCommandTest.php b/module/CLI/test/Command/Db/MigrateDatabaseCommandTest.php index 7bdbfca0..ac4283d7 100644 --- a/module/CLI/test/Command/Db/MigrateDatabaseCommandTest.php +++ b/module/CLI/test/Command/Db/MigrateDatabaseCommandTest.php @@ -9,7 +9,7 @@ use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use Shlinkio\Shlink\CLI\Command\Db\MigrateDatabaseCommand; use Shlinkio\Shlink\CLI\Util\ProcessRunnerInterface; -use ShlinkioTest\Shlink\CLI\CliTestUtilsTrait; +use ShlinkioTest\Shlink\CLI\Util\CliTestUtils; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Tester\CommandTester; use Symfony\Component\Lock\LockFactory; @@ -18,8 +18,6 @@ use Symfony\Component\Process\PhpExecutableFinder; class MigrateDatabaseCommandTest extends TestCase { - use CliTestUtilsTrait; - private CommandTester $commandTester; private MockObject & ProcessRunnerInterface $processHelper; @@ -36,7 +34,7 @@ class MigrateDatabaseCommandTest extends TestCase $this->processHelper = $this->createMock(ProcessRunnerInterface::class); $command = new MigrateDatabaseCommand($locker, $this->processHelper, $phpExecutableFinder); - $this->commandTester = $this->testerForCommand($command); + $this->commandTester = CliTestUtils::testerForCommand($command); } #[Test] diff --git a/module/CLI/test/Command/Domain/DomainRedirectsCommandTest.php b/module/CLI/test/Command/Domain/DomainRedirectsCommandTest.php index 48125d91..0bc77aca 100644 --- a/module/CLI/test/Command/Domain/DomainRedirectsCommandTest.php +++ b/module/CLI/test/Command/Domain/DomainRedirectsCommandTest.php @@ -14,22 +14,20 @@ use Shlinkio\Shlink\Core\Domain\DomainServiceInterface; use Shlinkio\Shlink\Core\Domain\Entity\Domain; use Shlinkio\Shlink\Core\Domain\Model\DomainItem; use Shlinkio\Shlink\Core\Options\NotFoundRedirectOptions; -use ShlinkioTest\Shlink\CLI\CliTestUtilsTrait; +use ShlinkioTest\Shlink\CLI\Util\CliTestUtils; use Symfony\Component\Console\Tester\CommandTester; use function substr_count; class DomainRedirectsCommandTest extends TestCase { - use CliTestUtilsTrait; - private CommandTester $commandTester; private MockObject & DomainServiceInterface $domainService; protected function setUp(): void { $this->domainService = $this->createMock(DomainServiceInterface::class); - $this->commandTester = $this->testerForCommand(new DomainRedirectsCommand($this->domainService)); + $this->commandTester = CliTestUtils::testerForCommand(new DomainRedirectsCommand($this->domainService)); } #[Test, DataProvider('provideDomains')] diff --git a/module/CLI/test/Command/Domain/GetDomainVisitsCommandTest.php b/module/CLI/test/Command/Domain/GetDomainVisitsCommandTest.php index bdee0ed4..7f4bd076 100644 --- a/module/CLI/test/Command/Domain/GetDomainVisitsCommandTest.php +++ b/module/CLI/test/Command/Domain/GetDomainVisitsCommandTest.php @@ -17,13 +17,11 @@ use Shlinkio\Shlink\Core\Visit\Entity\VisitLocation; use Shlinkio\Shlink\Core\Visit\Model\Visitor; use Shlinkio\Shlink\Core\Visit\VisitsStatsHelperInterface; use Shlinkio\Shlink\IpGeolocation\Model\Location; -use ShlinkioTest\Shlink\CLI\CliTestUtilsTrait; +use ShlinkioTest\Shlink\CLI\Util\CliTestUtils; use Symfony\Component\Console\Tester\CommandTester; class GetDomainVisitsCommandTest extends TestCase { - use CliTestUtilsTrait; - private CommandTester $commandTester; private MockObject & VisitsStatsHelperInterface $visitsHelper; private MockObject & ShortUrlStringifierInterface $stringifier; @@ -33,7 +31,7 @@ class GetDomainVisitsCommandTest extends TestCase $this->visitsHelper = $this->createMock(VisitsStatsHelperInterface::class); $this->stringifier = $this->createMock(ShortUrlStringifierInterface::class); - $this->commandTester = $this->testerForCommand( + $this->commandTester = CliTestUtils::testerForCommand( new GetDomainVisitsCommand($this->visitsHelper, $this->stringifier), ); } diff --git a/module/CLI/test/Command/Domain/ListDomainsCommandTest.php b/module/CLI/test/Command/Domain/ListDomainsCommandTest.php index 05cc95eb..cfa09e18 100644 --- a/module/CLI/test/Command/Domain/ListDomainsCommandTest.php +++ b/module/CLI/test/Command/Domain/ListDomainsCommandTest.php @@ -15,20 +15,18 @@ use Shlinkio\Shlink\Core\Domain\DomainServiceInterface; use Shlinkio\Shlink\Core\Domain\Entity\Domain; use Shlinkio\Shlink\Core\Domain\Model\DomainItem; use Shlinkio\Shlink\Core\Options\NotFoundRedirectOptions; -use ShlinkioTest\Shlink\CLI\CliTestUtilsTrait; +use ShlinkioTest\Shlink\CLI\Util\CliTestUtils; use Symfony\Component\Console\Tester\CommandTester; class ListDomainsCommandTest extends TestCase { - use CliTestUtilsTrait; - private CommandTester $commandTester; private MockObject & DomainServiceInterface $domainService; protected function setUp(): void { $this->domainService = $this->createMock(DomainServiceInterface::class); - $this->commandTester = $this->testerForCommand(new ListDomainsCommand($this->domainService)); + $this->commandTester = CliTestUtils::testerForCommand(new ListDomainsCommand($this->domainService)); } #[Test, DataProvider('provideInputsAndOutputs')] diff --git a/module/CLI/test/Command/ShortUrl/CreateShortUrlCommandTest.php b/module/CLI/test/Command/ShortUrl/CreateShortUrlCommandTest.php index 46063485..de0fe26b 100644 --- a/module/CLI/test/Command/ShortUrl/CreateShortUrlCommandTest.php +++ b/module/CLI/test/Command/ShortUrl/CreateShortUrlCommandTest.php @@ -20,14 +20,12 @@ use Shlinkio\Shlink\Core\ShortUrl\Helper\ShortUrlStringifierInterface; use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlCreation; use Shlinkio\Shlink\Core\ShortUrl\Model\UrlShorteningResult; use Shlinkio\Shlink\Core\ShortUrl\UrlShortenerInterface; -use ShlinkioTest\Shlink\CLI\CliTestUtilsTrait; +use ShlinkioTest\Shlink\CLI\Util\CliTestUtils; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Tester\CommandTester; class CreateShortUrlCommandTest extends TestCase { - use CliTestUtilsTrait; - private CommandTester $commandTester; private MockObject & UrlShortenerInterface $urlShortener; private MockObject & ShortUrlStringifierInterface $stringifier; @@ -45,7 +43,7 @@ class CreateShortUrlCommandTest extends TestCase defaultShortCodesLength: 5, ), ); - $this->commandTester = $this->testerForCommand($command); + $this->commandTester = CliTestUtils::testerForCommand($command); } #[Test] diff --git a/module/CLI/test/Command/ShortUrl/DeleteShortUrlCommandTest.php b/module/CLI/test/Command/ShortUrl/DeleteShortUrlCommandTest.php index 06081983..0402dc8c 100644 --- a/module/CLI/test/Command/ShortUrl/DeleteShortUrlCommandTest.php +++ b/module/CLI/test/Command/ShortUrl/DeleteShortUrlCommandTest.php @@ -12,7 +12,7 @@ use Shlinkio\Shlink\CLI\Command\ShortUrl\DeleteShortUrlCommand; use Shlinkio\Shlink\Core\Exception; use Shlinkio\Shlink\Core\ShortUrl\DeleteShortUrlServiceInterface; use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlIdentifier; -use ShlinkioTest\Shlink\CLI\CliTestUtilsTrait; +use ShlinkioTest\Shlink\CLI\Util\CliTestUtils; use Symfony\Component\Console\Tester\CommandTester; use function sprintf; @@ -21,15 +21,13 @@ use const PHP_EOL; class DeleteShortUrlCommandTest extends TestCase { - use CliTestUtilsTrait; - private CommandTester $commandTester; private MockObject & DeleteShortUrlServiceInterface $service; protected function setUp(): void { $this->service = $this->createMock(DeleteShortUrlServiceInterface::class); - $this->commandTester = $this->testerForCommand(new DeleteShortUrlCommand($this->service)); + $this->commandTester = CliTestUtils::testerForCommand(new DeleteShortUrlCommand($this->service)); } #[Test] diff --git a/module/CLI/test/Command/ShortUrl/DeleteShortUrlVisitsCommandTest.php b/module/CLI/test/Command/ShortUrl/DeleteShortUrlVisitsCommandTest.php index 88c3657a..2a281a8a 100644 --- a/module/CLI/test/Command/ShortUrl/DeleteShortUrlVisitsCommandTest.php +++ b/module/CLI/test/Command/ShortUrl/DeleteShortUrlVisitsCommandTest.php @@ -13,20 +13,18 @@ use Shlinkio\Shlink\CLI\Util\ExitCode; use Shlinkio\Shlink\Core\Exception\ShortUrlNotFoundException; use Shlinkio\Shlink\Core\Model\BulkDeleteResult; use Shlinkio\Shlink\Core\ShortUrl\ShortUrlVisitsDeleterInterface; -use ShlinkioTest\Shlink\CLI\CliTestUtilsTrait; +use ShlinkioTest\Shlink\CLI\Util\CliTestUtils; use Symfony\Component\Console\Tester\CommandTester; class DeleteShortUrlVisitsCommandTest extends TestCase { - use CliTestUtilsTrait; - private CommandTester $commandTester; private MockObject & ShortUrlVisitsDeleterInterface $deleter; protected function setUp(): void { $this->deleter = $this->createMock(ShortUrlVisitsDeleterInterface::class); - $this->commandTester = $this->testerForCommand(new DeleteShortUrlVisitsCommand($this->deleter)); + $this->commandTester = CliTestUtils::testerForCommand(new DeleteShortUrlVisitsCommand($this->deleter)); } #[Test, DataProvider('provideCancellingInputs')] diff --git a/module/CLI/test/Command/ShortUrl/GetShortUrlVisitsCommandTest.php b/module/CLI/test/Command/ShortUrl/GetShortUrlVisitsCommandTest.php index 13d36f4b..f93ab5ec 100644 --- a/module/CLI/test/Command/ShortUrl/GetShortUrlVisitsCommandTest.php +++ b/module/CLI/test/Command/ShortUrl/GetShortUrlVisitsCommandTest.php @@ -20,7 +20,7 @@ use Shlinkio\Shlink\Core\Visit\Model\Visitor; use Shlinkio\Shlink\Core\Visit\Model\VisitsParams; use Shlinkio\Shlink\Core\Visit\VisitsStatsHelperInterface; use Shlinkio\Shlink\IpGeolocation\Model\Location; -use ShlinkioTest\Shlink\CLI\CliTestUtilsTrait; +use ShlinkioTest\Shlink\CLI\Util\CliTestUtils; use Symfony\Component\Console\Tester\CommandTester; use function Shlinkio\Shlink\Common\buildDateRange; @@ -28,8 +28,6 @@ use function sprintf; class GetShortUrlVisitsCommandTest extends TestCase { - use CliTestUtilsTrait; - private CommandTester $commandTester; private MockObject & VisitsStatsHelperInterface $visitsHelper; @@ -37,7 +35,7 @@ class GetShortUrlVisitsCommandTest extends TestCase { $this->visitsHelper = $this->createMock(VisitsStatsHelperInterface::class); $command = new GetShortUrlVisitsCommand($this->visitsHelper); - $this->commandTester = $this->testerForCommand($command); + $this->commandTester = CliTestUtils::testerForCommand($command); } #[Test] diff --git a/module/CLI/test/Command/ShortUrl/ListShortUrlsCommandTest.php b/module/CLI/test/Command/ShortUrl/ListShortUrlsCommandTest.php index d81172ed..2c6a0979 100644 --- a/module/CLI/test/Command/ShortUrl/ListShortUrlsCommandTest.php +++ b/module/CLI/test/Command/ShortUrl/ListShortUrlsCommandTest.php @@ -21,7 +21,7 @@ use Shlinkio\Shlink\Core\ShortUrl\ShortUrlListServiceInterface; use Shlinkio\Shlink\Core\ShortUrl\Transformer\ShortUrlDataTransformer; use Shlinkio\Shlink\Rest\ApiKey\Model\ApiKeyMeta; use Shlinkio\Shlink\Rest\Entity\ApiKey; -use ShlinkioTest\Shlink\CLI\CliTestUtilsTrait; +use ShlinkioTest\Shlink\CLI\Util\CliTestUtils; use Symfony\Component\Console\Tester\CommandTester; use function count; @@ -29,8 +29,6 @@ use function explode; class ListShortUrlsCommandTest extends TestCase { - use CliTestUtilsTrait; - private CommandTester $commandTester; private MockObject & ShortUrlListServiceInterface $shortUrlService; @@ -40,7 +38,7 @@ class ListShortUrlsCommandTest extends TestCase $command = new ListShortUrlsCommand($this->shortUrlService, new ShortUrlDataTransformer( new ShortUrlStringifier([]), )); - $this->commandTester = $this->testerForCommand($command); + $this->commandTester = CliTestUtils::testerForCommand($command); } #[Test] diff --git a/module/CLI/test/Command/ShortUrl/ResolveUrlCommandTest.php b/module/CLI/test/Command/ShortUrl/ResolveUrlCommandTest.php index 21452ed6..9c9bbb93 100644 --- a/module/CLI/test/Command/ShortUrl/ResolveUrlCommandTest.php +++ b/module/CLI/test/Command/ShortUrl/ResolveUrlCommandTest.php @@ -12,7 +12,7 @@ use Shlinkio\Shlink\Core\Exception\ShortUrlNotFoundException; use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl; use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlIdentifier; use Shlinkio\Shlink\Core\ShortUrl\ShortUrlResolverInterface; -use ShlinkioTest\Shlink\CLI\CliTestUtilsTrait; +use ShlinkioTest\Shlink\CLI\Util\CliTestUtils; use Symfony\Component\Console\Tester\CommandTester; use function sprintf; @@ -21,15 +21,13 @@ use const PHP_EOL; class ResolveUrlCommandTest extends TestCase { - use CliTestUtilsTrait; - private CommandTester $commandTester; private MockObject & ShortUrlResolverInterface $urlResolver; protected function setUp(): void { $this->urlResolver = $this->createMock(ShortUrlResolverInterface::class); - $this->commandTester = $this->testerForCommand(new ResolveUrlCommand($this->urlResolver)); + $this->commandTester = CliTestUtils::testerForCommand(new ResolveUrlCommand($this->urlResolver)); } #[Test] diff --git a/module/CLI/test/Command/Tag/DeleteTagsCommandTest.php b/module/CLI/test/Command/Tag/DeleteTagsCommandTest.php index d818ba54..7bbd5966 100644 --- a/module/CLI/test/Command/Tag/DeleteTagsCommandTest.php +++ b/module/CLI/test/Command/Tag/DeleteTagsCommandTest.php @@ -9,20 +9,18 @@ use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use Shlinkio\Shlink\CLI\Command\Tag\DeleteTagsCommand; use Shlinkio\Shlink\Core\Tag\TagServiceInterface; -use ShlinkioTest\Shlink\CLI\CliTestUtilsTrait; +use ShlinkioTest\Shlink\CLI\Util\CliTestUtils; use Symfony\Component\Console\Tester\CommandTester; class DeleteTagsCommandTest extends TestCase { - use CliTestUtilsTrait; - private CommandTester $commandTester; private MockObject & TagServiceInterface $tagService; protected function setUp(): void { $this->tagService = $this->createMock(TagServiceInterface::class); - $this->commandTester = $this->testerForCommand(new DeleteTagsCommand($this->tagService)); + $this->commandTester = CliTestUtils::testerForCommand(new DeleteTagsCommand($this->tagService)); } #[Test] diff --git a/module/CLI/test/Command/Tag/GetTagVisitsCommandTest.php b/module/CLI/test/Command/Tag/GetTagVisitsCommandTest.php index ef34952d..a2dc059f 100644 --- a/module/CLI/test/Command/Tag/GetTagVisitsCommandTest.php +++ b/module/CLI/test/Command/Tag/GetTagVisitsCommandTest.php @@ -17,13 +17,11 @@ use Shlinkio\Shlink\Core\Visit\Entity\VisitLocation; use Shlinkio\Shlink\Core\Visit\Model\Visitor; use Shlinkio\Shlink\Core\Visit\VisitsStatsHelperInterface; use Shlinkio\Shlink\IpGeolocation\Model\Location; -use ShlinkioTest\Shlink\CLI\CliTestUtilsTrait; +use ShlinkioTest\Shlink\CLI\Util\CliTestUtils; use Symfony\Component\Console\Tester\CommandTester; class GetTagVisitsCommandTest extends TestCase { - use CliTestUtilsTrait; - private CommandTester $commandTester; private MockObject & VisitsStatsHelperInterface $visitsHelper; private MockObject & ShortUrlStringifierInterface $stringifier; @@ -33,7 +31,7 @@ class GetTagVisitsCommandTest extends TestCase $this->visitsHelper = $this->createMock(VisitsStatsHelperInterface::class); $this->stringifier = $this->createMock(ShortUrlStringifierInterface::class); - $this->commandTester = $this->testerForCommand( + $this->commandTester = CliTestUtils::testerForCommand( new GetTagVisitsCommand($this->visitsHelper, $this->stringifier), ); } diff --git a/module/CLI/test/Command/Tag/ListTagsCommandTest.php b/module/CLI/test/Command/Tag/ListTagsCommandTest.php index e1020667..1cfb3d3b 100644 --- a/module/CLI/test/Command/Tag/ListTagsCommandTest.php +++ b/module/CLI/test/Command/Tag/ListTagsCommandTest.php @@ -12,20 +12,18 @@ use Shlinkio\Shlink\CLI\Command\Tag\ListTagsCommand; use Shlinkio\Shlink\Common\Paginator\Paginator; use Shlinkio\Shlink\Core\Tag\Model\TagInfo; use Shlinkio\Shlink\Core\Tag\TagServiceInterface; -use ShlinkioTest\Shlink\CLI\CliTestUtilsTrait; +use ShlinkioTest\Shlink\CLI\Util\CliTestUtils; use Symfony\Component\Console\Tester\CommandTester; class ListTagsCommandTest extends TestCase { - use CliTestUtilsTrait; - private CommandTester $commandTester; private MockObject & TagServiceInterface $tagService; protected function setUp(): void { $this->tagService = $this->createMock(TagServiceInterface::class); - $this->commandTester = $this->testerForCommand(new ListTagsCommand($this->tagService)); + $this->commandTester = CliTestUtils::testerForCommand(new ListTagsCommand($this->tagService)); } #[Test] diff --git a/module/CLI/test/Command/Tag/RenameTagCommandTest.php b/module/CLI/test/Command/Tag/RenameTagCommandTest.php index 7dfe474f..296926b8 100644 --- a/module/CLI/test/Command/Tag/RenameTagCommandTest.php +++ b/module/CLI/test/Command/Tag/RenameTagCommandTest.php @@ -12,20 +12,18 @@ use Shlinkio\Shlink\Core\Exception\TagNotFoundException; use Shlinkio\Shlink\Core\Tag\Entity\Tag; use Shlinkio\Shlink\Core\Tag\Model\TagRenaming; use Shlinkio\Shlink\Core\Tag\TagServiceInterface; -use ShlinkioTest\Shlink\CLI\CliTestUtilsTrait; +use ShlinkioTest\Shlink\CLI\Util\CliTestUtils; use Symfony\Component\Console\Tester\CommandTester; class RenameTagCommandTest extends TestCase { - use CliTestUtilsTrait; - private CommandTester $commandTester; private MockObject & TagServiceInterface $tagService; protected function setUp(): void { $this->tagService = $this->createMock(TagServiceInterface::class); - $this->commandTester = $this->testerForCommand(new RenameTagCommand($this->tagService)); + $this->commandTester = CliTestUtils::testerForCommand(new RenameTagCommand($this->tagService)); } #[Test] diff --git a/module/CLI/test/Command/Visit/DeleteOrphanVisitsCommandTest.php b/module/CLI/test/Command/Visit/DeleteOrphanVisitsCommandTest.php index c18fe7f4..cd39c63a 100644 --- a/module/CLI/test/Command/Visit/DeleteOrphanVisitsCommandTest.php +++ b/module/CLI/test/Command/Visit/DeleteOrphanVisitsCommandTest.php @@ -11,20 +11,18 @@ use Shlinkio\Shlink\CLI\Command\Visit\DeleteOrphanVisitsCommand; use Shlinkio\Shlink\CLI\Util\ExitCode; use Shlinkio\Shlink\Core\Model\BulkDeleteResult; use Shlinkio\Shlink\Core\Visit\VisitsDeleterInterface; -use ShlinkioTest\Shlink\CLI\CliTestUtilsTrait; +use ShlinkioTest\Shlink\CLI\Util\CliTestUtils; use Symfony\Component\Console\Tester\CommandTester; class DeleteOrphanVisitsCommandTest extends TestCase { - use CliTestUtilsTrait; - private CommandTester $commandTester; private MockObject & VisitsDeleterInterface $deleter; protected function setUp(): void { $this->deleter = $this->createMock(VisitsDeleterInterface::class); - $this->commandTester = $this->testerForCommand(new DeleteOrphanVisitsCommand($this->deleter)); + $this->commandTester = CliTestUtils::testerForCommand(new DeleteOrphanVisitsCommand($this->deleter)); } #[Test] diff --git a/module/CLI/test/Command/Visit/DownloadGeoLiteDbCommandTest.php b/module/CLI/test/Command/Visit/DownloadGeoLiteDbCommandTest.php index 7e904caa..78e14fa9 100644 --- a/module/CLI/test/Command/Visit/DownloadGeoLiteDbCommandTest.php +++ b/module/CLI/test/Command/Visit/DownloadGeoLiteDbCommandTest.php @@ -13,22 +13,20 @@ use Shlinkio\Shlink\CLI\Exception\GeolocationDbUpdateFailedException; use Shlinkio\Shlink\CLI\GeoLite\GeolocationDbUpdaterInterface; use Shlinkio\Shlink\CLI\GeoLite\GeolocationResult; use Shlinkio\Shlink\CLI\Util\ExitCode; -use ShlinkioTest\Shlink\CLI\CliTestUtilsTrait; +use ShlinkioTest\Shlink\CLI\Util\CliTestUtils; use Symfony\Component\Console\Tester\CommandTester; use function sprintf; class DownloadGeoLiteDbCommandTest extends TestCase { - use CliTestUtilsTrait; - private CommandTester $commandTester; private MockObject & GeolocationDbUpdaterInterface $dbUpdater; protected function setUp(): void { $this->dbUpdater = $this->createMock(GeolocationDbUpdaterInterface::class); - $this->commandTester = $this->testerForCommand(new DownloadGeoLiteDbCommand($this->dbUpdater)); + $this->commandTester = CliTestUtils::testerForCommand(new DownloadGeoLiteDbCommand($this->dbUpdater)); } #[Test, DataProvider('provideFailureParams')] diff --git a/module/CLI/test/Command/Visit/GetNonOrphanVisitsCommandTest.php b/module/CLI/test/Command/Visit/GetNonOrphanVisitsCommandTest.php index dabfdb06..439b33bd 100644 --- a/module/CLI/test/Command/Visit/GetNonOrphanVisitsCommandTest.php +++ b/module/CLI/test/Command/Visit/GetNonOrphanVisitsCommandTest.php @@ -17,13 +17,11 @@ use Shlinkio\Shlink\Core\Visit\Entity\VisitLocation; use Shlinkio\Shlink\Core\Visit\Model\Visitor; use Shlinkio\Shlink\Core\Visit\VisitsStatsHelperInterface; use Shlinkio\Shlink\IpGeolocation\Model\Location; -use ShlinkioTest\Shlink\CLI\CliTestUtilsTrait; +use ShlinkioTest\Shlink\CLI\Util\CliTestUtils; use Symfony\Component\Console\Tester\CommandTester; class GetNonOrphanVisitsCommandTest extends TestCase { - use CliTestUtilsTrait; - private CommandTester $commandTester; private MockObject & VisitsStatsHelperInterface $visitsHelper; private MockObject & ShortUrlStringifierInterface $stringifier; @@ -33,7 +31,7 @@ class GetNonOrphanVisitsCommandTest extends TestCase $this->visitsHelper = $this->createMock(VisitsStatsHelperInterface::class); $this->stringifier = $this->createMock(ShortUrlStringifierInterface::class); - $this->commandTester = $this->testerForCommand( + $this->commandTester = CliTestUtils::testerForCommand( new GetNonOrphanVisitsCommand($this->visitsHelper, $this->stringifier), ); } diff --git a/module/CLI/test/Command/Visit/GetOrphanVisitsCommandTest.php b/module/CLI/test/Command/Visit/GetOrphanVisitsCommandTest.php index 226cb927..b90e6af6 100644 --- a/module/CLI/test/Command/Visit/GetOrphanVisitsCommandTest.php +++ b/module/CLI/test/Command/Visit/GetOrphanVisitsCommandTest.php @@ -15,20 +15,18 @@ use Shlinkio\Shlink\Core\Visit\Entity\VisitLocation; use Shlinkio\Shlink\Core\Visit\Model\Visitor; use Shlinkio\Shlink\Core\Visit\VisitsStatsHelperInterface; use Shlinkio\Shlink\IpGeolocation\Model\Location; -use ShlinkioTest\Shlink\CLI\CliTestUtilsTrait; +use ShlinkioTest\Shlink\CLI\Util\CliTestUtils; use Symfony\Component\Console\Tester\CommandTester; class GetOrphanVisitsCommandTest extends TestCase { - use CliTestUtilsTrait; - private CommandTester $commandTester; private MockObject & VisitsStatsHelperInterface $visitsHelper; protected function setUp(): void { $this->visitsHelper = $this->createMock(VisitsStatsHelperInterface::class); - $this->commandTester = $this->testerForCommand(new GetOrphanVisitsCommand($this->visitsHelper)); + $this->commandTester = CliTestUtils::testerForCommand(new GetOrphanVisitsCommand($this->visitsHelper)); } #[Test] diff --git a/module/CLI/test/Command/Visit/LocateVisitsCommandTest.php b/module/CLI/test/Command/Visit/LocateVisitsCommandTest.php index 6ff8c242..031e8e45 100644 --- a/module/CLI/test/Command/Visit/LocateVisitsCommandTest.php +++ b/module/CLI/test/Command/Visit/LocateVisitsCommandTest.php @@ -21,7 +21,7 @@ use Shlinkio\Shlink\Core\Visit\Geolocation\VisitToLocationHelperInterface; use Shlinkio\Shlink\Core\Visit\Model\Visitor; use Shlinkio\Shlink\IpGeolocation\Exception\WrongIpException; use Shlinkio\Shlink\IpGeolocation\Model\Location; -use ShlinkioTest\Shlink\CLI\CliTestUtilsTrait; +use ShlinkioTest\Shlink\CLI\Util\CliTestUtils; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Exception\RuntimeException; use Symfony\Component\Console\Output\OutputInterface; @@ -34,8 +34,6 @@ use const PHP_EOL; class LocateVisitsCommandTest extends TestCase { - use CliTestUtilsTrait; - private CommandTester $commandTester; private MockObject & VisitLocatorInterface $visitService; private MockObject & VisitToLocationHelperInterface $visitToLocation; @@ -53,8 +51,8 @@ class LocateVisitsCommandTest extends TestCase $command = new LocateVisitsCommand($this->visitService, $this->visitToLocation, $locker); - $this->downloadDbCommand = $this->createCommandMock(DownloadGeoLiteDbCommand::NAME); - $this->commandTester = $this->testerForCommand($command, $this->downloadDbCommand); + $this->downloadDbCommand = CliTestUtils::createCommandMock(DownloadGeoLiteDbCommand::NAME); + $this->commandTester = CliTestUtils::testerForCommand($command, $this->downloadDbCommand); } #[Test, DataProvider('provideArgs')] diff --git a/module/CLI/test/Factory/ApplicationFactoryTest.php b/module/CLI/test/Factory/ApplicationFactoryTest.php index 3d75c647..83b0fc66 100644 --- a/module/CLI/test/Factory/ApplicationFactoryTest.php +++ b/module/CLI/test/Factory/ApplicationFactoryTest.php @@ -9,12 +9,10 @@ use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; use Shlinkio\Shlink\CLI\Factory\ApplicationFactory; use Shlinkio\Shlink\Core\Options\AppOptions; -use ShlinkioTest\Shlink\CLI\CliTestUtilsTrait; +use ShlinkioTest\Shlink\CLI\Util\CliTestUtils; class ApplicationFactoryTest extends TestCase { - use CliTestUtilsTrait; - private ApplicationFactory $factory; protected function setUp(): void @@ -32,8 +30,8 @@ class ApplicationFactoryTest extends TestCase 'baz' => 'baz', ], ]); - $sm->setService('foo', $this->createCommandMock('foo')); - $sm->setService('bar', $this->createCommandMock('bar')); + $sm->setService('foo', CliTestUtils::createCommandMock('foo')); + $sm->setService('bar', CliTestUtils::createCommandMock('bar')); $instance = ($this->factory)($sm); diff --git a/module/CLI/test/CliTestUtilsTrait.php b/module/CLI/test/Util/CliTestUtils.php similarity index 57% rename from module/CLI/test/CliTestUtilsTrait.php rename to module/CLI/test/Util/CliTestUtils.php index 761567ae..e5fd7477 100644 --- a/module/CLI/test/CliTestUtilsTrait.php +++ b/module/CLI/test/Util/CliTestUtils.php @@ -2,20 +2,33 @@ declare(strict_types=1); -namespace ShlinkioTest\Shlink\CLI; +namespace ShlinkioTest\Shlink\CLI\Util; use PHPUnit\Framework\Assert; +use PHPUnit\Framework\MockObject\Generator; use PHPUnit\Framework\MockObject\MockObject; use Symfony\Component\Console\Application; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputDefinition; use Symfony\Component\Console\Tester\CommandTester; -trait CliTestUtilsTrait +class CliTestUtils { - private function createCommandMock(string $name): MockObject & Command + public static function createCommandMock(string $name): MockObject & Command { - $command = $this->createMock(Command::class); + static $generator = null; + + if ($generator === null) { + $generator = new Generator(); + } + + $command = $generator->getMock( + Command::class, + callOriginalConstructor: false, + callOriginalClone: false, + cloneArguments: false, + allowMockingUnknownTypes: false, + ); $command->method('getName')->willReturn($name); $command->method('isEnabled')->willReturn(true); $command->method('getAliases')->willReturn([]); @@ -25,7 +38,7 @@ trait CliTestUtilsTrait return $command; } - private function testerForCommand(Command $mainCommand, Command ...$extraCommands): CommandTester + public static function testerForCommand(Command $mainCommand, Command ...$extraCommands): CommandTester { $app = new Application(); $app->add($mainCommand); diff --git a/module/Core/test/Util/ApiKeyDataProviders.php b/module/Core/test/Util/ApiKeyDataProviders.php new file mode 100644 index 00000000..72956e5b --- /dev/null +++ b/module/Core/test/Util/ApiKeyDataProviders.php @@ -0,0 +1,16 @@ + [null]; + yield 'admin API key' => [ApiKey::create()]; + } +}