Skip to content

PHP

This guide provides a walkthrough for a PHP-based application and how to configure common CI tasks. If you’re new to Razorops please read our Tutorial and configuration guides first.

We will show how to download dependencies, run tests, generate artifacts or coverage reports using various popular tools in PHP community. We also provide a CI optimized container image for PHP as well.

Choosing PHP version

Usually the first step before writing CI spec is to decide the language runtime and support for the version compatible with your application. We support most active versions of PHP. You have the following options if you want to pin-down a version of PHP -

  • For Docker runner environment, you can specify a container image (razorci/php or official php) with tag.
  • For Linux-VM environment, you can change the version (or install new version if not available) using phpbrew.

Selecting PHP version on Linux-VM environment

In Linux-VM build environment, we use phpbrew to manage the various PHP versions. You can find currently supported versions in toolbox reference. You can also install a different version using phpbrew install <version> command if it's not supported by us. It also comes with few popular PHP versions, extensions and Composer pre-installed. Refer to toolbox to know more.

Here are few commands to use, switch and install a version -

    # part of .razorops.yaml file
    steps:
    - commands:
      # list installed versions
      - phpbrew list
      - php --version

      - phpbrew switch 7.4
      - php --version
      # PHP 7.4.30 (cli) (built: Jul 28 2022 06:19:49) ( NTS )

      # install PHP 8.1
      - phpbrew known ## list stable versions
      - phpbrew --no-progress install 8.1

      - phpbrew switch 8.1
      - php --version
      # PHP 8.1.2 (cli) (built: Jul 28 2022 06:33:21) (NTS)
      # Copyright (c) The PHP Group
      # Zend Engine v4.1.2, Copyright (c) Zend Technologies

Note

You can also use Docker build environment if the version of PHP that you need (described below), is not available in Linux VM environment.

Available PHP versions pre-installed via phpbrew - 8.2.7

Selecting PHP version in Docker environment

You can control Php version using an appropriate docker tag in runner property. To know more about runner, see here.

tasks:
  php74: # or any name
    runner: razorci/php:7.4
    ...
tasks:
  php72: # or any name
    runner: razorci/php:7.2
    ...

Downloading dependencies

Downloading PHP third-party modules/dependencies from the internet can be time-consuming, you can greatly speed up this process by caching it using an unique key. For more information, see Caching dependencies.

In the below example we demonstrate a pipeline that restores a cache, executes composer require, and rebuilds the cache. Cache directory location will vary depending on your tool (composer or pear).

  ...
  steps:
    - cache/pull: composer-cache-{{ checksum "composer.lock" }}
    ...
    - run: composer install -n --prefer-dist
    ...
    - cache/push:
      - key: composer-cache-{{ checksum "composer.lock" }}
        paths:
        - vendor # composer vendor directory
        - ~/.composer/vendor # (default vendor path in linux-vm environment)
  ...

Execute tests and generate reports

You can use the same command that you use locally with run property in steps and combine with various lint and coverage tools to generate reports. To run the tests, it is recommended that you use phpunit.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
    steps:
      ...
      - commands: 
        - composer install -n --prefer-dist

        # read more at https://phpunit.readthedocs.io/en/9.3/code-coverage-analysis.html  
        - phpunit --coverage-html cover --coverage-xml coverage.xml
      ...

      - reports/html: 
          dir: cover # generated from line 7

      - reports/junit:
        - coverage.xml # generated from line 7