mirror of
https://github.com/pestphp/pest.git
synced 2026-03-09 17:27:22 +01:00
feat: always use attributes instead of annotations
This commit is contained in:
@ -6,8 +6,8 @@ namespace Pest\Factories;
|
||||
|
||||
use ParseError;
|
||||
use Pest\Concerns;
|
||||
use Pest\Contracts\AddsAnnotations;
|
||||
use Pest\Contracts\HasPrintableTestCaseName;
|
||||
use Pest\Evaluators\Attributes;
|
||||
use Pest\Exceptions\DatasetMissing;
|
||||
use Pest\Exceptions\ShouldNotHappen;
|
||||
use Pest\Exceptions\TestAlreadyExist;
|
||||
@ -26,25 +26,12 @@ final class TestCaseFactory
|
||||
{
|
||||
use HigherOrderable;
|
||||
|
||||
/**
|
||||
* The list of annotations.
|
||||
*
|
||||
* @var array<int, class-string<AddsAnnotations>>
|
||||
*/
|
||||
private const ANNOTATIONS = [
|
||||
Annotations\Depends::class,
|
||||
Annotations\Groups::class,
|
||||
Annotations\TestDox::class,
|
||||
];
|
||||
|
||||
/**
|
||||
* The list of attributes.
|
||||
*
|
||||
* @var array<int, class-string<\Pest\Factories\Attributes\Attribute>>
|
||||
* @var iterable<int, Attribute>
|
||||
*/
|
||||
private const ATTRIBUTES = [
|
||||
Attributes\Covers::class,
|
||||
];
|
||||
public iterable $attributes = [];
|
||||
|
||||
/**
|
||||
* The FQN of the Test Case class.
|
||||
@ -145,32 +132,21 @@ final class TestCaseFactory
|
||||
$className = 'InvalidTestName'.Str::random();
|
||||
}
|
||||
|
||||
$classAvailableAttributes = array_filter(self::ATTRIBUTES, fn (string $attribute): bool => $attribute::$above);
|
||||
$methodAvailableAttributes = array_filter(self::ATTRIBUTES, fn (string $attribute): bool => ! $attribute::$above);
|
||||
$this->attributes = [
|
||||
new Attribute(
|
||||
\PHPUnit\Framework\Attributes\TestDox::class,
|
||||
[$this->filename],
|
||||
),
|
||||
...$this->attributes,
|
||||
];
|
||||
|
||||
$classAttributes = [];
|
||||
|
||||
foreach ($classAvailableAttributes as $attribute) {
|
||||
$classAttributes = array_reduce(
|
||||
$methods,
|
||||
fn (array $carry, TestCaseMethodFactory $methodFactory): array => (new $attribute())->__invoke($methodFactory, $carry),
|
||||
$classAttributes
|
||||
);
|
||||
}
|
||||
$attributesCode = Attributes::code($this->attributes);
|
||||
|
||||
$methodsCode = implode('', array_map(
|
||||
fn (TestCaseMethodFactory $methodFactory): string => $methodFactory->buildForEvaluation(
|
||||
self::ANNOTATIONS,
|
||||
$methodAvailableAttributes
|
||||
),
|
||||
fn (TestCaseMethodFactory $methodFactory): string => $methodFactory->buildForEvaluation(),
|
||||
$methods
|
||||
));
|
||||
|
||||
$classAttributesCode = implode('', array_map(
|
||||
static fn (string $attribute): string => sprintf("\n%s", $attribute),
|
||||
array_unique($classAttributes),
|
||||
));
|
||||
|
||||
try {
|
||||
$classCode = <<<PHP
|
||||
namespace $namespace;
|
||||
@ -178,10 +154,7 @@ final class TestCaseFactory
|
||||
use Pest\Repositories\DatasetsRepository as __PestDatasets;
|
||||
use Pest\TestSuite as __PestTestSuite;
|
||||
|
||||
/**
|
||||
* @testdox $filename
|
||||
*/
|
||||
$classAttributesCode
|
||||
$attributesCode
|
||||
#[\AllowDynamicProperties]
|
||||
final class $className extends $baseClass implements $hasPrintableTestCaseClassFQN {
|
||||
$traitsCode
|
||||
|
||||
Reference in New Issue
Block a user