move covers attribute above the class

This commit is contained in:
danilopolani
2022-03-05 17:23:03 +01:00
parent 50d8688b79
commit 21364779f9
4 changed files with 41 additions and 13 deletions

View 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;
}

View File

@ -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.
*

View File

@ -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

View File

@ -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(