Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Freek Van der Herten:
A tool to automatically rerun PHPUnit tests when source code changes
Aug 04, 2017 @ 10:12:37

In this post to his site Freek Van der Herten shares information about a tool that can help to streamline your development workflow. The phpunit-watcher tool mimics tools from other languages to watch for changes in the source of your application and automatically execute your PHPUnit tests when modifications are made.

In the JavaScript world Jest, built by Facebook, is an excellent tool to run tests. Not only can it automatically rerun your tests when source code changes, but there’s also an interactive mode where you can set a filter on which tests to run while the tool is running. Would it be great if we could have these awesome features while working with PHPUnit?

Our newly released phpunit-watcher tool provides a Jest like experience. You can simply install it by running composer global require spatie/phpunit-watcher.

Once the tool is installed it can then be run from the command line, sitting and waiting for changes to the target files and directory. It's configurable in case you don't follow some of the usual package directory structures and can be filtered to only run on certain filename matches. This can also be done manually (real-time) via the tool. Screenshots are included showing the tool in action too.

tagged: phpunit watcher automatically execute unittest package

Link: https://murze.be/2017/08/tool-automatically-rerun-phpunit-tests-source-code-changes/

SitePoint PHP Blog:
Re-Introducing PHPUnit – Getting Started with TDD in PHP
Aug 01, 2017 @ 10:16:02

The SitePoint PHP blog has posted a tutorial that re-introduces PHPUnit and TDD (Test-Driven Development) showing you how to get started with this pre-code testing process if you want to integrate it into your development.

There are a lot of PHPUnit posts on our site already (just check the tag), but it’s been a while since we’ve actually introduced people to it, and the tool has evolved significantly since then.

This article aims to re-introduce the tool in a modern way, to a modern audience, in a modern PHP environment – if you’re unfamiliar with PHPUnit or testing, this post is for you.

They assume you're already familiar with PHPUnit and have an environment already set up with PHP and PHPUnit. They start by briefly introducing Test-Driven Development and the PHPUnit for those not overly familiar with the basics. They then bootstrap a sample application using the PDS-Skeleton project. The tutorial then walks you through the setup of the testing environment, Composer configuration and the creation of your first test. With all of that in place they start in with some failing tests (remember, TDD is "test first") and then writing the code to make it pass. They add more tests and use a data provider to set up different cases and run them all through the same test. The post then looks at code coverage reports and the results showing how much of the code is covered by tests.

tagged: tutorial phpunit tdd testdrivendevelopment introduction

Link: https://www.sitepoint.com/re-introducing-phpunit-getting-started-tdd-php/

SitePoint PHP Blog:
How to Write JavaScript-Style Test Watchers in PHP
Jul 20, 2017 @ 13:18:43

On the SitePoint PHP blog there's a new tutorial that talks about creating "watchers" in your PHP code similar to the ones that the Javascript world has used to detect changes in files and automatically perform actions.

In the land of JavaScript, it’s not uncommon to preprocess source code. In the land of JavaScript, developers write in syntax not widely supported, and the code is transformed into syntax that is widely supported, usually using a tool called Babel.

In order to reduce the burden of invoking the transformation scripts, boilerplate projects have started to include scripts to automatically watch for file changes; and thereafter invoke these scripts.

These projects I’ve worked on have used a similar approach to re-run unit tests. When I change the JavaScript files, these files are transformed and the unit tests are re-run. This way, I can immediately see if I’ve broken anything.

The author (Christopher Pitt) then shows how to create the project and make use of pre-processing and some sample scripts to run tests against. He shows how to set up the test configuration and provides an example test. Finally he includes a script that's used by the "watcher" to recompile the scripts before the tests are executed. To to the watching, he uses the yosymfony/resource-watcher package and a long running PHP script ("watch-test").

tagged: watcher javascript tutorial unittest phpunit resourcewatcher

Link: https://www.sitepoint.com/write-javascript-style-test-watchers-php/

BitExpert Blog:
Mocking callables in an Expressive app
May 01, 2017 @ 11:18:28

On the BitExpert blog Stephan Hochdörfer shows you how to mock callables in a Zend Expressive application based on a way he found during his own unit testing.

While working with Zend Expressive, a PSR-7 middleware microframework, I wanted to apply some unit testing with a nice coverage to my middlewares. Middlewares are called by the __invoke method if you provide them as an object and not as a closure. [...] Additionally, my middleware implementation does some stuff, but the middleware itself does not return a response, which is fine. Instead, my implementation calls the $next middleware in line.

He finishes the post with a quick example of how to mock out this $next call in his testing using the createPartialMock functionality in PHPUnit. He uses this method to create a mock that covers the __invoke method and returns a ResponseInterface instance of his choosing.

tagged: zendexpressive mocking phpunit unittest invoke callable testing tutorial

Link: https://blog.bitexpert.de/blog/mocking-callables-in-an-expressive-app/

Hackernoon.com:
Automatically Running PHPUnit With Watchman
Apr 12, 2017 @ 10:40:55

On the Hackernoon site today Sebastian De Deyne has written up a tutorial showing you how to use Watchman to automatically run PHPUnit tests for your application when things change. Watchman is a tool from Facebook that watches files and directories for updates and execute actions based on the changes.

Watchman watches files and triggers actions when they change. The reasoning behing choosing Watchman: it’s easy to install, simple to configure, and reliable.

The watchman-make command - which ships with Watchman - is a specialised interface for Watchman to invoke build tools in response to file changes - exactly what we need!

In the setup he creates, Watchman is used to look for changes on files in either the project's src/ or tests/ directories and execute a bash script (code provided) that runs the tests and outputs the results. He walks through each line of the script and Watchman command, explaining how it works and what the option points to. You can see the results here of an edit to a test and the output in a Terminal window once it's saved.

tagged: watchman phpunit test automatic execution change facebook tutorial

Link: https://hackernoon.com/automatically-running-phpunit-with-watchman-e02757e733e7

Medium.com:
A Package for Snapshot Testing in PHPUnit
Mar 29, 2017 @ 12:05:03

In this post on Medium.com Sebastian De Deyne shares a package from Spatie that makes it simpler to perform "snapshot testing" in your PHP applications.

The gist of snapshot testing is asserting that a set of data hasn’t changed compared to a previous version, which is a snapshot of the data, to prevent regressions. The difference between a classic assertEquals and an assertMatchesSnapshot is that you don't write the expectation yourself when snapshot testing. When a snapshot assertion happens for the first time, it creates a snapshot file with the actual output, and marks the test as incomplete. Every subsequent run will compare the output with the existing snapshot file to check for regressions.

Snapshot testing is most useful larger datasets that can change over time, like serializing an object for an XML export or a JSON API endpoint.

Our package, which exposes a trait to add snapshot testing capabilities to your tests, can be installed via composer and is available on GitHub.

He starts with a basic example, checking to verify that a string hasn't changed between test runs. The first run creates the snapshot (marking the test as incomplete) and then following runs verify - one passing (no change) and one failing (changing of the return string). He also includes examples of tests on objects, mentions where the snapshot files are stored and how to add drivers for additional snapshot storage types.

tagged: snapshot testing phpunit package tutorial

Link: https://medium.com/@sebdedeyne/a-package-for-snapshot-testing-in-phpunit-2e4558c07fe3

Raphael Stolt:
Keeping your CLI integration tests green on Windows
Mar 27, 2017 @ 10:55:08

Raphael Stolt has a new post to his site for the PHP on Windows users out there helping you to keep your integration tests green when little things (like line ending differences) can cause issues between the Windows and unix-based platforms.

Lately on a Windows system, some failing integration tests for CLI commands utilising the Symfony Console component caused me some blip headaches by PHPUnit insisting that two strings are not identical due to different line endings. The following post documents the small steps I took to overcome these headaches.

He starts with the error message that the failing test was giving, a slightly unhelpful response with the "actual" and "expected" values looking basically the same. He suspected it to be something different with the encoding or line endings (a common issue between Windows and Linux platforms). To confirm his suspicions he looked deeper into the Symfony Console code and, on locating the difference, extended the "CommandTester" class to automatically disable the "decorated" setting across all tests.

tagged: phpunit testing integration green windows platform decorate tutorial

Link: http://raphaelstolt.blogspot.com/2017/03/keeping-your-cli-integration-tests.html

Colin O'Dell:
PHPUnicorn - Visualizing PHPUnit Tests
Mar 16, 2017 @ 09:32:57

Colin O'Dell, as mentioned on his blog, has put together the instructions for something he calls "PHPUnicorn" (not to be confused with the PHP Unicorn conference) - a real-time system for visualizing unit test results via a Raspberry Pi, some LEDs and a Unicorn pHAT board.

For Pi Day 2017 I created a really fun project - the PHPUnicorn!

A simple PHPUnit listener collects test results and sends them to a Raspberry Pi Zero Wireless device in real-time. As the device receives the stats it lights up LEDs green, red, or orange to visualize the progress and results of your unit tests.

The full instructions are over in this article on the Hackster.io site providing you with a list of the components needed, how you'll need to extend PHPUnit with a custom listener and a simple Python script to interface with the Pi and Unicorn board. The end result is a set of LEDs on the board showing the progress (and failures) of your unit tests being run.

tagged: visualize phpunit test progress raspberrypi unicorn

Link: https://www.hackster.io/colinodell/phpunicorn-visualizing-phpunit-tests-896208

Mark Baker:
Closures, Anonymous Classes and an alternative approach to Test Mocking (Part 1)
Mar 06, 2017 @ 11:12:04

On his site Mark Baker has posted the first part of a series of articles covering the use of closures and anonymous classes in testing and mocking. In this first part of the series he focuses on introducing some of the basics of the topics to be covered and what the closures/anonymous classes can replace.

Since their first introduction with PHP 5.3, Closures have proven an incredibly useful feature for writing simple callback code, making it cleaner and more intuitive. Anonymous Functions can be used inline for many of the array functions or assigned to a variable as a Lambda that can be referenced many times in different places in your code.

[...] But this isn’t an article about the differences between Anonymous and Lambda Functions and Closures [...] Instead, I want to take a look at binding Closures to objects as a first step to demonstrating an alternative approach to test mocking.

He goes on to talk about the mocking the PHPUnit already includes, other libraries that help with mocking/stubs but then pushing those off for the focus of the article - the use of the closures/anonymous classes. He gets into some details about how PHP handles closures internally and how to bind a closure to a class or object instance (via the bindTo function). He then attaches this to an object and shows how to create a "snooper" to work with an object, perform some processing and return some values from it.

tagged: closure anonymous function tutorial testing unittest phpunit snooper

Link: https://markbakeruk.net/2017/03/05/closures-anonymous-classes-test-mocking-1/

Alejandro Celaya:
Run PHPUnit tests inside a docker container from PhpStorm
Feb 02, 2017 @ 11:14:04

Alejandro Celaya has a tutorial posted on his site showing you how you can improve your PHP workflow by running your unit tests in a Docker container from inside of PHPStorm.

Docker is, without any doubt, the trending tool these days. Everybody wants to use it, because it is very useful, allowing to easily generate development environments for any kind of application.

A couple months ago I started working with docker myself (it has taken me a while, I know), and now I can't imagine working without it. I started using it at work, but now I'm migrating all of my OSS projects too.

With Docker involved things get a bit more tricky when it comes to running your unit tests directly from PHPStorm (unlike local where it's just a few clicks away). Thankfully recent versions of PHPStorm come with a feature in the "Build, Execution, Deployment" that lets you define the location of the Docker executable. Then you'll need to set up a remote interpreter to link to the PHP binary then take that and link it back to the Docker installation. He ends the post showing how you can ensure it's working complete with a screenshot of the console showing the test results.

tagged: phpunit docker phpstorm container ide tutorial unittest

Link: https://blog.alejandrocelaya.com/2017/02/01/run-phpunit-tests-inside-docker-container-from-phpstorm/