简体   繁体   中英

How to use PHPUnit in Behat when using Symfony's PHPUnit Bridge?

In my Behat context, I've been included PHPUnit's assertions as described in the manual :

require_once 'PHPUnit/Autoload.php';
require_once 'PHPUnit/Framework/Assert/Functions.php';

However, I'm now using Symfony's PHPUnit bridge and so I've removed my explicitly-installed PHPUnit and am using the one brought in by the bridge. I needed to do this to avoid the "multiple versions" error described here .

But now, I can't use PHPUnit in behat!

PHP Warning:  Uncaught exception 'Symfony\Component\Debug\Exception\ContextErrorException' with message 'Warning: require_once(PHPUnit/Autoload.php): failed to open stream: No such file or directory'

How can I reference the PHPUnit instance brought in by the PHPUnit bridge?

It turned out that I needed to require the relevant files in vendor by their path instead. For me, that was:

require_once(__DIR__.'/../../vendor/symfony/phpunit-bridge/bin/.phpunit/phpunit-5.7/vendor/autoload.php');
require_once(__DIR__.'/../../vendor/symfony/phpunit-bridge/bin/.phpunit/phpunit-5.7/src/Framework/Assert/Functions.php');

For Symfony 4, this was enough to get it working for me:

require_once(__DIR__ . '/../../vendor/bin/.phpunit/phpunit-5.7/vendor/autoload.php');

However it's not a great solution ongoing, as PHPUnit Bridge will install PHPUnit 6.3 instead of 5.7 if you're using PHP 7.2.

I tried changing composer.json as follows:

"autoload-dev": {
    "psr-4": {
        "App\\Tests\\": "tests/"
    },
    "classmap": [
        "vendor/bin/.phpunit"
    ]
},

which does work, but breaks a fresh install because the folder doesn't exist until you run simple-phpunit .

I suspect this could be worked around using composer scripts to run simple-phpunit , but my brief experiments with post-install-cmd and pre-autoload-dump got me nowhere.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM