From 7f04d3d8089e83c04e067e1ac0cddb5fe6918c86 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Wed, 20 Mar 2024 10:17:56 +0100 Subject: [PATCH] Optimize test suite by bypassing Package::setRepository network calls --- tests/Controller/ApiControllerTest.php | 4 +--- tests/Controller/ControllerTestCase.php | 16 ++++++++++++++++ tests/Controller/WebControllerTest.php | 17 +++-------------- tests/Package/UpdaterTest.php | 5 ++--- tests/bootstrap.php | 2 ++ 5 files changed, 24 insertions(+), 20 deletions(-) diff --git a/tests/Controller/ApiControllerTest.php b/tests/Controller/ApiControllerTest.php index e79fd4501..2184f50c8 100644 --- a/tests/Controller/ApiControllerTest.php +++ b/tests/Controller/ApiControllerTest.php @@ -39,9 +39,7 @@ public function testGitHubFailsWithInvalidCredentials(): void #[DataProvider('githubApiProvider')] public function testGithubApi($url): void { - $package = new Package; - $package->setName('test/'.md5(uniqid())); - $package->setRepository($url); + $package = $this->createPackage('test/'.md5(uniqid()), $url); $user = new User; $user->addPackage($package); diff --git a/tests/Controller/ControllerTestCase.php b/tests/Controller/ControllerTestCase.php index d2a6a990f..c73b25455 100644 --- a/tests/Controller/ControllerTestCase.php +++ b/tests/Controller/ControllerTestCase.php @@ -12,9 +12,11 @@ namespace App\Tests\Controller; +use App\Entity\Package; use Doctrine\DBAL\Connection; use Doctrine\ORM\EntityManagerInterface; use Doctrine\Persistence\ManagerRegistry; +use ReflectionProperty; use Symfony\Bundle\FrameworkBundle\KernelBrowser; use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; use Symfony\Component\DomCrawler\Crawler; @@ -54,4 +56,18 @@ protected function assertFormError(string $message, string $formName, Crawler $c $formCrawler->html()."\nShould find an .alert-danger within the form with the message: '$message'", ); } + + /** + * Creates a Package entity without running the slow network-based repository initialization step + */ + protected function createPackage(string $name, string $repository, ?string $remoteId = null) + { + $package = new Package(); + + $package->setName($name); + $package->setRemoteId($remoteId); + (new ReflectionProperty($package, 'repository'))->setValue($package, $repository); + + return $package; + } } diff --git a/tests/Controller/WebControllerTest.php b/tests/Controller/WebControllerTest.php index 8b366219d..ad69b0e52 100644 --- a/tests/Controller/WebControllerTest.php +++ b/tests/Controller/WebControllerTest.php @@ -113,20 +113,9 @@ protected function initializePackages(): array { $em = $this->getEM(); - $twigPackage = new Package(); - - $twigPackage->setName('twig/twig'); - $twigPackage->setRepository('https://github.com/twig/twig'); - - $packagistPackage = new Package(); - - $packagistPackage->setName('composer/packagist'); - $packagistPackage->setRepository('https://github.com/composer/packagist'); - - $symfonyPackage = new Package(); - - $symfonyPackage->setName('symfony/symfony'); - $symfonyPackage->setRepository('https://github.com/symfony/symfony'); + $twigPackage = $this->createPackage('twig/twig', 'https://github.com/twigphp/Twig', 'github.com/330275'); + $packagistPackage = $this->createPackage('composer/packagist', 'https://github.com/composer/packagist'); + $symfonyPackage = $this->createPackage('symfony/symfony', 'https://github.com/symfony/symfony', 'github.com/458058'); $em->persist($twigPackage); $em->persist($packagistPackage); diff --git a/tests/Package/UpdaterTest.php b/tests/Package/UpdaterTest.php index 9623334c9..3903c18b5 100644 --- a/tests/Package/UpdaterTest.php +++ b/tests/Package/UpdaterTest.php @@ -52,9 +52,8 @@ protected function setUp(): void $this->config = new Config(); $this->package = new Package(); $this->package->setName('test/pkg'); - $this->package->setRepository('https://example.com/test/pkg'); - $reflProp = new \ReflectionProperty(Package::class, 'id'); - $reflProp->setValue($this->package, 1); + (new \ReflectionProperty($this->package, 'repository'))->setValue($this->package, 'https://example.com/test/pkg'); + (new \ReflectionProperty($this->package, 'id'))->setValue($this->package, 1); $this->ioMock = $this->createMock(NullIO::class); $this->repositoryMock = $this->createMock(VcsRepository::class); diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 39d62d990..acd636a7f 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -53,3 +53,5 @@ function executeCommand(string $command, bool $errorHandling = true): void { executeCommand('php ./bin/console doctrine:database:create --env=test -q'); executeCommand('php ./bin/console doctrine:schema:create --env=test -q'); executeCommand('php ./bin/console redis:query flushall --env=test -n -q'); + +\Composer\Util\Platform::putEnv('PACKAGIST_TESTS_ARE_RUNNING', '1');