PHPUnit模拟功能? [英] PHPUnit mock function?

查看:51
本文介绍了PHPUnit模拟功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个有趣的场景,我需要定义一个函数才能对另一个函数进行测试.我要测试的功能看起来像这样:

I have an interesting scenario in that I need a function to be defined in order to make tests for another function. The function I want to test looks something like this:

if (function_exists('foo') && ! function_exists('baz')) {
    /**
     * Baz function
     * 
     * @param integer $n
     * @return integer
     */
    function baz($n)
    {
        return foo() + $n;
    }
}

我检查foo是否存在的原因是因为它可能在开发人员的项目中定义,也可能未定义,并且功能baz依赖于foo.因此,如果要调用foo,我只希望定义baz.

The reason I am checking for the existence of foo is because it may or may not be defined in a developer's project and the function baz relies on foo. Because of this, I only want baz to be defined if it can call foo.

唯一的问题是,到目前为止,尚不可能为其编写测试.我尝试在PHPUnit配置中创建引导脚本,该脚本将定义伪造的foo函数,然后需要Composer自动加载器,但是我的主脚本仍然认为未定义foo. foo不是Composer软件包,因此我的项目不需要它.显然,Mockery对此也不起作用.我的问题是,是否有对PHPUnit更有经验的人遇到这个问题并找到了解决方案.

The only problem is that so far it has been impossible to write tests for. I tried creating a bootstrap script in the PHPUnit configuration that would define a fake foo function and then require the Composer autoloader, but my main script still thinks foo is not defined. foo is not a Composer package and can not otherwise be required by my project. Obviously Mockery will not work for this either. My question is if anyone more experienced with PHPUnit has come across this issue and found a solution.

谢谢!

推荐答案

从稍微重构代码开始,使其更具可测试性.

Start with a slight refactor of the code to make it more testable.

function conditionalDefine($baseFunctionName, $defineFunctionName)
{
    if(function_exists($baseFunctionName) && ! function_exists($defineFunctionName))
    {
        eval("function $defineFunctionName(\$n) { return $baseFunctionName() + \$n; }");
    }
}

然后这样称呼它:

conditionalDefine('foo', 'bar');

您的PHPUnit测试类将包含以下测试:

Your PHPUnit test class will contain the following tests:

public function testFunctionIsDefined()
{
    $baseName = $this->mockBaseFunction(3);
    $expectedName = uniqid('testDefinedFunc');
    conditionalDefine($baseName, $expectedName);
    $this->assertTrue(function_exists($expectedName));
    $this->assertEquals(5, $expectedName(2)); 
}
public function testFunctionIsNotDefinedBecauseItExists()
{
    $baseName = $this->mockBaseFunction(3);
    $expectedName = $this->mockBaseFunction($value = 'predefined');
    conditionalDefine($base, $expectedName);
    // these are optional, you can't override a func in PHP
    // so all that is necessary is a call to conditionalDefine and if it doesn't
    // error, you're in the clear
    $this->assertTrue(function_exists($expectedName));
    $this->assertEquals($value, $expectedName()); 
}
public function testFunctionIsNotDefinedBecauseBaseFunctionDoesNotExists()
{
    $baseName = uniqid('testBaseFunc');
    $expectedName = uniqid('testDefinedFunc');
    conditionalDefine($base, $expectedName);
    $this->assertFalse(function_exists($expectedName));     
}

protected function mockBaseFunction($returnValue)
{
    $name = uniqid('testBaseFunc');
    eval("function $name() { return '$value'; }");
    return $name;
}

这足以满足您的要求.但是,您可以进一步重构此代码,将函数生成提取到代码生成器中.您可以针对生成器编写单元测试,以确保它可以创建您期望的功能.

That is sufficient for what you're asking. You can however, further refactor this code extracting the function generation into a code generator. That what you can write unit tests against the generator to make sure it creates the kind of function you expect.

这篇关于PHPUnit模拟功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆