A tool to automatically rerun PHPUnit tests when source code changes

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. When you start it with phpunit-watcher watch this is what it looks like. By default it will watch all files in the src, app and tests subdirectories in the directory where it is started. If any of the files in those directories changes (or when a file in there gets created or deleted) the tests will rerun automatically. Cool, right? If you need to watch another directory just add a file named .phpunit-watcher.yml in your project directory. Here's some example content:
watch:
directories:
- myApplication
- tests
fileMask: '*.php'
Extra options can be passed to PHPUnit just by tacking them on the phpunit-watcher command. Here's an example where we only run tests whose name contains the word "includes": Now, if you want to get rid of that filter, there's no need to stop the tool. Like the little manual at the bottom will tell you, just press "a" to run all tests again. If you only want to run tests in a specific file or path, just type "p" to get to this screen: On that screen, just type the name of the file or directory that you want to filter on. And that's all there's really to it. The tool is fairly new, but the past days I've really enjoyed using our own tool myself and I wonder how I could ever have worked without it. Now I know that some IDE's, such as PHPStorm, ship with a PHPUnit watcher built-in, but our tool feels nicer (that's very subjective of course) and will work in combination with any editor or IDE (that's very objective). Some cool packages are powering our tool. The watching part is covered by the awesome Resource-watcher package. The interactivity is enabled by ReactPHP. I also want to give a shoutout to Colin O'Dell who helped fix a nasty performance problem and Christoper Pitt who wrote a nice blogpost that kickstarted the work on this package. If you want to know more about phpunit-watcher, be sure to check it out on GitHub. This is not the first time we've created something inspired by Jest. Take a look at this package which brings Jest's snapshot testing to PHPUnit. Want to see some mooarrrr packages we created previously, then check out the opensource pages on our company website.

How to write JavaScript-style test watchers in PHP

Christoper Pitt published another excellent piece over at Sitepoint. This time he describes how he built a watcher to automatically recompile his preprocessed code and rerun the tests.
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.
https://www.sitepoint.com/write-javascript-style-test-watchers-php/

A practical introduction to snapshot testing

While working a little bit on laravel-sitemap I decided to refactor the tests to make use of our own snapshot assertions package. The snapshot package enables you to generate snapshots with the output of your tests. On subsequent runs of those tests it can assert if the output still matches the contents of the snapshot. In this video I demonstrate how the refactor of the tests of laravel-sitemap was done. (double-click on it to view it full-screen) If you want to know more about snapshot testing, then read this blog post written by my colleague Sebastian. The refactor of the tests can be viewed in this commit on GitHub. (Little note on the video itself: this is my first tutorial video I ever made. After recording it I noticed that I used a wrong ratio resulting in those black bars on the side. Ah well, you live you learn. Next time it will be better. For those interested, I used a Blue Yeti Pro mic to record my voice and ScreenFlow to record and edit the video.)