测试抽象类中的私有方法扩展了另一个方法

发布于 2024-12-10 13:11:48 字数 1732 浏览 0 评论 0原文

我正在尝试在抽象类中测试私有方法。

我有三个抽象类:

abstract class AbstractClass1 extends AbstractClass2
{
   private function _privateFunction()
    {
        //method's body
    }
}

abstract class AbstractClass2 extends AbstractClass3
{
    public function __construct($param)
    {
        parent::__construct($param)
    }
}

abstract class AbstractClass3
{
    public function __construct($param = array()) 
    {
        //something
    }
}

测试类:

class AbstractClass1Test extends PHPUnit_Framework_TestCase
{
    public function test_privateFunction()
    {
        $stub = $this->getMockForAbstractClass("AbstractClass1");
        $class = new ReflectionClass($stub);
        $method = $class->getMethod("_privateFunction");
        $method->setAccessible(true);

        //some assertings with $method->invoke($stub)
    }
}

测试失败,因为错误:

AbstractClass2::__construct() 缺少参数 1,在 /usr/share/php/PHPUnit/Framework/MockObject/Generator.php 第 190 行调用并定义

AbstractClass2.php

public function __construct($param)

AbstractClass1.php

$classMock = $this->getMockForAbstractClass("AbstractClass1");

Generator.php:190

if ($callOriginalConstructor &&
    !interface_exists($originalClassName, $callAutoload)) {
    if (count($arguments) == 0) {
        <strong>$mockObject = new $mock['mockClassName'];</strong>
    } else {
        $mockClass  = new ReflectionClass($mock['mockClassName']);
        $mockObject = $mockClass->newInstanceArgs($arguments);
    }
} else ...

我错了什么?或者在这种情况下如何测试我的私有功能?

I'm trying to test a private method in an abstract class.

I've got three abstract classes:

abstract class AbstractClass1 extends AbstractClass2
{
   private function _privateFunction()
    {
        //method's body
    }
}

abstract class AbstractClass2 extends AbstractClass3
{
    public function __construct($param)
    {
        parent::__construct($param)
    }
}

abstract class AbstractClass3
{
    public function __construct($param = array()) 
    {
        //something
    }
}

The test class:

class AbstractClass1Test extends PHPUnit_Framework_TestCase
{
    public function test_privateFunction()
    {
        $stub = $this->getMockForAbstractClass("AbstractClass1");
        $class = new ReflectionClass($stub);
        $method = $class->getMethod("_privateFunction");
        $method->setAccessible(true);

        //some assertings with $method->invoke($stub)
    }
}

The test failed, because of the error:

Missing argument 1 for AbstractClass2::__construct(), called in /usr/share/php/PHPUnit/Framework/MockObject/Generator.php on line 190 and defined

AbstractClass2.php

public function __construct($param)

AbstractClass1.php

$classMock = $this->getMockForAbstractClass("AbstractClass1");

Generator.php:190

if ($callOriginalConstructor &&
    !interface_exists($originalClassName, $callAutoload)) {
    if (count($arguments) == 0) {
        <strong>$mockObject = new $mock['mockClassName'];</strong>
    } else {
        $mockClass  = new ReflectionClass($mock['mockClassName']);
        $mockObject = $mockClass->newInstanceArgs($arguments);
    }
} else ...

What do I wrong? Or how can I test my private function in this situation?

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

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

发布评论

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

评论(2

隔岸观火 2024-12-17 13:11:48

您需要将参数传递给 AbstractClass1 的构造函数。将数组中的构造函数参数作为第二个参数传递给 getMockForAbstractClass()

$stub = $this->getMockForAbstractClass("AbstractClass1", array('param'));

You need to pass an argument to AbstractClass1's constructor. Pass constructor arguments in an array as the second argument to getMockForAbstractClass().

$stub = $this->getMockForAbstractClass("AbstractClass1", array('param'));
浸婚纱 2024-12-17 13:11:48

当您覆盖原始构造函数时,

public function __construct($param = array())  //Allow null $param as it would default to array();

使用新的构造函数:

public function __construct($param) //Does not allow null $param.

在初始化对象时,您将需要定义 $param 。这可能是你的问题。

PHP 中的对象与 JavaScript 不同,它们不能像关联数组那样被调用。您的对象初始化应如下所示:

$mockObject = new ClassExtendingAbstractClass1Or2('parameter');

new 关键字不能用在变量前面。

Seeing as you overrode the original constructor,

public function __construct($param = array())  //Allow null $param as it would default to array();

With a new one:

public function __construct($param) //Does not allow null $param.

You will require to define the $param when you initialize the object. That's probably your problem.

Objects in PHP are not like JavaScript, they cannot be called like associative arrays. Your object initialization should look like:

$mockObject = new ClassExtendingAbstractClass1Or2('parameter');

The new keyword cannot be used in front of a variable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文