-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
104 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Neusta\Pimcore\TestingFramework\Internal; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; | ||
|
||
/** @internal */ | ||
final class AttributeProvider | ||
{ | ||
/** | ||
* @template T | ||
* | ||
* @param class-string<T> $name | ||
* | ||
* @return list<T> | ||
*/ | ||
public static function getAttributes(KernelTestCase $testCase, string $name): array | ||
{ | ||
$class = new \ReflectionClass($testCase); | ||
$method = $class->getMethod($testCase->getName(false)); | ||
|
||
$attributes = [ | ||
...self::doGetAttributes($class, $name), | ||
...self::doGetAttributes($method, $name), | ||
...self::getAttributesFromProvidedData($testCase, $name), | ||
]; | ||
|
||
// remove them from the arguments passed to the test method | ||
self::removeAttributesFromProvidedData($testCase, $name); | ||
|
||
return $attributes; | ||
} | ||
|
||
/** | ||
* @template T | ||
* | ||
* @param class-string<T> $name | ||
* | ||
* @return list<T> | ||
*/ | ||
private static function doGetAttributes(\ReflectionClass|\ReflectionMethod $source, string $name): array | ||
{ | ||
$attributes = []; | ||
foreach ($source->getAttributes($name, \ReflectionAttribute::IS_INSTANCEOF) as $attribute) { | ||
$attributes[] = $attribute->newInstance(); | ||
} | ||
|
||
return $attributes; | ||
} | ||
|
||
/** | ||
* @template T | ||
* | ||
* @param class-string<T> $name | ||
* | ||
* @return list<T> | ||
*/ | ||
private static function getAttributesFromProvidedData(KernelTestCase $testCase, string $name): array | ||
{ | ||
$attributes = []; | ||
foreach ($testCase->getProvidedData() as $data) { | ||
if ($data instanceof $name) { | ||
$attributes[] = $data; | ||
} | ||
} | ||
|
||
return $attributes; | ||
} | ||
|
||
/** | ||
* @param class-string $name | ||
*/ | ||
private static function removeAttributesFromProvidedData(KernelTestCase $testCase, string $name): void | ||
{ | ||
if ([] === $providedData = $testCase->getProvidedData()) { | ||
return; | ||
} | ||
|
||
$filteredData = array_values(array_filter($providedData, fn ($data) => !$data instanceof $name)); | ||
|
||
(new \ReflectionProperty(TestCase::class, 'data'))->setValue($testCase, $filteredData); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters