• Stars
    star
    373
  • Rank 114,600 (Top 3 %)
  • Language
    PHP
  • License
    MIT License
  • Created almost 5 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

🍹 A lightweight package for testing Laravel views in isolation

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.

More Repositories

1

phpinsights

πŸ”° Instant PHP quality checks from your console
PHP
5,265
star
2

larastan

βš—οΈ Adds code analysis to Laravel improving developer productivity and code quality.
PHP
4,882
star
3

collision

πŸ’₯ Collision is a beautiful error reporting tool for command-line applications
PHP
4,497
star
4

termwind

πŸƒ In short, it's like Tailwind CSS, but for the PHP command-line applications.
PHP
2,269
star
5

laravel-console-menu

πŸ”˜ Beautiful PHP CLI menus. Is a php-school/cli-menu wrapper for Laravel/Artisan Console Commands
PHP
794
star
6

skeleton-php

⚑️ This package provides a wonderful PHP skeleton to start building your next package idea.
PHP
647
star
7

laravel-desktop-notifier

πŸ’» Send notifications to your desktop from your Laravel Artisan Commands. An JoliNotif wrapper for Laravel.
PHP
415
star
8

pest

This repository contains an old version of PEST. A new and better version is being coded in private and will be out soon: https://github.com/pestphp/pest.
343
star
9

patrol

Patrol is an elegant command-line tool that keeps your PHP Project's dependencies in check.
PHP
266
star
10

laravel-console-task

βœ… Laravel Console Task is a output method for Laravel Console Commands.
PHP
252
star
11

awesome-php-src

πŸš€ A curated list of awesome resources related to PHP source code
224
star
12

curryable

An elegant and simple curry(f) implementation in PHP.
PHP
175
star
13

dd

✨ The most popular way of debugging in PHP is now available in JavaScript.
JavaScript
159
star
14

yorn

βš—οΈ Modules in PHP with the `import` and `export` syntax
PHP
146
star
15

laravel-console-dusk

Laravel Console Dusk allows the usage of Laravel Dusk in Laravel/Laravel Zero artisan commands.
PHP
144
star
16

laracon-schedule

πŸ‘¨πŸ»β€πŸš€ A command-line tool that gives you the @LaraconOnline schedule in your timezone. πŸš€
PHP
101
star
17

laravel-pot

Provides Artisan commands to inspect Laravel Application's container. πŸͺ΄
PHP
88
star
18

mock-final-classes

πŸ„β€β™‚οΈ Allows mocking final classes in PHP.
PHP
83
star
19

awesome-laravel-vapor

πŸš€ A curated list of awesome resources related to Laravel Vapor.
80
star
20

laravel-console-summary

πŸ“– Beautiful Laravel Console Summary for Artisan or Laravel Zero.
PHP
62
star
21

collision-adapter-symfony

Collision's adapter for Symfony applications. Error Reporting for console/command-line PHP applications.
PHP
45
star
22

dig

πŸ‘¨πŸ»β€πŸŽ¨ A beautiful debug tool for the command line.
PHP
40
star
23

laravel-any

πŸ“ Laravel collection macro that determine if `any` item from the collection passes the given truth test.
PHP
38
star
24

nunomaduro.com

Rust
34
star
25

skeleton-c

⚑️ This package provides a wonderful C skeleton to start building your next package idea.
Makefile
33
star
26

php-interminal

PHP Interminal is a command-line tool that gives you access to PHP Internals discussions in your terminal.
PHP
32
star
27

bombe

Bombe is a tool for benchmarking the given url response status and response time.
C
23
star
28

skeleton-js

πŸ‘¨πŸ»β€πŸ’» A skeleton repository for my open source JavaScript packages
JavaScript
21
star
29

elegant-sublime

πŸ‘”An collection of sublime text preferences
17
star
30

pint-strict-preset

Pint strict preset is an insanely defensive coding style preset for those who demand meticulous precision in their projects.
11
star
31

alpine-day-schedule

πŸ‘¨πŸ»β€πŸš€ A command-line tool that gives you the Alpine Day 2021 schedule in your timezone. πŸš€
PHP
11
star
32

talks

🎀 My talks about PHP, Laravel or side projects of mine
10
star
33

laravel-meetups

πŸ•It's Meetup time! Find out Laravel Meetups near you!
PHP
8
star
34

laravel-console-spinner

❃ A spinning activity indicator for Laravel/Laravel Zero artisan commands.
7
star
35

laravel-optimize-database

πŸ’¨ Publishes migrations that make your sqlite database production ready. it's a work-in-progress
PHP
6
star
36

laravel-9-test

PHP
5
star
37

forge-octane

PHP
5
star
38

friend.com

Blade
3
star
39

scout-extended-demo

PHP
3
star
40

skeleton-typescript

βš—οΈ The perfect starting for typescript libraries.
3
star
41

laravel-zero-weather

PHP
3
star
42

nextjs-blog-wqdqwdqwd

JavaScript
2
star
43

dotfiles

2
star
44

twitter-stream

An demo application of Laravel Zero that works with the Twitter Streaming API
PHP
2
star
45

skeleton-laravel

Just my Laravel skeleton
2
star
46

learn-javascript

2
star
47

my-site

HTML
1
star
48

vapor-github-test

PHP
1
star
49

qwdpowkqdopwqkd

JavaScript
1
star
50

skeleton-php-1

My PHP Package Skeleton
PHP
1
star
51

exclude-dev-files-bug

PHP
1
star
52

kpop-stack

TypeScript
1
star
53

nunomaduro

1
star
54

helpers-tap

🚰 Tap that method
TypeScript
1
star
55

test

HTML
1
star
56

perna

πŸ›‹ Optimises the workflow around managing multi-package repositories.
1
star
57

demo-p

PHP
1
star
58

laravel-collections-examples

PHP
1
star
59

test-cs

PHP
1
star