php.md 7.9 KB
Newer Older
1
# Testing PHP projects
2

3
This guide covers basic building instructions for PHP projects.
4

5 6
There are covered two cases: testing using the Docker executor and testing
using the Shell executor.
7

8
## Test PHP projects using the Docker executor
9

10 11 12
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.
13

14
This will allow us to test PHP projects against different versions of PHP.
15
However, not everything is plug 'n' play, you still need to configure some
16
things manually.
17

18 19
As with every build, you need to create a valid `.gitlab-ci.yml` describing the
build environment.
20

21 22 23
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)).
24

25
Start by adding the image to your `.gitlab-ci.yml`:
26

27 28 29
```yaml
image: php:5.6
```
30

31 32 33 34
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.
35

36 37
Let's create a `ci/docker_install.sh` file in the root directory of our
repository with the following content:
38

39 40
```bash
#!/bin/bash
41

42
# We need to install dependencies only for Docker
43
[[ ! -e /.dockerenv ]] && exit 0
44

45
set -xe
46

47 48 49
# Install git (the php image doesn't have it) which is required by composer
apt-get update -yqq
apt-get install git -yqq
50

51
# Install phpunit, the tool that we will use for testing
52
curl --location --output /usr/local/bin/phpunit https://phar.phpunit.de/phpunit.phar
53
chmod +x /usr/local/bin/phpunit
54

55 56 57 58
# Install mysql driver
# Here you can install any other extension that you need
docker-php-ext-install pdo_mysql
```
59

60 61 62
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
T
Takuya Noguchi 已提交
63
<https://hub.docker.com/r/_/php/>.
64

65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
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).
91 92 93

The final `.gitlab-ci.yml` should look similar to this:

94
```yaml
T
Takuya Noguchi 已提交
95
# Select image from https://hub.docker.com/r/_/php/
96 97 98 99
image: php:5.6

before_script:
# Install dependencies
100
- bash ci/docker_install.sh > /dev/null
101 102 103 104 105 106 107 108 109 110 111 112 113 114

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
115
- bash ci/docker_install.sh > /dev/null
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139

# 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
```
140

141
Of course, `my_php.ini` must be present in the root directory of your repository.
142

143
## Test PHP projects using the Shell executor
144

145 146 147
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.
148

149 150
For example, in a VM running Debian 8 we first update the cache, then we
install `phpunit` and `php5-mysql`:
151

152 153 154 155
```bash
sudo apt-get update -y
sudo apt-get install -y phpunit php5-mysql
```
156

157
Next, add the following snippet to your `.gitlab-ci.yml`:
158

159 160 161 162 163
```yaml
test:app:
  script:
  - phpunit --configuration phpunit_myapp.xml
```
164

165
Finally, push to GitLab and let the tests begin!
166

167
### Test against different PHP versions in Shell builds
168

169 170 171
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.
172

173 174
You will have to install it on your build machine under the `gitlab-runner`
user following [the upstream installation guide][phpenv-installation].
175

176
Using phpenv also allows to easily configure the PHP environment with:
177

178 179 180
```
phpenv config-add my_config.ini
```
181

182 183 184 185 186 187 188 189
*__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.*

190
### Install custom extensions
191

192 193
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.
194

195
To install additional extensions simply execute:
196

197 198 199
```bash
pecl install <extension>
```
200

201 202
It's not advised to add this to `.gitlab-ci.yml`. You should execute this
command once, only to setup the build environment.
203

204
## Extend your tests
205

206
### Using atoum
207

208 209
Instead of PHPUnit, you can use any other tool to run unit tests. For example
you can use [atoum](https://github.com/atoum/atoum):
210

211 212 213
```yaml
before_script:
- wget http://downloads.atoum.org/nightly/mageekguy.atoum.phar
214

215 216 217 218
test:atoum:
  script:
  - php mageekguy.atoum.phar
```
219

220
### Using Composer
221

222 223 224
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`:
225

226 227
```yaml
...
228

229 230 231 232 233 234
# 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/
235

236 237
before_script:
# Install composer dependencies
238
- curl --silent --show-error https://getcomposer.org/installer | php
239
- php composer.phar install
240

241 242
...
```
243

244
## Access private packages / dependencies
245

246 247
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.
248

249
## Use databases or other services
250

251 252
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
253
link to other containers. In GitLab Runner lingo, this can be achieved by
254
defining a `service`.
255

256 257
This functionality is covered in [the CI services](../services/README.md)
documentation.
258

259
## Testing things locally
260

261 262
With GitLab Runner 1.0 you can also test any changes locally. From your
terminal execute:
263

264 265
```bash
# Check using docker executor
266
gitlab-ci-multi-runner exec docker test:app
267

268
# Check using shell executor
269
gitlab-ci-multi-runner exec shell test:app
270
```
271

272
## Example project
273

274 275 276
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).
277

278
Want to hack on it? Simply fork it, commit and push  your changes. Within a few
279
moments the changes will be picked by a public runner and the build will begin.
280

T
Takuya Noguchi 已提交
281
[php-hub]: https://hub.docker.com/r/_/php/
282 283
[phpenv]: https://github.com/phpenv/phpenv
[phpenv-installation]: https://github.com/phpenv/phpenv#installation
284
[php-example-repo]: https://gitlab.com/gitlab-examples/php