From 8324cd63884718a70cc65cecacf71d80b525e96f 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) --- .../Test/KernelShutdownOnTearDownTrait.php | 45 +++++++++++++++++++ .../FrameworkBundle/Test/KernelTestCase.php | 12 ++--- 2 files changed, 48 insertions(+), 9 deletions(-) create mode 100644 src/Symfony/Bundle/FrameworkBundle/Test/KernelShutdownOnTearDownTrait.php diff --git a/src/Symfony/Bundle/FrameworkBundle/Test/KernelShutdownOnTearDownTrait.php b/src/Symfony/Bundle/FrameworkBundle/Test/KernelShutdownOnTearDownTrait.php new file mode 100644 index 0000000000000..7eb4d0726e820 --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Test/KernelShutdownOnTearDownTrait.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 that added a `void` return-type to the tearDown method + +if (method_exists(\ReflectionMethod::class, 'hasReturnType') && (new \ReflectionMethod(TestCase::class, 'tearDown'))->hasReturnType()) { +eval(' + /** + * @internal + */ + trait KernelShutdownOnTearDownTrait + { + protected function tearDown(): void + { + static::ensureKernelShutdown(); + } + } +'); +} else { + /** + * @internal + */ + trait KernelShutdownOnTearDownTrait + { + /** + * @return void + */ + protected function tearDown() + { + static::ensureKernelShutdown(); + } + } +} diff --git a/src/Symfony/Bundle/FrameworkBundle/Test/KernelTestCase.php b/src/Symfony/Bundle/FrameworkBundle/Test/KernelTestCase.php index 0feb15380e1c4..978f65863220c 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 KernelShutdownOnTearDownTrait; + 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(); - } }