mirror of
https://github.com/pestphp/pest.git
synced 2026-03-06 07:47:22 +01:00
@ -7,7 +7,6 @@ namespace Pest\Factories;
|
||||
use ParseError;
|
||||
use Pest\Concerns;
|
||||
use Pest\Contracts\HasPrintableTestCaseName;
|
||||
use Pest\Datasets;
|
||||
use Pest\Exceptions\DatasetMissing;
|
||||
use Pest\Exceptions\ShouldNotHappen;
|
||||
use Pest\Exceptions\TestAlreadyExist;
|
||||
@ -73,9 +72,10 @@ final class TestCaseFactory
|
||||
{
|
||||
$methodsUsingOnly = $this->methodsUsingOnly();
|
||||
|
||||
$methods = array_values(array_filter($this->methods, function ($method) use ($methodsUsingOnly) {
|
||||
return count($methodsUsingOnly) === 0 || in_array($method, $methodsUsingOnly, true);
|
||||
}));
|
||||
$methods = array_values(array_filter(
|
||||
$this->methods,
|
||||
fn ($method) => count($methodsUsingOnly) === 0 || in_array($method, $methodsUsingOnly, true)
|
||||
));
|
||||
|
||||
if (count($methods) > 0) {
|
||||
$this->evaluate($this->filename, $methods);
|
||||
@ -141,57 +141,11 @@ final class TestCaseFactory
|
||||
$classFQN .= $className;
|
||||
}
|
||||
|
||||
$methodsCode = implode('', array_map(static function (TestCaseMethodFactory $method): string {
|
||||
if ($method->description === null) {
|
||||
throw ShouldNotHappen::fromMessage('The test description may not be empty.');
|
||||
}
|
||||
|
||||
$methodName = Str::evaluable($method->description);
|
||||
|
||||
$datasetsCode = '';
|
||||
$annotations = ['@test'];
|
||||
|
||||
foreach (self::$annotations as $annotation) {
|
||||
/** @phpstan-ignore-next-line */
|
||||
$annotations = (new $annotation())->__invoke($method, $annotations);
|
||||
}
|
||||
|
||||
if (count($method->datasets) > 0) {
|
||||
$dataProviderName = $methodName . '_dataset';
|
||||
$annotations[] = "@dataProvider $dataProviderName";
|
||||
|
||||
Datasets::with($method->filename, $methodName, $method->datasets);
|
||||
|
||||
$datasetsCode = <<<EOF
|
||||
|
||||
public function $dataProviderName()
|
||||
{
|
||||
return __PestDatasets::get(self::\$__filename, "$methodName");
|
||||
}
|
||||
|
||||
EOF;
|
||||
}
|
||||
|
||||
$annotations = implode('', array_map(
|
||||
static fn ($annotation) => sprintf("\n * %s", $annotation), $annotations,
|
||||
$methodsCode = implode('', array_map(
|
||||
fn (TestCaseMethodFactory $methodFactory) => $methodFactory->buildForEvaluation(self::$annotations),
|
||||
$methods
|
||||
));
|
||||
|
||||
return <<<EOF
|
||||
|
||||
/**$annotations
|
||||
*/
|
||||
public function $methodName()
|
||||
{
|
||||
return \$this->__runTest(
|
||||
\$this->__test,
|
||||
...func_get_args(),
|
||||
);
|
||||
}
|
||||
|
||||
$datasetsCode
|
||||
EOF;
|
||||
}, $methods));
|
||||
|
||||
try {
|
||||
eval("
|
||||
namespace $namespace;
|
||||
|
||||
@ -5,8 +5,10 @@ declare(strict_types=1);
|
||||
namespace Pest\Factories;
|
||||
|
||||
use Closure;
|
||||
use Pest\Datasets;
|
||||
use Pest\Exceptions\ShouldNotHappen;
|
||||
use Pest\Factories\Concerns\HigherOrderable;
|
||||
use Pest\Support\Str;
|
||||
use Pest\TestSuite;
|
||||
use PHPUnit\Framework\Assert;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
@ -17,6 +19,7 @@ use PHPUnit\Framework\TestCase;
|
||||
final class TestCaseMethodFactory
|
||||
{
|
||||
use HigherOrderable;
|
||||
|
||||
/**
|
||||
* Determines if the Test Case will be the "only" being run.
|
||||
*/
|
||||
@ -51,11 +54,9 @@ final class TestCaseMethodFactory
|
||||
public ?string $description,
|
||||
public ?Closure $closure,
|
||||
) {
|
||||
if ($this->closure === null) {
|
||||
$this->closure = function () {
|
||||
$this->closure ??= function () {
|
||||
Assert::getCount() > 0 ?: self::markTestIncomplete(); // @phpstan-ignore-line
|
||||
};
|
||||
}
|
||||
|
||||
$this->bootHigherOrderable();
|
||||
}
|
||||
@ -100,4 +101,68 @@ final class TestCaseMethodFactory
|
||||
{
|
||||
return count($this->datasets) > 0 || count($this->depends) > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a PHPUnit method as a string ready for evaluation.
|
||||
*
|
||||
* @param array<int, class-string> $annotationsToUse
|
||||
*/
|
||||
public function buildForEvaluation(array $annotationsToUse): string
|
||||
{
|
||||
if ($this->description === null) {
|
||||
throw ShouldNotHappen::fromMessage('The test description may not be empty.');
|
||||
}
|
||||
|
||||
$methodName = Str::evaluable($this->description);
|
||||
|
||||
$datasetsCode = '';
|
||||
$annotations = ['@test'];
|
||||
|
||||
foreach ($annotationsToUse as $annotation) {
|
||||
/** @phpstan-ignore-next-line */
|
||||
$annotations = (new $annotation())->__invoke($this, $annotations);
|
||||
}
|
||||
|
||||
if (count($this->datasets) > 0) {
|
||||
$dataProviderName = $methodName . '_dataset';
|
||||
$annotations[] = "@dataProvider $dataProviderName";
|
||||
$datasetsCode = $this->buildDatasetForEvaluation($methodName, $dataProviderName);
|
||||
}
|
||||
|
||||
$annotations = implode('', array_map(
|
||||
static fn ($annotation) => sprintf("\n * %s", $annotation), $annotations,
|
||||
));
|
||||
|
||||
return <<<EOF
|
||||
|
||||
/**$annotations
|
||||
*/
|
||||
public function $methodName()
|
||||
{
|
||||
return \$this->__runTest(
|
||||
\$this->__test,
|
||||
...func_get_args(),
|
||||
);
|
||||
}
|
||||
|
||||
$datasetsCode
|
||||
EOF;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a PHPUnit Data Provider as a string ready for evaluation.
|
||||
*/
|
||||
private function buildDatasetForEvaluation(string $methodName, string $dataProviderName): string
|
||||
{
|
||||
Datasets::with($this->filename, $methodName, $this->datasets);
|
||||
|
||||
return <<<EOF
|
||||
|
||||
public function $dataProviderName()
|
||||
{
|
||||
return __PestDatasets::get(self::\$__filename, "$methodName");
|
||||
}
|
||||
|
||||
EOF;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user