2015-12-03 11:13:19 -05:00
|
|
|
# Testing PHP projects
|
2015-11-25 08:41:14 -05:00
|
|
|
|
2015-12-03 11:13:19 -05:00
|
|
|
This guide covers basic building instructions for PHP projects.
|
2015-11-25 08:41:14 -05:00
|
|
|
|
2015-12-03 11:13:19 -05:00
|
|
|
There are covered two cases: testing using the Docker executor and testing
|
|
|
|
using the Shell executor.
|
2015-11-25 08:41:14 -05:00
|
|
|
|
2015-12-03 11:13:19 -05:00
|
|
|
## Test PHP projects using the Docker executor
|
2015-11-25 08:41:14 -05:00
|
|
|
|
2015-12-03 11:13:19 -05:00
|
|
|
While it is possible to test PHP apps on any system, this would require manual
|
|
|
|
configuration from the developer. To overcome this we will be using the
|
|
|
|
official [PHP docker image][php-hub] that can be found in Docker Hub.
|
2015-11-25 08:41:14 -05:00
|
|
|
|
2015-12-03 11:13:19 -05:00
|
|
|
This will allow us to test PHP projects against different versions of PHP.
|
2016-01-20 13:38:15 -05:00
|
|
|
However, not everything is plug 'n' play, you still need to configure some
|
2015-12-03 11:13:19 -05:00
|
|
|
things manually.
|
2015-11-25 08:41:14 -05:00
|
|
|
|
2017-02-13 11:59:57 -05:00
|
|
|
As with every job, you need to create a valid `.gitlab-ci.yml` describing the
|
2015-12-03 11:13:19 -05:00
|
|
|
build environment.
|
2015-11-25 08:41:14 -05:00
|
|
|
|
2017-02-13 11:59:57 -05:00
|
|
|
Let's first specify the PHP image that will be used for the job process
|
2015-12-03 11:13:19 -05:00
|
|
|
(you can read more about what an image means in the Runner's lingo reading
|
|
|
|
about [Using Docker images](../docker/using_docker_images.md#what-is-image)).
|
2015-11-25 08:41:14 -05:00
|
|
|
|
2015-12-03 11:13:19 -05:00
|
|
|
Start by adding the image to your `.gitlab-ci.yml`:
|
2015-11-25 08:41:14 -05:00
|
|
|
|
2015-12-03 11:13:19 -05:00
|
|
|
```yaml
|
|
|
|
image: php:5.6
|
|
|
|
```
|
2015-11-25 08:41:14 -05:00
|
|
|
|
2015-12-03 11:13:19 -05:00
|
|
|
The official images are great, but they lack a few useful tools for testing.
|
|
|
|
We need to first prepare the build environment. A way to overcome this is to
|
|
|
|
create a script which installs all prerequisites prior the actual testing is
|
|
|
|
done.
|
2015-11-25 08:41:14 -05:00
|
|
|
|
2015-12-03 11:13:19 -05:00
|
|
|
Let's create a `ci/docker_install.sh` file in the root directory of our
|
|
|
|
repository with the following content:
|
2015-11-25 08:41:14 -05:00
|
|
|
|
2015-12-03 11:13:19 -05:00
|
|
|
```bash
|
|
|
|
#!/bin/bash
|
2015-11-25 08:41:14 -05:00
|
|
|
|
2015-12-03 11:13:19 -05:00
|
|
|
# We need to install dependencies only for Docker
|
2016-12-01 13:08:37 -05:00
|
|
|
[[ ! -e /.dockerenv ]] && exit 0
|
2015-11-25 08:41:14 -05:00
|
|
|
|
2015-12-03 11:13:19 -05:00
|
|
|
set -xe
|
2015-11-25 08:41:14 -05:00
|
|
|
|
2015-12-03 11:13:19 -05:00
|
|
|
# Install git (the php image doesn't have it) which is required by composer
|
|
|
|
apt-get update -yqq
|
|
|
|
apt-get install git -yqq
|
2015-11-25 08:41:14 -05:00
|
|
|
|
2015-12-03 11:13:19 -05:00
|
|
|
# Install phpunit, the tool that we will use for testing
|
2016-08-08 03:47:17 -04:00
|
|
|
curl --location --output /usr/local/bin/phpunit https://phar.phpunit.de/phpunit.phar
|
2015-12-03 11:13:19 -05:00
|
|
|
chmod +x /usr/local/bin/phpunit
|
2015-11-25 08:41:14 -05:00
|
|
|
|
2015-12-03 11:13:19 -05:00
|
|
|
# Install mysql driver
|
|
|
|
# Here you can install any other extension that you need
|
|
|
|
docker-php-ext-install pdo_mysql
|
|
|
|
```
|
2015-11-25 08:41:14 -05:00
|
|
|
|
2015-12-03 11:13:19 -05:00
|
|
|
You might wonder what `docker-php-ext-install` is. In short, it is a script
|
2017-02-13 11:59:57 -05:00
|
|
|
provided by the official php docker image that you can use to easily install
|
|
|
|
extensions. For more information read the documentation at
|
2016-05-07 00:29:24 -04:00
|
|
|
<https://hub.docker.com/r/_/php/>.
|
2015-11-25 08:41:14 -05:00
|
|
|
|
2015-12-03 11:13:19 -05:00
|
|
|
Now that we created the script that contains all prerequisites for our build
|
|
|
|
environment, let's add it in `.gitlab-ci.yml`:
|
|
|
|
|
|
|
|
```yaml
|
|
|
|
...
|
|
|
|
|
|
|
|
before_script:
|
|
|
|
- bash ci/docker_install.sh > /dev/null
|
|
|
|
|
|
|
|
...
|
|
|
|
```
|
|
|
|
|
|
|
|
Last step, run the actual tests using `phpunit`:
|
|
|
|
|
|
|
|
```yaml
|
|
|
|
...
|
|
|
|
|
|
|
|
test:app:
|
|
|
|
script:
|
|
|
|
- phpunit --configuration phpunit_myapp.xml
|
|
|
|
|
|
|
|
...
|
|
|
|
```
|
|
|
|
|
|
|
|
Finally, commit your files and push them to GitLab to see your build succeeding
|
|
|
|
(or failing).
|
2015-11-25 08:41:14 -05:00
|
|
|
|
|
|
|
The final `.gitlab-ci.yml` should look similar to this:
|
|
|
|
|
2015-12-03 11:13:19 -05:00
|
|
|
```yaml
|
2016-05-07 00:29:24 -04:00
|
|
|
# Select image from https://hub.docker.com/r/_/php/
|
2015-12-03 11:13:19 -05:00
|
|
|
image: php:5.6
|
|
|
|
|
|
|
|
before_script:
|
|
|
|
# Install dependencies
|
2016-01-26 21:10:03 -05:00
|
|
|
- bash ci/docker_install.sh > /dev/null
|
2015-12-03 11:13:19 -05:00
|
|
|
|
|
|
|
test:app:
|
|
|
|
script:
|
|
|
|
- phpunit --configuration phpunit_myapp.xml
|
|
|
|
```
|
|
|
|
|
|
|
|
### Test against different PHP versions in Docker builds
|
|
|
|
|
|
|
|
Testing against multiple versions of PHP is super easy. Just add another job
|
|
|
|
with a different docker image version and the runner will do the rest:
|
|
|
|
|
|
|
|
```yaml
|
|
|
|
before_script:
|
|
|
|
# Install dependencies
|
2016-01-26 21:10:03 -05:00
|
|
|
- bash ci/docker_install.sh > /dev/null
|
2015-12-03 11:13:19 -05:00
|
|
|
|
|
|
|
# We test PHP5.6
|
|
|
|
test:5.6:
|
|
|
|
image: php:5.6
|
|
|
|
script:
|
|
|
|
- phpunit --configuration phpunit_myapp.xml
|
|
|
|
|
|
|
|
# We test PHP7.0 (good luck with that)
|
|
|
|
test:7.0:
|
|
|
|
image: php:7.0
|
|
|
|
script:
|
|
|
|
- phpunit --configuration phpunit_myapp.xml
|
|
|
|
```
|
|
|
|
|
|
|
|
### Custom PHP configuration in Docker builds
|
|
|
|
|
|
|
|
There are times where you will need to customise your PHP environment by
|
|
|
|
putting your `.ini` file into `/usr/local/etc/php/conf.d/`. For that purpose
|
|
|
|
add a `before_script` action:
|
|
|
|
|
|
|
|
```yaml
|
|
|
|
before_script:
|
|
|
|
- cp my_php.ini /usr/local/etc/php/conf.d/test.ini
|
|
|
|
```
|
2015-11-25 08:41:14 -05:00
|
|
|
|
2015-12-03 11:13:19 -05:00
|
|
|
Of course, `my_php.ini` must be present in the root directory of your repository.
|
2015-11-25 08:41:14 -05:00
|
|
|
|
2015-12-03 11:13:19 -05:00
|
|
|
## Test PHP projects using the Shell executor
|
2015-11-25 08:41:14 -05:00
|
|
|
|
2017-02-13 11:59:57 -05:00
|
|
|
The shell executor runs your job in a terminal session on your server.
|
2015-12-03 11:13:19 -05:00
|
|
|
Thus, in order to test your projects you first need to make sure that all
|
|
|
|
dependencies are installed.
|
2015-11-25 08:41:14 -05:00
|
|
|
|
2015-12-03 11:13:19 -05:00
|
|
|
For example, in a VM running Debian 8 we first update the cache, then we
|
|
|
|
install `phpunit` and `php5-mysql`:
|
2015-11-25 08:41:14 -05:00
|
|
|
|
2015-12-03 11:13:19 -05:00
|
|
|
```bash
|
|
|
|
sudo apt-get update -y
|
|
|
|
sudo apt-get install -y phpunit php5-mysql
|
|
|
|
```
|
2015-11-25 08:41:14 -05:00
|
|
|
|
2015-12-03 11:13:19 -05:00
|
|
|
Next, add the following snippet to your `.gitlab-ci.yml`:
|
2015-11-25 08:41:14 -05:00
|
|
|
|
2015-12-03 11:13:19 -05:00
|
|
|
```yaml
|
|
|
|
test:app:
|
|
|
|
script:
|
|
|
|
- phpunit --configuration phpunit_myapp.xml
|
|
|
|
```
|
2015-11-25 08:41:14 -05:00
|
|
|
|
2015-12-03 11:13:19 -05:00
|
|
|
Finally, push to GitLab and let the tests begin!
|
2015-11-25 08:41:14 -05:00
|
|
|
|
2015-12-03 11:13:19 -05:00
|
|
|
### Test against different PHP versions in Shell builds
|
2015-11-25 08:41:14 -05:00
|
|
|
|
2015-12-03 11:13:19 -05:00
|
|
|
The [phpenv][] project allows you to easily manage different versions of PHP
|
|
|
|
each with its own config. This is specially usefull when testing PHP projects
|
|
|
|
with the Shell executor.
|
2015-11-25 08:41:14 -05:00
|
|
|
|
2015-12-03 11:13:19 -05:00
|
|
|
You will have to install it on your build machine under the `gitlab-runner`
|
|
|
|
user following [the upstream installation guide][phpenv-installation].
|
2015-11-25 08:41:14 -05:00
|
|
|
|
2015-12-03 11:13:19 -05:00
|
|
|
Using phpenv also allows to easily configure the PHP environment with:
|
2015-11-25 08:41:14 -05:00
|
|
|
|
2015-12-03 11:13:19 -05:00
|
|
|
```
|
|
|
|
phpenv config-add my_config.ini
|
|
|
|
```
|
2015-11-25 08:41:14 -05:00
|
|
|
|
2015-12-08 14:35:08 -05:00
|
|
|
*__Important note:__ It seems `phpenv/phpenv`
|
|
|
|
[is abandoned](https://github.com/phpenv/phpenv/issues/57). There is a fork
|
|
|
|
at [madumlao/phpenv](https://github.com/madumlao/phpenv) that tries to bring
|
|
|
|
the project back to life. [CHH/phpenv](https://github.com/CHH/phpenv) also
|
|
|
|
seems like a good alternative. Picking any of the mentioned tools will work
|
|
|
|
with the basic phpenv commands. Guiding you to choose the right phpenv is out
|
|
|
|
of the scope of this tutorial.*
|
|
|
|
|
2015-12-03 11:13:19 -05:00
|
|
|
### Install custom extensions
|
2015-11-25 08:41:14 -05:00
|
|
|
|
2015-12-03 11:13:19 -05:00
|
|
|
Since this is a pretty bare installation of the PHP environment, you may need
|
|
|
|
some extensions that are not currently present on the build machine.
|
2015-11-25 08:41:14 -05:00
|
|
|
|
2015-12-03 11:13:19 -05:00
|
|
|
To install additional extensions simply execute:
|
2015-11-25 08:41:14 -05:00
|
|
|
|
2015-12-03 11:13:19 -05:00
|
|
|
```bash
|
|
|
|
pecl install <extension>
|
|
|
|
```
|
2015-11-25 08:41:14 -05:00
|
|
|
|
2015-12-03 11:13:19 -05:00
|
|
|
It's not advised to add this to `.gitlab-ci.yml`. You should execute this
|
|
|
|
command once, only to setup the build environment.
|
2015-11-25 08:41:14 -05:00
|
|
|
|
2015-12-03 11:13:19 -05:00
|
|
|
## Extend your tests
|
2015-11-25 08:41:14 -05:00
|
|
|
|
2015-12-03 11:13:19 -05:00
|
|
|
### Using atoum
|
2015-11-25 08:41:14 -05:00
|
|
|
|
2015-12-03 11:13:19 -05:00
|
|
|
Instead of PHPUnit, you can use any other tool to run unit tests. For example
|
|
|
|
you can use [atoum](https://github.com/atoum/atoum):
|
2015-11-25 08:41:14 -05:00
|
|
|
|
2015-12-03 11:13:19 -05:00
|
|
|
```yaml
|
|
|
|
before_script:
|
|
|
|
- wget http://downloads.atoum.org/nightly/mageekguy.atoum.phar
|
2015-11-25 08:41:14 -05:00
|
|
|
|
2015-12-03 11:13:19 -05:00
|
|
|
test:atoum:
|
|
|
|
script:
|
|
|
|
- php mageekguy.atoum.phar
|
|
|
|
```
|
2015-11-25 08:41:14 -05:00
|
|
|
|
2015-12-03 11:13:19 -05:00
|
|
|
### Using Composer
|
2015-11-25 08:41:14 -05:00
|
|
|
|
2015-12-03 11:13:19 -05:00
|
|
|
The majority of the PHP projects use Composer for managing their PHP packages.
|
|
|
|
In order to execute Composer before running your tests, simply add the
|
|
|
|
following in your `.gitlab-ci.yml`:
|
2015-11-25 08:41:14 -05:00
|
|
|
|
2015-12-03 11:13:19 -05:00
|
|
|
```yaml
|
|
|
|
...
|
2015-11-25 08:41:14 -05:00
|
|
|
|
2015-12-03 11:13:19 -05:00
|
|
|
# Composer stores all downloaded packages in the vendor/ directory.
|
|
|
|
# Do not use the following if the vendor/ directory is commited to
|
|
|
|
# your git repository.
|
|
|
|
cache:
|
|
|
|
paths:
|
|
|
|
- vendor/
|
2015-11-25 08:41:14 -05:00
|
|
|
|
2015-12-03 11:13:19 -05:00
|
|
|
before_script:
|
|
|
|
# Install composer dependencies
|
2016-05-29 16:15:56 -04:00
|
|
|
- wget https://composer.github.io/installer.sig -O - -q | tr -d '\n' > installer.sig
|
|
|
|
- php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
|
|
|
|
- php -r "if (hash_file('SHA384', 'composer-setup.php') === file_get_contents('installer.sig')) { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
|
|
|
|
- php composer-setup.php
|
|
|
|
- php -r "unlink('composer-setup.php'); unlink('installer.sig');"
|
2015-12-03 11:13:19 -05:00
|
|
|
- php composer.phar install
|
2015-11-25 08:41:14 -05:00
|
|
|
|
2015-12-03 11:13:19 -05:00
|
|
|
...
|
|
|
|
```
|
2015-11-25 08:41:14 -05:00
|
|
|
|
2015-12-03 11:13:19 -05:00
|
|
|
## Access private packages / dependencies
|
2015-11-25 08:41:14 -05:00
|
|
|
|
2015-12-03 11:13:19 -05:00
|
|
|
If your test suite needs to access a private repository, you need to configure
|
|
|
|
[the SSH keys](../ssh_keys/README.md) in order to be able to clone it.
|
2015-11-25 08:41:14 -05:00
|
|
|
|
2015-12-03 11:13:19 -05:00
|
|
|
## Use databases or other services
|
2015-11-25 08:41:14 -05:00
|
|
|
|
2015-12-03 11:13:19 -05:00
|
|
|
Most of the time you will need a running database in order for your tests to
|
|
|
|
run. If you are using the Docker executor you can leverage Docker's ability to
|
2015-12-03 14:18:34 -05:00
|
|
|
link to other containers. In GitLab Runner lingo, this can be achieved by
|
2015-12-03 11:13:19 -05:00
|
|
|
defining a `service`.
|
2015-11-25 08:41:14 -05:00
|
|
|
|
2015-12-03 11:13:19 -05:00
|
|
|
This functionality is covered in [the CI services](../services/README.md)
|
|
|
|
documentation.
|
2015-11-25 08:41:14 -05:00
|
|
|
|
2015-12-03 11:13:19 -05:00
|
|
|
## Testing things locally
|
2015-11-25 08:41:14 -05:00
|
|
|
|
2015-12-03 11:13:19 -05:00
|
|
|
With GitLab Runner 1.0 you can also test any changes locally. From your
|
|
|
|
terminal execute:
|
2015-11-25 08:41:14 -05:00
|
|
|
|
2015-12-03 11:13:19 -05:00
|
|
|
```bash
|
|
|
|
# Check using docker executor
|
2016-06-08 03:28:23 -04:00
|
|
|
gitlab-ci-multi-runner exec docker test:app
|
2015-11-25 08:41:14 -05:00
|
|
|
|
2015-12-03 11:13:19 -05:00
|
|
|
# Check using shell executor
|
2016-06-08 03:28:23 -04:00
|
|
|
gitlab-ci-multi-runner exec shell test:app
|
2015-12-03 11:13:19 -05:00
|
|
|
```
|
2015-11-25 08:41:14 -05:00
|
|
|
|
2015-12-03 11:13:19 -05:00
|
|
|
## Example project
|
2015-11-25 08:41:14 -05:00
|
|
|
|
2015-12-04 04:47:02 -05:00
|
|
|
We have set up an [Example PHP Project][php-example-repo] for your convenience
|
|
|
|
that runs on [GitLab.com](https://gitlab.com) using our publicly available
|
|
|
|
[shared runners](../runners/README.md).
|
2015-11-25 08:41:14 -05:00
|
|
|
|
2015-12-04 04:47:02 -05:00
|
|
|
Want to hack on it? Simply fork it, commit and push your changes. Within a few
|
2017-02-13 11:59:57 -05:00
|
|
|
moments the changes will be picked by a public runner and the job will begin.
|
2015-11-25 08:41:14 -05:00
|
|
|
|
2016-05-07 00:29:24 -04:00
|
|
|
[php-hub]: https://hub.docker.com/r/_/php/
|
2015-12-03 11:13:19 -05:00
|
|
|
[phpenv]: https://github.com/phpenv/phpenv
|
|
|
|
[phpenv-installation]: https://github.com/phpenv/phpenv#installation
|
2015-12-04 04:47:02 -05:00
|
|
|
[php-example-repo]: https://gitlab.com/gitlab-examples/php
|