raffaelecarelle/phpunit-test-generator 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

raffaelecarelle/phpunit-test-generator

Composer 安装命令:

composer require raffaelecarelle/phpunit-test-generator

包简介

Generate PHPUnit test classes for a PHP class.

README 文档

README

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

$ composer require --dev raffaelecarelle/phpunit-test-generator

Generate Test Class

Take a class named App\Services\MyService located in src/Services/MyService.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

namespace App\Services;

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

Now you can generate a test class for MyService with the following command:

$ php vendor/bin/generate-unit-test "App\Services\MyService"

You can also pass a path to a class instead of giving the class name:

$ php vendor/bin/generate-unit-test src/Services/MyService.php

A test would be generated at tests/Services/MyServiceTest.php that looks like this:

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

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

统计信息

  • 总下载量: 2
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 0
  • Forks: 7
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-11-04