Search code examples
phpsimpletest

PHP simpletest tomfoolery. Test doesn't pass on working method


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());

Solution

  • The pdo query is evidently failing, thus you are trying to call fetchAll on false.

    I'd check why it's failing.