Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/mikerogne/laravel-tag-assertions

Adds useful tag assertions to Laravel's TestResponse class.
https://github.com/mikerogne/laravel-tag-assertions

laravel php phpunit testing

Last synced: 4 days ago
JSON representation

Adds useful tag assertions to Laravel's TestResponse class.

Awesome Lists containing this project

README

        

# Laravel Tag Assertions

[Laravel](https://laravel.com/) ships with a number of awesome features, but one of my favorites is how easy it makes [testing your application](https://laravel.com/docs/master/testing).

Laravel Tag Assertions aims to make the incredible [HTTP tests](https://laravel.com/docs/master/http-tests) functionality that Laravel offers even more powerful by adding useful assertions for HTML tags.

### Motivations

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.

# Installation

```
composer require --dev mikerogne/laravel-tag-assertions
```

Once installed, your TestResponse instances now have access to new assertions. See below for usage & examples.

# Usage

### TestResponse::assertSeeTag(string $selector, array $attributes)

**$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] |

### TestResponse::assertSeeTag(string $selector, $callback)

If you specify a callback, three parameters will be passed to it:

1. **$tag**: This is the name of the tag itself, ie: `button` or `a`.
2. **$attributes**: This is an array of attributes for the tag, ie: `["class" => "btn btn-default"]`.
3. **$content**: This is a string representing the content (innerHtml). Whitespace is included.

### TestResponse::assertSeeTagContent(string $selector, string $content)

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);
```

# Examples

## Form Validation

```html

Contrived Example





First Name



Last Name



Email



Register


```

```php
'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'],
]);
}
}
```

## Vue Component

```html

Another Contrived Example



```

```php
create();

$response = $this->get('/', $data);

$response->assertSeeTagContent('h1', 'Another Contrived Example');

$response->assertSeeTag('blog-posts', [
':posts' => e($posts->toJson()),
]);
}
}
```

## Callback Example

```html

Callback Example



Product Review


Lorem ipsum dolor sit amet, consectetur adipiscing elit.


```

```php
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.');
}
}
```

# License

This code is open-sourced software licensed under the [MIT license](https://opensource.org/licenses/MIT).