This guide covers basic building instructions for PHP projects.
Is it possible to test PHP apps on any system.
However, it will require manual configuration.
The simplest is to use Docker executor as described below.
There are covered two cases: testing using the Docker executor and testing
using the Shell executor.
### PHP projects on Docker executor
It's possible to official [PHP](https://hub.docker.com/_/php/) repositories on Docker Hub.
They allow to test PHP projects against different versions of the runtime.
However, they require additional configuration.
## Test PHP projects using the Docker executor
To build PHP project you need to create valid `.gitlab-ci.yml` describing the build environment:
1. First you need to specify PHP image as described here: http://doc.gitlab.com/ce/ci/docker/using_docker_images.html#what-is-image. To your `.gitlab-ci.yml` add:
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.
image: php:5.6
This will allow us to test PHP projects against different versions of PHP.
However, not everything is plug 'n' play, you still need to onfigure some
things manually.
2. The official images are great, but they are lacking a few useful tools for testing. We need to install them first in build environment. Create `ci/docker_install.sh` file with following content:
As with every build, you need to create a valid `.gitlab-ci.yml` describing the
build environment.
#!/bin/bash
Let's first specify the PHP image that will be used for the build process
(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)).
# We need to install dependencies only for Docker
[[ ! -e /.dockerinit ]] && exit 0
Start by adding the image to your `.gitlab-ci.yml`:
set -xe
```yaml
image:php:5.6
```
# Install git (the php image doesn't have it) which is required by composer
apt-get update -yqq
apt-get install git -yqq
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.
# Install phpunit, the tool that we will use for testing
# Here you can install any other extension that you need
docker-php-ext-install pdo_mysql
```
You might wonder what `docker-php-ext-install` is. In short, it is a script
provided by the official php docker image that you can use to easilly install
extensions. For more information read the the documentation at
<https://hub.docker.com/_/php/>.
Now that we created the script that contains all prerequisites for our build
environment, let's add it in `.gitlab-ci.yml`:
```yaml
...
5. Commit your files, and push them to GitLab to see if it works. With GitLab Runner 1.0 you can also test the changes locally. From your terminal execute:
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
# Check using docker executor
gitlab-runner exec docker test:app
...
```
# Check using shell executor
gitlab-runner exec shell test:app
Finally, commit your files and push them to GitLab to see your build succeeding
(or failing).
The final `.gitlab-ci.yml` should look similar to this:
Of course, `my_php.ini` must be present in the root directory of your repository.
Shell executor runs your builds in terminal session of your server. Thus in order to test your projects you need to have all dependencies installed as root.
## Test PHP projects using the Shell executor
1. Install PHP dependencies:
The shell executor runs your builds in a terminal session on your server.
Thus, in order to test your projects you first need to make sure that all
dependencies are installed.
sudo apt-get update -qy
sudo apt-get install phpunit php5-mysql -y
For example, in a VM running Debian 8 we first update the cache, then we
install `phpunit` and `php5-mysql`:
This will install the PHP version available for your distribution.
```bash
sudo apt-get update -y
sudo apt-get install-y phpunit php5-mysql
```
2. Now you can run your tests. Usually it will be `phpunit` with arguments:
Next, add the following snippet to your `.gitlab-ci.yml`:
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.
## Use databases or other services
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
connect to other containers. In GitLab Runner lingo, this can be achieved by
defining a `service`.
This functionality is covered in [the CI services](../services/README.md)
documentation.
## Testing things locally
### Access private packages / dependencies
With GitLab Runner 1.0 you can also test any changes locally. From your
terminal execute:
You need to configure [the SSH keys](../ssh_keys/README.md) in order to checkout the repositories.
```bash
# Check using docker executor
gitlab-runner exec docker test:app
### Use databases or other services
# Check using shell executor
gitlab-runner exec shell test:app
```
Please checkout the docs about configuring [the CI services](../services/README.md).
## Example project
### Example project
We have set up an [Example PHP Project](https://gitlab.com/gitlab-examples/php)
for your convenience that runs on [GitLab.com](https://gitlab.com) using our
publicly available [shared runners](../runners/README.md).
You maybe interested in our [Example Project](https://gitlab.com/gitlab-examples/php) that runs on [GitLab.com](https://gitlab.com) using our publically available shared runners.
Want to hack it? Simply fork it, commit and push your changes. Within a few
moments the changes will be picked by a public runner and the build will begin.
Want to hack it? Simply fork it, commit and push changes. Within a few moments the changes will be picked and rebuilt by public runners.