【发布时间】:2012-07-13 00:37:28
【问题描述】:
所以这是我用于单元测试的父类(短版):
<?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()
{
// ...
}
}
这是我的子类,一个实际的单元测试用例:
class FooTestCase extends TestCaseAbstract
{
public function setUp()
{
$this->_rawPostData = '<?xml version="1.0"?><request><bogus /></request>';
parent::setUp();
}
}
现在当我运行单元测试用例时:
.phpunit --debug FooTestCase.php
我明白了:
.
Starting test 'FooTestCase::testFoo with data set #0 ('')'.
.
Starting test 'FooTestCase::testFoo with data set #1 (NULL)'.
.
如您所见,使用数据 $this->_rawData 的第二个单元测试表明它使用 NULL 数据运行。我的代码有什么问题?似乎测试方法无法访问受保护的属性 $this->_rawData。
我希望我的继承模型没有搞砸。我做了一个快速测试,以确保 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());
这可以正常工作并输出“Hello”。任何想法为什么在我的单元测试中数据提供者返回 NULL?
【问题讨论】:
标签: php zend-framework phpunit