Skip to content

Commit

Permalink
Optimize test suite by bypassing Package::setRepository network calls
Browse files Browse the repository at this point in the history
  • Loading branch information
Seldaek committed Mar 20, 2024
1 parent 1444a68 commit 7f04d3d
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 20 deletions.
4 changes: 1 addition & 3 deletions tests/Controller/ApiControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
16 changes: 16 additions & 0 deletions tests/Controller/ControllerTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}
17 changes: 3 additions & 14 deletions tests/Controller/WebControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 2 additions & 3 deletions tests/Package/UpdaterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');

0 comments on commit 7f04d3d

Please sign in to comment.