PHPUnit 在测试函数中模拟私有方法

发布于 12-01 01:10 字数 1487 浏览 0 评论 0原文

为已经编写的代码编写单元测试有时很有趣。

我正在为以下代码编写一个测试用例(示例):

<?php

class mockPrivate {

    public static function one($a){
        $var = static::_two($a);
        return $var;
    }

    private static function _two($a){
        return $a+1;
    }
}
?>

测试类是这样的:

<?php

require_once 'mockPvt.php';

class mockPrivate_test extends PHPUnit_Framework_TestCase {
    public $classMock;
    protected function setUp(){ 
         $this->classMock = $this->getMock('mockPrivate', array('_two'));
    }

    public function test_one(){
        $a = 1;
        $retVal = 2;
        $classmock = $this->classMock;
        $classmock::staticExpects($this->once())
            ->method('_two')
            ->with($a)
            ->will($this->returnValue($retVal));
        $value = $classmock::one($a);
        $this->assertEquals($value, $retVal);                
    }    
}

?>

通过 $ phpunit mockPrivate_test.php 运行后,我收到此错误:

PHP Fatal error:  Call to private method Mock_mockPrivate_531a1619::_two() from context 'mockPrivate' in /data/www/dev-sumit/tests/example
s/mockPvt.php on line 6

但如果我更改它,

private    static function _two() 
to 
public     static function _two() or 
protected  static function _two() 

它完全可以工作美好的。由于这是遗留代码,我无法将 private 更改为 public/protected。那么有什么方法可以测试这个函数或者这是 phpunit 的限制吗?

Writing unit tests for code which is already written is fun sometimes.

I am writing a test case for the following code (an example):

<?php

class mockPrivate {

    public static function one($a){
        $var = static::_two($a);
        return $var;
    }

    private static function _two($a){
        return $a+1;
    }
}
?>

The test class is like this:

<?php

require_once 'mockPvt.php';

class mockPrivate_test extends PHPUnit_Framework_TestCase {
    public $classMock;
    protected function setUp(){ 
         $this->classMock = $this->getMock('mockPrivate', array('_two'));
    }

    public function test_one(){
        $a = 1;
        $retVal = 2;
        $classmock = $this->classMock;
        $classmock::staticExpects($this->once())
            ->method('_two')
            ->with($a)
            ->will($this->returnValue($retVal));
        $value = $classmock::one($a);
        $this->assertEquals($value, $retVal);                
    }    
}

?>

After running by $ phpunit mockPrivate_test.php I got this error:

PHP Fatal error:  Call to private method Mock_mockPrivate_531a1619::_two() from context 'mockPrivate' in /data/www/dev-sumit/tests/example
s/mockPvt.php on line 6

But if I change the

private    static function _two() 
to 
public     static function _two() or 
protected  static function _two() 

it works totally fine. Since this is a legacy code I can't change the private to public/protected. So is there any way I can test the function one or Is this a limitation of phpunit?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

他夏了夏天2024-12-08 01:10:15

另一种选择是创建一个扩展mockPrivate的类,允许访问您想要测试的对象。您的工程师应该认真思考为什么某些东西是私有的(因为这意味着该类不容易扩展)。另请记住,如果您需要覆盖测试类返回的内容,则可以模拟测试类。

class Test_MockPrivate extends MockPrivate
{
    /**
     * Allow public access to normally protected function
     */
    public static function _two($a){
        return parent::_two($a);
    }
}

// Code to force the return value of a now public function
$mock = $this->getMock('Test_MockPrivate', array('_two'));
$mock->expects($this->any())
    ->method('_two')
    ->will($this->returnValue('Some Overridden Value');

Another option is to create a class that extends mockPrivate, allowing accessibility to the object you wish to test. Your engineers should be thinking long and hard about why something is private (because that means the class is not easily extensible). Also remember that you can mock the test class if you need to override what it returns.

class Test_MockPrivate extends MockPrivate
{
    /**
     * Allow public access to normally protected function
     */
    public static function _two($a){
        return parent::_two($a);
    }
}

// Code to force the return value of a now public function
$mock = $this->getMock('Test_MockPrivate', array('_two'));
$mock->expects($this->any())
    ->method('_two')
    ->will($this->returnValue('Some Overridden Value');
硬不硬你别怂2024-12-08 01:10:15

您可以使用反射来更改方法的可见性。您可以在以下位置找到更多信息
PHP对象,如何引用?

You can use reflection for changing visibility of methods. You can find more info in
PHP object, how to reference?

送你一个梦2024-12-08 01:10:15

使用模拟和反射...(发布此解决方案,因为这是谷歌的最佳结果)

$oMock = $this->getMock("Your_class", array('methodToOverride'));
$oMock->expects( $this->any() )
    ->method('methodToOverride')
    ->will( $this->returnValue( true ) );

$oReflection = new ReflectionClass("Your_Class");

$oMethod = $oReflection->getMethod('privateMethodToInvoke');
$oMethod->setAccessible( true );
$oMethod->invoke( $oMock );

Use mock and reflection... (posted this solution, since this is the top google result)

$oMock = $this->getMock("Your_class", array('methodToOverride'));
$oMock->expects( $this->any() )
    ->method('methodToOverride')
    ->will( $this->returnValue( true ) );

$oReflection = new ReflectionClass("Your_Class");

$oMethod = $oReflection->getMethod('privateMethodToInvoke');
$oMethod->setAccessible( true );
$oMethod->invoke( $oMock );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文