{"id":15014496,"url":"https://github.com/mihaeu/php-test-generator","last_synced_at":"2025-04-12T08:08:48.026Z","repository":{"id":62528569,"uuid":"95807030","full_name":"mihaeu/php-test-generator","owner":"mihaeu","description":"Generate test cases for existing PHP files","archived":false,"fork":false,"pushed_at":"2019-01-16T21:54:19.000Z","size":233,"stargazers_count":48,"open_issues_count":4,"forks_count":10,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-04-12T08:06:09.887Z","etag":null,"topics":["codegenerator","mockery","mocking","php","phpunit","testing-tools"],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mihaeu.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-06-29T18:22:36.000Z","updated_at":"2024-09-10T14:58:47.000Z","dependencies_parsed_at":"2022-11-02T10:45:46.030Z","dependency_job_id":null,"html_url":"https://github.com/mihaeu/php-test-generator","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mihaeu%2Fphp-test-generator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mihaeu%2Fphp-test-generator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mihaeu%2Fphp-test-generator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mihaeu%2Fphp-test-generator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mihaeu","download_url":"https://codeload.github.com/mihaeu/php-test-generator/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248537137,"owners_count":21120709,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["codegenerator","mockery","mocking","php","phpunit","testing-tools"],"created_at":"2024-09-24T19:45:41.981Z","updated_at":"2025-04-12T08:08:47.996Z","avatar_url":"https://github.com/mihaeu.png","language":"PHP","readme":"\u003cp align=\"center\"\u003e\n  \u003cimg src=\"doc/testgenerator-logo.png\"\u003e\n\u003c/p\u003e\n\n[![Travis branch](https://img.shields.io/travis/mihaeu/php-test-generator/develop.svg)](https://travis-ci.org/mihaeu/php-test-generator)\n[![Codecov branch](https://img.shields.io/codecov/c/github/mihaeu/php-test-generator/develop.svg)](https://codecov.io/gh/mihaeu/php-test-generator)\n[![Infection MSI](https://badge.stryker-mutator.io/github.com/mihaeu/php-test-generator/master)](https://infection.github.io)\n![](https://img.shields.io/badge/PHP-7.3-brightgreen.svg)\n![](https://img.shields.io/badge/PHP-7.2-brightgreen.svg)\n![](https://img.shields.io/badge/PHP-7.1-yellow.svg)\n![](https://img.shields.io/badge/PHP-7.0-yellow.svg)\n![](https://img.shields.io/badge/PHP-5.6-yellow.svg)\n![](https://img.shields.io/badge/PHP-5.5-yellow.svg)\n\n\u003e Generate test cases for existing files\n\n## Use Cases\n\n - PHPStorm has Apache Velocity support for file templates, but it is annoying to work with and limited\n - other IDEs or editors like Vim or Emacs don't have built-in code generation\n - somehow the test files never end up where they belong forcing you to rearrange code manually\n\n`test-generator` saves you all the tedious work of typing repetitive code when testing legacy applications. Next time you write a test for a class with too many dependencies and you start mocking away think of how much time you could've saved if you could automate this.\n\nThis is where `test-generator` comes into play. Try it out, configure everything to your needs and create an alias for your shell or even better include it as an external tool in your editor/IDE ([like PHPStorm](https://www.jetbrains.com/help/phpstorm/external-tools.html)).\n\n## Usage\n\n### CLI\n\n```bash\nbin/test-generator --help\n```\n\n```\nTest-Generator\n\nUsage:\n  test-generator [options] [(--src-base --test-base)] \u003cfile\u003e\n\nOptions:\n  --php5                        Generate PHP5 compatible code [default:false].\n  --phpunit5                    Generate a test for PHPUnit 5 [default:false].\n  --mockery                     Generates mocks using Mockery [default:false].\n  --covers                      Adds the @covers annotation   [default:false].\n  --base-class=\u003cbase-class\u003e     Inherit from this base class e.g. \"Example\\TestCase\".\n  --subject-format=\u003cformat\u003e     Format the field for the subject class.\n  --field-format=\u003cformat\u003e       Format the fields for dependencies.\n  -s, --src-base=\u003cpath\u003e         Base directory for source files; requires --test-base\n  -t, --test-base=\u003cpath\u003e        Base directory for test files; requires --src-base; writes output to that directory\n\nFormat:\n  %n                            Name starting with a lower-case letter.\n  %N                            Name starting with an upper-case letter.\n  %t                            Type starting with a lower-case letter.\n  %T                            Type starting with a upper-case letter.\n\nFormat Examples:\n  \"mock_%t\"                      Customer =\u003e mock_customer\n  \"%NTest\"                       arg =\u003e ArgTest\n  \"testClass\"                    SomeName =\u003e TestClass\n```\n\n### PHPStorm\n\nI recommend integrating `test-generator` as an external tool in PHPStorm. This works, because PHPStorm can pass the\nfilename of the currently active file as an argument to `test-generator`, which will then generate and write the\ntest to your preconfigured location. \n\nNavigate to Settings \u003e Tools \u003e External Tools and klick on **+**. Add the following information: \n\n| Field             | Value                                                                        |\n|-------------------|------------------------------------------------------------------------------|\n| Name              | test-generator                                                               |\n| Description       | Generate Test Stubs                                                          |\n| Program           | `$PhpExecutable`                                                             |\n| Arguments         | `vendor/bin/test-generator $FilePath$ -s base=src -t tests/unit`             |\n| Working directory | `$ProjectFileDir$`                                                           |\n\nRemember to adjust *Program* and *Arguments* in case you are using the `.phar` file.\n\nIn case you want to generate different tests with different settings and locations, simply create more external tool entries.\n\n![How to integrate test-generator in PHPStorm](doc/phpstorm-integration.png)\n\n**Pro Tip**: Assign a shortcut to this tool, because you might end up using it a lot ;)\n\n## Installation\n\n### Composer (PHP 7.1+)\n\n```bash\n# local install\ncomposer require \"mihaeu/test-generator:^1.0\"\n\n# global install\ncomposer global require \"mihaeu/test-generator:^1.0\"\n```\n\n### Phar (PHP 5.5+)\n\nSince I actually need to use this on 5.5 legacy projects (should work with 5.4 as well, but didn't test for it), I also release a phar file which works for older versions:\n\n```bash\nwget https://github.com/mihaeu/php-test-generator/releases/download/1.2.0/test-generator-1.2.0.phar\nchmod +x test-generator-1.2.0.phar\n```\n\n**Please note that by doing this we should be disgusted at ourselves for not upgrading to PHP 7.1 (soon 7.2).**\n\n### Git\n\n```bash\ngit clone https://github.com/mihaeu/php-test-generator\ncd php-test-generator\ncomposer install\nbin/test-generator --help\n```\n\nIf you don't have PHP 7.1 installed you can run `bin/remove-php7-features` to convert the source files. I won't however except pull requests without PHP 7.1 support.\n\n## Example\n\nGiven a PHP file like:\n\n```php\n\u003c?php declare(strict_types=1);\nnamespace Mihaeu\\TestGenerator;\nuse Twig_TemplateWrapper;\nclass TwigRenderer\n{\n    // ...\n    public function __construct(\\Twig_Environment $twig, TemplateConfiguration $templateConfiguration)\n    {\n        // ...\n    }\n    // ...\n}\n```\n\nRunning the following command:\n\n```bash\n# re-formatted for legibility\nbin/test-generator src/TwigRenderer.php\n    --field-format=\"mock%N\" \n    --subject-format=\"classUnderTest\" \n    --php5 \n    --phpunit5 \n    --mockery \n    --base-class=\"Vendor\\\\TestCase\"\n```\nwill produce a test including mocked dependencies:\n```php\n\u003c?php\n\nnamespace Mihaeu\\PhpDependencies\\Analyser;\n\nuse Mockery;\nuse Mockery\\MockInterface;\nuse Vendor\\TestCase;\n\nclass StaticAnalyserTest extends TestCase\n{\n    /** @var StaticAnalyser */\n    private $classUnderTest;\n\n    /** @var PhpParser\\NodeTraverser | MockInterface */\n    private $mockNodeTraverser;\n\n    /** @var Mihaeu\\PhpDependencies\\Analyser\\DependencyInspectionVisitor | MockInterface */\n    private $mockDependencyInspectionVisitor;\n\n    /** @var Mihaeu\\PhpDependencies\\Analyser\\Parser | MockInterface */\n    private $mockParser;\n\n    protected function setUp()\n    {\n        $this-\u003emockNodeTraverser = Mockery::mock(PhpParser\\NodeTraverser::class);\n        $this-\u003emockDependencyInspectionVisitor = Mockery::mock(Mihaeu\\PhpDependencies\\Analyser\\DependencyInspectionVisitor::class);\n        $this-\u003emockParser = Mockery::mock(Mihaeu\\PhpDependencies\\Analyser\\Parser::class);\n        $this-\u003eclassUnderTest = new StaticAnalyser(\n            $this-\u003emockNodeTraverser,\n            $this-\u003emockDependencyInspectionVisitor,\n            $this-\u003emockParser\n        );\n    }\n\n    public function testMissing()\n    {\n        $this-\u003efail('Test not yet implemented');\n    }\n}\n```\n\n## Roadmap\n\n - avoid FQNs by default by including (`use`) all required namespaces\n - `--template=\u003cpath\u003e` for custom templates\n - and many more features are planned, just [check out the functional backlog](https://github.com/mihaeu/php-test-generator/tree/master/tests/functional/backlog)\n\n## Contributing\n\nIf you have any ideas for new features or are willing to contribute yourself you are more than welcome to do so.\n\nMake sure to keep the code coverage at 100% (and run humbug for mutation testing) and stick to PSR-2. The `Makefile` in the repo is making lots of assumptions and probably won't work on your machine, but it might help.\n\n## LICENSE\n\n\u003e Copyright (c) 2017-2019 Michael Haeuslmann\n\u003e \n\u003e Permission is hereby granted, free of charge, to any person obtaining a copy\n\u003e of this software and associated documentation files (the \"Software\"), to deal\n\u003e in the Software without restriction, including without limitation the rights\n\u003e to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n\u003e copies of the Software, and to permit persons to whom the Software is\n\u003e furnished to do so, subject to the following conditions:\n\u003e \n\u003e The above copyright notice and this permission notice shall be included in all\n\u003e copies or substantial portions of the Software.\n\u003e \n\u003e THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n\u003e IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n\u003e FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n\u003e AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n\u003e LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n\u003e OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n\u003e SOFTWARE.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmihaeu%2Fphp-test-generator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmihaeu%2Fphp-test-generator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmihaeu%2Fphp-test-generator/lists"}