PHP code example of soyhuce / laravel-embuscade

1. Go to this page and download the library: Download soyhuce/laravel-embuscade library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

soyhuce / laravel-embuscade example snippets


$this->get('/')->expectView()->toContain('Laravel');

$this->view('menu', ['links' => $links])
    ->expectView()
    ->in('.links')
    ->first('a')
    ->toHave('href', 'https://laravel.com/docs')
    ->toHaveClass('btn')
    ->toHaveText('Documentation');

use Soyhuce\LaravelEmbuscade\ViewExpect;

new ViewExpect($html);

// From a TestResponse 
$expect = $this->get('/')->expectView(); 

// From a TestView
$expect = $this->view('home', ['links' => $links])->expectView();

// From a TestComponent
$expect = $this->component(Home::class, ['links' => $links])->expectView();

$expect = Livewire::test(HomePage::class, ['links' => $links])->expectView();

 // Selects all elements matching the CSS selector
$expect->in($cssSeclector);
 // Selects the nth element matching the CSS selector, index starts at 0 !
$expect->at($cssSelector, $index);
 // Selects the first element matching the CSS selector
$expect->first($cssSelector);
 // Selects the last element matching the CSS selector
$expect->last($cssSeclector);
 // Selects the only element matching the CSS selector
$expect->sole($cssSelector);

$expect->sole('@login-button')
    ->...

> @foreach ($array as $key => $value)
>     <div @embuscade('login-button-{{ $key }}')>{{ $value}}</div>
> @endforeach
> 

use Soyhuce\LaravelEmbuscade\Embuscade;

Embuscade::selectorHtmlAttribute('data-test'); // or 'dusk' if you use Dusk and want to leverage existing Dusk selectors.

if (!class_exists(EmbuscadeServiceProvider::class)) {
    $this->app->get('blade.compiler')->prepareStringsForCompilationUsing(
        fn (string $input) => preg_replace( '/@embuscade\\(\'([^)]+)\'\\)/x', '', $input)
    );
}

// Expects the view to contain at least an element matching the CSS selector
$expect->toHave('.links a');
// Expects the view to contain exactly n elements matching the CSS selector
$expect->toHave('.links a', 2);
// Expects the view to contain at least one element pointing to $link
$expect->toHaveLink('https://laravel.com/docs');
// Expect the view contains a meta tag with the given attributes in head section
$expect->toHaveMeta(['property' => 'og:title', 'content' => 'Laravel']);
// Expect the view text equals given text
$expect->toHaveText('Laravel');
// Expect the view text contains given text
$expect->toContainText('Documentation');
// Expect the view text is empty
$expect->toBeEmpty();
// Expect the view html contains given content
$expect->toContain('<a href="https://laravel.com/docs">Documentation</a>');

// Expect the element to have the given attribute
$expect->toHaveAttribute('disabled');
// Expect the element to have the given attribute with the given value
$expect->toHaveAttribute('href', 'https://laravel.com/docs');
// Expect the element to have the given attribute containing the given value
$expect->toHaveAttributeContaining('class', 'btn');

$expect->toAccept($value); // same as toHaveAttribute('accept', $value)
$expect->toBeDisabled(); // same as toHaveAttribute('disabled')
$expect->toHaveAlt($value); // same as toHaveAttribute('alt', $value)
$expect->toHaveClass($value); // same as toHaveAttributeContaining('class', $value)
$expect->toHaveHref($value); // same as toHaveAttribute('href', $value)
$expect->toHaveSrc($value); // same as toHaveAttribute('src', $value)
$expect->toHaveValue($value); // same as toHaveAttribute('value', $value)

$expect->not->toHave('.links a');
$expect->not->toBeDisabled();
 
$this->view('menu', ['links' => $links])
    ->expectView()
    ->in('.links')
    ->first('a')
    ->toHave('href', 'https://laravel.com/docs')
    ->not->toHaveAttribute('target')
    ->toHaveClass('btn')
    ->toHaveText('Documentation');

use Soyhuce\LaravelEmbuscade\ViewExpect;

$this->view('test')
    ->expectView()
    ->toBeDisabled()
    ->sole('legend', fn(ViewExpect $expect) => $expect->toHaveText('Disabled fieldset'))
    ->at('p input', 0, fn(ViewExpect $expect) => $expect->toHaveAttribute('type', 'radio'))
    ->at('p input', 1, fn(ViewExpect $expect) => $expect->toHaveAttribute('type', 'number'));

ViewExpect::macro('toHaveCharset', function (string $charset) {
        return $this->in('head')->first('meta')->toHaveAttribute('charset', $charset);
    });
});

$this->view('home')->expectView()->toHaveCharset('utf-8');

$this->view('home')->expectView()->in('a')->dump();