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); + } +}