Symfony2 functional test - check table contents

时间:2015-08-07 01:32:55

标签: php symfony functional-testing

I have only ever used things like contains() in my assertions, so I'm not sure how I'd go about something as complex as this.

Let's say I have an array of expected answers - in this case it's YES, YES, NO.

So that means effectively, for the first and second question I'd expect to see <span class="glyphicon glyphicon-ok"></span> inside the third <td> and for the third question I'd expect to see it inside the fourth <td>.

Here is my HTML code:

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

How should I go about writing a test for this? Is it hard to iterate through the <tr>'s programatically?

Thank you

3 个答案:

答案 0 :(得分:2)

我认为您应该查看有关Testing和DomCrawler组件的文档页面:

有一些非常简单的方法可以过滤html或xml内容。

答案 1 :(得分:2)

参考文献:

if("<%= @device.brand%>"=="apple")
    {
    alert("apple"); 
    }
    else
    {
    alert("<%= @device.brand %>");
    }

答案 2 :(得分:1)

请注意,我根本不会使用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;
}