detain / test-generator
为现有文件生成测试用例(https://github.com/detain/php-test-generator 的分支)
Requires
- php: ^7.1
- docopt/docopt: ^1.0
- nikic/php-parser: ^3.0
- twig/twig: ^1.0
Requires (Dev)
- humbug/humbug: dev-master@DEV
- phpmd/phpmd: ^2.6
- phpstan/phpstan: ^0.7.0
- phpunit/phpunit: ^6.5
- squizlabs/php_codesniffer: *
This package is auto-updated.
Last update: 2024-09-27 23:42:55 UTC
README
为现有文件生成测试用例
用例
- PHPStorm 支持 Apache Velocity 文件模板,但使用起来很麻烦,并且功能有限
- 其他 IDE 或编辑器,如 Vim 或 Emacs,没有内置代码生成功能
- 测试文件始终没有放在正确的位置,迫使您手动重新排列代码
test-generator
可以让您在测试遗留应用程序时避免输入重复代码的繁琐工作。下次您为一个具有太多依赖关系的类编写测试时,并且开始模拟时,想想如果您能自动化这项工作,您可以节省多少时间。
这就是 test-generator
发挥作用的地方。试用一下,根据您的需求进行配置,并为您的 shell 创建别名,或者在您的编辑器/IDE(如 PHPStorm)中将其包含为外部工具。
用法
命令行界面
bin/test-generator --help
Test-Generator
Usage:
test-generator [options] [(--src-base --test-base)] <file>
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=<base-class> Inherit from this base class e.g. "Example\TestCase".
--subject-format=<format> Format the field for the subject class.
--field-format=<format> Format the fields for dependencies.
-s, --src-base=<path> Base directory for source files; requires --test-base
-t, --test-base=<path> 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
我建议将 test-generator
集成到 PHPStorm 的外部工具中。这是因为 PHPStorm 可以将当前活动文件的文件名作为参数传递给 test-generator
,然后它会生成并将测试写入您预先配置的位置。
转到设置 > 工具 > 外部工具,然后点击 +。添加以下信息
如果您正在使用 .phar
文件,请记得调整 程序 和 参数。
如果您想使用不同的设置和位置生成不同的测试,只需创建更多的外部工具条目。
技巧:为此工具分配一个快捷键,因为您可能最终会经常使用它 ;)
安装
Composer (PHP 7.1+)
# local install composer require "detain/test-generator:^1.0" # global install composer global require "detain/test-generator:^1.0"
Phar (PHP 5.5+)
由于我实际上需要在 5.5 的遗留项目中使用此工具(应该也适用于 5.4,但未对其进行测试),因此我还发布了一个适用于旧版本的 phar 文件。
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
请注意,通过这样做,我们应该对我们自己没有升级到 PHP 7.1(很快是 7.2)感到厌恶。
Git
git clone https://github.com/detain/php-test-generator
cd php-test-generator
composer install
bin/test-generator --help
如果您没有安装 PHP 7.1,可以运行 bin/remove-php7-features
来转换源文件。但是,我不会接受没有 PHP 7.1 支持的拉取请求。
示例
给定一个 PHP 文件,如下所示
<?php declare(strict_types=1); namespace Detain\TestGenerator; use Twig_TemplateWrapper; class TwigRenderer { // ... public function __construct(\Twig_Environment $twig, TemplateConfiguration $templateConfiguration) { // ... } // ... }
运行以下命令
# re-formatted for legibility bin/test-generator src/TwigRenderer.php --field-format="mock%N" --subject-format="classUnderTest" --php5 --phpunit5 --mockery --base-class="Vendor\\TestCase"
将生成一个包括模拟依赖项的测试用例
<?php namespace Detain\PhpDependencies\Analyser; use Mockery; use Mockery\MockInterface; use Vendor\TestCase; class StaticAnalyserTest extends TestCase { /** @var StaticAnalyser */ private $classUnderTest; /** @var PhpParser\NodeTraverser | MockInterface */ private $mockNodeTraverser; /** @var Detain\PhpDependencies\Analyser\DependencyInspectionVisitor | MockInterface */ private $mockDependencyInspectionVisitor; /** @var Detain\PhpDependencies\Analyser\Parser | MockInterface */ private $mockParser; protected function setUp() { $this->mockNodeTraverser = Mockery::mock(PhpParser\NodeTraverser::class); $this->mockDependencyInspectionVisitor = Mockery::mock(Detain\PhpDependencies\Analyser\DependencyInspectionVisitor::class); $this->mockParser = Mockery::mock(Detain\PhpDependencies\Analyser\Parser::class); $this->classUnderTest = new StaticAnalyser( $this->mockNodeTraverser, $this->mockDependencyInspectionVisitor, $this->mockParser ); } public function testMissing() { $this->fail('Test not yet implemented'); } }
路线图
- 默认情况下通过包含(
use
)所有必需的命名空间来避免 FQNs - 使用
--template=<path>
为自定义模板 - 还有更多功能计划,请查看功能待办事项
贡献
如果您有任何新功能的想法或愿意自己贡献,您非常欢迎这样做。
请确保代码覆盖率保持在 100%(并运行 humbug 进行变异测试),并坚持 PSR-2。仓库中的 Makefile
做了许多假设,可能不会在您的机器上工作,但它可能有所帮助。
许可证
版权所有(c)2017-2019 Michael Haeuslmann
特此授予任何获得本软件及其相关文档文件(以下简称“软件”)副本的任何人免费使用软件的权利,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或出售软件副本的权利,并允许向软件提供方提供软件的人行使上述权利,但受以下条件的约束:
上述版权声明和本许可声明应包含在软件的所有副本或主要部分中。
软件按“原样”提供,不提供任何形式的保证,无论是明示的还是暗示的,包括但不限于适销性、适用于特定目的和非侵权的保证。在任何情况下,作者或版权所有者不对任何索赔、损害或其他责任负责,无论该责任是因合同、侵权或其他原因产生,无论该责任是否与软件、软件的使用或其他与软件相关的操作有关。