It's never too late to get a life

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.