Phpunit Fatal Error Cannot Redeclare Class
Contents |
here for a quick overview of the site phpunit cannot redeclare function Help Center Detailed answers to any questions you might
Phpunit Process Isolation
have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Cannot redeclare class error when generating PHPUnit code coverage report up vote 16 down vote favorite 2 Starting a project with Zend Framework 1.10 and Doctrine 2 (Beta1). I am using namespaces in my own library code. When generating code coverage reports I get a Fatal Error about Redeclaring a class. To provide more info, I've commented out the xdebug_disable() call in my phpunit executable so you can see the function trace (disabled local variables output because there was too much output). Here's my Terminal output: $ phpunit PHPUnit 3.4.12 by Sebastian Bergmann. ........ Time: 4 seconds, Memory: 16.50Mb OK (8 tests, 14 assertions) Generating code coverage report, this may take a moment.PHP Fatal error: Cannot redeclare class Cob\Application\Resource\HelperBroker in /Users/Cobby/Sites/project/trunk/code/library/Cob/Application/Resource/HelperBroker.php on line 93 PHP Stack trace: PHP 1. {main}() /usr/local/zend/bin/phpunit:0 PHP 2. PHPUnit_TextUI_Command::main() /usr/local/zend/bin/phpunit:54 PHP 3. PHPUnit_TextUI_Command->run() /usr/local/zend/share/pear/PHPUnit/TextUI/Command.php:146 PHP 4. PHPUnit_TextUI_TestRunner->doRun() /usr/local/zend/share/pear/PHPUnit/TextUI/Command.php:213 PHP 5. PHPUnit_Util_Report::render() /usr/local/zend/share/pear/PHPUnit/TextUI/TestRunner.php:478 PHP 6. PHPUnit_Framework_TestResult->getCodeCoverage
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of http://stackoverflow.com/questions/2816173/cannot-redeclare-class-error-when-generating-phpunit-code-coverage-report 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up PHPUnit - Cannot redeclare class when processIsolation set to false up vote 2 down vote favorite 1 A similar question has been asked several times here PHPUnit loads all classes at once. Causes PHP Fatal error: http://stackoverflow.com/questions/30049347/phpunit-cannot-redeclare-class-when-processisolation-set-to-false Cannot redeclare class Cannot redeclare class error when generating PHPUnit code coverage report PHPUnit triggers a new fatal error Fatal error: Cannot redeclare class Validator in /some/path/to/Validator.php on line 6 The ValidatorTest class class ValidatorTest extends PHPUnit_Framework_TestCase { /** * @dataProvider data_provider_rules */ public function test_factory($rules) { define('DIR_SEP', DIRECTORY_SEPARATOR); define('SYS_DIR', '.'.DIR_SEP.'..'.DIR_SEP.'classes'); //relative path here require SYS_DIR.DIR_SEP.'Validator.php'; $validator = Validator::factory(); return $validator; } } And the Validator class class Validator { private static $validator = FALSE; public function __construct() { } public static function factory() { if (empty(self::$validator)) { self::$validator = new Validator(); } return self::$validator; } } The classes are trivial, there is no autoloading or include/require whatsoever. I am running the tests from seperate tests dir that contains only the ValidatorTest.php and configuration file phpunit.xml (copied from here). The tests run fine when processIsolation="true" The tests also work when using require_once instead of require. So my question is which (if any) and why: do I have to explicitly the processIsola
Sign in https://github.com/brianium/paratest/issues/36 Pricing Blog Support Search GitHub This http://jeremyharris.me/phpunit-solution-php-fatal-error-cannot-redeclare-class-foo/ repository Watch 28 Star 399 Fork 97 brianium/paratest Code Issues 25 Pull requests 5 Projects 0 Pulse Graphs New issue PHP Fatal error: Cannot redeclare class when trying to test cannot redeclare a Symfony2 application. #36 Closed tarjei opened this Issue Apr 12, 2013 · 6 comments Projects None yet Labels None yet Milestone No milestone Assignees No one assigned 2 participants tarjei commented Apr 12, phpunit fatal error 2013 Hi, I get this error when I try to run paratest on a largish Symfony2 application. I've tried various combinations of paratest options but they all yield the same error. Any ideas why this happens? bin/phpunit --version PHPUnit 3.7.15 by Sebastian Bergmann. bin/paratest --version ParaTest version 0.4.4 Symfony version is 2.2.1. PHP Fatal error: Cannot redeclare class Fpg\BackgroundBundle\Tests\JobServerControlTest in /home/tarjei/htdocs/frisvold/fistup/src/Fpg/BackgroundBundle/Tests/JobServerHandlerTest.php on line 70 PHP Stack trace: PHP 1. {main}() /home/tarjei/htdocs/frisvold/fistup/vendor/brianium/paratest/bin/paratest:0 PHP 2. Symfony\Component\Console\Application->run() /home/tarjei/htdocs/frisvold/fistup/vendor/brianium/paratest/bin/paratest:35 PHP 3. ParaTest\Console\ParaTestApplication->doRun() /home/tarjei/htdocs/frisvold/fistup/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php:106 PHP 4. Symfony\Component\Console\Application->doRun() /home/tarjei/htdocs/frisvold/fistup/vendor/brianium/paratest/src/ParaTest/Console/ParaTestApplication.php:27 PHP 5. Symfony\Component\Console\Command\Command->run() /home/tarjei/htdocs/frisvold/fistup/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php:193 PHP 6. ParaTest\Console\Commands\ParaTestCommand->execute() /home/tarjei/htdocs/frisvold/fistup/vendor/symfony/symfony/src/Symfony/Component/Console/Command/Command.php:240 PHP 7. ParaTest\Console\Testers\PHPUnit->execute() /home/tarjei/htdocs/frisvold/fistup/vendor/brianium/paratest/src/ParaTest/Console/Commands/ParaTestCommand.php:30 PHP 8. ParaTest\Runners\PHPUnit\Runner->run() /home/tarjei/htdocs/frisvold/fistup/vendor/brianium/paratest/src/ParaTest/Console/Testers/PHPUnit.php:32 PHP 9. ParaTest\Runners\PHPUnit\Runner->load() /home/tarjei/htdocs/frisvold/fistup/vendor/brianium/paratest/src/ParaTest/Runners/PHPUnit/Runner.php:27 PHP 10. ParaTest\Runners\PHPUnit\SuiteLoader->load() /home/tarjei/htdocs/frisvold/fistup/vendor/brianium/paratest/src/ParaTest/Runners/PHPUnit/Runner.php:64 PHP 11. ParaTes
and ran into an error that gave me a lot of headaches. PHP Fatal error: Cannot redeclare class PHPUnit_Util_Filesystem in /usr/share/php/PHPUnit/Util/Filesystem.php on line 59 1 PHP Fatal error:Cannot redeclare class PHPUnit_Util_Filesystem in /usr/share/php/PHPUnit/Util/Filesystem.php on line 59 This appears to be a common problem in many variations of classes and so I decided to offer the solution I came up with. I had to trace backwards to see where the culprit is and it turns out the file usr/share/pear/PHPUnit/Autoload.php (in PHPUnit 3.6.10) is the little demon giving me hell. What that file does is autoload every possible class PHPUnit will need. It places them all in a massive array and loops through including them. That sounds all fine and gravy until the part where it actually includes the class. For me, it is right at line 203 and looks like this: require $file; 1 require $file; If this doesn't strike you immediately as something wrong, let me explain. When you do require you are forcing it to include the class whether it has been initialized already or not. It's sister function, require_once, will see if it has already been initialized and only if it hasn't does it include it. *SIGH* I'm sure Sebastian Bergmann had a good reason for coding it this way, but frankly I want to be able to UnitTest so I had to fix this. I made this change: require_once $file; 1 require_once $file; My next try at running phpunit in the command line went beautifully! This hack isn't guaranteed. Do so at your own risk and only if you feel you have enough code foo ninja powers 😉 This entry was posted in Development, PHP and tagged phpunit, ubuntu. Bookmark the permalink. Post navigation ← How to Create a Cross Domain Javascript Widget With jQueryRaspberry Pi: A Leap Year Storm is Brewing → 2 thoughts on “PHPUnit Solution to PHP fatal error: Cannot redeclare class Foo” Jonathan says: September 19, 2014 a