PHPUnit loads all classes at once. PHP Causes Fatal Error: Cannot Override Class

I have done my due diligence, but I do not think that any issues have so far addressed this issue.

I have a situation where my PHP code generates class definitions based on configuration properties.

Class definitions are mainly data holders and can have:

  • public properties
  • or protected properties with a public interface that supplies getters / setters.

In some configuration cases, the generated cluster names will be the same, but their FILE NAME will be VARIOUS. In a real environment, the user simply generates one set of class definitions based on the required configuration and uses the classes in his application. There is no problem there, even if the user creates several sets of class definitions. PHP will happily work by providing only one set of definitions in an application.

I have two PHPUnit test cases where I do basically the same tests using classes with different definitions generated by either config. I use require_once in all my code and do the same in unit tests, require_once uses only the class files that I need for this test.

PHPUnit is configured to run all the tests it finds in my test directory and it works as usual. When I added tests to generate different classes, PHPUnit stopped working with the PHP Fatal error: you cannot override a class.

Now, the default behavior of PHPUnit is to load ALL classes for which ALL tests must be run at a time. This leads to a fatal PHP error even before running any test, because PHPUnit chooses the second definition for the generated classes (they have the same class name but different file names), although I only have “require_once” correct class definitions in each test file.

, , , . .

, PHPUnit , "require_once" , , , phpunit.xml. , PHPUnit PHP "require_once" .

? PHPUnit, . PHPUnit , loaduite- .

, . PHPUnit foo1Test.php foo2test.php. , _ , Foo. , . 'testfoo' PHPunit . , .

foo1Test.php

<?php

require_once dirname ( __FILE__ ).'/Foo1.php';

class Foo1TestCase extends PHPUnit_Framework_TestCase
{
}

?>

foo2Test.php

<?php

require_once dirname ( __FILE__ ).'/Foo2.php';

class Foo2TestCase extends PHPUnit_Framework_TestCase
{
}

?>

Foo1.php

<?php

class Foo
{
}

?>

Foo2.php

<?php

class Foo
{
}

?>

foo1Test.php foo2Test.php PHPunit

phpunit foo1Test.php
phpunit foo2Test.php

, PHPUnit , .

phpunit.xml, .

<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupStaticAttributes="false"
         cacheTokens="false"
         colors="false"
         convertErrorsToExceptions="true"
         convertNoticesToExceptions="true"
         convertWarningsToExceptions="true"
         forceCoversAnnotation="false"
         mapTestClassNameToCoveredClassName="false"
         printerClass="PHPUnit_TextUI_ResultPrinter"
         processIsolation="false"
         stopOnError="false"
         stopOnFailure="false"
         stopOnIncomplete="false"
         stopOnSkipped="false"
         testSuiteLoaderClass="PHPUnit_Runner_StandardTestSuiteLoader"
         strict="false"
         verbose="true">
    <testsuite name="fooTests">
        <directory>.</directory>
    </testsuite>
</phpunit>

PHPUnit

PHPUnit

:

PHP Fatal error:  Cannot redeclare class Foo in /var/www/testfoo/Foo2.php on line 4
PHP Stack trace:
PHP   1. {main}() /usr/bin/phpunit:0
PHP   2. PHPUnit_TextUI_Command::main() /usr/bin/phpunit:46
PHP   3. PHPUnit_TextUI_Command->run() /usr/share/php/PHPUnit/TextUI/Command.php:130
PHP   4. PHPUnit_TextUI_Command->handleArguments() /usr/share/php/PHPUnit/TextUI/Command.php:139
PHP   5. PHPUnit_Util_Configuration->getTestSuiteConfiguration() /usr/share/php/PHPUnit/TextUI/Command.php:671
PHP   6. PHPUnit_Util_Configuration->getTestSuite() /usr/share/php/PHPUnit/Util/Configuration.php:768
PHP   7. PHPUnit_Framework_TestSuite->addTestFiles() /usr/share/php/PHPUnit/Util/Configuration.php:848
PHP   8. PHPUnit_Framework_TestSuite->addTestFile() /usr/share/php/PHPUnit/Framework/TestSuite.php:419
PHP   9. PHPUnit_Util_Fileloader::checkAndLoad() /usr/share/php/PHPUnit/Framework/TestSuite.php:358
PHP  10. PHPUnit_Util_Fileloader::load() /usr/share/php/PHPUnit/Util/Fileloader.php:79
PHP  11. include_once() /usr/share/php/PHPUnit/Util/Fileloader.php:95
PHP  12. require_once() /var/www/testfoo/foo2Test.php:3

. , PHPUnit testuite, , , , .

PHPUnit , , .

+4

Source: https://habr.com/ru/post/1540624/


All Articles