I am trying to create a PHPunit test accessing services from the container in symfony. To do so I call self::bootKernel
in setUpBeforeClass
. In the first test, the self::$kernel
is ok, but the second test self::$kernel
is null.
This is a simple test to check it:
<?php
namespace App\Tests;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class KernelTest extends WebTestCase
{
public static function setUpBeforeClass(): void
{
self::bootKernel();
}
public function testMethod1(){
$this->assertNotNull(self::$kernel);
}
public function testMethod2(){
$this->assertNotNull(self::$kernel);
}
}
This produces the following output:
$ bin/phpunit tests/KernelTest.php
PHPUnit 7.5.18 by Sebastian Bergmann and contributors.
Notice: Undefined index: warnings in /app/vendor/phpunit/phpunit/src/TextUI/TestRunner.php on line 334
Warning: Invalid argument supplied for foreach() in /app/vendor/phpunit/phpunit/src/TextUI/TestRunner.php on line 334
Testing App\Tests\KernelTest
.F 2 / 2 (100%)
Time: 58 ms, Memory: 16.00 MB
There was 1 failure:
1) App\Tests\KernelTest::testMethod2
Failed asserting that null is not null.
/app/tests/KernelTest.php:19
FAILURES!
Tests: 2, Assertions: 2, Failures: 1.
As you can see, the second test fails the assertion.
This is the version that I am running of phpunit:
phpunit/phpunit 8.5.2 The PHP Unit Testing framework.
symfony/phpunit-bridge v5.0.2 Symfony PHPUnit Bridge
What I am doing wrong? Should I boot kernel in each test? I would like to boot it only once.
test...
and just call them in a combinedtestAll
method, of course). the kernel assumes it's clean when it's confronted with a new request, what tests usually test, so it's reasonable to kill the kernel after a test.