PHP code example of jwage / phpunit-test-generator

1. Go to this page and download the library: Download jwage/phpunit-test-generator library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

jwage / phpunit-test-generator example snippets


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;
    }
}



namespace App\Services;

class Dependency
{
    public function getSomething() : null
    {
        return null;
    }
}

declare(strict_types=1);

namespace App\Tests\Services;

use App\Services\Dependency;
use App\Services\MyService;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

class MyServiceTest extends TestCase
{
    /** @var Dependency|MockObject */
    private $dependency;

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

    /** @var MyService */
    private $myService;

    public function testGetDependency() : void
    {
        self::assertInstanceOf(Dependency::class, $this->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
        );
    }
}
console
$ php vendor/bin/generate-unit-test src/Services/MyService.php