If your application uses Symfony 3.2 and you execute PHPUnit via its PHAR file, you'll end up with the following error message:
1 2 3 4 5 6 7 8 | $ cd my-project/
$ php phpunit.phar
PHPUnit 5.6.2 by Sebastian Bergmann and contributors.
PHP Fatal error: Undefined class constant 'PARSE_CONSTANT' in
vendor/symfony/symfony/src/Symfony/Component/DependencyInjection/
Loader/YamlFileLoader.php on line 396
|
In Symfony 3.2 applications you can't use the PHAR file of PHPUnit and you must use instead the PHPUnit Bridge. First, if this component is not installed in your project, install it as follows:
1 2 | $ cd my-project/
$ composer require --dev symfony/phpunit-bridge
|
Then, execute your tests with the following command (the first time it will take some extra time because it must build the PHPUnit archive):
1 2 | $ cd my-project/
$ ./bin/simple-phpunit
|
This error happens when the code under test shares dependencies with PHPUnit but requires different versions than the ones bundled in the PHPUnit PHAR. In Symfony applications this happens with the Yaml component.
PHPUnit is working to solve this issue by adding a custom prefix to all their PHP namespaces automatically (see details) but they haven't achieved that yet.
Meanwhile, the best working solution is to make use of the PHPUnit Bridge
component. In addition to not embedding conflicting dependencies (such as
symfony/yaml
) it provides some additional features:
In the Symfony project we've suffeed this problem for a long time. That's why
you can see the use of simple-phpunit
in the .travis.yml
config file of
losts of bundles and libraries, such as SensioFrameworkExtraBundle and
Sami.
In addition to symfony/yaml
you can also suffer this problem when using
prophecy
. Read the PHPUnit Bridge docs to learn more about this and the
use of the SYMFONY_PHPUNIT_REMOVE
environment variable.
What a Symfony developer should know about the framework: News, Jobs, Tweets, Events, Videos,...