mirror of
https://github.com/pestphp/pest.git
synced 2026-03-07 00:07:22 +01:00
move covers attribute above the class
This commit is contained in:
18
src/Factories/Attributes/Attribute.php
Normal file
18
src/Factories/Attributes/Attribute.php
Normal file
@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Pest\Factories\Attributes;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
abstract class Attribute
|
||||
{
|
||||
/**
|
||||
* Determine if the attribute should be placed above the classe instead of above the method.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
public const ABOVE_CLASS = false;
|
||||
}
|
||||
@ -11,8 +11,15 @@ use Pest\Factories\TestCaseMethodFactory;
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
final class Covers
|
||||
final class Covers extends Attribute
|
||||
{
|
||||
/**
|
||||
* Determine if the attribute should be placed above the classe instead of above the method.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
public const ABOVE_CLASS = true;
|
||||
|
||||
/**
|
||||
* Adds attributes regarding the "covers" feature.
|
||||
*
|
||||
|
||||
@ -38,7 +38,7 @@ final class TestCaseFactory
|
||||
/**
|
||||
* The list of annotations.
|
||||
*
|
||||
* @var array<int, class-string>
|
||||
* @var array<int, \Pest\Factories\Attributes\Attribute>
|
||||
*/
|
||||
private static array $attributes = [
|
||||
Attributes\Covers::class,
|
||||
@ -155,6 +155,19 @@ final class TestCaseFactory
|
||||
$methods
|
||||
));
|
||||
|
||||
$classAttributes = [];
|
||||
|
||||
foreach (self::$attributes as $attribute) {
|
||||
if ($attribute::ABOVE_CLASS) {
|
||||
/** @phpstan-ignore-next-line */
|
||||
$classAttributes = (new $attribute())->__invoke($this, $classAttributes);
|
||||
}
|
||||
}
|
||||
|
||||
$classAttributes = implode('', array_map(
|
||||
static fn ($attribute) => sprintf("\n %s", $attribute), $classAttributes,
|
||||
));
|
||||
|
||||
try {
|
||||
eval("
|
||||
namespace $namespace;
|
||||
@ -162,6 +175,7 @@ final class TestCaseFactory
|
||||
use Pest\Repositories\DatasetsRepository as __PestDatasets;
|
||||
use Pest\TestSuite as __PestTestSuite;
|
||||
|
||||
$classAttributes
|
||||
final class $className extends $baseClass implements $hasPrintableTestCaseClassFQN {
|
||||
$traitsCode
|
||||
|
||||
|
||||
@ -129,18 +129,12 @@ final class TestCaseMethodFactory
|
||||
|
||||
$datasetsCode = '';
|
||||
$annotations = ['@test'];
|
||||
$attributes = [];
|
||||
|
||||
foreach ($annotationsToUse as $annotation) {
|
||||
/** @phpstan-ignore-next-line */
|
||||
$annotations = (new $annotation())->__invoke($this, $annotations);
|
||||
}
|
||||
|
||||
foreach ($attributesToUse as $attribute) {
|
||||
/** @phpstan-ignore-next-line */
|
||||
$attributes = (new $attribute())->__invoke($this, $attributes);
|
||||
}
|
||||
|
||||
if (count($this->datasets) > 0) {
|
||||
$dataProviderName = $methodName . '_dataset';
|
||||
$annotations[] = "@dataProvider $dataProviderName";
|
||||
@ -151,15 +145,10 @@ final class TestCaseMethodFactory
|
||||
static fn ($annotation) => sprintf("\n * %s", $annotation), $annotations,
|
||||
));
|
||||
|
||||
$attributes = implode('', array_map(
|
||||
static fn ($attribute) => sprintf("\n %s", $attribute), $attributes,
|
||||
));
|
||||
|
||||
return <<<EOF
|
||||
|
||||
/**$annotations
|
||||
*/
|
||||
$attributes
|
||||
public function $methodName()
|
||||
{
|
||||
return \$this->__runTest(
|
||||
|
||||
Reference in New Issue
Block a user