mardi 5 mai 2015

PHPUnit - Cannot redeclare class when processIsolation set to false

A similar question has been asked several times here

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 processIsolation attribute to true (since default is false), use require_once (don't feel like the best solution) or refactor the code (stop using static scopes, relative paths, etc.) ?

Aucun commentaire:

Enregistrer un commentaire