简体   繁体   English

无法访问父类中的子属性(从子对象中的父类继承的调用方法)

[英]Cannot access child property in parent class (calling method inherited from parent in child object)

So this is my parent class for unit testing (short version): 这是我用于单元测试的父类(简短版本):

<?php
class TestCaseAbstract extends PHPUnit_Framework_TestCase
{
    protected $_rawPostData;

    public function setUp()
    {
        // ...
    }

    /**
     *
     * @dataProvider provider 
     */
    public function testFoo($rawData)
    {
        // ...
    }

    public function provider()
    {
        return array(
            array(''),
            array($this->_rawData),
        );
    }

    public function tearDown()
    {
        // ...
    }
}

This is my child class, an actual unit test case: 这是我的孩子班,一个实际的单元测试用例:

class FooTestCase extends TestCaseAbstract
{
    public function setUp()
    {
        $this->_rawPostData = '<?xml version="1.0"?><request><bogus /></request>';

        parent::setUp();
    }
}

Now when I run unit test case: 现在,当我运行单元测试用例时:

.phpunit --debug FooTestCase.php

I get this: 我得到这个:

.
Starting test 'FooTestCase::testFoo with data set #0 ('')'.
.
Starting test 'FooTestCase::testFoo with data set #1 (NULL)'.
.

As you can see, the second unit test with data $this->_rawData says it ran with NULL data. 如您所见,使用数据$ this-> _ rawData的第二个单元测试说它运行的是NULL数据。 What is wrong with my code? 我的代码有什么问题? It seems the test method is not able to access protected property $this->_rawData. 似乎测试方法无法访问受保护的属性$ this-> _ rawData。


I hope my inheritance model is not messed up. 我希望我的继承模型不会搞砸。 I have done a quick test just to make sure that the inheritance in PHP works as I think it works: 我已经做了一个快速测试,以确保PHP中的继承按我认为的方式起作用:

<?php

class ParentClass
{
    protected $_property;

    public function getProperty()
    {
        return $this->_property;
    }
}

class ChildClass extends ParentClass
{
    public function __construct()
    {
        $this->_property = 'Hello';
    }
}

$childClass = new ChildClass();
var_dump($childClass->getProperty());

This works and outputs "Hello" as it should. 这样可以正常工作并输出“ Hello”。 Any ideas why in my unit tests the data provider returns NULL? 有什么想法为什么在我的单元测试中数据提供者返回NULL?

This is caused by the way PHPUnit handles data provider methods. 这是由PHPUnit处理数据提供程序方法的方式引起的。 Each provider method is executed once with a new test case instance before any tests start. 在开始任何测试之前,每个提供者方法都使用一个新的测试用例实例执行一次。 This is to allow it to get an accurate test count. 这是为了使其获得准确的测试计数。

However, setUp() is not called before the data provider method. 但是,在数据提供者方法之前不会调用setUp() You can get around this by adding a method to return the desired data that the provider method can call. 您可以通过添加一种方法来返回此问题,以返回提供者方法可以调用的所需数据。 Define this new method as abstract in the base class and override it in the child. 在基类中将此新方法定义为抽象,并在子级中将其覆盖。 Also, don't forget to make your base class abstract. 另外,不要忘记使您的基类抽象化。

abstract class TestCaseAbstract extends PHPUnit_Framework_TestCase
{
    /**
     * @dataProvider provider 
     */
    public function testFoo($rawPostData)
    {
        // ...
    }

    public function provider()
    {
        return array(
            array(''),
            array($this->_getRawPostData()),
        );
    }

    protected abstract function _getRawPostData();
}

class FooTestCase extends TestCaseAbstract
{
    protected function _getRawPostData()
    {
        return '<?xml version="1.0"?><request><bogus /></request>';
    }
}

Update: I wrote a small test case to verify that PHPUnit doesn't call setUp() before data provider methods. 更新:我编写了一个小测试用例,以验证PHPUnit在数据提供者方法之前没有调用setUp()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM