Testing with Laravel

Just like any other developer, I always believe that testing is a critical part of the development lifecycle. But I also strongly stand that testing should not stand in the way of development itself!

When should you NOT test

Bear with me, as I know that the inner developer in you starts roaring ?

MVP (Minimum Viable Product)

MVP is a term often used in the "startup world". An MVP is where an idea is still being tested. An where it is extremely important to release quickly a working system that provides the core functionality to be presented to both the end-customer (usually a set of ) as well as potential investors.

This MVP is still not business ready and doesn't generate revenue and would most likely need countless modifications before being accepted and move on to the acceptance stage.

As long as the developer is skilled at finding bugs and very reactive at fixing them (We pride ourselves to be those type of developer and you can Contact Us to know more) you will be able to adapt very quickly to the needs of your market

How should you test

Laravel covers all ways you can think of for testing - it is extensively explained in their documentation related to testing: Laravel Testing

You will be able to provide the standard Feature and Unit Tests but I would recommend focusing on the Browser tests provided by Laravel Dusk

Laravel Dusk uses Chrome Driver to run the test in the background the SAME way a user would see it.

A test example

<?php

namespace Tests\Browser;

use Faker\Factory;
use GuzzleHttp\Client;
use function json_decode;
use Laravel\Dusk\Browser;
use function preg_match;
use function quoted_printable_decode;
use Tests\DuskTestCase;

class RegisterTest extends DuskTestCase
{

    protected $user_first_name;
    protected $user_last_name;
    protected $user_email;

    /**
     * Prepare the test setup
     *
     * @return void
     * @throws \Exception
     * @throws \Throwable
     */

    public function setUp(): void
    {
        parent::setUp();

        $faker = Factory::create();
        $this->user_email = $faker->email;
        $this->user_first_name = $faker->firstName;
        $this->user_last_name = $faker->lastName;
    }

    /**
     * Perform the registration
     *
     * @return void
     * @throws \Exception
     * @throws \Throwable
     */
    public function testRegister()
    {
        $this->browse(function (Browser $browser) {

            app()->setLocale('tr');

            // Create the MailHog Client
            $email_client = new Client();
            // Deleting all emails, so that inbox would be empty
            $email_client->delete('http://mailhog:8025/api/v1/messages');

            $browser->logout();
            $browser->visit(app()->getLocale());
            $browser->storeSource('main');
            $browser->waitForText(__('Login'));
            $browser->assertVisible('#nav_login');
            $browser->clickLink(__('Start Now'));
            $browser->assertUrlIs(route('register', ['locale' => app()->getLocale()]));
            $browser->type('first_name', $this->user_first_name);
            $browser->type('last_name', $this->user_last_name);
            $browser->type('email', $this->user_email);
            $browser->type('password', 'password_secured');
            $browser->type('password_confirmation', 'password_secured');
            $browser->press(__('Register'));
            $browser->assertUrlIs(route('register', ['locale' => app()->getLocale()]));
            $browser->assertSee('must be accepted');
            $browser->type('password', 'secured_password');
            $browser->type('password_confirmation', 'secured_password');
            $browser->check('i_agree');
            $browser->press(__('Register'));
            $browser->assertUrlIs(route('verification.notice', ['locale' => app()->getLocale()]));

            $response = $email_client->get('http://mailhog:8025/api/v1/messages');
            $body = quoted_printable_decode(json_decode((string)$response->getBody())[0]->Content->Body);

            // Extract the URL from the body
            preg_match('#\[(http:.+?verify.+?)\]#', $body, $match);
            $verify_url = end($match);

            $browser->visit($verify_url);
            $browser->waitForText(__('My Dashboard'));
            $browser->pause(1000);
            // $browser->storeSource('verified');
            $browser->screenshot('verified');
            $browser->assertSee("{$this->user_first_name} {$this->user_last_name}");
        });
    }
}

Leave a comment


7 kogus

1 an ago

This is so fun with being able to choose your own colors and patterns and whatnot. What a great idea to repurpose a coffee can this way.

watch

1 an ago

Thanks for sharing, this is a fantastic blog. Really thank you! Awesome.

Copyright 2017 FRESHFACE © All Rights Reserved