模仿这样的PDO对象之后:
class AdProvidersTest extends PHPUnit_Framework_TestCase
{
public function dataProvider()
{
$providers = array (
array (1, '1st', 'desc_1', 101),
array (2, '2nd', 'desc_2', 202),
array (3, '3rd', 'desc_3', 303)
);
return $providers;
}
/**
* @dataProvider dataProvider
*/
public function testAdProviders_getConnection($id, $name, $desc, $account_id)
{
$data = array (
array (
'id' => $id,
'name' => $name,
'desc' => $desc,
'account_id' => $account_id,
)
);
$stmt = $this->getMock('PDOStatement', array ('execute','fetchAll'));
$stmt->expects($this->any())
->method('execute')
->will($this->returnValue(true));
$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));
}
}
我想使用此功能测试连接:
function getDbh()
{
if ($this->db === null){
$this->db = Slim::getInstance()->db;
}
return $this->db->getConnection();
}
但在使用pdo mock设置数据库后,我在尝试获取连接时遇到此错误:
PHP Fatal error: Call to undefined method AdProvidersTest::getMockConnection() in /home/al/adserver/adserver/test/classes/AdProvidersTest.php on line 48
有没有办法将此getConnection
函数添加到模拟的PDO中?
答案 0 :(得分:0)
查看方法getMock()必须调用getMockConnection()。