From f4b1111aaf072cd7c0a0d590b27e4ed974a60385 Mon Sep 17 00:00:00 2001 From: Maxim Smakouz Date: Thu, 28 Sep 2023 13:29:10 +0300 Subject: [PATCH] Add the ability to use setUp and tearDown methods in traits --- src/TestCase.php | 26 +++++++ .../Fixture/Trait/WithMethodsTrait.php | 21 ++++++ .../TestCase/Fixture/Trait/WithSetUpTrait.php | 15 +++++ .../Fixture/Trait/WithTearDownTrait.php | 15 +++++ .../Fixture/Trait/WithoutMethodsTrait.php | 13 ++++ tests/src/TestCase/Fixture/WithMethods.php | 13 ++++ tests/src/TestCase/Fixture/WithSetUp.php | 13 ++++ tests/src/TestCase/Fixture/WithTearDown.php | 13 ++++ tests/src/TestCase/Fixture/WithoutMethods.php | 13 ++++ tests/src/TestCase/Fixture/WithoutTraits.php | 11 +++ tests/src/TestCase/TestCaseTest.php | 67 +++++++++++++++++++ 11 files changed, 220 insertions(+) create mode 100644 tests/src/TestCase/Fixture/Trait/WithMethodsTrait.php create mode 100644 tests/src/TestCase/Fixture/Trait/WithSetUpTrait.php create mode 100644 tests/src/TestCase/Fixture/Trait/WithTearDownTrait.php create mode 100644 tests/src/TestCase/Fixture/Trait/WithoutMethodsTrait.php create mode 100644 tests/src/TestCase/Fixture/WithMethods.php create mode 100644 tests/src/TestCase/Fixture/WithSetUp.php create mode 100644 tests/src/TestCase/Fixture/WithTearDown.php create mode 100644 tests/src/TestCase/Fixture/WithoutMethods.php create mode 100644 tests/src/TestCase/Fixture/WithoutTraits.php create mode 100644 tests/src/TestCase/TestCaseTest.php diff --git a/src/TestCase.php b/src/TestCase.php index 22227e6..79f6bfa 100644 --- a/src/TestCase.php +++ b/src/TestCase.php @@ -83,6 +83,8 @@ protected function setUp(): void $variables = [...static::ENV, ...$this->getEnvVariablesFromConfig()]; $this->initApp($variables); } + + $this->setUpTraits(); } public function beforeBooting(Closure $callback): void @@ -168,6 +170,8 @@ protected function tearDown(): void { parent::tearDown(); + $this->tearDownTraits(); + (new \ReflectionClass(ContainerScope::class)) ->setStaticPropertyValue('container', null); } @@ -195,4 +199,26 @@ protected function getTestAttributes(string $attribute, string $method = null): return []; } } + + protected function setUpTraits(): void + { + $ref = new \ReflectionClass(static::class); + + foreach ($ref->getTraits() as $trait) { + if (\method_exists($this, $method = 'setUp' . $trait->getShortName())) { + $this->{$method}(); + } + } + } + + protected function tearDownTraits(): void + { + $ref = new \ReflectionClass(static::class); + + foreach ($ref->getTraits() as $trait) { + if (\method_exists($this, $method = 'tearDown' . $trait->getShortName())) { + $this->{$method}(); + } + } + } } diff --git a/tests/src/TestCase/Fixture/Trait/WithMethodsTrait.php b/tests/src/TestCase/Fixture/Trait/WithMethodsTrait.php new file mode 100644 index 0000000..0b7fe3b --- /dev/null +++ b/tests/src/TestCase/Fixture/Trait/WithMethodsTrait.php @@ -0,0 +1,21 @@ +calledSetUp = true; + } + + public function tearDownWithMethodsTrait(): void + { + $this->calledTearDown = true; + } +} diff --git a/tests/src/TestCase/Fixture/Trait/WithSetUpTrait.php b/tests/src/TestCase/Fixture/Trait/WithSetUpTrait.php new file mode 100644 index 0000000..bedf7ca --- /dev/null +++ b/tests/src/TestCase/Fixture/Trait/WithSetUpTrait.php @@ -0,0 +1,15 @@ +calledSetUp = true; + } +} diff --git a/tests/src/TestCase/Fixture/Trait/WithTearDownTrait.php b/tests/src/TestCase/Fixture/Trait/WithTearDownTrait.php new file mode 100644 index 0000000..7c26c6a --- /dev/null +++ b/tests/src/TestCase/Fixture/Trait/WithTearDownTrait.php @@ -0,0 +1,15 @@ +calledTearDown = true; + } +} diff --git a/tests/src/TestCase/Fixture/Trait/WithoutMethodsTrait.php b/tests/src/TestCase/Fixture/Trait/WithoutMethodsTrait.php new file mode 100644 index 0000000..39236a9 --- /dev/null +++ b/tests/src/TestCase/Fixture/Trait/WithoutMethodsTrait.php @@ -0,0 +1,13 @@ +setUp(); + } + + #[DoesNotPerformAssertions] + public function testItDoesNotThrowWhenCallingTearDown(): void + { + $testCase = new WithoutTraits('foo'); + $testCase->tearDown(); + } + + public function testTraitWithoutMethods(): void + { + $testCase = new WithoutMethods('foo'); + $testCase->setUp(); + $testCase->tearDown(); + + $this->assertTrue($testCase->isAvailable()); + } + + public function testTraitWithSetUp(): void + { + $testCase = new WithSetUp('foo'); + $testCase->setUp(); + $testCase->tearDown(); + + $this->assertTrue($testCase->calledSetUp); + } + + public function testTraitWithTearDown(): void + { + $testCase = new WithTearDown('foo'); + $testCase->setUp(); + $testCase->tearDown(); + + $this->assertTrue($testCase->calledTearDown); + } + + public function testTraitWithSetUpAndTearDownMethods(): void + { + $testCase = new WithMethods('foo'); + $testCase->setUp(); + $testCase->tearDown(); + + $this->assertTrue($testCase->calledSetUp); + $this->assertTrue($testCase->calledTearDown); + } +}