Skip to content

Commit

Permalink
Merge pull request #584 from k20human/bugfix/583
Browse files Browse the repository at this point in the history
Fix new static method getContainer in KernelTestCase collision
  • Loading branch information
alexislefebvre authored Jun 1, 2021
2 parents 436c03c + 4754c6a commit 51fd5ba
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
matrix:
php-version: ['7.2', '7.3', '7.4', '8.0']
composer-flags: ['']
symfony-version: ['5.1']
symfony-version: ['^5.3']
include:
- php-version: 7.2
symfony-version: "^4.4"
Expand Down
39 changes: 28 additions & 11 deletions src/Test/WebTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class_alias(KernelBrowser::class, Client::class);
* @author Lea Haensenberger
* @author Lukas Kahwe Smith <[email protected]>
* @author Benjamin Eberlei <[email protected]>
*
* @method ContainerInterface getContainer()
*/
abstract class WebTestCase extends BaseWebTestCase
{
Expand Down Expand Up @@ -68,7 +70,7 @@ abstract class WebTestCase extends BaseWebTestCase
*/
protected function getServiceMockBuilder(string $id): MockBuilder
{
$service = $this->getContainer()->get($id);
$service = $this->getDependencyInjectionContainer()->get($id);
$class = get_class($service);

return $this->getMockBuilder($class)->disableOriginalConstructor();
Expand All @@ -87,7 +89,7 @@ protected function runCommand(string $name, array $params = [], bool $reuseKerne
$kernel = static::$kernel = static::createKernel(['environment' => $this->environment]);
$kernel->boot();
} else {
$kernel = $this->getContainer()->get('kernel');
$kernel = $this->getDependencyInjectionContainer()->get('kernel');
}

$application = new Application($kernel);
Expand Down Expand Up @@ -127,7 +129,7 @@ protected function getVerbosityLevel(): int
// If `null`, is not yet set
if (null === $this->verbosityLevel) {
// Set the global verbosity level that is set as NORMAL by the TreeBuilder in Configuration
$level = strtoupper($this->getContainer()->getParameter('liip_functional_test.command_verbosity'));
$level = strtoupper($this->getDependencyInjectionContainer()->getParameter('liip_functional_test.command_verbosity'));
$verbosity = '\Symfony\Component\Console\Output\StreamOutput::VERBOSITY_'.$level;

$this->verbosityLevel = constant($verbosity);
Expand Down Expand Up @@ -182,7 +184,7 @@ protected function getDecorated(): bool
{
if (null === $this->decorated) {
// Set the global decoration flag that is set to `true` by the TreeBuilder in Configuration
$this->decorated = $this->getContainer()->getParameter('liip_functional_test.command_decoration');
$this->decorated = $this->getDependencyInjectionContainer()->getParameter('liip_functional_test.command_decoration');
}

// Check the local decorated flag
Expand All @@ -202,14 +204,13 @@ public function isDecorated(bool $decorated): void
* Get an instance of the dependency injection container.
* (this creates a kernel *without* parameters).
*/
protected function getContainer(): ContainerInterface
protected function getDependencyInjectionContainer(): ContainerInterface
{
$cacheKey = $this->environment;
if (empty($this->containers[$cacheKey])) {
$options = [
$kernel = static::createKernel([
'environment' => $this->environment,
];
$kernel = $this->createKernel($options);
]);
$kernel->boot();

$container = $kernel->getContainer();
Expand All @@ -223,6 +224,22 @@ protected function getContainer(): ContainerInterface
return $this->containers[$cacheKey];
}

/**
* Keep support of Symfony < 5.3.
*/
public function __call(string $name, $arguments)
{
if ('getContainer' === $name) {
if (method_exists($this, $name)) {
return self::getContainer();
}

return $this->getDependencyInjectionContainer();
}

throw new \Exception("Method {$name} is not supported.");
}

/**
* Creates an instance of a lightweight Http client.
*
Expand All @@ -244,9 +261,9 @@ protected function makeClient(array $params = []): Client
*/
protected function makeAuthenticatedClient(array $params = []): Client
{
$username = $this->getContainer()
$username = $this->getDependencyInjectionContainer()
->getParameter('liip_functional_test.authentication.username');
$password = $this->getContainer()
$password = $this->getDependencyInjectionContainer()
->getParameter('liip_functional_test.authentication.password');

return $this->createClientWithParams($params, $username, $password);
Expand Down Expand Up @@ -296,7 +313,7 @@ protected function createUserToken(UserInterface $user, string $firewallName): T
*/
protected function getUrl(string $route, array $params = [], int $absolute = UrlGeneratorInterface::ABSOLUTE_PATH): string
{
return $this->getContainer()->get('router')->generate($route, $params, $absolute);
return $this->getDependencyInjectionContainer()->get('router')->generate($route, $params, $absolute);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/Test/WebTestCaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public function testAssertStatusCodeException(): void
try {
$this->assertStatusCode(-1, $client);
} catch (AssertionFailedError $e) {
$this->assertStringContainsString('No route found for "GET /9999"', $e->getMessage());
$this->assertStringContainsString('No route found', $e->getMessage());
$this->assertStringContainsString('Symfony\Component\HttpKernel\EventListener\RouterListener->onKernelRequest(', $e->getMessage());
$this->assertStringContainsString('Failed asserting that 404 matches expected -1.', $e->getMessage());

Expand Down
4 changes: 2 additions & 2 deletions tests/Traits/LiipAcmeFixturesTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ trait LiipAcmeFixturesTrait
public function schemaUpdate(): void
{
// Create database
$kernel = $this->getContainer()->get('kernel');
$kernel = $this->getDependencyInjectionContainer()->get('kernel');

$application = new Application($kernel);

Expand All @@ -46,7 +46,7 @@ public function loadTestFixtures(): User
$user1->setEnabled(true);
$user1->setConfirmationToken(null);

$manager = $this->getContainer()->get('doctrine')->getManager();
$manager = $this->getDependencyInjectionContainer()->get('doctrine')->getManager();
$manager->persist($user1);

$user2 = clone $user1;
Expand Down

0 comments on commit 51fd5ba

Please sign in to comment.