PHP

3 minute read

Versions and setup

CodeShip uses phpenv to manage PHP versions. 8.2 is the default version.

Versions 7.1, 7.2, 7.3, 7.4, 8.0, 8.1 and 8.3 are preinstalled as well. These are aliases that point to the specific versions installed.

The following PHP versions are preinstalled:

7.1 7.1.33 7.2 7.2.34 7.3 7.3.33 7.4 7.4.33 8.0 8.0.30 8.1 8.1.27 * 8.2 (set by /home/rof/.phpenv/version) 8.2.15 8.3 8.3.2

Specifying version

You can change the version you want to use by running phpenv local PHP_VERSION in your Setup Commands. For example:

phpenv local 7.4

PHP.ini

You can access and change the php.ini file in

/home/rof/.phpenv/versions/$(phpenv version-name)/etc/php.ini

phpenv Rehashing

If you install a new executable through a pear package make sure to run

phpenv rehash

so phpenv is aware of the new executable and sets it up correctly

Dependencies

You can install dependencies through pear and composer in your setup commands.

For example:

composer install

Dependency Cache

If you want cache composer dependencies during builds, please add the following environment variable to your build configuration.

COMPOSER_HOME=${HOME}/cache/composer

To make sure that the dependency cache is used by all of your dependencies, please call composer via the following snippet.

composer install --prefer-dist --no-interaction

Extensions

Extensions can be installed through pecl. If you need any other tools or are having trouble getting an extension to build, please send us a message.

xdebug

By default xdebug is installed and enabled on the build VMs. If you want to remove it, add the following command to your builds.

rm -f /home/rof/.phpenv/versions/$(phpenv version-name)/etc/conf.d/xdebug.ini

Pear

pear install pear/PHP_CodeSniffer

Composer

composer install --no-interaction

Pecl

pecl install -f memcache

GitHub API

To speed up Composer you can install packages from dist by replacing --prefer-source with --prefer-dist. However as GitHub’s API is rate limited, you then might see errors similar to this:

- Installing phpunit/phpunit (3.7.37) Downloading: connection... Failed to download phpunit/phpunit from dist: Could not authenticate against github.com Now trying to download from source

To avoid this create a new personal access token on GitHub. For the description you can use something like CodeShip Composer and you can unselect all scopes. Copy your personal access token and add it to the environment variables in your CodeShip project settings (on the Environment page) as GITHUB_ACCESS_TOKEN.

Then use the following Setup Commands in your CodeShip project settings:

composer config -g github-oauth.github.com $GITHUB_ACCESS_TOKEN composer install --prefer-dist --no-interaction

Frameworks And Testing

CodeShip supports essentially all popular PHP frameworks, such as Laravel, Symfony, CodeIgniter and CakePHP.

Additionally, all testing frameworks, such as phpunit and codeception, will work on CodeShip.

Parallel Testing

If you are running parallel test pipelines, you will want separate your tests into groups and call a group specifically in each pipeline. For instance:

Pipeline 1:

phpunit tests/tests_1

Pipeline 2:

phpunit tests/tests_2

Parallelization Modules

In addition to parallelizing your tests explicitly via parallel pipelines, some customers have found using the paratest module is a great way to speed up your tests.

Note that we do not officially support or integrate with this module and that it is possible for this to cause resource and build failure issues, as well.

Notes And Known Issues

  • When setting environment variables with PHP, the syntax can be either $_ENV["VAR_NAME"] or $varname. Individual frameworks may have their own formatting. For instance, Symfony uses %env(VAR_NAME)% for environment variables in configuration files, such as database configuration.

  • Due to PHP version issues, you may find it helpful to test your commands with different versions via an SSH debug session if tests are running differently on CodeShip compared to your local machine.

Running your PHP Server

If you want to test against a running PHP Server you can use the builtin one to start a server in the current directory. It will serve files from this directory.

nohup bash -c "php -S 127.0.0.1:8000 2>&1 &" && sleep 1; cat nohup.out

You can access it in your tests on 127.0.0.1:8000.

Also take a look at the PHP built-in webserver docs in the PHP documentation

Exiting Tests

All commands must return an exit code 0 to CodeShip to report a successful result, or any other error code to report an unsuccessful result. This means you must configure your test scripts to exit with a 0 status if they do not do so by default.

Custom PHPUnit Version

Note that if you need to install a custom version of PHPUnit, you can do so with the following commands:

composer global remove phpunit/phpunit composer global require phpunit/phpunit:7.*

Memcached

If you need to install the Memcached extension for PHP 7 and up you may need to use this command to get it installed:

printf "/usr \n\n\n\n\n\n\nno\n" | pecl install memcached

Sodium (libsodium)

The Sodium extension is available by default in PHP 7.2.0 and above.

For earlier PHP versions you can install it with a script by adding this command to your setup steps:

\curl -sSL https://raw.githubusercontent.com/codeship/scripts/master/packages/libsodium.sh | bash -s

Following the install add this command to install with pecl:

LD_LIBRARY_PATH=$HOME/cache/libsodium/lib PKG_CONFIG_PATH=$HOME/cache/libsodium/lib/pkgconfig LDFLAGS="-L$HOME/cache/libsodium/lib" pecl install libsodium