mikerogne/laravel-tag-assertions

为Laravel的TestResponse类添加有用的标签断言。

2.0.2 2021-02-21 05:28 UTC

README

Laravel附带了许多令人惊叹的功能,但其中我最喜欢的是它使测试应用程序变得非常容易。

Laravel Tag Assertions旨在通过添加有用的HTML标签断言,使Laravel提供的令人难以置信的HTTP测试功能更加强大。

动机

我经常想要断言响应包含某些元素(例如:具有特定属性的Vue组件),但又不希望换行符和其他空白字符影响结果。使用像$response->assertSee(...)这样的方法并不适合这种特定的用例。Laravel Dusk也不是一个理想的选择,因为它可能运行缓慢,有时也很脆弱。

安装

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

安装完成后,您的TestResponse实例现在可以访问新的断言。下面是用法和示例。

用法

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

$selector是要匹配的标签名称。您可以根据需要尽可能具体。 $attributes是标签必须具有的属性数组。

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

如果您指定了一个回调,则会将三个参数传递给它

  1. $tag: 这是标签本身的名称,例如:buttona
  2. $attributes: 这是标签的属性数组,例如:["class" => "btn btn-default"]
  3. $content: 这是表示内容的字符串(innerHtml)。包括空白字符。

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

有时我们只关心页面上是否存在具有特定内容的标签。这种用例的一个常见例子是文本区域字段。

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

Vue组件

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

许可证

此代码是开源软件,受MIT许可证许可。