Running into issues returning a Mock from a Mock

30 views
Skip to first unread message

Kasi Prasad

unread,
Nov 11, 2013, 4:59:15 PM11/11/13
to phake...@googlegroups.com
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?
Reply all
Reply to author
Forward
0 new messages