traviscarden/behat-table-comparison

Provides an equality assertion for comparing Behat TableNode tables.

Maintainers

Package info

github.com/TravisCarden/behat-table-comparison

pkg:composer/traviscarden/behat-table-comparison

Statistics

Installs: 2 676 013

Dependents: 4

Suggesters: 0

Stars: 8

Open Issues: 0

v0.3.0 2021-06-11 18:20 UTC

README

Packagist Build Status Coverage Status

The Behat Table Comparison library provides an equality assertion for comparing Behat TableNode tables.

Installation & Usage

Install the library via Composer:

composer require --dev traviscarden/behat-table-comparison

Then use the TableEqualityAssertion class in your FeatureContext class:

<?php

use Behat\Behat\Context\Context;
use Behat\Gherkin\Node\TableNode;
use TravisCarden\BehatTableComparison\TableEqualityAssertion;

class FeatureContext implements Context
{

    /**
     * @Then I should include the following characters in the Company of the Ring
     */
    public function iShouldIncludeTheFollowingCharactersInTheCompanyOfTheRing(TableNode $expected)
    {
        // Get the data from the application and create a table from it.
        $application_data = [
            ['Frodo Baggins', 'Hobbit'],
            ['Samwise "Sam" Gamgee', 'Hobbit'],
            ['Saruman the White', 'Wizard'],
            ['Legolas', 'Elf'],
            ['Gimli', 'Dwarf'],
            ['Aragorn (Strider)', 'Man'],
            ['Boromir', 'Man'],
            ['Meriadoc "Merry" Brandybuck', 'Hobbit'],
            ['Peregrin "Pippin" Took', 'Hobbit'],
        ];
        $actual = new TableNode($application_data);

        // Build and execute assertion.
        (new TableEqualityAssertion($expected, $actual))
            ->expectHeader(['name', 'race'])
            ->ignoreRowOrder()
            ->setMissingRowsLabel('Missing characters')
            ->setUnexpectedRowsLabel('Unexpected characters')
            ->setDuplicateRowsLabel('Duplicate characters')
            ->assert();
    }

}

Output is like the following:

Example Output

Error Message Specification

When tables are unequal, the assertion throws a detailed error message with labeled sections.

Difference sections

  • --- Missing rows: Rows present in expected but not in actual.
  • +++ Unexpected rows: Rows present in actual but not in expected.
  • *** Duplicate rows: Rows present on both sides with different multiplicity, shown as (appears N time/times, expected M).

Row-order diagnostics

When row order is respected and rows are out of order:

  • *** Row order mismatch is shown.
  • Per-row diagnostics are listed as ... should be at position X, found at Y.
  • Full order context is appended under:
    • Expected order
    • Actual order

When row content differs while respecting row order, semantic missing/unexpected/duplicate sections are shown first, then full expected/actual order tables.

Header mismatch diagnostics

When expectHeader(...) is used and the first row does not match:

  • --- Expected header
  • +++ Given header

Label customization

All user-facing section labels are configurable via defaults plus getter/setter pairs:

  • Missing rows
  • Unexpected rows
  • Duplicate rows
  • Row order mismatch
  • Expected header
  • Given header
  • Expected order subheading
  • Actual order subheading

Examples

See features/bootstrap/FeatureContext.php and features/examples.feature for more examples.

Contribution

All contributions are welcome according to normal open source practice.