PHPackages                             detain/test-generator - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Testing &amp; Quality](/categories/testing)
4. /
5. detain/test-generator

ActiveLibrary[Testing &amp; Quality](/categories/testing)

detain/test-generator
=====================

Generate test cases for existing files (fork of https://github.com/detain/php-test-generator )

1.3.0(7y ago)019MITPHPPHP ^7.1

Since Aug 27Pushed 3y agoCompare

[ Source](https://github.com/detain/php-test-generator)[ Packagist](https://packagist.org/packages/detain/test-generator)[ Docs](https://github.com/mihaeu/php-test-generator)[ RSS](/packages/detain-test-generator/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (8)Versions (5)Used By (0)

 [![](doc/testgenerator-logo.png)](doc/testgenerator-logo.png)

[![Travis branch](https://camo.githubusercontent.com/0bcfae6351779a4b5c0954bd7d699f1696916430c17c0caa4b3c93d9e4274b21/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f64657461696e2f7068702d746573742d67656e657261746f722f646576656c6f702e737667)](https://travis-ci.org/detain/php-test-generator)[![Codecov branch](https://camo.githubusercontent.com/1165dda8f6896127f45f4f2f501b79bb97b3a9e287ee54ab79c7c4d645c527a3/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636f762f632f6769746875622f64657461696e2f7068702d746573742d67656e657261746f722f646576656c6f702e737667)](https://codecov.io/gh/detain/php-test-generator)[![Infection MSI](https://camo.githubusercontent.com/6e23b320026ea141ca51d7a2068dcce213212a6aa4086b16064be87c3a13f77e/68747470733a2f2f62616467652e737472796b65722d6d757461746f722e696f2f6769746875622e636f6d2f64657461696e2f7068702d746573742d67656e657261746f722f6d6173746572)](https://infection.github.io)[![](https://camo.githubusercontent.com/408a39a4a5c4efe19166bafa8856ef8bf2aa67f2e7470f7fdd79a8cf62eed6e2/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d372e332d627269676874677265656e2e737667)](https://camo.githubusercontent.com/408a39a4a5c4efe19166bafa8856ef8bf2aa67f2e7470f7fdd79a8cf62eed6e2/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d372e332d627269676874677265656e2e737667)[![](https://camo.githubusercontent.com/bce01bc060914f5743ce2f722115f9ddc50269843ad8c107d298ae54a1b57e4c/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d372e322d627269676874677265656e2e737667)](https://camo.githubusercontent.com/bce01bc060914f5743ce2f722115f9ddc50269843ad8c107d298ae54a1b57e4c/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d372e322d627269676874677265656e2e737667)[![](https://camo.githubusercontent.com/7bb1f3b17d4df34dc38843f330a346086b824c8b8878f9f9d7ecda59435cf2cb/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d372e312d79656c6c6f772e737667)](https://camo.githubusercontent.com/7bb1f3b17d4df34dc38843f330a346086b824c8b8878f9f9d7ecda59435cf2cb/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d372e312d79656c6c6f772e737667)[![](https://camo.githubusercontent.com/3982484ff63828f40b66a9d6533668558f31f36c1970b66854c69149c01f627a/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d372e302d79656c6c6f772e737667)](https://camo.githubusercontent.com/3982484ff63828f40b66a9d6533668558f31f36c1970b66854c69149c01f627a/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d372e302d79656c6c6f772e737667)[![](https://camo.githubusercontent.com/ba2e28557da98323fe86d0a894cb98ef960652d036d4ce9e261b5cba950410ad/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d352e362d79656c6c6f772e737667)](https://camo.githubusercontent.com/ba2e28557da98323fe86d0a894cb98ef960652d036d4ce9e261b5cba950410ad/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d352e362d79656c6c6f772e737667)[![](https://camo.githubusercontent.com/6bf34d28a4555c27b42d9d3f245dc635313e9b705c1d21dc8dca1232bbc0f17b/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d352e352d79656c6c6f772e737667)](https://camo.githubusercontent.com/6bf34d28a4555c27b42d9d3f245dc635313e9b705c1d21dc8dca1232bbc0f17b/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d352e352d79656c6c6f772e737667)

> Generate test cases for existing files

Use Cases
---------

[](#use-cases)

- PHPStorm has Apache Velocity support for file templates, but it is annoying to work with and limited
- other IDEs or editors like Vim or Emacs don't have built-in code generation
- somehow the test files never end up where they belong forcing you to rearrange code manually

`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.

This 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)).

Usage
-----

[](#usage)

### CLI

[](#cli)

```
bin/test-generator --help
```

```
Test-Generator

Usage:
  test-generator [options] [(--src-base --test-base)]

Options:
  --php5                        Generate PHP5 compatible code [default:false].
  --phpunit5                    Generate a test for PHPUnit 5 [default:false].
  --mockery                     Generates mocks using Mockery [default:false].
  --covers                      Adds the @covers annotation   [default:false].
  --base-class=     Inherit from this base class e.g. "Example\TestCase".
  --subject-format=     Format the field for the subject class.
  --field-format=       Format the fields for dependencies.
  -s, --src-base=         Base directory for source files; requires --test-base
  -t, --test-base=        Base directory for test files; requires --src-base; writes output to that directory

Format:
  %n                            Name starting with a lower-case letter.
  %N                            Name starting with an upper-case letter.
  %t                            Type starting with a lower-case letter.
  %T                            Type starting with a upper-case letter.

Format Examples:
  "mock_%t"                      Customer => mock_customer
  "%NTest"                       arg => ArgTest
  "testClass"                    SomeName => TestClass

```

### PHPStorm

[](#phpstorm)

I recommend integrating `test-generator` as an external tool in PHPStorm. This works, because PHPStorm can pass the filename of the currently active file as an argument to `test-generator`, which will then generate and write the test to your preconfigured location.

Navigate to Settings &gt; Tools &gt; External Tools and klick on **+**. Add the following information:

FieldValueNametest-generatorDescriptionGenerate Test StubsProgram`$PhpExecutable`Arguments`vendor/bin/test-generator $FilePath$ -s base=src -t tests/unit`Working directory`$ProjectFileDir$`Remember to adjust *Program* and *Arguments* in case you are using the `.phar` file.

In case you want to generate different tests with different settings and locations, simply create more external tool entries.

[![How to integrate test-generator in PHPStorm](doc/phpstorm-integration.png)](doc/phpstorm-integration.png)

**Pro Tip**: Assign a shortcut to this tool, because you might end up using it a lot ;)

Installation
------------

[](#installation)

### Composer (PHP 7.1+)

[](#composer-php-71)

```
# local install
composer require "detain/test-generator:^1.0"

# global install
composer global require "detain/test-generator:^1.0"
```

### Phar (PHP 5.5+)

[](#phar-php-55)

Since 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:

```
wget https://github.com/detain/php-test-generator/releases/download/1.2.0/test-generator-1.2.0.phar
chmod +x test-generator-1.2.0.phar
```

**Please note that by doing this we should be disgusted at ourselves for not upgrading to PHP 7.1 (soon 7.2).**

### Git

[](#git)

```
git clone https://github.com/detain/php-test-generator
cd php-test-generator
composer install
bin/test-generator --help
```

If 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.

Example
-------

[](#example)

Given a PHP file like:

```
