From d18a2d88ccbc507ed84edcc6878afcfee8e9a9cf Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Sat, 9 Feb 2019 08:43:34 +0100 Subject: [PATCH] Fix KernelTestCase compatibility for PhpUnit 8 (bis) --- .../FrameworkBundle/Test/KernelTestCase.php | 12 ++--- .../Test/KernelTestCaseTrait.php | 45 +++++++++++++++++++ 2 files changed, 48 insertions(+), 9 deletions(-) create mode 100644 src/Symfony/Bundle/FrameworkBundle/Test/KernelTestCaseTrait.php diff --git a/src/Symfony/Bundle/FrameworkBundle/Test/KernelTestCase.php b/src/Symfony/Bundle/FrameworkBundle/Test/KernelTestCase.php index 0feb15380e1c4..beccc8b0a7781 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Test/KernelTestCase.php +++ b/src/Symfony/Bundle/FrameworkBundle/Test/KernelTestCase.php @@ -23,6 +23,8 @@ */ abstract class KernelTestCase extends TestCase { + use KernelTestCaseTrait; + protected static $class; /** @@ -208,7 +210,7 @@ protected static function createKernel(array $options = []) } /** - * Shuts the kernel down if it was used in the test. + * Shuts the kernel down if it was used in the test - called by the tearDown method by default. */ protected static function ensureKernelShutdown() { @@ -220,12 +222,4 @@ protected static function ensureKernelShutdown() } } } - - /** - * Clean up Kernel usage in this test. - */ - protected function tearDown() - { - static::ensureKernelShutdown(); - } } diff --git a/src/Symfony/Bundle/FrameworkBundle/Test/KernelTestCaseTrait.php b/src/Symfony/Bundle/FrameworkBundle/Test/KernelTestCaseTrait.php new file mode 100644 index 0000000000000..4b9b5f5aa5997 --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Test/KernelTestCaseTrait.php @@ -0,0 +1,45 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bundle\FrameworkBundle\Test; + +use PHPUnit\Framework\TestCase; + +// Auto-adapt to PHPUnit 8 who added a `void` return-type to the tearDown method + +if (method_exists(\ReflectionMethod::class, 'hasReturnType') && (new \ReflectionMethod(TestCase::class, 'tearDown'))->hasReturnType()) { +eval(' + /** + * @internal + */ + trait KernelTestCaseTrait + { + protected function tearDown(): void + { + static::ensureKernelShutdown(); + } + } +'); +} else { + /** + * @internal + */ + trait KernelTestCaseTrait + { + /** + * @return void + */ + protected function tearDown() + { + static::ensureKernelShutdown(); + } + } +}