stevegrunwell/phpunit-markup-assertions
Composer 安装命令:
composer require --dev stevegrunwell/phpunit-markup-assertions
包简介
Assertions for PHPUnit to verify the presence or state of elements within markup
README 文档
README
This library introduces the MarkupAssertionsTrait trait for use in PHPUnit tests.
These assertions enable you to inspect generated markup without having to muddy tests with DOMDocument or nasty regular expressions. If you're generating markup at all with PHP, the PHPUnit Markup Assertions trait aims to make the output testable without making your tests fragile.
Example
use PHPUnit\Framework\TestCase; use SteveGrunwell\PHPUnit_Markup_Assertions\MarkupAssertionsTrait; class MyUnitTest extends TestCase { use MarkupAssertionsTrait; /** * Ensure the #first-name and #last-name selectors are present in the form. */ public function testRenderFormContainsInputs() { $markup = render_form(); $this->assertContainsSelector('#first-name', $markup); $this->assertContainsSelector('#last-name', $markup); } }
Installation
To add PHPUnit Markup Assertions to your project, first install the library via Composer:
$ composer require --dev stevegrunwell/phpunit-markup-assertions
Next, import the SteveGrunwell\PHPUnit_Markup_Assertions\MarkupAssertionsTrait trait into each test case that will leverage the assertions:
use PHPUnit\Framework\TestCase; use SteveGrunwell\PHPUnit_Markup_Assertions\MarkupAssertionsTrait; class MyTestCase extends TestCase { use MarkupAssertionsTrait; }
Making PHPUnit Markup Assertions available globally
If you'd like the methods to be available across your entire test suite, you might consider sub-classing the PHPUnit test case and applying the trait there:
# tests/TestCase.php namespace Tests; use PHPUnit\Framework\TestCase as BaseTestCase; use SteveGrunwell\PHPUnit_Markup_Assertions\MarkupAssertionsTrait; class TestCase extends BaseTestCase { use MarkupAssertionsTrait; }
Then update your other test cases to use your new base:
# tests/Unit/ExampleTest.php namespace Tests/Unit; use Tests\TestCase; class MyUnitTest extends TestCase { // This class now automatically has markup assertions. }
Available methods
These are the assertions made available to PHPUnit via the MarkupAssertionsTrait.
assertContainsSelector()assertNotContainsSelector()assertSelectorCount()assertHasElementWithAttributes()assertNotHasElementWithAttributes()assertElementContains()assertElementNotContains()assertElementRegExp()assertElementNotRegExp()
assertContainsSelector()
Assert that the given string contains an element matching the given selector.
- (string) $selector
- A query selector for the element to find.
- (string) $markup
- The markup that should contain the
$selector. - (string) $message
- A message to display if the assertion fails.
Example
public function testBodyContainsImage() { $body = getPageBody(); $this->assertContainsSelector('img', $body, 'Did not find an image in the page body.'); }
assertNotContainsSelector()
Assert that the given string does not contain an element matching the given selector.
This method is the inverse of assertContainsSelector().
- (string) $selector
- A query selector for the element to find.
- (string) $markup
- The markup that should not contain the
$selector. - (string) $message
- A message to display if the assertion fails.
assertSelectorCount()
Assert the number of times an element matching the given selector is found.
- (int) $count
- The number of matching elements expected.
- (string) $selector
- A query selector for the element to find.
- (string) $markup
- The markup to run the assertion against.
- (string) $message
- A message to display if the assertion fails.
Example
public function testPostList() { factory(Post::class, 10)->create(); $response = $this->get('/posts'); $this->assertSelectorCount(10, 'li.post-item', $response->getBody()); }
assertHasElementWithAttributes()
Assert that an element with the given attributes exists in the given markup.
- (array) $attributes
- An array of HTML attributes that should be found on the element.
- (string) $markup
- The markup that should contain an element with the provided
$attributes. - (string) $message
- A message to display if the assertion fails.
Example
public function testExpectedInputsArePresent() { $user = getUser(); $form = getFormMarkup(); $this->assertHasElementWithAttributes( [ 'name' => 'first-name', 'value' => $user->first_name, ], $form, 'Did not find the expected input for the user first name.' ); }
assertNotHasElementWithAttributes()
Assert that an element with the given attributes does not exist in the given markup.
- (array) $attributes
- An array of HTML attributes that should not be found on the element.
- (string) $markup
- The markup that should not contain an element with the provided
$attributes. - (string) $message
- A message to display if the assertion fails.
assertElementContains()
Assert that the element with the given selector contains a string.
- (string) $contents
- The string to look for within the DOM node's contents.
- (string) $selector
- A query selector for the element to find.
- (string) $markup
- The markup that should contain the
$selector. - (string) $message
- A message to display if the assertion fails.
Example
public function testColumnShowsUserEmail() { $user = getUser(); $table = getTableMarkup(); $this->assertElementContains( $user->email, 'td.email', $table, 'The <td class="email"> should contain the user\'s email address.' ); }
assertElementNotContains()
Assert that the element with the given selector does not contain a string.
This method is the inverse of assertElementContains().
- (string) $contents
- The string to look for within the DOM node's contents.
- (string) $selector
- A query selector for the element to find.
- (string) $markup
- The markup that should contain the
$selector. - (string) $message
- A message to display if the assertion fails.
assertElementRegExp()
Assert that the element with the given selector contains a string.
This method works just like assertElementContains(), but uses regular expressions instead of simple string matching.
- (string) $regexp
- The regular expression pattern to look for within the DOM node.
- (string) $selector
- A query selector for the element to find.
- (string) $markup
- The markup that should contain the
$selector. - (string) $message
- A message to display if the assertion fails.
assertElementNotRegExp()
Assert that the element with the given selector does not contain a string.
This method is the inverse of assertElementRegExp() and behaves like assertElementNotContains() except with regular expressions instead of simple string matching.
- (string) $regexp
- The regular expression pattern to look for within the DOM node.
- (string) $selector
- A query selector for the element to find.
- (string) $markup
- The markup that should contain the
$selector. - (string) $message
- A message to display if the assertion fails.
stevegrunwell/phpunit-markup-assertions 适用场景与选型建议
stevegrunwell/phpunit-markup-assertions 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 156.4k 次下载、GitHub Stars 达 16, 最近一次更新时间为 2017 年 10 月 25 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「testing」 「phpunit」 「dom」 「markup」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 stevegrunwell/phpunit-markup-assertions 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 stevegrunwell/phpunit-markup-assertions 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 stevegrunwell/phpunit-markup-assertions 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A library that provides basic utilities for creating HTML documents.
HiDev plugin for PHPUnit
A bridge between SimpleXML and the DOM extension, plus a bunch of convenience methods
Testing Suite For Lumen like Laravel does.
A cli tool which generates unit tests.
BigPipe: Pipelining web pages for high performance built in PHP.
统计信息
- 总下载量: 156.4k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 16
- 点击次数: 29
- 依赖项目数: 5
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-10-25