Polyscope - The agent-first dev environment for Laravel

Laravel Mojito

nunomaduro/laravel-mojito image

Laravel Mojito stats

Downloads
281.4K
Stars
369
Open Issues
2
Forks
15

View on GitHub →

A lightweight package for testing Laravel views.

Mojito example

Build Status Total Downloads Latest Version License

About Mojito

Mojito was created by, and is maintained by Nuno Maduro, and is a lightweight package for testing Laravel views in isolation.

Installation & Usage

Requires PHP 8.0+

Require Mojito using Composer:

composer require nunomaduro/laravel-mojito --dev

How to use:

class WelcomeTest extends TestCase
{
// First, add the `InteractsWithViews` trait to your test case class.
use InteractsWithViews;
 
public function testDisplaysLaravel()
{
// Then, get started with Mojito using the `assertView` method.
$this->assertView('welcome')->contains('Laravel');
}
}

Optionally, you can also perform view testing from your HTTP Tests:

class WelcomeTest extends TestCase
{
public function testDisplaysLaravel()
{
$response = $this->get('/');
 
$response->assertStatus(200);
 
$response->assertView()->contains('Laravel');
}
}

contains

Asserts that the view contains the given text.

$this->assertView('button')->contains('Click me');
$this->assertView('button', ['submitText' => 'Cancel'])->contains('Cancel');
 
$this->assertView('welcome')->in('title')->contains('Laravel');
$this->assertView('welcome')->in('.content')->contains('Nova');

empty

Asserts that the view has no text content.

Note: empty html nodes are not considered in this check.

$this->assertView('empty')->in('.empty-div')->empty();

first

Filters the view and returns only the first element matching the selector.

$this->assertView('welcome')->first('.links a')->contains('Docs');

has

Asserts that the view has the given selector.

$this->assertView('button')->has('button');
 
$this->assertView('welcome')->has('head');
$this->assertView('welcome')->in('body')->has('.content');

hasAttribute

Asserts that the view root element has the given attribute value.

$this->assertView('button')->hasAttribute('attribute', 'value');
$this->assertView('button')->hasAttribute('data-attribute', 'value');
 
$this->assertView('welcome')->hasAttribute('lang', 'en');
$this->assertView('welcome')->in('head')->first('meta')->hasAttribute('charset','utf-8');

hasClass

Asserts that the view has an element with the given class.

$this->assertView('button')->hasClass('btn');
 
$this->assertView('welcome')->in('.content')->at('div > p', 0)->hasClass('title');

hasLink

Asserts that the view has an element with the given link.

$this->assertView('button')->hasLink(route('welcome'));
 
$this->assertView('welcome')->in('.links')->first('a')->hasLink('https://laravel.com/docs');
$this->assertView('welcome')->in('.links')->at('a', 6)->hasLink('https://vapor.laravel.com');
$this->assertView('welcome')->in('.links')->last('a')->hasLink('https://github.com/laravel/laravel');

in

Filters the view and returns only the elements matching the selector.

$this->assertView('welcome')->in('.links a')->contains('Laracast');

last

Filters the view and returns only the last element matching the selector.

$this->assertView('welcome')->last('.links a')->contains('GitHub');

hasMeta

Asserts that the view has a given metatag in the head section.

$response->assertView()->hasMeta(['property' => 'og:title']);
$response->assertView()->hasMeta(['property' => 'og:title', 'content' => 'Laravel']);

Macroable

Feel free to add your own macros to the ViewAssertion::class.

use NunoMaduro\LaravelMojito\ViewAssertion;
 
// Within a service provider:
ViewAssertion::macro('hasCharset', function (string $charset) {
return $this->in('head')->first('meta')->hasAttribute('charset', $charset);
});
 
// In your tests:
$this->assertView('welcome')->hasCharset('utf-8');

Contributing

Thank you for considering to contribute to Mojito. All the contribution guidelines are mentioned here.

You can have a look at the CHANGELOG for constant updates & detailed information about the changes. You can also follow the twitter account for latest announcements or just come say hi!: @enunomaduro

Support the development

Do you like this project? Support it by donating

License

Mojito is an open-sourced software licensed under the MIT license.

nunomaduro photo

Software engineer at @laravel — working on Laravel, Forge, and Vapor. Created @pestphp, @laravel-zero, collision, @openai-php, php insights, termwind, etc.

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.


Nunomaduro Laravel Mojito Related Articles

Vapor UI Dashboard Package image

Vapor UI Dashboard Package

Read article
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
DreamzTech logo

DreamzTech

Hire 6-10+ Yrs. experienced skilled Laravel Developers from DreamzTech. We ensure NDA protected, 100% quality delivery. Contact Us & Discuss Your Need.

DreamzTech
PhpStorm logo

PhpStorm

The go-to PHP IDE with extensive out-of-the-box support for Laravel and its ecosystem.

PhpStorm
CodeKudu logo

CodeKudu

Stand-ups, Retrospectives, and 360° Feedback for the entire team. 50% off with code LARAVELNEWS.

CodeKudu
Typesense Search logo

Typesense Search

Typesense is an open source, blazing-fast search engine, optimized for helping you build delightful search experiences for your sites and apps. Natively integrated with Laravel Scout.

Typesense Search
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit