qubit05/phpunit-mockfunction

PHPUnit 扩展,使用 Runkit 模拟 PHP 内部函数。

1.0.1 2013-04-22 13:20 UTC

This package is not auto-updated.

Last update: 2024-09-23 10:54:26 UTC


README

PHPUnit 扩展,使用 Runkit 模拟 PHP 内部函数。

要求

*这是一个可选要求。目前 Runkit 不支持覆盖内部函数(exit、die 等)。

安装

使用 composer,将以下内容添加到 composer.json 文件中

{
   "require": {
       "qubit05/phpunit-mockfunction": "1.*"
   }
}

示例

ExampleClass.php

<?php
class ExampleClass
{
    public function doExample()
    {
        return date();
    }
}

ExampleClassTest.php

<?php
class ExampleClassTest extends \PHPUnit_Framework_TestCase
{
    public function testExample()
    {
        $param = 'Y-m-d H:i:s';
        $value = 'non date value';
    
        $mockFunction = new PHPUnit_Extensions_MockFunction('date', $this);
        $mockFunction->expects($this->once())
            ->with($this->equalTo($param))
            ->will($this->returnValue($value));
        
        $exampleClass = new ExampleClass();
        $this->assertEquals($value, $exampleClass->doExample($param));
    }
}

致谢

在创建此类时,我们从 tcz/phpunit-mockfunction 中汲取了一些灵感。这两个类相似但并不相同。