Test driven development: a php starter kit
Tags: php, docker
Published: 2022-09-27 10:35:15

---
title: Test driven development: a php starter kit
published: true
description: I've build a walking skeleton for brand new php project
tags: php, docker
series: tdd
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/usx1bw709rszvqxtgxif.jpg
---

## docker-compose.yml

This configuration includes just a service and expose a port to reach the container via browser. There is no more to talk about this file.

```
version: '3.7'
services:
  server:
    build:
      context: ./docker/server/
    volumes:
      - ./:/var/www/html
    ports:
      - "8888:80"
```

## /docker/server/Dockerfile

Previous file needs a Dockerfile in /docker/server/ folder. The Docker file is here. It is a php image with apache already instaled. This Dockerfile includes the minimum requirements to works with php and zdebug. git and zip are mantadory to work with composer. Actually zip is not mandatory, .. but composer can download compressed repositories when it is installed.

```
FROM php:8.1-apache
COPY 000-default.conf /etc/apache2/sites-available/000-default.conf
COPY ./ /var/www/html
COPY --from=composer:latest /usr/bin/composer /usr/local/bin/composer
RUN apt-get update && \
    apt-get upgrade && \
    apt-get install -y git zip
RUN pecl install xdebug && \
    docker-php-ext-enable xdebug
```

## /docker/server/000-deafult.conf

This is a very very simple virtualhost. 

```
<VirtualHost *:80>
	ServerAdmin webmaster@localhost
	DocumentRoot /var/www/html/public
	ErrorLog ${APACHE_LOG_DIR}/error.log
	CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
```

## composer.json

This composer.json file contains phpunit to start test driven development and two basic configurations.

First of all, bin folder is declared so then we can run ./bin/command instead of ./vendor/bin/command.

Second psr-4 is configured for files autoloading.

And third, phpunit is installed in dev enviroment.

```
{
    "config": {
        "bin-dir": "bin"
    },
    "autoload": {
        "psr-4": {
            "": ["src/"]
        }
    },
    "require-dev": {
        "phpunit/phpunit": "^9.5"
    }
}
```

## Makefile

In every project I work, a Makefile is present. Main target in this file are

 - coverage, for code coverage
 - rebuild, to stop, erase, and restart containers
 - composer, to run composer from host 

```
php := server
docker := docker-compose
compose := $(docker) --file docker-compose.yml
docker_exec := $(compose) exec
args = $(filter-out $@,$(MAKECMDGOALS))

up:
	$(docker) up -d

bash:
	$(docker_exec) $(php) bash

test:
	$(docker_exec) $(php) bash -c "./bin/phpunit --testdox --color"

coverage:
	$(docker_exec) $(php) bash -c "php -dxdebug.mode=coverage ./bin/phpunit --testdox --color --coverage-html coverage"
.PHONY: coverage

stop:
	$(docker) stop

rm:
	$(docker) rm $(php) --force

build:
	$(docker) up -d --build

rebuild: stop rm build

composer:
	$(docker_exec) $(php) composer $(args)
```

## related repository

You can find the repository [here](https://github.com/sensorario/youtube-docker). Very useful for kata of brand new php project.

## Link

The link to the video I've made is here: [the walking skeleton](https://youtu.be/_MuxJYm1BfE). I this video I speak in italian but the code is in php.