Philipp
Philipp

Reputation: 11351

Run test in phpunit with specific php version

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

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

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


Update:

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.

Upvotes: 15

Views: 10765

Answers (3)

Yevgeniy Afanasyev
Yevgeniy Afanasyev

Reputation: 41330

Laravel vagrant box Homestaead has a set of functons

like

$ php74
or 
$ php56

that set your PHP CLI to a specific version

Alternatively you can set it yourself like that

sudo update-alternatives --set php /usr/bin/php7.4
sudo update-alternatives --set phar /usr/bin/phar7.4
sudo update-alternatives --set phar.phar /usr/bin/phar.phar7.4
sudo update-alternatives --set phpize /usr/bin/phpize7.4
sudo update-alternatives --set php-config /usr/bin/php-config7.4

Upvotes: 0

Bedram Tamang
Bedram Tamang

Reputation: 4365

I had two version of php, default is 7.4 and older is 7.3, so I can test my testcase with older version like this.

php7.3 ./vendor/bin/phpunit

Upvotes: 12

Gerard Roche
Gerard Roche

Reputation: 6381

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

Create a script e.g. phpunit-using-5.2

#!/bin/sh
set -e

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

Now you can run it:

$ phpunit-using-5.2

Or you can create a bash alias too.

Upvotes: 14

Related Questions