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:
- $tag: This is the name of the tag itself, ie:
button
ora
. - $attributes: This is an array of attributes for the tag, ie:
["class" => "btn btn-default"]
. - $content: This is a string representing the content (innerHtml). Whitespace is included.
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 Example</h1>
<form>
<p>
<label>First Name</label>
<input type="text" name="first_name" value="{{ old('first_name') }}">
</p>
<p>
<label>Last Name</label>
<input type="text" name="last_name" value="{{ old('last_name') }}">
</p>
<p>
<label>Email</label>
<input type="text" name="email" value="{{ old('email') }}">
</p>
<p>
<button type="submit">Register</button>
</p>
</form>
</body>
<?php
namespace Tests\Feature;
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 Example</h1>
<blog-posts
:posts="{{ $posts->toJson() }}"
></blog-posts>
</body>
<?php
namespace Tests\Feature;
class VueTest extends TestCase
{
/** @test */
public function lists_blog_posts()
{
$posts = factory(\App\Post::class, 5)->create();
$response = $this->get('/', $data);
$response->assertSeeTagContent('h1', 'Another Contrived Example');
$response->assertSeeTag('blog-posts', [
':posts' => e($posts->toJson()),
]);
}
}
<body>
<h1>Callback Example</h1>
<!-- notice the whitespace in the h2's content -->
<h2 class="section-title" data-foobar="bazburk">
Product Review
</h2>
<p class="summary">Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
</body>
<?php
namespace Tests\Feature;
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 \Illuminate\Support\Str::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.