I am trying out the Phake framework and running into a problem with what I would expect to be a pretty common scenario. In this case, I want to Mock an instance of class called Db which has a method named "query". In this test, when the "query" method is called, I want it to return a mock instance of PDO. When the mock PDO instance's "fetch" method is called, I want to return some static data.
Please see the below example:
public function testGetData() {
$pdo = Phake::mock('PDO');
Phake::when($pdo)->fetch(Phake::anyParameters())->thenReturn(array("foo" => "bar"));
$db = Phake::mock('Db');
Phake::when($db)->query(Phake::anyParameters())->thenReturn($pdo);
$blog = new Blog(123, $db);
$result = $blog->getData();
}
class Blog {
public $id;
protected $db;
public function __construct($id, $db) {
$this->id = $id;
$this->db = $db;
}
public function getData() {
$query = $this->db->query('SELECT * FROM entries WHERE id = ' . (int) $this->id);
return $query->fetch();
}
}
Error message is:
PHP Fatal error: Call to undefined method PDO_PHAKE5281514c26e2f::fetch()
What's puzzling me about this is that I explicitly defined what I want to happen when the PDO instance's "fetch" method is called but it is reporting that there is no "fetch" method defined.
What am I doing wrong?