DEV Community

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

Posted on • Edited on

Test driven development: a php starter kit

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

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

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

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

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

related repository

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

Link

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)