Note that there are some explanatory texts on larger screens.

plurals
  1. POCannot access child property in parent class (calling method inherited from parent in child object)
    primarykey
    data
    text
    <p>So this is my parent class for unit testing (short version):</p> <pre><code>&lt;?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-&gt;_rawData), ); } public function tearDown() { // ... } } </code></pre> <p>This is my child class, an actual unit test case:</p> <pre><code>class FooTestCase extends TestCaseAbstract { public function setUp() { $this-&gt;_rawPostData = '&lt;?xml version="1.0"?&gt;&lt;request&gt;&lt;bogus /&gt;&lt;/request&gt;'; parent::setUp(); } } </code></pre> <p>Now when I run unit test case:</p> <pre><code>.phpunit --debug FooTestCase.php </code></pre> <p>I get this:</p> <pre><code>. Starting test 'FooTestCase::testFoo with data set #0 ('')'. . Starting test 'FooTestCase::testFoo with data set #1 (NULL)'. . </code></pre> <p>As you can see, the second unit test with data $this->_rawData says it ran with NULL data. What is wrong with my code? It seems the test method is not able to access protected property $this->_rawData.</p> <hr> <p>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:</p> <pre><code>&lt;?php class ParentClass { protected $_property; public function getProperty() { return $this-&gt;_property; } } class ChildClass extends ParentClass { public function __construct() { $this-&gt;_property = 'Hello'; } } $childClass = new ChildClass(); var_dump($childClass-&gt;getProperty()); </code></pre> <p>This works and outputs "Hello" as it should. Any ideas why in my unit tests the data provider returns NULL?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload