PHPackages                             schranz/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. schranz/test-generator

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

schranz/test-generator
======================

A cli tool which generates unit tests.

0.4.6(2y ago)824.8k↓50%1[1 issues](https://github.com/alexander-schranz/test-generator/issues)[1 PRs](https://github.com/alexander-schranz/test-generator/pulls)MITPHPPHP ^8.1

Since Feb 20Pushed 2y ago3 watchersCompare

[ Source](https://github.com/alexander-schranz/test-generator)[ Packagist](https://packagist.org/packages/schranz/test-generator)[ GitHub Sponsors](https://github.com/alexander-schranz)[ RSS](/packages/schranz-test-generator/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (13)Versions (11)Used By (0)

PHP Test Generator
==================

[](#php-test-generator)

This project make usages of [PHPStan](https://github.com/phpstan/phpstan) (WIP) and [PHPParser](https://github.com/nikic/PHP-Parser)to generate test cases for a given PHP File by parsing its [AST](https://en.wikipedia.org/wiki/Abstract_syntax_tree).

Why?
----

[](#why)

With static code analyzer it is possible to generate tests which where mostly forgotten. The target of the project is not to generate a whole test cases instead it should generate the most boilerplate code of the test case and tell which method for the class methods should be implemented.

So example if we have a method like the following:

```
public function setTitle(?string $title): void
{
    $this->title = $title;
}

public function getTitle(): void
{
    $this->title = $title;
}
```

If you are using code coverage you will get 100% when you are testing:

```
public function testSetTitle(): void
{
    $model = $this->createInstance();
    $model->setTitle('Test');
    $this->assertSame('Test', $model->getTitle());
}
```

But as `?string` can be seen as an union type of `string|null` the testcase for `null` type is missing:

```
public function testSetTitleNull(): void
{
    $model = $this->createInstance();
    $model->setTitle(null);
    $this->assertNull($model->getTitle());
}
```

The project target is to already generate also the boilerplate for that test case.

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

[](#installation)

```
composer require --dev schranz/test-generator
```

Config
------

[](#config)

Create a new `tests/generator-config.php` file:

```
