xepozz/test-it

该包的最新版本(1.2.1)没有提供许可证信息。

1.2.1 2023-01-25 22:31 UTC

This package is auto-updated.

Last update: 2024-08-30 01:55:20 UTC


README

一个基于类方法签名的测试用例文件生成工具。

安装

composer require xepozz/test-it --dev

使用

从控制台运行脚本,并传递 source 目录和 target 目录以满足您的需求。默认值分别为 srctests

./vendor/bin/test-it src tests

描述

该包读取 source 目录下的所有 .php 文件,分析它们,并在 target 目录中创建与 source 目录的相对路径镜像的文件。

该工具尊重参数类型和方法返回值,并生成所有可能的测试用例。

示例

输入

<?php

declare(strict_types=1);

namespace Xepozz\TestIt\Tests\Integration\OneParameter\src;

class UserController
{
    public function inverse(bool $value): bool
    {
        return !$value;
    }
}

输出

<?php

declare(strict_types=1);

namespace Xepozz\TestIt\Tests\Integration\OneParameter\tests;

use Xepozz\TestIt\Tests\Integration\OneParameter\src\UserController;

final class UserControllerTest extends \PHPUnit\Framework\TestCase
{
    /**
     * @dataProvider dataProviderInverse
     */
    public function testInverse(bool $expectedValue, bool $valueValue): void
    {
        // arrange
        $userController = new UserController();

        // act
        $actualValue = $userController->inverse($valueValue);

        // assert
        $this->assertEquals($expectedValue, $actualValue);
    }


    public static function dataProviderInverse(): iterable
    {
        yield [false, true];
        yield [true, false];
    }
}

如我们所见,它生成了一个与函数相关的 dataProvider,评估返回值并将其保存到数据提供者函数中。

配置文件

如果您需要配置生成过程,请在项目根目录下创建一个名为 test-it.php 的文件,并按您的意愿进行配置。

以下是一些可能的配置选项示例

<?php

declare(strict_types=1);

use Xepozz\TestIt\Config;

return function (Config $config) {
    $config
        // disabled results substitution
        ->evaluateCases(false)
        // sets a directory to scan
        ->setSourceDirectory('src')
        // excludes particular files from scanning
        ->excludeFiles([
            __DIR__ . '/src/Kernel.php',
        ])
        // excludes particular directories and all child directories from scanning
        ->excludeDirectories([
            __DIR__ . '/src/Asset',
            __DIR__ . '/src/Controller',
            __DIR__ . '/src/View',
        ])
        // includes subdirectories when parent directories were ignored
        ->includeDirectories([
            __DIR__ . '/src/Controller/DTO',
        ]);
};

传递命令行参数不会更改配置

帮助

使用 --help 标志调用脚本以查看所有可能的选项。

./vendor/bin/test-it --help
Usage:
./test-it [<source> [<target>]]

Arguments:
source                The directory that will be processed [default: "src"]
target                The output directory where tests will be placed [default: "tests"]

Options:
-h, --help            Display help for the given command. When no command is given display help for the ./test-it command
-q, --quiet           Do not output any message
-V, --version         Display this application version
--ansi|--no-ansi  Force (or disable --no-ansi) ANSI output
-n, --no-interaction  Do not ask any interactive question
-v|vv|vvv, --verbose  Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug

附加文档

路线图

  • 模拟类创建
  • 支持多种测试方法名称策略(test_function_nametestFunctionName
  • 当方法始终返回相同的结果时,测试常量表达式
  • 添加基准测试
  • 添加静态分析器
  • 添加排除列表
    • 路径(目录、文件)
    • 继承树(接口、父类)
  • 使用命令行参数覆盖配置
  • 添加 Codeception 支持

限制

不与未命名空间类一起使用。

只有一个测试方法名称生成策略。请参阅路线图。