私は小さなクラスの単体テストに取り組んでいます。私はこのクラスを使用して PHPUnit に取り掛かり、将来私が作成するより大きなコードを適切にテストできるようにしています。
テストしようとしている次のコードを検討してください。
/**
* 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;
}
以下は実際のテストコードです。
public function testBadStartTick()
{
$this->assertFalse($this->bm->start("What_Invalid_Key_Fits_Here?"))
}
問題は、このテスト関数は、true
何度返そうとしても常に返されることfalse
です。null 値、300 文字以上のキー、空の配列、さらには新しいオブジェクトのインスタンスを指定しようとしました。いずれの場合も、PHP は中断するか、なんらかの警告をスローします。PHP が壊れない場合、私の値は、PHP が配列キーで受け入れる何かに変換され、$this->assertFalse()
..
可能な限り多くのコード カバレッジを実現したいと考えています。
私の質問は、現在のコードを考えると、これらのメソッドがfalse
通常の操作で戻るかどうかです。
私はテキストを追加しているので(これは管理目的です)、私が何を与えるかに関係なく、PHPが受け入れるある種のキーを常に提供していると考えています$id
。
何かご意見は?
前もって感謝します!