PHP: How can I tweak PHPUnit to use a different PH

2019-02-12 15:24发布

问题:

My system has two PHP interpreters. One came bundled with the OS and the other I installed via the XAMPP package. All of my PHP extensions are applied to the XAMPP installation but PHPUnit seems to only run the version of PHP that came with my machine.

Does anybody know of a way I can configure or rebuild PHPUnit so that it uses my XAMPP PHP interpreter exclusively?

回答1:

Find the folder you installed PHPUnit in. There should be a file called phpunit.bat. It should have a line that reads something like

set PHPBIN="C:\php\php.exe"
%PHPBIN% "C:\php\phpunit" %*

Change it to read

set PHPBIN="C:\xampp\php\php.exe"
%PHPBIN% "C:\xampp\php\phpunit" %*

Or whatever the path to your PHP executable is



回答2:

For Mac/Linux, the first line of the phpunit script with starts with

#!/usr/bin/php

change that to

#!/Applications/XAMPP/xamppfiles/bin/php

or whatever other php interpret you want to use.



回答3:

In agreement with Thomas' statement, additionally there's a line further below

if (strpos('/Applications/MAMP/bin/php5.3/bin/php', '@php_bin') === 0) {
    set_include_path(dirname(__FILE__) . PATH_SEPARATOR . get_include_path());
}

That I've been told you're also supposed to change to reflect the PHP you're interested in using (I've set mine to MAMP obviously)

I've switched back and forth from 5.2 and 5.3 a lot recently :)



回答4:

This applies to phpunit installed using Homebrew on Mac OS 10.9. I’ve editing the file located at /usr/local/Cellar/phpunit/4.2.6/bin as seen below. CAVEAT: I don’t know how Homebrew will handle this on a PhpUnit update, but for now it’s working to be able to select the php version that PhpUnit is using for it's testing.

#!/usr/bin/env bash

php=/Applications/MAMP/bin/php/php5.3.14/bin/php
#php=/Applications/MAMP/bin/php/php5.4.4/bin/php

/usr/bin/env $php -d allow_url_fopen=On -d detect_unicode=Off /usr/local/Cellar/phpunit/4.2.6/libexec/phpunit-4.2.6.phar $*


回答5:

On Windows, this may be achieved using a similar approach to the ones mentioned in other replies.

In your /path/to/composer/phpunit directory, open the phpunit file in an editor. The first line should look like this:

#!/usr/bin/env php

Simply download your desired version of PHP for Windows, place the contents of the ZIP file somewhere to your liking, and reference the fully quantified path to the php.exe file, instead of just php. Like so:

#!/usr/bin/env /c/misc/php-5.5.9-nts-Win32-VC11-x86/php.exe

In my case, I put it in /c/misc/php-5.5.9-nts-Win32-VC11-x86/, which corresponds to C:\misc\php-5.5.9-nts-Win32-VC11-x86\ using Windows path syntax.

Remember to verify that the correct php.ini file is being used (php --ini or in a script file php_ini_loaded_file()).



回答6:

Since modifying phpunit file did not work for me because of phar signature errors, I was running phpunit with different php version by calling interpreter explicitly (on Linux):

php7.1 /usr/local/bin/phpunit
php5.6 /usr/local/bin/phpunit

Following the example with XAMPP, full path to php interpreter could be provided:

/Applications/XAMPP/xamppfiles/bin/php /usr/local/bin/phpunit