DEV Community

Cover image for Test driven development: a php starter kit
Simone Gentili
Simone Gentili

Posted on • Updated on


Test driven development: a php starter kit


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'
      context: ./docker/server/
      - ./:/var/www/html
      - "8888:80"
Enter fullscreen mode Exit fullscreen mode


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
Enter fullscreen mode Exit fullscreen mode


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
Enter fullscreen mode Exit fullscreen mode


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"
Enter fullscreen mode Exit fullscreen mode


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

    $(docker) up -d

    $(docker_exec) $(php) bash

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

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

    $(docker) stop

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

    $(docker) up -d --build

rebuild: stop rm build

    $(docker_exec) $(php) composer $(args)
Enter fullscreen mode Exit fullscreen mode

related repository

You can find the repository here. Very useful for kata of brand new php project.


The link to the video I've made is here: the walking skeleton. I this video I speak in italian but the code is in php.

Top comments (0)

50 CLI Tools You Can't Live Without

>> Check out this classic DEV post <<