In case you didn't notice, recently I have been working in a secret project called Pest: a delightful PHP Testing Framework with a focus on simplicity.
This project is under works, and it will be out soon! I've put together a website where you can subscribe to the @pestphp newsletter. Join now, and be the first to get news about this project: pestphp.com.
In this article, we are going to transform a PHPUnit test into a Pest test.
Disclaimer: PHPUnit is great! In fact, Pest is built on top of it. Pest is just an different API of creating tests.
Let's take this PHPUnit test from the Laravel.io website:
<?php
namespace Tests\Feature;
use Illuminate\Foundation\Testing\DatabaseMigrations;
class HomeTest extends BrowserKitTestCase
{
use DatabaseMigrations;
/** @test */
public function users_can_see_the_homepage()
{
$this->visit('/')
->see('Laravel.io')
->see('The Laravel Community Portal');
}
}
Looking at this code, only 2 things are important:
- There is a home page
- When users visit that home page they see "Laravel.io" and "The Laravel Community Portal".
So let's put what really matters in our test with Pest:
<?php
it('has home page', function () {
$this->visit('/')
->see('Laravel.io')
->see('The Laravel Community Portal');
});
Next, Pest also provides support for higher order messages, which are shortcuts for performing common actions while writing your tests. The best way of thinking about this is: If you don't provide a closure, the chained methods are going to create a closure for you. So let's apply this principle in our test:
<?php
it('has home page')->visit('/')
->see('Laravel.io')
->see('The Laravel Community Portal');
This Pest test is equivalent to the PHPUnit test, and it only has 5 lines, down from the 17 original lines. Most importantly, the test contains only what really matters.
Stay tuned, the early beta version will be out soon!