ingenerator / behat-tableassert
behat中各种表格数据的断言
Requires
- php: ~8.0.0 || ~8.1.0 || ~8.2.0 || ~8.3.0
- ext-dom: *
- ext-libxml: *
- ext-simplexml: *
- behat/gherkin: >=2.0.0 <5.0.0
- masterminds/html5: ^2.7.5
Requires (Dev)
- behat/mink: ^1.7
- phpunit/phpunit: ^9.5
Suggests
- behat/mink: For parsing tables from a browser response
README
Behat-TableAssert简化了在Behat中处理和断言各种表格数据的工作。它目前支持Behat v2和v3。与Mink一起使用时有一些额外功能,但也可以在不使用Mink的情况下正常工作。
安装Behat-TableAssert
$> composer require --dev ingenerator/behat-tableassert
这个包本身不是一个Behat扩展,因此您不需要进行任何进一步的配置 - 类将被Composer自动加载器获取。
解析外部数据中的表格
通常您需要将功能文件中的表格数据与您的应用程序生成的表格进行比较。例如,您的应用程序可能会在屏幕上生成HTML表格或CSV文件供下载。
您可以使用提供的解析器之一将来自各种来源的数据加载到标准的Behat TableNode中。
解析HTML表格
HTML表格解析器期望一个格式良好的表格,其中包含一个包含列标题的单行<thead>
,以及<tbody>
中的零行或多行。所有行应具有相同数量的列,可以是任何混合的<th>
和<td>
元素。
如果行包含具有colspan属性的单元格,则解析的表格将在第一列中包含单元格内容,其余的colspan宽度将由包含
...
的单元格填充。
如果行有意外额外或缺少的单元格(例如由于渲染错误),则解析的表格将在每行的末尾添加额外的单元格,以使所有行具有相同的宽度。我们不够智能,无法在行中间填充,因此缺失单元格右侧的列将不会与上面的列对齐。
如果您想忽略表格中的某些行 - 例如,如果它们提供额外的可读性标题或其他无关的呈现内容 - 您可以像这样标记它们:
<tr data-behat-table="ignore">
,解析器将像它们不存在一样处理。
如果您想在某些表格单元格的可见文本前添加前缀 - 例如,当您有一些对人类来说是视觉上不同的列,但需要以某种方式为功能文件标记时 - 您可以使用数据属性来完成此操作。
<td data-behat-table-prefix="Times - ">Before</td>
将解析为Times - Before
。
use \Ingenerator\BehatTableAssert\TableParser\HTMLTable; // Without mink you can parse from a string containing just the <table> tag and children $html = '<table><thead><tr><th>Col</th></tr></thead><tbody><tr><td>Cell</td></tr></tbody></table>'; $table = HTMLTable::fromHTMLString($html); // With mink you can parse from a NodeElement - again this must be a <table> element $assert = $this->getMink()->assertSession(); $table = HTMLTable::fromMinkTable($assert->elementExists('css', 'table'));
解析CSV表格
CSV表格解析器期望标准的CSV内容。与HTML一样,解析的表格将在右侧填充以确保所有行具有相同数量的列,但这可能导致存在缺失列的行的列对齐不齐。
use \Ingenerator\BehatTableAssert\TableParser\CSVTable; // Without mink you can parse from a stream $file = fopen('/some/csv/file', 'r'); $table = CSVTable::::fromStream($file); fclose($file); // Or a string if that's easier $table = CSVTable::::fromString(file_get_contents('/some/csv/file'); // With mink you can also parse directly from a server response *if*: // - the CSV is rendered to the browser as the full raw content of the response // - the response has a text/csv content-type header // - either you omit any `Content-Disposition: Attachment` header, or you're // using a goutte-like driver that ignores it // // If your driver respects a Content-Disposition attachment header then it will save // your server response to disk somewhere just like a normal browser - you will need // to locate the downloaded file and use the string or stream parsing option. $table = CSVTable::fromMinkResponse($this->getMink()->getSession());
比较表格
一旦您已解析您的表格(或从您选择的数组构建它),您将想要将值与您在功能规范中提供的进行比较。
简单比较
$tableassert = new \Ingenerator\BehatTableAssert\AssertTable; // Throws unless tables are identical - same columns, in same order, containing same data $tableassert->isSame($expected, $actual, 'Optional extra message'); // Throws unless tables are equal - same columns containing same data, but in any order $tableassert->isEqual($expected, $actual, 'Optional extra message'); // Throws unless actual table contains at least all the specified columns with the same data, // ignoring any extra columns. // Useful if your application produces a large dataset but you only want to specify a few // values in your feature file. $tableassert->containsColumns($expected, $actual, 'Optional extra message');
自定义比较
所有简单比较都依赖于每个表格单元格值之间的严格字符串比较。有时这不够灵活,您需要实现自定义逻辑来决定两个值是否等效。我们支持您。`isComparable`断言接受diff引擎的完整选项数组,包括`comparators`数组 - 自定义可调用,如果预期值和实际值相同则返回TRUE,否则返回FALSE。
一个常见的例子是,当您的应用程序生成具有固定日期的报告,但您想在场景中使用相对日期,或者您不介意报告中存在小的舍入误差。
Then I should see a report containing: | date | status | uptime | | today | good | 99.9 | | yesterday | poor | 50.1 | | -2 days | fair | 99.7 |
/** * @Then /^I should see a report containing$/ */ function assertReport(TableNode $expected) { $actual = \Ingenerator\BehatTableAssert\TableParser\CSVTable::fromMinkResponse($this->getMink()->getSession()); $assert = new \Ingenerator\BehatTableAssert\AssertTable; $assert->isComparable( $expected, $actual, [ 'comparators' => [ 'date' => function ($expected, $actual) { return new \DateTime($expected) == new \DateTime($actual); }, 'uptime' => function ($expected, $actual) { return round($expected, 0) == round($actual, 0); }, ], 'ignoreColumnSequence' => TRUE, 'ignoreExtraColumns' => TRUE ] ); }
断言失败
失败的断言将抛出 \Ingenerator\BehatTableAssert\TableAssertionFailureException
。如果表之间存在结构差异——例如,缺少列、列顺序不正确或意外的额外列,您将看到类似以下内容
Failed asserting that two tables were equal: some custom message
Structural Differences:
-----------------------
- Missing columns: 'date' (got 'day', 'status', 'uptime')
- Unexpected columns: 'day'
如果结构正常,但个别单元格的值存在差异,您将看到一个消息,其中包含实际收到的表值,并突出显示无效值。包含错误的列和行用X标记,并且每个无效的行之后都有一行,只显示单元格失败的预期值,单元格值如预期则用~标记。
Failed asserting that two tables were equal: some other custom message
| | X date | X status | uptime |
| | 2015-10-09 | good | 99.8 |
| X | X 2015-09-01 | poor | 50.1 |
| i | ^ 2015-10-08 | ~ | ~ |
| X | 2015-10-07 | X good | 99.7 |
| i | ~ | ^ fair | ~ |
贡献
欢迎贡献——请在开始任何贡献工作之前查看 CONTRIBUTING.md。
贡献者
本软件包由 inGenerator Ltd 赞助
- Andrew Coulton acoulton - 主开发者
许可证
本软件基于 BSD-3-Clause 许可证 许可