I am getting a strange behavior with PHPUnit and I cannot figure if I'm doing something wrong or if it is a bug from the test framework. Here is my case:
My project has several classes: BsInput, BsEmail, BsHidden, BsNumber. BsEmail, BsHidden and BsNumber extend BsInput. All classes have unit test classes (BsInputTest for BsInput, BsEmailTest for BsEmail, etc.).
Each unit test executed individually goes fine.
Now if I try to run all tests in my project, i get an error and message "PHP Fatal error: cannot redeclare class B in /some/path/to/B.php on line 80".
Here is the content of each file:
BsInput.php
require_once __DIR__ . "/BsInputControl.php";
class BsInput extends BsInputControl {
...
}
BsEmail.php
require_once __DIR__ . "/BsInput.php";
class BsEmail extends BsInput {
...
}
BsHidden.php
require_once __DIR__ . "/BsInput.php";
class BsHidden extends BsInput {
...
}
BsNumber.php
require_once __DIR__ . "/BsInput.php";
class BsNumber extends BsInput {
...
}
BsInputTest.php
require_once "../colibri/bs/BsInput.php";
class BsInputTest extends PHPUnit_Framework_TestCase {
...
}
BsEmailTest.php
require_once "../colibri/bs/BsEmail.php";
class BsEmailTest extends PHPUnit_Framework_TestCase {
...
}
BsHiddenTest.php
require_once "../colibri/bs/BsHidden.php";
class BsHiddenTest extends PHPUnit_Framework_TestCase {
...
}
BsNumberTest.php
require_once "../colibri/bs/BsNumber.php";
class BsNumberTest extends PHPUnit_Framework_TestCase {
...
}
Now the last little thing that drives me insane: if I comment out all code in BsHidden.php and BsHiddenTest.php, PHPUnit All Tests execution goes smooth over BsNumber and following classes !
Has anyone already seen something similar ? Any clues what I should look at to get my problem solved ?
I have tried an ugly workaround: inserting the following code in BsInput.php
var_dump(class_exists('BsInput', FALSE));
if (class_exists('BsInput', FALSE)) { return; }
And I get this result:
bool(true)
PHP Fatal error: Cannot redeclare class BsInput in /.../BsInput.php on line 87
I have looked at many issues up to now including the ones above, but up to now I couldn't find any solution.
- PHPUnit loads all classes at once. Causes PHP Fatal error: Cannot redeclare class
- PHPUnit Test Suite - Cannot redeclare class Mocking & Concrete classes
- "Fatal error: Cannot redeclare class" ... but the class was not declared
I have also tried updating PHPUnit to the latest version (4.6), with no more success.
My platform is: Mac OS X 10.10 PHP 5.5.20 PHPUnit 4.6.6 Netbeans 8.0.2 (if this has anything related)
Any clue anyone ?
After trying to solve this i finally deleted my BsHidden.php
file, the attached test file and recreated them exactly (I mean with exact same content I copy/pasted)... And now it works ! This really makes me think of a bug somewhere between PHP and PHPUnit.
However a quick & dirty workaround seems to be:
- copy-paste file and unit test file contents to another file
- delete problematic file and unit test file if any)
- re-create file and unit test file and paste contents