haijin/testing

此包已被弃用且不再维护。作者建议使用 haijin/specs 包。

扩展 PHPUnit 以简化测试并提高测试的表达能力。

0.0.2 2018-12-06 15:38 UTC

This package is auto-updated.

Last update: 2022-02-01 13:15:19 UTC


README

扩展 PHPUnit 以简化测试并提高测试的表达能力。

此库已弃用,将不再开发和维护。它已被 haijin/specs 完全替代。

Latest Stable Version Latest Unstable Version Build Status License

目录

  1. 安装
  2. 用法
    1. 将特性添加到测试类中
    2. 期望
      1. expectExactExceptionRaised
      2. expectObjectToBeLike
      3. expectObjectToBeExactly
      4. expectFileContents
  3. 运行测试

安装

将此库包含在您的项目 composer.json 文件中

{
    ...

    "require-dev": {
        ...
        "haijin/testing": "^0.0.2",
        ...
    },

    ...
}

用法

将特性添加到测试类中

在 TestCase 类中添加以下内容

class YourTest extends TestCase
{
    use \Haijin\Testing\AllExpectationsTrait;

    // ...
}

期望

使用以下任一期望

expectExactExceptionRaised

期望一个测试闭包抛出确切的期望异常类。

没有断言闭包的异常使用的示例

class YourTest extends TestCase
{
    use \Haijin\Testing\ExceptionsExpectationsTrait;

    function someTest() {
        // test code here ...

         // Expects the closure (second parameter) to raise an exception named SomeException.
        $this->expectExactExceptionRaised(
            'SomeException',
            function() {
                $this->some_object->do_something_that_may_raise_the_exception();
            }
        );

        // more test code here ...
    }
}

有断言闭包的异常使用的示例

class YourTest extends TestCase
{
    use \Haijin\Testing\ExceptionsExpectationsTrait;

    function someTest() {
        // test code here ...

         // Expects the closure (second parameter) to raise an exception named SomeException.
        $this->expectExactExceptionRaised(
            'SomeException',
            function() {
                $this->some_object->do_something_that_may_raise_the_exception();
            },
            function($raised_exception) {
                $this->assertEquals( 123, $raised_exception->get_value() );
            }
        );

        // more test code here ...
    }
}

expectObjectToBeLike

期望一个对象或字典与给定的规范相似,递归地断言对象的每个属性。

期望可以是常量值,用于与 assertEquals 断言相等

$this->expectObjectToBeLike( $object, [
    "name" => "Lisa",
    "last_name" => "Simpson",
    "address" => [
        "street" => "Evergreen 742"
    ]
]);

或闭包以使用任何断言

$this->expectObjectToBeLike( $object, [
    "name" => function($value) { $this->assertEquals( "Lisa", $value ); },
    "last_name" => "Simpson",
    "address" => [
        // the closure also accepts an optional parameter with the attribute path:
        "street" => function($value, $attribute_path) { $this->assertEquals( "Evergreen 742", $value ); }
    ]
]);

访问器可以是数组属性、对象公共属性或对象公共获取器方法

$this->expectObjectToBeLike( $object, [
    "get_name()" => function($value) { $this->assertEquals( "Lisa", $value ); },
    "get_last_name()" => "Simpson",
    "get_address()" => [
        "street" => "Evergreen 742"
    ]
]);

expectObjectToBeExactly

类似于 expectObjectToBeLike,但如果验证的对象是一个具有规范中未期望的属性的数组,则断言失败。

expectFileContents

断言文件具有期望的内容。

class YourTest extends TestCase
{
    use \Haijin\Testing\FilesExpectationsTrait;

    function someTest() {
        // test code here ...

        $this->expectFileContents(
            'File contents',
            $file_path
        );

        // more test code here ...
    }
}

或使用闭包

class YourTest extends TestCase
{
    use \Haijin\Testing\FilesExpectationsTrait;

    function someTest() {
        // test code here ...

        $this->expectFileContents(
            function($file_contents) {
                $this->assertEquals( "File contents", $file_contents );
            },
            $file_path
        );

        // more test code here ...
    }
}

运行测试

composer test