From 3d0f267a5c02a641477544a38a022af61f2e738d Mon Sep 17 00:00:00 2001 From: luke Date: Sat, 27 Nov 2021 08:08:09 +0000 Subject: [PATCH] Moves method evaluation to the method factory --- src/Factories/TestCaseFactory.php | 55 ++------------------- src/Factories/TestCaseMethodFactory.php | 66 +++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 51 deletions(-) diff --git a/src/Factories/TestCaseFactory.php b/src/Factories/TestCaseFactory.php index 448734cc..b214631b 100644 --- a/src/Factories/TestCaseFactory.php +++ b/src/Factories/TestCaseFactory.php @@ -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; @@ -142,56 +141,10 @@ 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 = << sprintf("\n * %s", $annotation), $annotations, - )); - - return <<__runTest( - \$this->__test, - ...func_get_args(), - ); - } - - $datasetsCode -EOF; - }, $methods)); + $methodsCode = implode('', array_map( + fn (TestCaseMethodFactory $methodFactory) => $methodFactory->buildForEvaluation(self::$annotations), + $methods + )); try { eval(" diff --git a/src/Factories/TestCaseMethodFactory.php b/src/Factories/TestCaseMethodFactory.php index 5de47009..1aaa2dae 100644 --- a/src/Factories/TestCaseMethodFactory.php +++ b/src/Factories/TestCaseMethodFactory.php @@ -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; @@ -99,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 $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 <<__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 <<