I recently ran across some code in a public repo that I'd thought... just plain wrong. The first thing I noticed was
/**
* Sets a test mode status.
*
* @param boolean $mode Mode to set
*/
public function testMode(bool $mode = true)
{
$this->testMode = $mode;
return $this;
}
Then another method used the testMode
property like this (mostly pseudo-code to protect the innocent).
public someDbFunc($table)
{
$sql = makeQueryStatement($table);
if ($this->testMode)
{
return $sql;
}
// code that really does database stuff
//...
}
Doesn't this kind of thing make unit testing pointless? Which is worse, the testing code or the code being tested?
Edit
Adding an approximation of an actual test.
// 'reservations' is the name of a table
public function testSomeDbFuncMakesGoodSql)
{
$generator = new QueryBuilder('reservations', $this->db);
$generator->testMode();
$expectedSQL = 'SELECT COUNT(*) AS "numrows" FROM "reservations"';
$this->assertEquals($expectedSQL, $builder->someDbFunc('reservations));
}
That is pretty typical of how $this->testMode()
is used. The test returns the query statement instead of carrying out the db operation. There are a few uses where it (for lack of a better term) mocks the return. These are all either write or delete type operations.