PHP code example of ingenerator / behat-tableassert

1. Go to this page and download the library: Download ingenerator/behat-tableassert library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ingenerator / behat-tableassert example snippets


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'));

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');

  /**
   * @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
      ]
    );
  }