darling/php-unit-test-utilities

一个特性集合,用于定义辅助实现phpunit测试的方法。

v1.0.9 2024-02-09 17:15 UTC

README

PHPUnitTestUtilities库提供了定义辅助实现phpunit测试的方法的特性。

概述

安装

composer require darling/php-unit-test-utilities

特性

Darling\PHPUnitTestUtilities\traits\PHPUnitConfigurationTests

此特性定义了以下测试方法

    /**
     * Test that phpunit tests run.
     *
     * If this test does not run then phpunit is not set up
     * correctly.
     *
     */
    public function test_php_unit_tests_are_run(): void;

注意:更详细的文档可以在特性本身中找到

tests/PHPUnitTestUtilities/traits/PHPUnitConfigurationTests.php

Darling\PHPUnitTestUtilities\traits\PHPUnitTestMessages

此特性定义了以下方法,用于返回可用来从phpunit测试中输出消息的格式化字符串,例如,当测试失败时。

    /**
     * Return a message that indicates the failure of a test.
     *
     */
    protected function testFailedMessage(
        object $testedInstance,
        string $testedMethod,
        string $expectation
    ): void;

注意:更详细的文档可以在特性本身中找到

tests/PHPUnitTestUtilities/traits/PHPUnitTestMessages.php

Darling\PHPUnitTestUtilities\traits\PHPUnitRandomValues

PHPUnitRandomValues特性定义了以下方法,这些方法返回各种类型的随机值。

    /**
     * Return a string composed of a random number of randomly
     * generated characters.
     *
     */
    protected function randomChars(): string

    /**
     * Return a random float.
     *
     */
    protected function randomFloat(): float

    /**
     * Return a random fully qualified class name, or object instance.
     *
     */
    protected function randomClassStringOrObjectInstance(): string|object

    /**
     * Return a random object instance.
     *
     */
    protected function randomObjectInstance(): object

注意:更详细的文档可以在特性本身中找到

tests/PHPUnitTestUtilities/traits/PHPUnitRandomValues.php

示例

以下是一个假设示例,说明如何在实现phpunit测试的类中使用PHPUnitTestUtilities库提供的特性。

<?php

namespace Darling\PHPUnitTestUtilities\Tests;

use PHPUnit\Framework\TestCase;
use Darling\PHPUnitTestUtilities\traits\PHPUnitConfigurationTests;
use Darling\PHPUnitTestUtilities\traits\PHPUnitTestMessages;
use Darling\PHPUnitTestUtilities\traits\PHPUnitRandomValues;

class ExampleTest extends TestCase
{
    use PHPUnitConfigurationTests;
    use PHPUnitTestMessages;
    use PHPUnitRandomValues;

    public function testArrayIsEmpty()
    {
        $testedInstance =
            (object) [
                'foo' => [
                    $this->randomChars(),
                    $this->randomFloat(),
                    $this->randomClassStringOrObjectInstance(),
                    $this->randomObjectInstance(),
                ]
            ];
        $this->assertEmpty(
            $testedInstance->foo,
            $this->testFailedMessage(
                $testedInstance,
                '',
                'The array assigned to the foo property must be empty'
            )
        );
    }

}

上面的示例测试在运行phpunit时将导致以下失败的测试

...

There was 1 failure:

1) tests\ExampleTest::testArrayIsEmpty
The stdClass implementation fails to fulfill the following expectation:

The array assigned to the foo property must be empty.
Failed asserting that an array is empty.

...