PHPUnit expectedExceptionアノテーションのように、アノテーションではなくて、 setExpectedException()メソッドを使うこともできます。
PHPUnit expectedExceptionアノテーションのRGBTest.phpは、次のように書き換えても同じように動作します。
<?php
require_once 'PHPUnit/Framework.php';
require_once 'RGB.php';
class RGBTest extends PHPUnit_Framework_TestCase {
public static function myDataProvider() {
return Array(
Array(-1,0,0),
Array(256,255,255)
);
}
/**
* @dataProvider myDataProvider
*/
public function testConstructorError($red, $green, $blue) {
$this->setExpectedException('InvalidArgumentException');
new RGB($red, $green, $blue);
}
}
?>
変更された部分は、@expectedExceptionアノテーションが無くなった代わりに、 setExpectedException()メソッドを使用して、InvalidArgumentExceptionが発生することを宣言している点です。
class PHPUnit_Framework_TestCase {
/**
* 例外の発生を期待することを設定する。
* @param $exceptionName
* 例外の名前
*/
void setExpectedException(String $exceptionName);
/**
* 設定されている期待される例外を文字列で受け取る。
*/
String getExpectedException();
}