When I run phpunit, my fixtures are null. I initialize the fixtures in setUp(), but my debugging has shown it is never called. I tried having the member variables be assigned the proper values in the child class, but they remain null, which boggles my mind.
// Unit/Base.Unit.php
Class BaseUnit extends PHPUnit_Framework_TestCase {
// Tests for Base Object methods
}
// Unit/Validation/Validation.Unit.php
Class ValidationUnit extends BaseUnit {
protected $validationClass;
protected $validData;
protected $invalidData;
protected function newValidationInstance($data) {
return new $this->validationClass($data);
}
public function testValidData() {
$instance = $this->newValidationInstance($this->validData);
$this->assertEquals('true', $instance->isValid());
}
// ... other tests for validation ...
}
// Unit/Validation/Email.Test.php
Class EmailTest extends ValidationUnit {
protected function setUp() {
$this->class = "Email";
$this->validData = "test@gmail.com";
$this->invalidData = "fdksljfa";
}
}
Running PHPUnit...
phpunit --bootstrap /some/config.php Unit/
PHPUnit 4.2.2 by Sebastian Bergmann.
.E
Fatal error: Class name must be a valid object or a string in /opt/bitnami/some/path/Unit/Validation/Validation.Unit.php on line 9
What I'm expecting to happen is for phpunit to start tests on EmailTest, and run the tests both in it and it's parent classes. The Validation class has a ton of subclasses (Email, ZipCode, etc) so having all the testing in Validation is a huge win.
But I can't call setUp(), so I can't instantiate the proper Validation classes.
Edit
So it turns out that setUp() is called before any tests in the class that setUp() is defined in but not before other tests, including tests in parent classes.
So in order to control the order of tests, I'm having each class define an invokeTests() function containing it's tests, and then calling it's parent's invokeTests() method. That way, tests will be executed with respect to OO hierarchy.