Skip to content

Commit

Permalink
[8.x] Inherits Attributes defined on parent TestCase (#233)
Browse files Browse the repository at this point in the history
* See parent class attributes

* Update InheritanceTest.php

* wip

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>

* wip

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>

* wip

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>

---------

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>
Co-authored-by: Mior Muhammad Zaki <crynobone@gmail.com>
  • Loading branch information
BlackLanzer and crynobone committed Sep 13, 2024
1 parent 57cc64e commit f8bce62
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 3 deletions.
9 changes: 8 additions & 1 deletion src/PHPUnit/AttributeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ class AttributeParser
public static function forClass(string $className): array
{
$attributes = [];
$reflection = new ReflectionClass($className);

foreach ((new ReflectionClass($className))->getAttributes() as $attribute) {
foreach ($reflection->getAttributes() as $attribute) {
if (! static::validAttribute($attribute->getName())) {
continue;
}
Expand All @@ -40,6 +41,12 @@ public static function forClass(string $className): array
}
}

$parent = $reflection->getParentClass();

if ($parent && $parent->isSubclassOf(TestCase::class)) {
$attributes = [...static::forClass($parent->getName()), ...$attributes];
}

return $attributes;
}

Expand Down
44 changes: 44 additions & 0 deletions tests/Attributes/AttributesInheritanceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Orchestra\Testbench\Tests\Attributes;

use Orchestra\Testbench\Attributes\WithConfig;
use Orchestra\Testbench\TestCase;

#[WithConfig('fake.parent_attribute', true)]
#[WithConfig('fake.override_attribute', 'parent')]
abstract class AttributesInheritanceTestBaseTestCase extends TestCase
{
/**
* @beforeClass
*/
public static function defineTestingFeatures()
{
static::usesTestingFeature(new WithConfig('fake.override_attribute_2', 'parent'));
}
}

#[WithConfig('fake.override_attribute', 'child')]
class AttributesInheritanceTest extends AttributesInheritanceTestBaseTestCase
{
/**
* @beforeClass
*/
public static function defineChildTestingFeatures()
{
static::usesTestingFeature(new WithConfig('fake.override_attribute_2', 'child'));
}

/** @test */
public function it_can_see_parent_attributes()
{
$this->assertSame(true, config('fake.parent_attribute'));
}

/** @test */
public function it_can_override_parent_attributes()
{
$this->assertSame('child', config('fake.override_attribute'));
$this->assertSame('child', config('fake.override_attribute_2'));
}
}
4 changes: 2 additions & 2 deletions tests/Attributes/UsesTestingFeaturesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Orchestra\Testbench\Attributes\WithConfig;
use Orchestra\Testbench\TestCase;

abstract class BaseTestCase extends TestCase
abstract class UsesTestingFeaturesTestBaseTestCase extends TestCase
{
/**
* @beforeClass
Expand All @@ -19,7 +19,7 @@ public static function defineTestingFeatures()
}

#[WithConfig('fake.override_attribute', 'child')]
class UsesTestingFeaturesTest extends BaseTestCase
class UsesTestingFeaturesTest extends UsesTestingFeaturesTestBaseTestCase
{
/**
* @beforeClass
Expand Down

0 comments on commit f8bce62

Please sign in to comment.