Reputation: 4089
I've been writing a test for a simple Datamapper class, and I know the method is working, however the test fails and gives me the error "Fatal error: Call to a member function fetchAll() on a non-object in C:\xampp\htdocs\Call log\tests\model_tests.php on line 13.
" Obviously, this can't be right, because I can verify that the method works.
Here is the code that it's supposedly erroring on:
function all() {
$calls = $this->pdo->query('SELECT * from calls');
return $calls->fetchAll();
}
Here's my test code:
class TestOfCallMapper extends UnitTestCase {
function testOfReturnsAll() {
$this->createSchema();
$mapper = Callmapper::getInstance();
$results = $mapper->all();
print_r($results);
}
private function createSchema() {
$mapper = CallMapper::getInstance();
$mapper->pdo->exec(file_get_contents('../database/create_schema.sql'));
}
private function destroySchema() {
$mapper = CallMapper::getInstance();
$mapper->pdo->exec(file_get_contents('../database/destroy_schema.sql'));
}
}
$test = new TestOfCallMapper('Test of CallMapper Methods');
$test->run(new HTMLReporter());
If I do this, it works just fine:
$mapper = CallMapper::getInstance();
$test = $mapper->all();
print_r($test->fetchAll());
Upvotes: 0
Views: 231
Reputation: 57268
Your PDO Query is returning false therefore its not and instance of PDO but a boolean, Try something like this!
function all() {
$calls = $this->pdo->query('SELECT * from calls');
if($calls === false)
{
throw new Exception("Unable to perform query");
}
return $calls->fetchAll();
}
and then within your TestOfCallMapper
you can do:
function testOfReturnsAll()
{
$this->createSchema();
$mapper = Callmapper::getInstance();
try
{
$results = $mapper->all();
}catch(Exception $e)
{
//Some Logging for $e->getMessage();
return;
}
//Use $results here
}
Upvotes: 1
Reputation: 630
The pdo query is evidently failing, thus you are trying to call fetchAll on false.
I'd check why it's failing.
Upvotes: 0