
Originally Posted by
XtrEM3
when Database->fetch() is called, an instance of SQLQuery is created
You can stub that out by putting the creation of the dependency in a factory. The simplest solution is to use a factory method, and then extend the Database class for the test. For example:
PHP Code:
class Database
{
function fetch() {
$query = $this->createSQLQuery();
return $query->execute();
}
function createSQLQuery() {
return new SQLQuery();
}
}
class MockDatabase extends Database
{
function createSQLQuery() {
return new MockSQLQuery();
}
}
This allows you to test Database in isolation from SQLQuery. You can then proceed to test SQLQuery on its own.
Testing that the constructor throws, can be done this way:
PHP Code:
function test_sqlquery_constructor_throws() {
try {
new SQLQuery("some-bogus-data-that-makes-it-throw");
$this->fail("Expected SQLQueryException wasn't thrown");
} catch (SQLQueryException $ex) {
$this->pass("Expected SQLQueryException caught");
}
}
I'm not entirely sure how you Log class is wired up with the rest of your application, so I can't advise on that.
Bookmarks