I'm working on a unit test for a small class. I'm using this class to get down and dirty with PHPUnit so I can start properly test bigger pieces of code that I write in the future.
Consider the following code I am trying to test:
/**
* Registers a starting benchmark tick.
*
* Registers a tick with the ticks registry representing the start of a benchmark timeframe.
*
* @param string $id The identifier to assign to the starting tick. Ending tick must be the same.
* @return bool Returns TRUE if a tick was registered successfully or FALSE if it was not.
* @since 0.1
*/
public function start($id)
{
$this->tick($id . "_start");
if($this->getStart($id) != false) {
return true;
}
return false;
}
/**
* Retrieves a registered start tick.
*
* Checks to see if a start tick is registered. If found the microtime value (as a float) is
* returned, otherwise FALSE is returned.
*
* @param string $id The identifier to lookup the tick under.
* @return mixed The microtime (as a float) assigned to the specified tick or FALSE if the tick
* start hasn't been registered.
* @since 0.1
*/
public function getStart($id)
{
if(isset($this->ticks[$id . "_start"])) {
return $this->ticks[$id . "_start"];
}
return false;
}
And the following is the actual test code:
public function testBadStartTick()
{
$this->assertFalse($this->bm->start("What_Invalid_Key_Fits_Here?"))
}
The problem is that this test function always returns true
no matter how many times I try to make it return false
. I have tried giving null values, keys of 300+ characters, empty arrays, even the instance of a new object. In all cases PHP either breaks or throws some kind of warning. When PHP doesn't break, my value gets converted to something that PHP will accept in the array key and then my test doesn't pass when attempting to do a $this->assertFalse()
.
I would like to achieve the highest amount of code coverage possible.
So my question is if these methods, given their current code, will ever return false
under normal operation?
I'm thinking that because I am appending text (which is for management purposes) I am always supplying some kind of key that PHP will accept regardless of what I give for $id
.
Any thoughts?
Thanks in advance!