I’m trying to upgrade my project’s php version from php 7.4 to php 8.1,
Now I changed the php version in my composer.json to php 8.1 like that:
"require": {
"php": "^8.1",
I made sure to install and update composer accordingly,
In order to make sure that the version has changed I wrote this test which should print out the php version:
class TestCest
{
public function _before(AcceptanceTester $I)
{
}
public function _after(AcceptanceTester $I)
{
}
public function testCountNonCountable(AcceptanceTester $I)
{
$I->comment("I'am running on php ". phpversion());
}
}
and I’m running my test using this command:
php codecept.phar run my_path_to/TestCest.php -vvv –env dev
but to my surprise, the output of the test is:
Scenario --
I'am running on php 8.3.2
PASSED
I’am confused!. Does this mean that when I run my tests it is running on php 8.3.2 ?
if not then why am I getting this output
if yes then why isn’t it php 8.1 as I specified in the composer.json ?
How can I change the php version for codeception tests and how can I tell that the version have actually been changed ?
2
Answers
The code
Has nothing to do with which PHP version you are using, it means this is the minimum version that should be used when getting other dependencies.
The actual PHP version is determined by which one you installed and in this case use in CLI.
Yes, the output "I’am running on php 8.3.2" from your test indicates that your Codeception tests are indeed being executed with PHP 8.3.2. This can happen even though you specified PHP "^8.1" in your
composer.json
file because the PHP version used to run scripts from the command line (CLI) is determined by the PHP binary configured in your system’s PATH or specifically set for your project’s environment.The
composer.json
file’s PHP version requirement ("php": "^8.1"
) only specifies the PHP versions that your project is compatible with, primarily for dependency management. It does not enforce or change the PHP version used to run your scripts.Steps to Resolve the Version Issue
To ensure your tests run with the desired PHP version, follow these steps:
Check Available PHP Versions:
You need to confirm which PHP versions are installed on your system. You can do this by running:
Switch PHP Versions for CLI:
If you have multiple PHP versions installed, you can switch between them using aliases in your shell configuration or by modifying the PATH. On Unix-like systems, this is commonly done in
.bashrc
,.bash_profile
, or.zshrc
files:After updating, you might need to source the profile or restart the terminal:
Configuring Codeception:
Codeception uses the default PHP interpreter available in the shell unless specified otherwise. You can enforce a specific PHP version by directly invoking it:
Update Composer:
After setting the correct PHP version, ensure that all dependencies are compatible by running:
Verify PHP Version:
To verify the PHP version used in your tests, continue using your existing test in
TestCest
. If correctly configured, it should now display PHP 8.1.Note on PHP Versions
If PHP 8.3.2 is installed and being used, it indicates newer features and possibly better performance but also potentially different behavior compared to 8.1. Make sure that your application is tested under the PHP version you intend to use in production.
Lastly
If you don’t manage the system or server configurations directly, or if you are using a shared or managed hosting environment, you might need to contact your hosting provider or system administrator to configure the specific PHP versions for your projects.