calgamo/test

此包已被弃用且不再维护。未建议替代包。

测试实用类/特性。支持 PHPUnit

0.5.1 2019-11-18 23:56 UTC

This package is auto-updated.

Last update: 2019-11-19 03:03:39 UTC


README

Latest Version on Packagist Software License Build Status Coverage Status Code Climate Total Downloads

描述

Calgamo/Test 是一个测试库。

特性

演示

示例 1: 获取私有/受保护属性

use Calgamo\Test\TestCase\PhpUnitTestCase;

class TestTarget
{
    private $foo;

    public function __construct()
    {
        $this->foo = 123;
    }
}

class MyTest extends PhpUnitTestCase
{
    public function testGetPrivateProperty()
    {
        $object = new TestTarget();

        // get private property from object
        $value = $this->getPrivatePropertyWithoutException($object, 'foo');

        // assert
        $this->assertSame(123, $value);
    }
}

示例 2: 调用私有/受保护方法

class TestTarget
{
    /** @noinspection PhpUnusedPrivateMethodInspection */
    private function sayHello()
    {
        return 'Hello';
    }
}

class MyTest extends PhpUnitTestCase
{
    public function testCallPrivateMethod()
    {
        $object = new TestTarget();

        // call private method
        $ret = $this->callPrivateMethodWithoutException($object, 'sayHello');

        // assert if result is not right
        $this->assertSame('Hello', $ret);
    }
}

示例 3: 调用私有/受保护静态方法

class TestTarget
{
    /** @noinspection PhpUnusedPrivateMethodInspection */
    private static function sayBye()
    {
        return 'Bye';
    }
}

class MyTest extends PhpUnitTestCase
{
    public function testCallPrivateMethod()
    {
        // call private static method
        $ret = $this->callPrivateStaticMethodWithoutException(TestTarget::class, 'sayBye');

        // assert if result is not right
        $this->assertSame('Bye', $ret);
    }
}

示例 4: 在指定的回调中抛出异常时调用回调

class TestTarget
{
    /**
     * @throws \Exception
     */
    public function doSomething()
    {
        throw new \Exception('Emergency');
    }
}

class MyTest extends PhpUnitTestCase
{
    public function testCallbackIfExceptionIsThrown()
    {
        $this->callbackIfExceptionIsThrown(
            function(){
                (new TestTarget)->doSomething();
            },
            function(\Throwable $e){
                $this->fail('something wrong: ' . $e->getMessage());
            });
    }
}

用法

要求

PHP 7.1 或更高版本

安装 calgamo/test

推荐通过 Composer 安装 calgamo/test。

composer require calgamo/test

安装后,您需要引入 Composer 的自动加载器

require 'vendor/autoload.php';

许可

MIT

作者

stk2k

免责声明

此软件不提供任何保证。

我们不承担使用此软件造成的任何结果的责任。

请自行负责。