使用特定 php 版本在 phpunit 中运行测试

2022-01-25 00:00:00 macos version php phpunit

我在我的 Mac 上安装了多个 PHP 版本,并希望针对特定 PHP 版本(或多个版本)运行单元测试

I have installed multiple PHP versions on my Mac and want to run unit-tests against a specific PHP version (or against multipls versions)

这是我拥有的 php 版本:

Here's the php versions I have:

$ php --version
  Output: PHP 5.4.23 ...

$ /Applications/MAMP/bin/php/php5.2.17/bin/php --version
  Output: PHP 5.2.17 ...

我的测试用例如下所示:

My test case looks like this:

function test_php_version() {
    $actual = phpversion();
    $expected = '5.2.17';
    $this->assertEquals( $expected, $actual, 'Wrong PHP version!' );
}

当我运行测试时,我得到这个响应:

When I run the test I get this response:

$ phpunit

  Wrong PHP version!
  Failed asserting that two strings are equal.
  --- Expected
  +++ Actual
  @@ @@
  -'5.2.17'
  +'5.4.24'

$ /Applications/MAMP/bin/php/php5.2.17/bin/php phpunit
  Error: Could not open input file: phpunit

如何使用 php 版本 5.2.17 运行测试?

How can I run the tests with the php version 5.2.17?

更新:

我发现 PHPUnit 不再与 php5.2.17 一起运行.所以我改变了我的要求,用支持的 php5.3.5 运行单元测试.

I discover that PHPUnit does not run with php5.2.17 anymore. So I change my requirements to run unit tests with php5.3.5, which is supported.

推荐答案

$ /Applications/MAMP/bin/php/php5.2.17/bin/php path/to/phpunit.phar

创建一个脚本,例如phpunit-using-5.2

#!/bin/sh
set -e

/Applications/MAMP/bin/php/php5.2.17/bin/php path/to/phpunit.phar "$@"

现在你可以运行它了:

$ phpunit-using-5.2

或者你也可以创建一个 bash 别名.

Or you can create a bash alias too.

相关文章