1. 云栖社区>
  2. PHP教程>
  3. 正文

Testing code with phpunit on travis-ci for PHP 5.5 and PHP7

作者:用户 来源:互联网 时间:2017-12-01 17:54:50

Testing code with phpunit on travis-ci for PHP 5.5 and PHP7 - 摘要: 本文讲的是Testing code with phpunit on travis-ci for PHP 5.5 and PHP7, I’ve recently added a pull-request to Zend/Ldap and realized that some of the tests were failing on PHP7. Reason was t

I’ve recently added a pull-request to Zend/Ldap and realized that some of the tests were failing on PHP7. Reason was that Error-handling has changed in PHP7 and the used PHPUnit-version 4.x didn’t know how to handle that. So I needed a version of PHPUnit that understood that, which is PHPUnit 5.x. Sadly the minimum PHP-Version for that is PHP5.6 so now the tests in PHP5.5 where failing.

How to handle that as there’s no way to tell composer to install a certain package-version when a certain PHP-Version is found and a different package-version for another PHP-version (At least I’m not aware of one – if you know of one please let me know!)

So what to do? As the tests are executed on Travis-CI I decided to rewrite the compser.json -file when the tests are executed for PHP5.5.

And here’s how I did that:

For Zend/Ldap we have this snippet in our .travis.yml -file:

before_install: - if [[ ${TRAVIS_PHP_VERSION:0:3} == "5.5" ]]; then composer require --dev --no-update phpunit/phpunit ~4; fiinstall: - travis_retry composer install --no-interaction --ignore-platform-reqs

The interesting part is the before_install part which checks whether the first three characters of the PHP-Version are 5.5 and then requires PHPUnit version 4. The default in the composer.json is PHPUnit version 5 so as soon as Zend/Ldap drops support for PHP5.5 we don’t have to remember to change that file, it just phases out.

After that PHPUnit 4 is installed and the tests run smoothly in all currently supported PHP-Versions (Actually they currently don’t, but that’s a different story and has nothing to do with the different PHPUnit-versions :wink: )

architectural daughtsman, brother, developer, father, husband, master of forestry sciences, scout

以上是云栖社区小编为您精心准备的的内容,在云栖社区的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索,以便于您获取更多的相关知识。