Turn your manual testers into automation experts Request a DemoStart testRigor Free

Laravel Testing

Laravel Testing
Laravel is one of the most popular PHP frameworks. However, it is not without its limitations when it comes to testing.

Here we will discuss all of the ways to test your Laravel application.

As always, there are 3 ways to test any web application:
  1. Unit Testing
  2. Integration Testing
  3. End-to-End Testing

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.

A sample test could look like this for a stack test:
<?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.

There are several main types of tests you might want to have for that:
  1. HTTP tests to test APIs described here
  2. Database test helper functionality described here helps you to do basic things like resetting the DB or seeding it
  3. Browser tests as described here or here, although browser tests are better to be part of end-to-end tests to provide more value
An example of the HTTP test would be something like this:
<?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);
  }
}
An example of a database refresh would look like this:
<?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('/');

    // ...
  }
}
Finally, there are two ways of testing web:
  1. With Laravel Dusk
  2. With testRigor

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.

There are several ways how to do it:
  1. Laravel Dusk
  2. Any Selenium-based framework out there
  3. testRigor

We won't go into Selenium-based stuff since it is nightmarishly complex, and you can learn about Selenium everywhere.

Laravel Dusk

Dusk is an abstraction on top of ChromeDriver to build tests in code. Before using it, you need to follow the installation instructions here. A sample code will look like this:
<?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.

Overall, it looks like a nice framework, better than most of the out there. However, it is coding, and you will suffer from the following:
  1. You'll need a way to run your tests - probably a more powerful CI server
  2. Dusk doesn't cover a lot of important parts of end-to-end tests like email testing or 2FA logins
  3. 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.

testRigor allows you to build web tests and also allows you to:
  1. Run those tests on web and mobile browsers with just a config change
  2. Support email testing, validation, and manipulation (like clicking a link in the email)
  3. 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!"

In testRigor you don't write code but rather a specification of how your application is supposed to work. For example, changing a button to an a-tag won't break the test. A test that is similar to the one provided for Dusk above would look like this:
login
check that URL ends with "/home"
Where 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}"
Finally, testRigor works differently with forms and tables. It simulates how a user would express the steps. Therefore it would, for instance, associate labels with inputs so that you can always refer to input by some visible text like a label (or text that looks like a label) or a placeholder. For the tables, it helps even more by enabling things like:
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.

Join the next wave of functional testing now.
A testRigor specialist will walk you through our platform with a custom demo.