Skip to content

Commit

Permalink
Search traits in parent classes
Browse files Browse the repository at this point in the history
  • Loading branch information
msmakouz committed Sep 28, 2023
1 parent f4b1111 commit 50e36bf
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 9 deletions.
27 changes: 18 additions & 9 deletions src/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,23 +202,32 @@ protected function getTestAttributes(string $attribute, string $method = null):

protected function setUpTraits(): void
{
$ref = new \ReflectionClass(static::class);

foreach ($ref->getTraits() as $trait) {
if (\method_exists($this, $method = 'setUp' . $trait->getShortName())) {
$this->{$method}();
}
}
$this->runTraitSetUpOrTearDown('setUp');
}

protected function tearDownTraits(): void
{
$this->runTraitSetUpOrTearDown('tearDown');
}

private function runTraitSetUpOrTearDown(string $method): void
{
$ref = new \ReflectionClass(static::class);

foreach ($ref->getTraits() as $trait) {
if (\method_exists($this, $method = 'tearDown' . $trait->getShortName())) {
$this->{$method}();
if (\method_exists($this, $name = $method . $trait->getShortName())) {
$this->{$name}();
}
}

while($parent = $ref->getParentClass()) {
foreach ($parent->getTraits() as $trait) {
if (\method_exists($this, $name = $method . $trait->getShortName())) {
$this->{$name}();
}
}

$ref = $parent;
}
}
}
9 changes: 9 additions & 0 deletions tests/src/TestCase/Fixture/OtherParentClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Spiral\Testing\Tests\TestCase\Fixture;

class OtherParentClass extends ParentClass
{
}
13 changes: 13 additions & 0 deletions tests/src/TestCase/Fixture/ParentClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Spiral\Testing\Tests\TestCase\Fixture;

use Spiral\Testing\TestCase;
use Spiral\Testing\Tests\TestCase\Fixture\Trait\WithMethodsTrait;

class ParentClass extends TestCase
{
use WithMethodsTrait;
}
9 changes: 9 additions & 0 deletions tests/src/TestCase/Fixture/WithMethodsInNestedParent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Spiral\Testing\Tests\TestCase\Fixture;

final class WithMethodsInNestedParent extends OtherParentClass
{
}
9 changes: 9 additions & 0 deletions tests/src/TestCase/Fixture/WithMethodsInParent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Spiral\Testing\Tests\TestCase\Fixture;

final class WithMethodsInParent extends ParentClass
{
}
28 changes: 28 additions & 0 deletions tests/src/TestCase/TestCaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,28 @@
use PHPUnit\Framework\Attributes\DoesNotPerformAssertions;
use PHPUnit\Framework\TestCase;
use Spiral\Testing\Tests\TestCase\Fixture\WithMethods;
use Spiral\Testing\Tests\TestCase\Fixture\WithMethodsInNestedParent;
use Spiral\Testing\Tests\TestCase\Fixture\WithMethodsInParent;
use Spiral\Testing\Tests\TestCase\Fixture\WithoutMethods;
use Spiral\Testing\Tests\TestCase\Fixture\WithoutTraits;
use Spiral\Testing\Tests\TestCase\Fixture\WithSetUp;
use Spiral\Testing\Tests\TestCase\Fixture\WithTearDown;

final class TestCaseTest extends TestCase
{
/**
* @doesNotPerformAssertions
*/
#[DoesNotPerformAssertions]
public function testItDoesNotThrowWhenCallingSetUp(): void
{
$testCase = new WithoutTraits('foo');
$testCase->setUp();
}

/**
* @doesNotPerformAssertions
*/
#[DoesNotPerformAssertions]
public function testItDoesNotThrowWhenCallingTearDown(): void
{
Expand Down Expand Up @@ -64,4 +72,24 @@ public function testTraitWithSetUpAndTearDownMethods(): void
$this->assertTrue($testCase->calledSetUp);
$this->assertTrue($testCase->calledTearDown);
}

public function testTraitWithSetUpAndTearDownMethodsInParentClass(): void
{
$testCase = new WithMethodsInParent('foo');
$testCase->setUp();
$testCase->tearDown();

$this->assertTrue($testCase->calledSetUp);
$this->assertTrue($testCase->calledTearDown);
}

public function testTraitWithSetUpAndTearDownMethodsInNestedParentClass(): void
{
$testCase = new WithMethodsInNestedParent('foo');
$testCase->setUp();
$testCase->tearDown();

$this->assertTrue($testCase->calledSetUp);
$this->assertTrue($testCase->calledTearDown);
}
}

0 comments on commit 50e36bf

Please sign in to comment.