我一直在关注创建pdo模拟对象的教程。我使用var_dump
来理解这个pdo模拟包含什么,我发现结构非常混乱。我想要做的是查询一个名为'providers'的表,它应该包含dataProvider
方法中的数据。
这是pdo创作:
class AdProvidersTest extends PHPUnit_Framework_TestCase
{
public function dataProvider()
{
return array (
array (1, '1st', 'desc_1', 11),
array (2, '2nd', 'desc_2', 22),
array (3, '3rd', 'desc_3', 33),
);
}
/**
* @dataProvider dataProvider
*/
public function testAdProviders($id, $name, $desc, $account_id)
{
$data = array (
array (
'id' => $id,
'name' => $name,
'description' => $desc,
'account_id' => $account_id
)
);
$stmt = $this->getMock('PDOStatement', array ('fetchAll'));
$stmt->expects($this->any())
->method('fetchAll')
->will($this->returnValue($data));
$pdo = $this->getMock('PDO', array('prepare'),
array('sqlite:dbname=:memory'),'PDOMock_' . uniqid(),true);
$pdo->expects($this->any())
->method('prepare')
->will($this->returnValue($stmt));
var_dump($pdo);
}
}
如何在模拟对象中创建一个名为“providers”的表,所以当我像这样查询SELECT * FROM providers
时,我会从dataProvider获取这三行?
我真正想要测试的功能:
function getProviders() {
if ($this->providers == null){
$DBH = $this->getDbh();
$query = "select * from providers";
$STH = $DBH->query($query);
$STH->setFetchMode(PDO::FETCH_ASSOC);
while($provider = $STH->fetch()) {
$this->providers[$provider['id']] = $provider;
}
}
return $this->providers;
}