Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion contribution/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Comment thread
homersimpsons marked this conversation as resolved.
Resolved
use App\Service\Configlet;
use League\Flysystem\Filesystem;
use League\Flysystem\Local\LocalFilesystemAdapter;
Expand Down
70 changes: 30 additions & 40 deletions contribution/src/Command/TestBootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -36,6 +39,7 @@ public function __construct(
/**
* @throws FilesystemException
* @throws RuntimeException
* @throws ExportException
*/
public function __invoke(
#[Argument('Exercise slug')]
Expand All @@ -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()'
Expand All @@ -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();
Expand All @@ -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);

Expand All @@ -101,6 +102,7 @@ private function createTestsFor(CanonicalData $canonicalData, string $exerciseCl
/**
* @param list<LabeledTestItem> $cases
* @return void
* @throws ExportException
*/
private function addCases(Class_ $class, BuilderFactory $builderFactory, array $cases): void
{
Expand All @@ -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);
Comment thread
homersimpsons marked this conversation as resolved.
Resolved

$class->addStmt($methodTest);
}
}
}
10 changes: 1 addition & 9 deletions contribution/src/Command/TestGenerate.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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',
Expand Down
16 changes: 16 additions & 0 deletions contribution/src/Service/PathResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
Loading