cekurte/tdd

PHPUnit 测试用例

v1.0.2 2016-04-29 19:19 UTC

This package is auto-updated.

Last update: 2024-09-16 01:59:40 UTC


README

Build Status Code Climate Coverage Status Latest Stable Version License SensioLabsInsight

  • 这是一个对PHPUnit库的简单扩展。
  • 目前此包仅包含对一种测试用例场景的支持 ReflectionTestCase 为这个项目做出贡献

安装

  • 该包可在Packagist上找到。
  • 源代码文件与PSR-2兼容。
  • 自动加载与PSR-4兼容。
composer require cekurte/tdd

如果你喜欢这个库,给我一个星标 =)

文档

此库的创建是为了允许开发者使用一个公共基类编写PHP单元测试,最初包括以下任务

  • 设置私有、受保护或公开属性的值;
  • 获取私有、受保护或公开属性的值;
  • 调用私有、受保护或公开的方法。

设置属性值

要设置属性值(无论可见性如何),可以使用如下示例中的ReflectionTestCase::propertySetValue方法

<?php

namespace Your\Namespace;

use Cekurte\Tdd\ReflectionTestCase;

class YourClassTest extends ReflectionTestCase
{
    public function testAnything()
    {
        // Instance of a class that has one
        // private property named "yourPrivateProperty".
        $instance = new YourClass();

        // Set the value "newValue" to the property
        // "yourPrivateProperty".
        $this->propertySetValue(
            $instance,
            'yourPrivateProperty',
            'newValue'
        );

        // ...
    }
}

获取属性值

要获取属性值(无论可见性如何),可以使用如下示例中的ReflectionTestCase::propertyGetValue方法

<?php

namespace Your\Namespace;

use Cekurte\Tdd\ReflectionTestCase;

class YourClassTest extends ReflectionTestCase
{
    public function testAnything()
    {
        // Instance of a class that has one
        // private property named "yourPrivateProperty".
        $instance = new YourClass();

        // Get the value of the property
        // "yourPrivateProperty".
        $currentValue = $this->propertyGetValue(
            $instance,
            'yourPrivateProperty'
        );

        // ...
    }
}

调用方法

要调用方法(无论可见性如何),可以使用如下示例中的ReflectionTestCase::invokeMethod方法

<?php

namespace Your\Namespace;

use Cekurte\Tdd\ReflectionTestCase;

class YourClassTest extends ReflectionTestCase
{
    public function testAnything()
    {
        // Instance of a class that has one
        // private property named "yourPrivateMethod".
        $instance = new YourClass();

        // Call the method
        // "yourPrivateMethod".
        $valueReturned = $this->invokeMethod(
            $instance,
            'yourPrivateMethod',
            ['param1', 'param2', 'paramN']
        );

        // ...
    }
}

贡献

  1. 给我一个星标 =)
  2. 分支它
  3. 创建你的功能分支(git checkout -b my-new-feature
  4. 进行更改
  5. 运行测试,如果需要添加新测试则添加(vendor/bin/phpunit
  6. 提交更改(git commit -am 'Added some feature'
  7. 推送到分支(git push origin my-new-feature
  8. 创建新的Pull Request