Search code examples
phpsymfonyfunctional-testing

Symfony2 functional test - check table contents


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


Solution

  • Note that I don't Symfony at all, but here's an answer that uses pure PHP DOM; it needs $values as an array with either 'pass' (to skip this <tr>) or an index of which column should have the glyphicon-ok class on it:

    <?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;
    }