A set of helpful assertions when testing Laravel applications.

Overview

Installation

composer require amirrezam75/laravel-assertions

I was working on a project and in order to test oauth2 redirection, I ended up with something like this:

assertRedirection

Before

$response = $this->get('integrations/calendars?provider=GOOGLE')
            ->assertStatus(302);

$location = $response->headers->get('location');
$query = parse_url($location, PHP_URL_QUERY);
parse_str($query, $params);

$this->assertEquals(secure_url('integrations/calendars/callback'), $params['redirect_uri']);
$this->assertStringContainsString('https://accounts.google.com/o/oauth2/auth', $location);
$this->assertEquals('https://www.googleapis.com/auth/calendar', $params['scope']);
$this->assertEquals(config('services.google.client_id'), $params['client_id']);
$this->assertEquals('true', $params['include_granted_scopes']);
$this->assertEquals('offline', $params['access_type']);
$this->assertEquals('code', $params['response_type']);
$this->assertTrue(strlen($params['state']) === 40);
$this->assertEquals('consent', $params['prompt']);

I think it would be cleaner and much more readable if have something similar to AssertableJson class.

After

$response = $this->get('integrations/calendars?provider=GOOGLE')
    ->assertRedirection(function (AssertableUri $uri) {
        $uri
            ->whereQuery('redirect_uri', secure_url('integrations/calendars/callback'))
            ->whereQuery('scope', 'https://www.googleapis.com/auth/calendar')
            ->whereQuery('client_id', config('services.google.client_id'))
            ->whereQuery('include_granted_scopes', 'true')
            ->whereQuery('access_type', 'offline')
            ->whereQuery('response_type', 'code')
            ->whereQuery('prompt', 'consent')
            ->whereQuery('state', function($state) {
                return strlen($state) === 40;
            });

    });

We can assert other parts of URI using these methods:

  • whereFragment($value)
  • whereHost($value)
  • wherePass($value)
  • wherePath($value)
  • wherePort($value)
  • whereScheme($value)
  • whereUser($value)

IMO It's not practical but we can only check for existence of component using these methods:

  • hasFragment()
  • hasHost()
  • hasPass()
  • hasPath()
  • hasPort()
  • hasScheme()
  • hasUser()

etc()

Like AssertableJson it uses Interaction trait, this will automatically fail your test when you haven't interacted with at least one of the props in a URI query string. Hence the sequence is fixed in other parts of URI; they don't need to be affected by interacted method and can be asserted using assertRedirectContains method.

$assert = new AssertableUri('https://foo.bar?name=Taylor&id=1');
        $assert->whereQuery('name', 'Taylor')
            ->etc() // If remove this line, this will fail
            ->interacted();
You might also like...
Full-stack testing PHP framework

Codeception Modern PHP Testing for everyone Codeception is a modern full-stack testing framework for PHP. Inspired by BDD, it provides an absolutely n

AST based PHP Mutation Testing Framework

Infection - Mutation Testing framework Please read documentation here: infection.github.io Twitter: @infection_php Discord: https://discord.gg/ZUmyHTJ

:computer: Parallel testing for PHPUnit

ParaTest The objective of ParaTest is to support parallel testing in PHPUnit. Provided you have well-written PHPUnit tests, you can drop paratest in y

The PHP Unit Testing framework.

PHPUnit PHPUnit is a programmer-oriented testing framework for PHP. It is an instance of the xUnit architecture for unit testing frameworks. Installat

PHP unit testing framework with built in mocks and stubs. Runs in the browser, or via the command line.

Enhance PHP A unit testing framework with mocks and stubs. Built for PHP, in PHP! Quick Start: Just add EnhanceTestFramework.php and you are ready to

Unit testing tips by examples in PHP

Unit testing tips by examples in PHP Introduction In these times, the benefits of writing unit tests are huge. I think that most of the recently start

Magic Test allows you to write browser tests by simply clicking around on the application being tested, all without the slowness of constantly restarting the testing environment.

Magic Test for Laravel Magic Test allows you to write browser tests by simply clicking around on the application being tested, all without the slownes

Pest is an elegant PHP Testing Framework with a focus on simplicity
Pest is an elegant PHP Testing Framework with a focus on simplicity

Pest is an elegant PHP Testing Framework with a focus on simplicity. It was carefully crafted to bring the joy of testing to PHP. Explore the docs: pe

PHP libraries that makes Selenium WebDriver + PHPUnit functional testing easy and robust
PHP libraries that makes Selenium WebDriver + PHPUnit functional testing easy and robust

Steward: easy and robust testing with Selenium WebDriver + PHPUnit Steward is a set of libraries made to simplify writing and running robust functiona

Releases(1.0.0)
Owner
Amir Reza Mehrbakhsh
PHP Developer
Amir Reza Mehrbakhsh
vfsStream is a stream wrapper for a virtual file system that may be helpful in unit tests to mock the real file system. It can be used with any unit test framework, like PHPUnit or SimpleTest.

vfsStream vfsStream is a stream wrapper for a virtual file system that may be helpful in unit tests to mock the real file system. It can be used with

null 1.4k Dec 23, 2022
Additional PHPUnit assertions and helper functions

Jasny PHPUnit extension Additional functionality for PHPUnit. Callback mock - assert that callback is called with correct arguments. Safe mocks - disa

Arnold Daniels 2 Jul 24, 2022
SimpleTest is a framework for unit testing, web site testing and mock objects for PHP

SimpleTest SimpleTest is a framework for unit testing, web site testing and mock objects for PHP. Installation Downloads All downloads are stored on G

SimpleTest 147 Jun 20, 2022
Infrastructure and testing helpers for creating CQRS and event sourced applications.

Broadway is a project providing infrastructure and testing helpers for creating CQRS and event sourced applications. Broadway tries hard to not get in your way.

null 1.5k Dec 30, 2022
Real-world Project to learning about Unit Testing/TDD with Laravel for everybody

KivaNote - a Laravel TDD Sample Project Let me introduce you to KivaNote, a simple real-world application using Laravel to show you how the TDD & Unit

(Seth) Phat Tran 10 Dec 31, 2022
Package for unit testing Laravel form request classes

Package for unit testing Laravel form request classes. Why Colin DeCarlo gave a talk on Laracon online 21 about unit testing Laravel form requests cla

null 18 Dec 11, 2022
A drop in fake logger for testing with the Laravel framework.

Log fake for Laravel A bunch of Laravel facades / services are able to be faked, such as the Dispatcher with Bus::fake(), to help with testing and ass

Tim MacDonald 363 Dec 19, 2022
A video course for laravel artisan to learn creating API using testing

About Laravel Laravel is a web application framework with expressive, elegant syntax. We believe development must be an enjoyable and creative experie

Bitfumes 16 Oct 6, 2022
The modern, simple and intuitive PHP unit testing framework.

atoum PHP version atoum version 5.3 -> 5.6 1.x -> 3.x 7.2 -> 8.x 4.x (current) A simple, modern and intuitive unit testing framework for PHP! Just lik

atoum 1.4k Nov 29, 2022