mirror of
https://github.com/pestphp/pest.git
synced 2026-03-12 10:47:25 +01:00
start covers attribute implementation
This commit is contained in:
35
src/Factories/Attributes/Covers.php
Normal file
35
src/Factories/Attributes/Covers.php
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Pest\Factories\Attributes;
|
||||||
|
|
||||||
|
use Pest\Factories\TestCaseMethodFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
final class Covers
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Adds attributes regarding the "covers" feature.
|
||||||
|
*
|
||||||
|
* @param \Pest\Factories\TestCaseMethodFactory $method
|
||||||
|
* @param array<int, string> $attributes
|
||||||
|
*
|
||||||
|
* @return array<int, string>
|
||||||
|
*/
|
||||||
|
public function __invoke(TestCaseMethodFactory $method, array $attributes): array
|
||||||
|
{
|
||||||
|
foreach ($method->covers as $covering) {
|
||||||
|
if (is_array($covering)) {
|
||||||
|
$attributes[] = "#[\PHPUnit\Framework\Attributes\CoversClass({$covering[0]}]";
|
||||||
|
$attributes[] = "#[\PHPUnit\Framework\Attributes\CoversFunction({$covering[1]}]";
|
||||||
|
} else {
|
||||||
|
$attributes[] = "#[\PHPUnit\Framework\Attributes\CoversClass($covering)]";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $attributes;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -47,6 +47,13 @@ final class TestCaseMethodFactory
|
|||||||
*/
|
*/
|
||||||
public array $groups = [];
|
public array $groups = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The covered classes and methods, if any.
|
||||||
|
*
|
||||||
|
* @var array<int, string>
|
||||||
|
*/
|
||||||
|
public array $covers = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new Factory instance.
|
* Creates a new Factory instance.
|
||||||
*/
|
*/
|
||||||
@ -108,7 +115,7 @@ final class TestCaseMethodFactory
|
|||||||
*
|
*
|
||||||
* @param array<int, class-string> $annotationsToUse
|
* @param array<int, class-string> $annotationsToUse
|
||||||
*/
|
*/
|
||||||
public function buildForEvaluation(string $classFQN, array $annotationsToUse): string
|
public function buildForEvaluation(string $classFQN, array $annotationsToUse, array $attributesToUse): string
|
||||||
{
|
{
|
||||||
if ($this->description === null) {
|
if ($this->description === null) {
|
||||||
throw ShouldNotHappen::fromMessage('The test description may not be empty.');
|
throw ShouldNotHappen::fromMessage('The test description may not be empty.');
|
||||||
@ -122,12 +129,18 @@ final class TestCaseMethodFactory
|
|||||||
|
|
||||||
$datasetsCode = '';
|
$datasetsCode = '';
|
||||||
$annotations = ['@test'];
|
$annotations = ['@test'];
|
||||||
|
$attributes = [];
|
||||||
|
|
||||||
foreach ($annotationsToUse as $annotation) {
|
foreach ($annotationsToUse as $annotation) {
|
||||||
/** @phpstan-ignore-next-line */
|
/** @phpstan-ignore-next-line */
|
||||||
$annotations = (new $annotation())->__invoke($this, $annotations);
|
$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) {
|
if (count($this->datasets) > 0) {
|
||||||
$dataProviderName = $methodName . '_dataset';
|
$dataProviderName = $methodName . '_dataset';
|
||||||
$annotations[] = "@dataProvider $dataProviderName";
|
$annotations[] = "@dataProvider $dataProviderName";
|
||||||
@ -138,10 +151,15 @@ final class TestCaseMethodFactory
|
|||||||
static fn ($annotation) => sprintf("\n * %s", $annotation), $annotations,
|
static fn ($annotation) => sprintf("\n * %s", $annotation), $annotations,
|
||||||
));
|
));
|
||||||
|
|
||||||
|
$attributes = implode('', array_map(
|
||||||
|
static fn ($attribute) => sprintf("\n %s", $attribute), $attributes,
|
||||||
|
));
|
||||||
|
|
||||||
return <<<EOF
|
return <<<EOF
|
||||||
|
|
||||||
/**$annotations
|
/**$annotations
|
||||||
*/
|
*/
|
||||||
|
$attributes
|
||||||
public function $methodName()
|
public function $methodName()
|
||||||
{
|
{
|
||||||
return \$this->__runTest(
|
return \$this->__runTest(
|
||||||
|
|||||||
@ -5,6 +5,7 @@ declare(strict_types=1);
|
|||||||
namespace Pest\PendingCalls;
|
namespace Pest\PendingCalls;
|
||||||
|
|
||||||
use Closure;
|
use Closure;
|
||||||
|
use InvalidArgumentException;
|
||||||
use Pest\Factories\TestCaseMethodFactory;
|
use Pest\Factories\TestCaseMethodFactory;
|
||||||
use Pest\Support\Backtrace;
|
use Pest\Support\Backtrace;
|
||||||
use Pest\Support\HigherOrderCallables;
|
use Pest\Support\HigherOrderCallables;
|
||||||
@ -168,6 +169,25 @@ final class TestCall
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the covered class and method.
|
||||||
|
*/
|
||||||
|
public function covers(string|array ...$classes): TestCall
|
||||||
|
{
|
||||||
|
foreach ($classes as $i => $class) {
|
||||||
|
if (is_array($class) && count($class) !== 2) {
|
||||||
|
throw new InvalidArgumentException(sprintf(
|
||||||
|
'The #%s covered class must be an array with exactly 2 items: class and method name.',
|
||||||
|
$i
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->testCaseMethod->covers[] = $class;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Saves the property accessors to be used on the target.
|
* Saves the property accessors to be used on the target.
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user