I'm trying to test, among others, the constructor of my class. It expects exactly one parameter which has to be a string. So I wrote this test:
class categoryTest extends PHPUnit_Framework_TestCase {
public function testConstructor() {
$this->setExpectedException('Exception', 'Unknown data type.');
$objCategory = new category(1);
$this->setExpectedException('Exception', 'Unknown data type.');
$objCategory = new category(-500);
$this->setExpectedException('Exception', 'Unknown data type.');
$objCategory = new category(true);
$this->setExpectedException('Exception', 'Unknown data type.');
$objCategory = new category(array());
...
}
public function testNextMethod() {
}
}
As you can see, I expect each time the same exception.
This works very well, so it seems, but the script will skip to testNextMethod() after finishing
$this->setExpectedException('Exception', 'Unknown data type.');
$objCategory = new category(1);
. Do I have to write for each test an own testMethod()? Or is there any workaround?
Best regards, muff
EDIT:
Hello Cyprian,
thank you very much for your response. I solved my problem like this:
class categoryTest extends PHPUnit_Framework_TestCase {
protected $backupGlobals = FALSE;
/**
*
* @dataProvider provider
*
**/
public function testMuff($strCategory) {
$this->setExpectedException('Exception', 'Unknown data type.');
$objCategory = new category($strCategory);
}
public function provider() {
$objHIS = new DDDBL('HIS');
return array(array(1),
array(-500),
array(true),
array(array()),
array($objHIS)
);
}
...
}
Now it works perfectly, even if I don't like the notation at all.