Few additional testing assertions for Laravel views

Overview

Laravel View Test Assertions

Few additional assertions for testing Laravel views.

Why

Laravel has well established and documented way of testing requests. However, this is not the case for the views. I always felt that views in Laravel are neglected when it comes to testing, however being confident that form, submit button, and input fields are present is essential.

Granted, you can use Dusk, but it is significantly slower than regular feature tests and adding Dusk as part of the test suite is not always desired.

That's why I created this package. It is my attempt/proposal for adding a bit of TDD concept to the views too. Hope you like it.

Installation

composer require --dev jcergolj/laravel-view-test-assertions

Assertions

assertViewHasForm(string $method = null, string $action = null)
assertFormHasCSRF()
assertFormHasSubmitButton(string $type = 'submit', string $text = null)
assertFormHasDropdown(string $name)
assertFormHasField(string $type, string $name = null)
assertFormHasField(string $type, string $name = null)
assertElementHasChild(string $parentSelector, string $childSelector)
assertFieldHasValidationErrorMsg(string $errorMsg)

Example

View

getLocale()) }}"> Laravel

Form

@csrf
The First Name must only contain letters.
">
// resources/welcome.blade.php


    
        
        

        Laravel
    

    
        

Form

@csrf
The First Name must only contain letters.

Example Test

assertElementHasChild('select[name="age"]', 'option[plaintext="5 Years"]') ->assertElementHasChild('div#parent', 'div.child'); } } ">


namespace Tests\Feature;

use Tests\TestCase;

class ExampleTest extends TestCase
{
    /**
     * A basic test example.
     *
     * @return void
     */
    public function test_example()
    {
        $response = $this->get('/');

        $response->assertStatus(200)
            ->assertViewHasForm()
            ->assertViewHasForm('post')
            ->assertViewHasForm(null, '/users')
            ->assertViewHasForm('post', '/users')
            ->assertFieldHasValidationErrorMsg(trans('validation.alpha', ['attribute' => 'First Name']))
            ->assertFormHasCSRF()
            ->assertFormHasField('text', 'first_name')
            ->assertFormHasField('select', 'age')
            ->assertFormHasDropdown('age')
            ->assertFormHasSubmitButton()
            ->assertElementHasChild('select[name="age"]', 'option[value="5"]')
            ->assertElementHasChild('select[name="age"]', 'option[plaintext="5 Years"]')
            ->assertElementHasChild('div#parent', 'div.child');
    }
}
You might also like...
: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

PHPUnit extension for database interaction testing.

This extension is no longer maintained DbUnit PHPUnit extension for database interaction testing. Installation Composer If you use Composer to manage

Very simple mock HTTP Server for testing Restful API, running via Docker.

httpdock Very simple mock HTTP Server for testing Restful API, running via Docker. Start Server Starting this server via command: docker run -ti -d -p

Comments
  • After latest update, tests with assertFormHasTextInput fail

    After latest update, tests with assertFormHasTextInput fail

    First off, this is such a great and needed idea for laravel testing. Thanks for sharing!

    Next, I don't know if the API has changed or not? I didn't see anything different in the README.

    I updated some other libraries in the project and ran a composer update. I hadn't checked to see if the versions of laravel-view-test-assertions changed or not but I'm guessing it has since the latest version is marked 24 days old (v1.0).

    Now, the same tests, using the same forms, produces a failed test using assertFormHasTextInput:

    e.g. ->assertFormHasTextInput('name')

    Form HTML: <input wire:model="name" type="text" name="name" id="name" autofocus >

    Failed test text:

      Form does not have text field named name.
      Failed asserting that an object is not empty.
    
      at vendor/jcergolj/laravel-view-test-assertions/src/ViewTestAssertions.php:271
        267▕             if ($value !== null && $type !== 'select') {
        268▕                 $filterable .='[value="'.$value.'"]';
        269▕             }
        270▕ 
      ➜ 271▕             Assert::assertNotEmpty($form->filter($filterable), $msg);
        272▕ 
        273▕             return $this;
        274▕         };
        275▕     }
    
          +3 vendor frames 
      4   tests/Feature/SupportingDataControllerTest.php:147
          Illuminate\Testing\TestResponse::__call("assertFormHasTextInput")
    

    Using: "jcergolj/laravel-view-test-assertions": "^1.0",

    Let me know if any more information would be helpful.

    opened by gluis 6
Releases(v2.6)
  • v2.6(Mar 9, 2022)

  • v2.3(Oct 29, 2021)

  • v2.2(Oct 29, 2021)

  • v2.1(Oct 26, 2021)

  • v2.0(Oct 26, 2021)

  • v1.0(Sep 28, 2021)

    View Assertions:

    • assertViewHasForm
    • assertFormHasCSRF
    • assertFormHasSubmitButton
    • assertFormHasTextInput
    • assertFormHasButtonInput
    • assertFormHasColorInput
    • assertFormHasDateInput
    • assertFormHasDateLocalInput
    • assertFormHasEmailInput
    • assertFormHasFileInput
    • assertFormHasHiddenInput
    • assertFormHasImageInput
    • assertFormHasMonthInput
    • assertFormHasNumberInput
    • assertFormHasPasswordInput
    • assertFormHasRangeInput
    • assertFormHasResetInput
    • assertFormHasSearchInput
    • assertFormHasTelInput
    • assertFormHasTextInput
    • assertFormHasUrlInput
    • assertFormHasWeekInput
    • assertFormHasDropdown
    • assertFormHasCheckboxInput
    • assertFormHasRadioInput
    • assertElementHasChild
    • assertFieldHasValidationErrorMsg
    • assertFormHasField
    • assertFormHasField
    • assertElementHasChild
    • assertFieldHasValidationErrorMsg
    Source code(tar.gz)
    Source code(zip)
Owner
null
A set of helpful assertions when testing Laravel applications.

Installation composer require amirrezam75/laravel-assertions I was working on a project and in order to test oauth2 redirection, I ended up with somet

Amir Reza Mehrbakhsh 2 Sep 23, 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
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
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

Codeception Testing Framework 4.6k Jan 7, 2023
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

Infection - Mutation Testing Framework for PHP 1.8k Jan 2, 2023