Improving the output of PHPUnit
Let's have a little fun! Yitzchok Willroth, better know as Coderabbi, created a new package that can improve the output of PHPUnit.
Here's what the output of a testrun looks like with his package installed.
Posts tagged with testing
Let's have a little fun! Yitzchok Willroth, better know as Coderabbi, created a new package that can improve the output of PHPUnit.
Here's what the output of a testrun looks like with his package installed.
Testing @spatie_be 's media library with the new #laravel upload fakes ? Trust, but verify ? #testing pic.twitter.com/Ss98rGloRf
— Christoph Rumpel (@christophrumpel) July 3, 2017
Read more [twitter.com]
Join 9,500+ smart developers
Get my monthly newsletter with what I learn from running Spatie, building Oh Dear, and maintaining 300+ open source packages. Practical takes on Laravel, PHP, and AI that you can actually use.
No spam. Unsubscribe anytime. You can also follow me on X.
"I’ve been reading freeks newsletter for a while now, and it’s easily one of my favorite developer newsletters. Freek shares practical, no-nonsense insights from running Spatie, maintaining hundreds of open-source packages, and shipping real products. Every time I get useful Laravel tips, smart tooling advice, always high quality and immediately applicable."
Curl is not your only tool when creating or testing out APIs. On her blog Lorna Jane Mitchell made a nice list of alternatives.
At a conference a few days ago, I put up a slide with a few of my favourite tools on it. I got some brilliant additional recommendations in return from twitter so I thought I'd collect them all in one place in case anyone is interested - all these tools are excellent for anyone working APIs (so that's everyone!).
Mattieu Napoli shows how you can use non breaking spaces to make long test function names more readable.
Yes. This article is about using non-breakable spaces to name tests. And the fact that it’s awesome. And why you should use them too.public function test a user can add a product to a wishlist() { // ... }
The code above is valid PHP code and works. Non-breaking spaces (aka in HTML) look like spaces in editors but are actually interpreted like any other character by PHP.
http://mnapoli.fr/using-non-breakable-spaces-in-test-method-names/
It's cool that it works, but I'm not really a fan of this. I very much prefer how test runners like jest go about this by passing the name of the test to a function so you can use spaces.
Cool little Laravel 5.5 Eloquent Collection macro to assert two model collections are the same ?? pic.twitter.com/zzbkr1fL9T
— Adam Wathan (@adamwathan) June 14, 2017
Read more [twitter.com]
In a new blogpost at Sitepoint Christopher Pitt talks about snapshot testing. He first explains how snapshot testing can help test React components. Then he demonstrates how you can use our home grown package to use snapshot testing in PHP. Finally he shows pretty cool use cases for snapshot tests.
Ah-ha moments are beautiful and rare in programming. Every so often, we’re fortunate enough to discover some trick or facet of a system that forever changes how we think of it.For me, that’s what snapshot testing is.
You probably write a lot of PHP code, but today I want to talk about something I learned in JavaScript. We’ll learn about what snapshot testing is and then see how it can help us write better PHP applications.
https://www.sitepoint.com/snapshot-testing-viable-php/
If you want know more about snapshot testing, check out this blogpost by my colleague Sebastian, or watch this video where I refactor some tests to use snapshots.
Welcome to part 5 of a series on how to test a Vue component. Here's the table of contents of the series: Starting out with tests The first test and snapshot testing Creating an instance of a component More tests and faking time Jest awesomeness and a skeleton to get started (you're here) Jest watch…
Welcome to part 4 of a series on how to test a Vue component. Here's the table of contents of the series: Starting out with tests The first test and snapshot testing Creating an instance of a component More tests and faking time (you're here) Jest awesomeness and a skeleton to get started More tests…
Welcome to part 3 of a series on how to test a Vue component. Here's the table of contents of the series: Starting out with tests The first test and snapshot testing Creating an instance of a component (you're here) More tests and faking time Jest awesomeness and a skeleton to get started Creating…
Welcome to part 2 of a series on how to test a Vue component. Here's the table of contents of the series: Starting out with tests The first test and snapshot testing (you're here) Creating an instance of a component More tests and faking time Jest awesomeness and a skeleton to get started Setting up…
Recently we released vue-tabs-component, a simple Vue component to render a tabular interface. If you want to know more about to the component itself I suggest you head to the introductory post on it. Like with the vast majority of our open source work this package comes with tests. In this series…
In a new post on his blog Taylor Otwell gives a nice example on how real time facades can make code more testable.
Recently, I worked on some code that surfaced my most common use-case for Laravel 5.4’s “real-time” facades. If you’re not familiar with this feature, it allows you to use any of your application’s classes as a Laravel “facade” on-demand by prefixingFacades to the namespace when importing the class. This is not a feature that is littered throughout my code, but I find it occasionally provides a clean, testable approach to writing expressive object APIs.
https://medium.com/@taylorotwell/expressive-code-real-time-facades-41c442914291
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.)
My colleague Sebastian recently released a new package called phpunit-snapshot-assertions. In a new post on his blog he tells all about it.
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.
https://medium.com/@sebdedeyne/a-package-for-snapshot-testing-in-phpunit-2e4558c07fe3
In an article on the Hackster.io site explains in detail how he made a nice visualization of unit test results. Pretty cool what you can do with a raspberry pi, some LEDS and a couple of scripts.
I wanted to create a desktop gadget to visualize the progress of unit tests run via PHPUnit.I've named this project PHPUnicorn (by combining "PHPUnit" with "Unicorn pHAT").
https://www.hackster.io/colinodell/phpunicorn-visualizing-phpunit-tests-896208
On the JetBrains blog Gary Hockin explains how to easily run a single PHPUnit test.
To run all the tests in a single file, right-click the test in the Project Pane (the left-hand navigation pane), and select Run. To run all the tests in a single class, right-click the class name in the editor, and select Run
. To run the tests in a single method, right-click the method name, and select Run
.
https://blog.jetbrains.com/phpstorm/2017/01/working-with-phpunit-and-phpstorm/
Most of the Laravel apps we create at Spatie will send mails. This can be a password reset mail, a welcome mail after registration, an order confirmation mail, ... One of the things we do is styling such mails so it has the same look and feel as the site it was sent from. When testing such mails our designers had to request a password reset or go through the entire checkout flow just to receive such an order confirmation mail. To make that testing process a lot easier we've created a package called laravel-mailable-test. This package provides an artisan command that can send a mailable to an mail-address.
To send any mailable issue this artisan command:
php artisan mail:send-test "App\Mail\MyMailable" recipient@mail.com
This will send the given mailable to the given email address. The to-, cc- and bcc-addresses that may be set in the given mailable will be cleared. The mail will only be sent to the email address given in the artisan command.
The package will provide a value for any typehinted argument of the constructor of the mailable. If an argument is a int, string or bool the package will generated a value using Faker. Any argument that typehints an Eloquent model will receive the first record of that model.
Image the constructor of your mailable looks like this:
public function __construct(string $title, Order $order)
{
...
}
That constructor will receive a string generated by the sentence method of Faker and the first Order in your database.
The values that are passed to the constructor of the mailable can be customized using the values option of the command.
php artisan mail:send-test "App\Mail\MyMailable" recipient@mail.com --values="title:My title,order:5"
Using this command My title will be passed to $title and an Order with id 5 will be passed to $order.
To learn more about the package head over to the readme on GitHub. Be sure take also take a look at this list of Laravel packages our team has previously made.
Matthieu Napoli, author of PHP-DI and externals.io, wrote a post on how you can use anonymous classes to quickly create mocks, spies and fixtures.
Anonymous classes were added to PHP 7. This article intends to show how useful they can be for writing tests.
In a mail sent to all subscribers on the testdrivenlaravel.com-mailinglist Adam Wathan mentioned a talk Sandi Metz gave a couple of years ago at Rails Conf. It's a really good talk that explains in a clear way when and what you should test.
Tests are supposed to save us money. How is it, then, that many times they become millstones around our necks, gradually morphing into fragile, breakable things that raise the cost of change? We write too many tests and we test the wrong kinds of things. This talk strips away the veil and offers simple, practical guidelines for choosing what to test and how to test it. Finding the right testing balance isn't magic, it's a magic trick; come and learn the secret of writing stable tests that protect your application at the lowest possible cost.
Today we released our newest package: spatie/laravel-uptime-monitor. It's a powerful, easy to configure uptime monitor. It's written in PHP and distributed as a Laravel package. It will notify you when your site is down (and when it comes back up). You can also be notified a few days before an SSL…