-
-
Notifications
You must be signed in to change notification settings - Fork 148
Test generator, step 1 #638
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
06b500f
Import test generator POC of tomasnorre
mk-mxp 1f344bc
Mark PHPUnit config in root as unused
mk-mxp 44ea6e7
Delete hardcoded POC data, disable test generation
mk-mxp 8b18ec3
Require exercise slug as argument
mk-mxp d13002c
Sort `use` by alphabet
mk-mxp 761964c
Make BuilderFactory a private class instance
mk-mxp 3f72223
Ensure configlet is usable
mk-mxp 25b3d18
Ensure exercise directory is usable
mk-mxp 65887a4
Construct path to canonical data from configlet
mk-mxp 7c273f7
Extract ensureConfigletCanBeUsed()
mk-mxp 69564c9
Extract ensurePracticeExerciseCanBeUsed()
mk-mxp 3815922
Extract canonical data handling
mk-mxp b7be21f
Sort out test creation things
mk-mxp 1f8bc15
Inject project dir and make track root from it
mk-mxp eec68e4
Extract class PracticeExercise
mk-mxp 74c2153
Extract class TestGenerator
mk-mxp 0d54518
Generate test stubs from problem specification
mk-mxp 2faf87d
Move method inPascalCase to command
mk-mxp ec01896
Rename $classBuilder to $class
mk-mxp 2bd8ede
Use class constant instead of string
mk-mxp 66e8eb5
Add test class doc block from exercise comment
mk-mxp 78a44b6
Add testdox to test methods
mk-mxp cec0d8a
Add use for PHPUnit TestCase
mk-mxp bd22d0c
Mark generated tests as incomplete
mk-mxp 4bb8fa8
Update README, more TODOs
mk-mxp c7f7a03
Fix code styles
mk-mxp 18295d5
Specify array type of $comments
mk-mxp f12ea9f
Explain configlet command in comment
mk-mxp 51297a4
Remove unrequired PHPUnit config file
mk-mxp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| # In all environments, the following files are loaded if they exist, | ||
| # the latter taking precedence over the former: | ||
| # | ||
| # * .env contains default values for the environment variables needed by the app | ||
| # * .env.local uncommitted file with local overrides | ||
| # * .env.$APP_ENV committed environment-specific defaults | ||
| # * .env.$APP_ENV.local uncommitted environment-specific overrides | ||
| # | ||
| # Real environment variables win over .env files. | ||
| # | ||
| # DO NOT DEFINE PRODUCTION SECRETS IN THIS FILE NOR IN ANY OTHER COMMITTED FILES. | ||
| # https://symfony.com/doc/current/configuration/secrets.html | ||
| # | ||
| # Run "composer dump-env prod" to compile .env files for production use (requires symfony/flex >=1.2). | ||
| # https://symfony.com/doc/current/best_practices.html#use-environment-variables-for-infrastructure-configuration | ||
|
|
||
| ###> symfony/framework-bundle ### | ||
| APP_ENV=dev | ||
| APP_SECRET=1be0b2dbd34333efb23cfefcff0ff718 | ||
| ###< symfony/framework-bundle ### |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| # define your env variables for the test env here | ||
| KERNEL_CLASS='App\Kernel' | ||
| APP_SECRET='$ecretf0rt3st' | ||
| SYMFONY_DEPRECATIONS_HELPER=999999 | ||
| PANTHER_APP_ENV=panther | ||
| PANTHER_ERROR_SCREENSHOT_DIR=./var/error-screenshots |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| # IDEs | ||
| .idea | ||
|
|
||
| ###> symfony/framework-bundle ### | ||
| /.env.local | ||
| /.env.local.php | ||
| /.env.*.local | ||
| /config/secrets/prod/prod.decrypt.private.php | ||
| /public/bundles/ | ||
| /var/ | ||
| /vendor/ | ||
| ###< symfony/framework-bundle ### | ||
|
|
||
| ###> phpunit/phpunit ### | ||
| /phpunit.xml | ||
| .phpunit.result.cache | ||
| ###< phpunit/phpunit ### |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| # Auto Creating of tests for Exercism PHP Track | ||
|
|
||
| This is a small poc on how we could auto generate tests for the PHP track based on the https://github.com/exercism/problem-specifications/. | ||
|
|
||
| How to test it: | ||
|
|
||
| ``` | ||
| git clone https://github.com/tomasnorre/exercism-tests-generation.git | ||
| cd exercism-tests-generation | ||
| composer install | ||
| bin/console app:create-tests | ||
| vendor/bin/phpunit src/Command/NucleotideCountTest.php | ||
| ``` | ||
|
|
||
| If you now make a `git status` you will see that the `src/Command/NucleotideCountTest.php` and you can now inspect the auto generated tests. | ||
|
|
||
| It's all based on the `nikic/php-parser` and the https://github.com/exercism/problem-specifications/ repository, I have made a local copy of that on file | ||
| for now to spare the http-requests. | ||
|
|
||
| Let me know what you think. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| #!/usr/bin/env php | ||
| <?php | ||
|
|
||
| use App\Kernel; | ||
| use Symfony\Bundle\FrameworkBundle\Console\Application; | ||
|
|
||
| if (!is_file(dirname(__DIR__).'/vendor/autoload_runtime.php')) { | ||
| throw new LogicException('Symfony Runtime is missing. Try running "composer require symfony/runtime".'); | ||
| } | ||
|
|
||
| require_once dirname(__DIR__).'/vendor/autoload_runtime.php'; | ||
|
|
||
| return function (array $context) { | ||
| $kernel = new Kernel($context['APP_ENV'], (bool) $context['APP_DEBUG']); | ||
|
|
||
| return new Application($kernel); | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| { | ||
| "type": "project", | ||
| "license": "proprietary", | ||
| "minimum-stability": "stable", | ||
| "prefer-stable": true, | ||
| "require": { | ||
| "php": ">=8.2", | ||
| "ext-ctype": "*", | ||
| "ext-iconv": "*", | ||
| "nikic/php-parser": "^5.0", | ||
| "symfony/console": "7.0.*", | ||
| "symfony/dotenv": "7.0.*", | ||
| "symfony/flex": "^2", | ||
| "symfony/framework-bundle": "7.0.*", | ||
| "symfony/runtime": "7.0.*", | ||
| "symfony/yaml": "7.0.*" | ||
| }, | ||
| "require-dev": { | ||
| "phpunit/phpunit": "^11.0", | ||
| "symfony/maker-bundle": "^1.54" | ||
| }, | ||
| "config": { | ||
| "allow-plugins": { | ||
| "php-http/discovery": true, | ||
| "symfony/flex": true, | ||
| "symfony/runtime": true | ||
| }, | ||
| "sort-packages": true | ||
| }, | ||
| "autoload": { | ||
| "psr-4": { | ||
| "App\\": "src/" | ||
| } | ||
| }, | ||
| "autoload-dev": { | ||
| "psr-4": { | ||
| "App\\Tests\\": "tests/" | ||
| } | ||
| }, | ||
| "replace": { | ||
| "symfony/polyfill-ctype": "*", | ||
| "symfony/polyfill-iconv": "*", | ||
| "symfony/polyfill-php72": "*", | ||
| "symfony/polyfill-php73": "*", | ||
| "symfony/polyfill-php74": "*", | ||
| "symfony/polyfill-php80": "*", | ||
| "symfony/polyfill-php81": "*", | ||
| "symfony/polyfill-php82": "*" | ||
| }, | ||
| "scripts": { | ||
| "auto-scripts": { | ||
| "cache:clear": "symfony-cmd", | ||
| "assets:install %PUBLIC_DIR%": "symfony-cmd" | ||
| }, | ||
| "post-install-cmd": [ | ||
| "@auto-scripts" | ||
| ], | ||
| "post-update-cmd": [ | ||
| "@auto-scripts" | ||
| ] | ||
| }, | ||
| "conflict": { | ||
| "symfony/symfony": "*" | ||
| }, | ||
| "extra": { | ||
| "symfony": { | ||
| "allow-contrib": false, | ||
| "require": "7.0.*" | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| <?php | ||
|
|
||
| return [ | ||
| Symfony\Bundle\FrameworkBundle\FrameworkBundle::class => ['all' => true], | ||
| Symfony\Bundle\MakerBundle\MakerBundle::class => ['dev' => true], | ||
| ]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| framework: | ||
| cache: | ||
| # Unique name of your app: used to compute stable namespaces for cache keys. | ||
| #prefix_seed: your_vendor_name/app_name | ||
|
|
||
| # The "app" cache stores to the filesystem by default. | ||
| # The data in this cache should persist between deploys. | ||
| # Other options include: | ||
|
|
||
| # Redis | ||
| #app: cache.adapter.redis | ||
| #default_redis_provider: redis://localhost | ||
|
|
||
| # APCu (not recommended with heavy random-write workloads as memory fragmentation can cause perf issues) | ||
| #app: cache.adapter.apcu | ||
|
|
||
| # Namespaced pools use the above "app" backend by default | ||
| #pools: | ||
| #my.dedicated.cache: null |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| # see https://symfony.com/doc/current/reference/configuration/framework.html | ||
| framework: | ||
| secret: '%env(APP_SECRET)%' | ||
| #csrf_protection: true | ||
|
|
||
| # Note that the session will be started ONLY if you read or write from it. | ||
| session: true | ||
|
|
||
| #esi: true | ||
| #fragments: true | ||
|
|
||
| when@test: | ||
| framework: | ||
| test: true | ||
| session: | ||
| storage_factory_id: session.storage.factory.mock_file |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| framework: | ||
| router: | ||
| # Configure how to generate URLs in non-HTTP contexts, such as CLI commands. | ||
| # See https://symfony.com/doc/current/routing.html#generating-urls-in-commands | ||
| #default_uri: http://localhost | ||
|
|
||
| when@prod: | ||
| framework: | ||
| router: | ||
| strict_requirements: null |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| <?php | ||
|
|
||
| if (file_exists(dirname(__DIR__).'/var/cache/prod/App_KernelProdContainer.preload.php')) { | ||
| require dirname(__DIR__).'/var/cache/prod/App_KernelProdContainer.preload.php'; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| controllers: | ||
| resource: | ||
| path: ../src/Controller/ | ||
| namespace: App\Controller | ||
| type: attribute | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| when@dev: | ||
| _errors: | ||
| resource: '@FrameworkBundle/Resources/config/routing/errors.xml' | ||
| prefix: /_error |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| # This file is the entry point to configure your own services. | ||
| # Files in the packages/ subdirectory configure your dependencies. | ||
|
|
||
| # Put parameters here that don't need to change on each machine where the app is deployed | ||
| # https://symfony.com/doc/current/best_practices.html#use-parameters-for-application-configuration | ||
| parameters: | ||
|
|
||
| services: | ||
| # default configuration for services in *this* file | ||
| _defaults: | ||
| autowire: true # Automatically injects dependencies in your services. | ||
| autoconfigure: true # Automatically registers your services as commands, event subscribers, etc. | ||
| bind: | ||
| $projectDir: '%kernel.project_dir%' | ||
|
|
||
| # makes classes in src/ available to be used as services | ||
| # this creates a service per class whose id is the fully-qualified class name | ||
| App\: | ||
| resource: '../src/' | ||
| exclude: | ||
| - '../src/DependencyInjection/' | ||
| - '../src/Entity/' | ||
| - '../src/Kernel.php' | ||
|
|
||
| # add more service definitions when explicit configuration is needed | ||
| # please note that last definitions always *replace* previous ones |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
|
|
||
| <!-- https://phpunit.readthedocs.io/en/latest/configuration.html --> | ||
| <phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd" | ||
| backupGlobals="false" | ||
| colors="true" | ||
| bootstrap="tests/bootstrap.php" | ||
| > | ||
| <php> | ||
| <ini name="display_errors" value="1" /> | ||
| <ini name="error_reporting" value="-1" /> | ||
| <server name="APP_ENV" value="test" force="true" /> | ||
| <server name="SHELL_VERBOSITY" value="-1" /> | ||
| <server name="SYMFONY_PHPUNIT_REMOVE" value="" /> | ||
| <server name="SYMFONY_PHPUNIT_VERSION" value="9.5" /> | ||
| </php> | ||
|
|
||
| <testsuites> | ||
| <testsuite name="Project Test Suite"> | ||
| <directory>tests</directory> | ||
| </testsuite> | ||
| </testsuites> | ||
|
|
||
| <extensions> | ||
| </extensions> | ||
| </phpunit> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| <?php | ||
|
|
||
| use App\Kernel; | ||
|
|
||
| require_once dirname(__DIR__).'/vendor/autoload_runtime.php'; | ||
|
|
||
| return function (array $context) { | ||
| return new Kernel($context['APP_ENV'], (bool) $context['APP_DEBUG']); | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace App\Command; | ||
|
|
||
| use App\TestGenerator; | ||
| use App\TrackData\PracticeExercise; | ||
| use Symfony\Component\Console\Attribute\AsCommand; | ||
| use Symfony\Component\Console\Command\Command; | ||
| use Symfony\Component\Console\Input\InputArgument; | ||
| use Symfony\Component\Console\Input\InputInterface; | ||
| use Symfony\Component\Console\Output\OutputInterface; | ||
| use Symfony\Component\Console\Style\SymfonyStyle; | ||
|
|
||
| #[AsCommand( | ||
| name: 'app:create-tests', | ||
| description: 'This will automatically create tests', | ||
| )] | ||
| class CreateTestsCommand extends Command | ||
| { | ||
| private string $trackRoot; | ||
|
|
||
| public function __construct( | ||
| private string $projectDir, | ||
| ) { | ||
| $this->trackRoot = realpath($projectDir . '/../..'); | ||
|
|
||
| parent::__construct(); | ||
| } | ||
|
|
||
| protected function configure(): void | ||
| { | ||
| $this->addArgument('exercise', InputArgument::REQUIRED, 'Exercise slug'); | ||
|
mk-mxp marked this conversation as resolved.
|
||
| } | ||
|
|
||
| protected function execute(InputInterface $input, OutputInterface $output): int | ||
| { | ||
| $exerciseSlug = $input->getArgument('exercise'); | ||
| $exercise = new PracticeExercise( | ||
| $this->trackRoot, | ||
| $exerciseSlug, | ||
| ); | ||
| $testGenerator = new TestGenerator(); | ||
|
|
||
| $io = new SymfonyStyle($input, $output); | ||
| $io->writeln('Generating tests for ' . $exerciseSlug . ' in ' . $exercise->pathToExercise()); | ||
|
|
||
| \file_put_contents( | ||
| // TODO: Make '$exercise->pathToTestFile()' | ||
| $exercise->pathToExercise() | ||
| . '/' | ||
| . $this->inPascalCase($exerciseSlug) | ||
| . 'Test.php', | ||
|
mk-mxp marked this conversation as resolved.
|
||
| $testGenerator->createTestsFor( | ||
| $exercise->canonicalData(), | ||
| $this->inPascalCase($exerciseSlug) | ||
|
mk-mxp marked this conversation as resolved.
|
||
| ), | ||
| ); | ||
| // TODO: Make '$exercise->pathToStudentsFile()' | ||
| // TODO: Make '$testGenerator->studentsFileFor()' | ||
|
|
||
| $io->success('Generating Tests - Finished'); | ||
| return Command::SUCCESS; | ||
| } | ||
|
|
||
| private function inPascalCase(string $text): string | ||
| { | ||
| return \str_replace(" ", "", \ucwords(\str_replace("-", " ", $text))); | ||
| } | ||
| } | ||
Empty file.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.