Skip to content

Instantly share code, notes, and snippets.

@gwleuverink
Last active January 8, 2024 22:03
Show Gist options
  • Save gwleuverink/136569d01ce3473bf3d6a52ad52f0443 to your computer and use it in GitHub Desktop.
Save gwleuverink/136569d01ce3473bf3d6a52ad52f0443 to your computer and use it in GitHub Desktop.
Dusk method for testing Blade
//--------------------------------------------------------------------------
// NOTE: This example uses Testbench Dusk inside of a package
//
// The `beforeServingApplication` hook doesn't exist in laravel dusk
// But i'm sure you can create a temporary route some other way
// when using this in a laravel project
//--------------------------------------------------------------------------
class DuskTestCase extends BaseTestCase
{
/**
* Renders a string into blade & navigates the Dusk browser to a temporary route
* Then it returns the Browser object to continue chaining assertions.
*/
public function blade(string $blade) {
// Wrap in basic HTML layout (include required js & css there if needed)
$blade = <<< BLADE
<x-layout>
$blade
</x-layout>
BLADE;
// Render the blade
$page = Blade::render($blade);
// Create a temporary route
$this->beforeServingApplication(
fn ($app) => $app->make(Route::class)::get('test-blade', fn() => $page)
);
// Point Dusk to the temporary route & return the Browser for chaining
$return = null;
$this->browse(function (Browser $browser) use (&$return) {
$return = $browser->visit('test-blade');
});
return $return;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment