Laravel ships with a number of awesome features, but one of my favorites is how easy it makes testing your application.
Laravel Tag Assertions aims to make the incredible HTTP tests functionality that Laravel offers even more powerful by adding useful assertions for HTML tags.
Frequently I've wanted to assert a response contains certain elements (ie: Vue component with certain props), but didn't want newlines and other whitespace to matter. Using methods like $response->assertSee(...)
is not ideal for this particular use-case. Laravel Dusk wasn't a desirable option either because it can be slow and sometimes fragile.
composer require --dev mikerogne/laravel-tag-assertions
Once installed, your TestResponse instances now have access to new assertions. See below for usage & examples.
$selector is the name of a tag you want to match. You can get as specific as you want. $attributes is either an array of attributes that the tag must have.
Simple | More Specific |
---|---|
button | button.btn.btn-default |
a | a[role=tab] |
If you specify a callback, three parameters will be passed to it:
button
or a
.["class" => "btn btn-default"]
.Sometimes we only care that a tag with specific content is on the page. A common use-case for this is a textarea field.
$response->assertSeeTagContent('textarea[name=about]', $user->about);
<body>
<h1>Contrived Exampleh1>
<form>
<p>
<label>First Namelabel>
<input type="text" name="first_name" value="{{ old('first_name') }}">
p>
<p>
<label>Last Namelabel>
<input type="text" name="last_name" value="{{ old('last_name') }}">
p>
<p>
<label>Emaillabel>
<input type="text" name="email" value="{{ old('email') }}">
p>
<p>
<button type="submit">Registerbutton>
p>
form>
body>
namespace TestsFeature;
class ExampleTest extends TestCase
{
/** @test */
public function uses_old_input_when_validation_fails()
{
$data = [
'first_name' => 'John',
'last_name' => 'Doe',
'email' => '', // oops!
];
$response = $this->post('/register', $data);
$response->assertSeeTag('input[name=first_name]', [
'value' => $data['first_name'],
]);
$response->assertSeeTag('input[name=last_name]', [
'value' => $data['last_name'],
]);
}
}
<body>
<h1>Another Contrived Exampleh1>
<blog-posts
:posts="{{ $posts->toJson() }}"
>blog-posts>
body>
namespace TestsFeature;
class VueTest extends TestCase
{
/** @test */
public function lists_blog_posts()
{
$posts = factory(AppPost::class, 5)->create();
$response = $this->get('/', $data);
$response->assertSeeTagContent('h1', 'Another Contrived Example');
$response->assertSeeTag('blog-posts', [
':posts' => e($posts->toJson()),
]);
}
}
<body>
<h1>Callback Exampleh1>
<h2 class="section-title" data-foobar="bazburk">
Product Review
h2>
<p class="summary">Lorem ipsum dolor sit amet, consectetur adipiscing elit.p>
body>
namespace TestsFeature;
class CallbackTest extends TestCase
{
/** @test */
public function shows_product_review()
{
$response = $this->get('/', $data);
$response->assertSeeTag('h2', function($tag, $attributes, $content) {
// $tag -> "h2"
// $attributes -> ['class' => 'section-title', 'data-foobar' => 'bazburk']
// $content -> Product Review (but including the whitespace!)
return IlluminateSupportStr::contains($content, 'Product Review');
});
$response->assertSeeTagContent('p.summary', 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.');
}
}
This code is open-sourced software licensed under the MIT license.