From 5ef254fa65ac0454918ff0ee62835e8da749d39c Mon Sep 17 00:00:00 2001 From: Massimiliano Arione Date: Thu, 7 Mar 2019 11:36:06 +0100 Subject: [PATCH] compatibility with phpunit8 --- .../Form/Test/FormIntegrationTestCase.php | 4 +- .../Form/Test/TestCaseSetUpTearDownTrait.php | 82 +++++++++++++++++++ .../Component/Form/Test/TypeTestCase.php | 6 +- .../Test/ConstraintValidatorTestCase.php | 6 +- .../Test/TestCaseSetUpTearDownTrait.php | 82 +++++++++++++++++++ 5 files changed, 175 insertions(+), 5 deletions(-) create mode 100644 src/Symfony/Component/Form/Test/TestCaseSetUpTearDownTrait.php create mode 100644 src/Symfony/Component/Validator/Test/TestCaseSetUpTearDownTrait.php diff --git a/src/Symfony/Component/Form/Test/FormIntegrationTestCase.php b/src/Symfony/Component/Form/Test/FormIntegrationTestCase.php index 7aa7c1c438..b50d943779 100644 --- a/src/Symfony/Component/Form/Test/FormIntegrationTestCase.php +++ b/src/Symfony/Component/Form/Test/FormIntegrationTestCase.php @@ -20,12 +20,14 @@ use Symfony\Component\Form\Forms; */ abstract class FormIntegrationTestCase extends TestCase { + use TestCaseSetUpTearDownTrait; + /** * @var FormFactoryInterface */ protected $factory; - protected function setUp() + private function doSetUp() { $this->factory = Forms::createFormFactoryBuilder() ->addExtensions($this->getExtensions()) diff --git a/src/Symfony/Component/Form/Test/TestCaseSetUpTearDownTrait.php b/src/Symfony/Component/Form/Test/TestCaseSetUpTearDownTrait.php new file mode 100644 index 0000000000..279755f758 --- /dev/null +++ b/src/Symfony/Component/Form/Test/TestCaseSetUpTearDownTrait.php @@ -0,0 +1,82 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Form\Test; + +use PHPUnit\Framework\TestCase; + +// Auto-adapt to PHPUnit 8 that added a `void` return-type to the setUp/tearDown methods + +if (method_exists(\ReflectionMethod::class, 'hasReturnType') && (new \ReflectionMethod(TestCase::class, 'tearDown'))->hasReturnType()) { + eval(' + namespace Symfony\Component\Form\Test; + + /** + * @internal + */ + trait TestCaseSetUpTearDownTrait + { + private function doSetUp(): void + { + } + + private function doTearDown(): void + { + } + + protected function setUp(): void + { + $this->doSetUp(); + } + + protected function tearDown(): void + { + $this->doTearDown(); + } + } +'); +} else { + /** + * @internal + */ + trait TestCaseSetUpTearDownTrait + { + /** + * @return void + */ + private function doSetUp() + { + } + + /** + * @return void + */ + private function doTearDown() + { + } + + /** + * @return void + */ + protected function setUp() + { + $this->doSetUp(); + } + + /** + * @return void + */ + protected function tearDown() + { + $this->doTearDown(); + } + } +} diff --git a/src/Symfony/Component/Form/Test/TypeTestCase.php b/src/Symfony/Component/Form/Test/TypeTestCase.php index 7c9aa96a9e..19fb5c32a0 100644 --- a/src/Symfony/Component/Form/Test/TypeTestCase.php +++ b/src/Symfony/Component/Form/Test/TypeTestCase.php @@ -17,6 +17,8 @@ use Symfony\Component\Form\Test\Traits\ValidatorExtensionTrait; abstract class TypeTestCase extends FormIntegrationTestCase { + use TestCaseSetUpTearDownTrait; + /** * @var FormBuilder */ @@ -27,7 +29,7 @@ abstract class TypeTestCase extends FormIntegrationTestCase */ protected $dispatcher; - protected function setUp() + private function doSetUp() { parent::setUp(); @@ -35,7 +37,7 @@ abstract class TypeTestCase extends FormIntegrationTestCase $this->builder = new FormBuilder('', null, $this->dispatcher, $this->factory); } - protected function tearDown() + private function doTearDown() { if (\in_array(ValidatorExtensionTrait::class, class_uses($this))) { $this->validator = null; diff --git a/src/Symfony/Component/Validator/Test/ConstraintValidatorTestCase.php b/src/Symfony/Component/Validator/Test/ConstraintValidatorTestCase.php index afe1f1a582..f07adb9423 100644 --- a/src/Symfony/Component/Validator/Test/ConstraintValidatorTestCase.php +++ b/src/Symfony/Component/Validator/Test/ConstraintValidatorTestCase.php @@ -29,6 +29,8 @@ use Symfony\Component\Validator\Mapping\PropertyMetadata; */ abstract class ConstraintValidatorTestCase extends TestCase { + use TestCaseSetUpTearDownTrait; + /** * @var ExecutionContextInterface */ @@ -48,7 +50,7 @@ abstract class ConstraintValidatorTestCase extends TestCase protected $constraint; protected $defaultTimezone; - protected function setUp() + private function doSetUp() { $this->group = 'MyGroup'; $this->metadata = null; @@ -70,7 +72,7 @@ abstract class ConstraintValidatorTestCase extends TestCase $this->setDefaultTimezone('UTC'); } - protected function tearDown() + private function doTearDown() { $this->restoreDefaultTimezone(); } diff --git a/src/Symfony/Component/Validator/Test/TestCaseSetUpTearDownTrait.php b/src/Symfony/Component/Validator/Test/TestCaseSetUpTearDownTrait.php new file mode 100644 index 0000000000..426c148913 --- /dev/null +++ b/src/Symfony/Component/Validator/Test/TestCaseSetUpTearDownTrait.php @@ -0,0 +1,82 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Validator\Test; + +use PHPUnit\Framework\TestCase; + +// Auto-adapt to PHPUnit 8 that added a `void` return-type to the setUp/tearDown methods + +if (method_exists(\ReflectionMethod::class, 'hasReturnType') && (new \ReflectionMethod(TestCase::class, 'tearDown'))->hasReturnType()) { + eval(' + namespace Symfony\Component\Validator\Test; + + /** + * @internal + */ + trait TestCaseSetUpTearDownTrait + { + private function doSetUp(): void + { + } + + private function doTearDown(): void + { + } + + protected function setUp(): void + { + $this->doSetUp(); + } + + protected function tearDown(): void + { + $this->doTearDown(); + } + } +'); +} else { + /** + * @internal + */ + trait TestCaseSetUpTearDownTrait + { + /** + * @return void + */ + private function doSetUp() + { + } + + /** + * @return void + */ + private function doTearDown() + { + } + + /** + * @return void + */ + protected function setUp() + { + $this->doSetUp(); + } + + /** + * @return void + */ + protected function tearDown() + { + $this->doTearDown(); + } + } +}