mikerogne/laravel-tag-assertions 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

mikerogne/laravel-tag-assertions

Composer 安装命令:

composer require mikerogne/laravel-tag-assertions

包简介

Adds useful tag assertions to Laravel's TestResponse class.

README 文档

README

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.

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

<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 Component

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

Callback Example

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

License

This code is open-sourced software licensed under the MIT license.

mikerogne/laravel-tag-assertions 适用场景与选型建议

mikerogne/laravel-tag-assertions 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.06k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2019 年 12 月 13 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 mikerogne/laravel-tag-assertions 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 mikerogne/laravel-tag-assertions 我们能提供哪些服务?
定制开发 / 二次开发

基于 mikerogne/laravel-tag-assertions 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 1.06k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 5
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-12-13