PHPUnit数据提供程序动态创建
关于PHPUnit数据提供者,我有一个非常有趣的问题PHPUnit数据提供程序动态创建,php,phpunit,automated-tests,Php,Phpunit,Automated Tests,关于PHPUnit数据提供者,我有一个非常有趣的问题 protected $controller; protected function setUp() { $this->controller = new ProductController(); } /** * @covers ProductsController::createItem * @dataProvider getTestDataProvider * @param number $name
protected $controller;
protected function setUp()
{
$this->controller = new ProductController();
}
/**
* @covers ProductsController::createItem
* @dataProvider getTestDataProvider
* @param number $name
*/
public function testCreateItem($name)
{
$prod = $this->controller->createItem($name);
$id = $prod->getId;
$this->assertInternalType('int', $id);
$this->assertInstanceOf('Product', $prod);
}
/**
* @covers ProductsController::getItemInfo
* @depends testCreateItem
* @param number $id
*/
public function testGetItemInfo($id)
{
$info = $this->controller->getItemInfo($id);
$this->assertArrayHasKey('id',$info);
$this->assertEquals($id, $info['id']);
}
我使用getTestDataProvider
从CSV文件中获取测试数据。然后testCreateItem
从CSV行创建10个新产品
如何创建新产品的$id
数组,并将其用作testGetItemInfo
的数据提供程序?我无法将其存储在会话或文件中,因为提供程序函数在安装之前运行
也许有人已经遇到了类似的问题?我只知道静态场(可能不是最好的,但如果有人有更好的,我会看看) 您必须记住,该字段在所有类中都是可见的,所以如果您想使用另一个数据集,则必须将该字段置零
private static $ids;
/**
* @dataProvider some
*/
public function testT1($id)
{
self::$ids[] = $id;
}
/**
* @depends testT1
*/
public function testT2()
{
var_dump(self::$ids);
}
public function some()
{
return [
[1],
[2],
[3]
];
}