Skip to content

Instantly share code, notes, and snippets.

@joshlopes
Created December 15, 2017 10:29
Show Gist options
  • Save joshlopes/b316eec24743d4befbaeb03b7cefcc51 to your computer and use it in GitHub Desktop.
Save joshlopes/b316eec24743d4befbaeb03b7cefcc51 to your computer and use it in GitHub Desktop.
/**
* @Given /^I should see a table with:$/
*/
public function iShouldSeeTableWith(TableNode $expectedTable)
{
$expectedArray = $expectedTable->getColumnsHash();
$currentArray = [];
$tables = $this->getSession()->getPage()->findAll('css', 'table');
/** @var NodeElement $table */
foreach ($tables as $table) {
$headers = null;
$numHeaders = null;
foreach ($table->findAll('css', 'tr') as $tableRow) {
if (!count($columns = $tableRow->findAll('css', 'td'))) {
$columns = $tableRow->findAll('css', 'th');
}
array_walk($columns, function (NodeElement &$column) {
$column = $this->normaliseDataToCompare($column->getHtml());
});
if (null === $headers) {
$headers = $columns;
$numHeaders = count($headers);
continue;
}
if (count($columns) === $numHeaders) {
$currentArray[] = array_combine($headers, $columns);
}
}
}
foreach ($expectedArray as $row) {
$found = false;
foreach ($currentArray as $key => $currentRow) {
if (0 === count(array_diff($row, $currentRow))) {
$found = true;
// remove this row so we can find duplicated entries
unset($currentArray[$key]);
break;
}
}
assert::assertTrue($found, sprintf('Row "%s" was not found', implode(', ', $row)));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment