PHP

Managing test doubles in PHP unit tests

The more dependencies a class has, the harder it becomes to initialize in unit tests with all its dependencies. Your tests will become longer and longer. This articles explores three ways to provide test doubles (also known as “mocks”) to your tested class, while keeping the tests as short and expressive as possible: Using properties in the test class, using a builder pattern with a fluent interface and using named parameters (available in PHP 8.

Using Doctrine connection array shapes with PHPStan

At work we’re using PHPStan to do a static analysis of our code, checking if the provided types match the required types. We are using the most restrictive setting that forces us to have type annotations for arrays. Recently, this led to an error when using Doctrine DBAL, using DriverManager::getConnection(). In this article I’ll explain how to use better type annotations for the parameter of DriverManager::getConnection(). Let’s say we have a test environment class that initializes a database connection, used by the tests for the database adapter.

PHPUnit auto-test without an IDE

In this article you’ll learn how to run your PHPUnit tests whenever a PHP file changes. This is useful if you don’t use an IDE that does this or want to share the results during a pairing session where only one party has access to a PHP execution environment. With the command line tool entr you can watch for changes in a list of files and run a command whenever one of the files changes.

Working with PHP dependencies in multiple repositories

Imagine yourself working on a PHP code base distributed between two or more Git repositories, for example a library and an application part. Ideally, the two repositories should be independent, but sometimes that lofty goal can’t be achieved. And having two repositories open in your editor and doing git commit, git push, composer update all the time becomes tedious. This article shows how to work faster with two dependent PHP code bases.

Migration from Spress to Hugo

History and Motivation The list of static site generators is very long, but which one should you choose? When I ported this blog from Drupal to static HTML, I chose Spress, written in PHP, because PHP was my main programming language, its code looked well-thought-out, relied on established libraries and did everything I wanted out of the box and with a minimum amount of configuration. However, during the last year there was only one minor release of Spress and I missed some features I saw in other projects:

Object Oriented File Access in PHP

Test-driven driven development in PHP can become a pain when you’re dealing with the file system. The builtin functions like stat, getfilemtime, fopen and fgets assume the existence of actual files. Until now, I assumed you’d have to add a library like FileFetcher, Flysystem, Gaufrette or vfsSystem to your dependencies. While those libraries are nice, they are additional dependencies and some add additional capabilities like caching or providing a unified interface to cloud storage.