【发布时间】:2017-09-06 16:10:56
【问题描述】:
我有一个类(称为FormFilters),该类在一个方法中调用其方法,在本例中为getProject。
class FormFilters extends KernelTestCase
{
public function getProject($filters)
{
$this->filters = $filters;
$this->getWhere($this->filters);
}
public function getWhere()
{
if ($this->filters->isValid()) {
$this->sql = $this->filterName($this->filters->get('name')->getData());
}
}
public function filterName()
{
//....
}
}
这是getProject方法测试:
public function test_getProject()
{
$formInterface = $this->createMock('Symfony\Component\Form\FormInterface');
$formInterface
->expects($this->at(0))
->method('isValid')
->willReturn(true); // come into conditional
$formInterface
->expects($this->at(1))
->method('get')
->with('name')
->will($this->returnSelf());
$formInterface
->expects($this->at(2))
->method('getData')
->will('data example');
$formFilters = new FormFilters();
$formFilters->getProject($formInterface); // my mock
}
到目前为止一切顺利。现在,我想测试getWhere方法,我可以独立完成,但是如果getProject有相同的测试(调用getWhere方法),我可以使用注释@dataProvider或@depends,像这样(示例):
/**
* @depends or/and @dataProvider test_getProject
*/
public function test_getWhere($dataToDepends)
{
// ... test ready !
}
有可能吗?
【问题讨论】:
标签: php unit-testing mocking phpunit