编程语言
首页 > 编程语言> > php – Symfony2功能测试 – 检查表内容

php – Symfony2功能测试 – 检查表内容

作者:互联网

我在断言中只使用过contains()这样的东西,所以我不确定如何处理像这样复杂的东西.

假设我有一系列预期答案 – 在这种情况下,它是YES,YES,NO.

所以这意味着有效,对于我希望看到的第一个和第二个问题< span class =“glyphicon glyphicon-ok”>< / span>在第三个< td>内对于第三个问题,我希望在第四个< td>内看到它.

这是我的HTML代码:

<table class="table table-curved">
    <tr>
        <th width="10%">Item</th>
        <th width="60%">Description</th>
        <th width="10%">YES</th>
        <th width="10%">NO</th>
        <th width="10%">NOT APPLICABLE</th>
    </tr>

    <tr>
        <td class="report-table-inner report-centre">1</td>
        <td class="report-table-inner">Check cargo is secure and undamaged.</td>
        <td class="report-centre success"><span class="glyphicon glyphicon-ok"></span></td>
        <td class="report-centre"></td>
        <td class="report-centre"></td>
    </tr>
    <tr>
        <td class="report-table-inner report-centre">2</td>
        <td class="report-table-inner">Is all cargo accounted for.</td>
        <td class="report-centre success"><span class="glyphicon glyphicon-ok"></span></td>
        <td class="report-centre"></td>
        <td class="report-centre"></td>
    </tr>
    <tr>
        <td class="report-table-inner report-centre">3</td>
        <td class="report-table-inner">Is all cargo checked by customs.</td>
        <td class="report-centre"></td>
        <td class="report-centre danger"><span class="glyphicon glyphicon-ok"></span></td>
        <td class="report-centre"></td>
    </tr>
    ...

我该如何为此编写测试?以编程方式迭代< tr>是否很难?

谢谢

解决方法:

请注意,我根本不是Symfony,但这是一个使用纯PHP DOM的答案;它需要$values作为一个数组,带有’pass'(跳过这个< tr>)或者哪个列应该有glyphicon-ok类的索引:

<?php
$data = <<<DATA
<table class="table table-curved">
    <tr>
        <th width="10%">Item</th>
        <th width="60%">Description</th>
        <th width="10%">YES</th>
        <th width="10%">NO</th>
        <th width="10%">NOT APPLICABLE</th>
    </tr>

    <tr>
        <td class="report-table-inner report-centre">1</td>
        <td class="report-table-inner">Check cargo is secure and undamaged.</td>
        <td class="report-centre success"><span class="glyphicon glyphicon-ok"></span></td>
        <td class="report-centre"></td>
        <td class="report-centre"></td>
    </tr>
    <tr>
        <td class="report-table-inner report-centre">2</td>
        <td class="report-table-inner">Is all cargo accounted for.</td>
        <td class="report-centre success"><span class="glyphicon glyphicon-ok"></span></td>
        <td class="report-centre"></td>
        <td class="report-centre"></td>
    </tr>
    <tr>
        <td class="report-table-inner report-centre">3</td>
        <td class="report-table-inner">Is all cargo checked by customs.</td>
        <td class="report-centre"></td>
        <td class="report-centre danger"><span class="glyphicon glyphicon-ok"></span></td>
        <td class="report-centre"></td>
    </tr>
</table>
DATA;


$dom = new DOMDocument();
$dom->loadXML($data);
$xpath = new DOMXPath($dom);
$values = ['pass', 2, 2, 3];
$idx = 0;
foreach($xpath->query('//tr') as $tr) {
  if ($values[$idx] != 'pass') {
    $tds = $tr->getElementsByTagName('td');
    $td = $tds->item($values[$idx]);
    if ($td instanceof DOMNode && $td->hasChildNodes()) {
      if (FALSE !== strpos($td->firstChild->getAttribute('class'), 'glyphicon-ok')) {
          echo "Matched on ", $tds->item(1)->textContent, "\n";
      } else {
          echo "Not matched on ", $tds->item(1)->textContent, "\n";
      }
    }
  }
  ++$idx;
}

标签:php,symfony,functional-testing
来源: https://codeday.me/bug/20190609/1203679.html