diff --git a/contribution/main.php b/contribution/main.php index a7aa05e1..0e37e5b9 100644 --- a/contribution/main.php +++ b/contribution/main.php @@ -6,8 +6,8 @@ require_once __DIR__ . '/vendor/autoload.php'; use App\Command\Deprecated; -use App\Command\TestGenerate; use App\Command\TestBootstrap; +use App\Command\TestGenerate; use App\Service\Configlet; use League\Flysystem\Filesystem; use League\Flysystem\Local\LocalFilesystemAdapter; diff --git a/contribution/src/Command/TestBootstrap.php b/contribution/src/Command/TestBootstrap.php index 19f88049..b6599ba4 100644 --- a/contribution/src/Command/TestBootstrap.php +++ b/contribution/src/Command/TestBootstrap.php @@ -10,10 +10,13 @@ use App\Model\CanonicalData\LabeledTestItem; use App\Service\Configlet; use App\Service\PathResolver; +use Brick\VarExporter\ExportException; +use Brick\VarExporter\VarExporter; use League\Flysystem\Filesystem; use League\Flysystem\FilesystemException; use PhpParser\Builder\Class_; use PhpParser\BuilderFactory; +use PhpParser\Comment; use PhpParser\Node; use PhpParser\PrettyPrinter; use PHPUnit\Framework\TestCase; @@ -36,6 +39,7 @@ public function __construct( /** * @throws FilesystemException * @throws RuntimeException + * @throws ExportException */ public function __invoke( #[Argument('Exercise slug')] @@ -51,8 +55,8 @@ public function __invoke( $this->exercisesFilesystem->write( // TODO(@mk-mxp): Make '$exercise->pathToTestFile()' - $exercisePath . '/' . $this->inPascalCase($exerciseSlug) . 'Test.php', - $this->createTestsFor($canonicalData, $this->inPascalCase($exerciseSlug)), + $exercisePath . '/' . PathResolver::toPascalCase($exerciseSlug) . 'Test.php', + $this->createTestsFor($canonicalData, PathResolver::toPascalCase($exerciseSlug)), ); // TODO(@mk-mxp): Make '$exercise->pathToStudentsFile()' // TODO(@mk-mxp): Make '$testGenerator->studentsFileFor()' @@ -61,11 +65,9 @@ public function __invoke( return Command::SUCCESS; } - private function inPascalCase(string $text): string - { - return \str_replace(' ', '', \ucwords(\str_replace('-', ' ', $text))); - } - + /** + * @throws ExportException + */ private function createTestsFor(CanonicalData $canonicalData, string $exerciseClass): string { $builderFactory = new BuilderFactory(); @@ -77,15 +79,14 @@ private function createTestsFor(CanonicalData $canonicalData, string $exerciseCl $class->setDocComment("/**\n * " . implode("\n * ", $canonicalData->comments) . "\n */"); // Include Setup Method - $methodSetup = 'setUpBeforeClass'; - $method = $builderFactory - ->method($methodSetup) + $methodSetUp = $builderFactory + ->method('setUpBeforeClass') ->makePublic() ->makeStatic() ->setReturnType('void') ->addStmt($builderFactory->funcCall('require_once', [$exerciseClass . '.php'])); - $class->addStmt($method); + $class->addStmt($methodSetUp); $this->addCases($class, $builderFactory, $canonicalData->cases); @@ -101,6 +102,7 @@ private function createTestsFor(CanonicalData $canonicalData, string $exerciseCl /** * @param list $cases * @return void + * @throws ExportException */ private function addCases(Class_ $class, BuilderFactory $builderFactory, array $cases): void { @@ -112,42 +114,30 @@ private function addCases(Class_ $class, BuilderFactory $builderFactory, array $ } assert($case instanceof LabeledTest, '$case is always a LabeledTest here.'); - // Generate a method for each test case - $description = \ucfirst($case->description); - $methodName = ucfirst(str_replace('-', ' ', $description)); - $methodName = 'test' . str_replace(' ', '', ucwords($methodName)); - // $exceptionClassName = new Node\Name\FullyQualified('Exception'); - $method = $builderFactory - ->method($methodName) + $description = \ucfirst($case->description); + $methodTest = $builderFactory + ->method(PathResolver::testFunctionName($description)) ->makePublic() ->setReturnType('void') ->setDocComment("/** uuid: {$case->uuid} */") ->addAttribute(new Node\Attribute( new Node\Name(\PHPUnit\Framework\Attributes\TestDox::class), [new Node\Arg(new Node\Scalar\String_($description))], - )) - ->addStmt($builderFactory->funcCall('$this->markTestIncomplete', [ - 'This test has not been implemented yet.', - ])); - // if (isset($case->expected->error)) { - // $method->addStmt( - // $builderFactory->funcCall('$this->expectException', - // [new Node\Arg(new Node\Expr\ClassConstFetch($exceptionClassName, 'class'))] - // ) - // ) - // ->addStmt($builderFactory->funcCall($case->property, [$case->input->strand ?? 'unknown'])) - // ; - // } else { - // $method->addStmt( - // $builderFactory->funcCall('$this->assertEquals', [ - // $case->expected, - // $builderFactory->funcCall($case->property, [$case->input->strand ?? 'unknown']) - // ]) - // ); - // } - - $class->addStmt($method); + )); + $methodTestBody = new Node\Stmt\Expression($builderFactory->funcCall('$this->markTestIncomplete', [ + 'This test has not been implemented yet.', + ])); + $methodTestComment = preg_replace('/^/m', '// ', VarExporter::export([ + 'property' => $case->property, + 'input' => $case->input, + 'expected' => $case->expected, + ], VarExporter::INLINE_SCALAR_LIST | VarExporter::TRAILING_COMMA_IN_ARRAY)); + assert($methodTestComment !== null, 'Regex pattern always match any string'); + $methodTestBody->setAttribute('comments', [new Comment($methodTestComment)]); + $methodTest->addStmt($methodTestBody); + + $class->addStmt($methodTest); } } } diff --git a/contribution/src/Command/TestGenerate.php b/contribution/src/Command/TestGenerate.php index 1223be7a..3cd581fb 100644 --- a/contribution/src/Command/TestGenerate.php +++ b/contribution/src/Command/TestGenerate.php @@ -35,9 +35,6 @@ use function assert; use function implode; use function json_decode; -use function preg_replace; -use function str_replace; -use function ucwords; use const JSON_THROW_ON_ERROR; @@ -113,12 +110,7 @@ public function generate(string $basePath, bool $check, CanonicalData $canonical $value, VarExporter::INLINE_ARRAY, ))); - $twigEnvironment->addFunction(new TwigFunction('testfn', static function (string $label) { - $labelSpaced = preg_replace('/[^a-zA-Z0-9]/', ' ', $label); - assert($labelSpaced !== null, 'preg_replace failed'); - - return 'test' . str_replace(' ', '', ucwords($labelSpaced)); - })); + $twigEnvironment->addFunction(new TwigFunction('testfn', [PathResolver::class, 'testFunctionName'])); $twigEnvironment->addFunction( new TwigFunction( 'sort_input', diff --git a/contribution/src/Service/PathResolver.php b/contribution/src/Service/PathResolver.php index 1df25f40..eecfb078 100644 --- a/contribution/src/Service/PathResolver.php +++ b/contribution/src/Service/PathResolver.php @@ -15,4 +15,20 @@ public static function exercisePath(string $category, string $exerciseSlug): str { return $category . '/' . $exerciseSlug; } + + /** + * Returns a php-compatible function name from a test description + */ + public static function testFunctionName(string $description): string + { + return 'test' . self::toPascalCase($description); + } + + public static function toPascalCase(string $text): string + { + $textSpaced = preg_replace('/[^a-zA-Z0-9]/', ' ', $text); + assert($textSpaced !== null, 'preg_replace failed'); + + return str_replace(' ', '', ucwords($textSpaced)); + } }