Laravel Testing

Here we will discuss all of the ways to test your Laravel application.
Laravel Unit Testing
As you already know, unit testing's main purpose is to test particular functions of your application to make sure it works correctly in a lot of different scenarios. A good example would be testing a function that email format for an email field. The idea of unit tests is that they run quickly, and, therefore, it is faster to write unit tests than test that the function works manually.
Lavarel is coming with PHPUnit included out of the box. Moreover, the phpunit.xml
file is already included and built correctly for your application. The unit tests are in Tests/Unit/
folder and designed to test specific isolated functions of an application and execute quickly.
<?php declare(strict_types=1); use PHPUnit\Framework\TestCase; final class StackTest extends TestCase { public function testPushAndPop(): void { $stack = []; $this->assertSame(0, count($stack)); array_push($stack, 'foo'); $this->assertSame('foo', $stack[count($stack)-1]); $this->assertSame(1, count($stack)); $this->assertSame('foo', array_pop($stack)); $this->assertSame(0, count($stack)); } }
Laravel Integration Testing
Integration tests are designed to help to test the integration of different components in action, such as a form, an API, or integration with a 3rd party system, like the one to calculate tax.
- HTTP tests to test APIs described here
- Database test helper functionality described here helps you to do basic things like resetting the DB or seeding it
- Browser tests as described here or here, although browser tests are better to be part of end-to-end tests to provide more value
<?php namespace Tests\Feature; use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Foundation\Testing\WithoutMiddleware; use Tests\TestCase; class ExampleTest extends TestCase { /** * A basic test example. * * @return void */ public function test_a_basic_request() { $response = $this->get('/'); $response->assertStatus(200); } }
<?php namespace Tests\Feature; use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Foundation\Testing\WithoutMiddleware; use Tests\TestCase; class ExampleTest extends TestCase { useRefreshDatabase; /** * A basic functional test example. * * @return void */ public function test_basic_example() { $response = $this->get('/'); // ... } }
We will get into more details of both in the next section.
Laravel End-to-End Testing
End-to-end testing is for testing your flows on your website end-to-end the way end-user's would do it.
We won't go into Selenium-based stuff since it is nightmarishly complex, and you can learn about Selenium everywhere.
Laravel Dusk
<?php namespace Tests\Browser; use App\Models\User; use Illuminate\Foundation\Testing\DatabaseMigrations; use Laravel\Dusk\Chrome; use Tests\DuskTestCase; class ExampleTest extends DuskTestCase { use DatabaseMigrations; /** * A basic browser test example. * * @return void */ public function test_basic_example() { $user = User::factory()->create([ 'email' => '[email protected]', ]); $this->browse(function ($browser) use ($user) { $browser->visit('/login') ->type('email', $user->email) ->type('password', 'password') ->press('Login') ->assertPathIs('/home'); }); } }
You can refer to buttons via text on it, or Dusk/CSS Selectors described here. Unfortunately, there is if you change your code from a button to a-tag, you need to migrate to using click instead of press. Form interaction relies on the same Dusk Selectors or names of the elements.
You can mock things and it is described here. You can mock functions and programmatic constructs as you'd expect to be able to do for a unit test.
- You'll need a way to run your tests - probably a more powerful CI server
- Dusk doesn't cover a lot of important parts of end-to-end tests like email testing or 2FA logins
- You'll constantly need to battle with ChromeDriver getting out of date with the Chrome you have installed on your machine since Chrome auto-updates
testRigor
testRigor is a declarative way of building tests designed purely for end-to-end tests. It is less applicable for unit/integration testing. However, it is orders of magnitude more powerful for end-to-end tests.
- Run those tests on web and mobile browsers with just a config change
- Support email testing, validation, and manipulation (like clicking a link in the email)
- 2FA-based logins with Gmail, SMS, Email, etc.
You can click on elements with click "Submit"
, enter data with enter "Peter" into "First Name"
and validate with check that page contains "Welcome, Peter!"
login check that URL ends with "/home"
login
is a built-in testRigor function that would log you in regardless of how login is implemented. Or, for the sign-up flow, the test might look like this:click "Sign up" generate unique email and enter it into "Email", then save it as "generatedEmail" generate unique name and enter it into "Name", then save it as "generatedName" enter "PasswordSuperSecure" into "Password" click "Submit" check that email to stored value "generatedEmail" was delivered click "Confirm email" check that page contains "Email was confirmed" check that page contains expression "Hello, ${generatedName}"
check that table at the row containing stored value "myId" and column "Status" contains "Paid"
which would work regardless if the table is rendered as an HTML table or if it is rendered as a div-based table.
As you can see, it is very much declarative and completely abstracted from the code of the application. This might be handy in case you need to approve how things work with a customer or involve other people to build tests for your app.