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.

Specify PHP version

You can specify the version of PHP based on type of the build environment.

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
    ...

Linux VM environment

For the Linux VM based build environment, we can use phpbrew to manage multiple PHP versions. It also comes with few popular PHP versions, extensions and Composer pre-installed. Refer to toolbox to know more.

You can use or switch to any pre-installed version or install a new version which phpbrew supports.

  ...
    runner:
      os_image: ubuntu # Use linux-vm build environment

    steps:
    - commands:
      - phpbrew switch 7.2.23

      # use composer to manage dependencies 
      - composer -V
    ...

Note: You can also use Docker build environment if the version of PHP that you need, is not available in VM environment.

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:
          paths: coverage.xml # generated from line 7