Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/jwage/phpunit-test-generator


https://github.com/jwage/phpunit-test-generator

Last synced: 3 months ago
JSON representation

Awesome Lists containing this project

README

        

# PHPUnit Test Generator

This PHP tool can generate PHPUnit test classes for your PHP classes.

This tool currently only supports the PSR4 autoloading strategy. If you would like to see it support
other autoloading strategies and application organizational structures, pull requests are welcome.

## Install

```console
$ composer require --dev jwage/phpunit-test-generator
```

You can also download the latest PHAR from the [releases](https://github.com/jwage/phpunit-test-generator/releases) page.

## Generate Test Class

Take a class named `App\Services\MyService` located in `src/Services/MyService.php`:

```php
namespace App\Services;

class MyService
{
/** @var Dependency */
private $dependency;

/** @var int */
private $value;

public function __construct(Dependency $dependency, int $value)
{
$this->dependency = $dependency;
$this->value = $value;
}

public function getDependency() : Dependency
{
return $this->dependency;
}

public function getValue() : int
{
return $this->value;
}
}
```

And a dependency to this class named `App\Services\Dependency` located in `src/Services/Dependency.php`:

```php
myService->getDependency());
}

public function testGetValue() : void
{
self::assertSame(1, $this->myService->getValue());
}

protected function setUp() : void
{
$this->dependency = $this->createMock(Dependency::class);
$this->value = 1;

$this->myService = new MyService(
$this->dependency,
$this->value
);
}
}
```

Now you have a skeleton unit test and you can fill in the defails of the generated methods.