composer global require spatie/phpunit-watcher. When you start it with
phpunit-watcher watchthis is what it looks like. By default it will watch all files in the
testssubdirectories 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.ymlin your project directory. Here's some example content:
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.
watch: directories: - myApplication - tests fileMask: '*.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.
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.)
Joe Ferguson shares a tip on how to make a version of PHPUnit's
assertArraySubsetwhere the order of the array does not matter.
The problem with this test is that `assertArraySubset()` will fail if the items in the array are out of order. And since we don’t have an `ORDER BY` on our query in our data source we cannot guarantee the order of the returned data.https://www.joeferguson.me/solidify-fragile-tests/