From: scribu Date: Tue, 13 Aug 2013 18:20:52 +0000 (+0300) Subject: behat: extract compareTables() helper X-Git-Tag: v0.12.0~85 X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=556f4c0bd600dce1343ee00ce825e63f62a0bead;p=wvm%2Fwvm.git behat: extract compareTables() helper --- diff --git a/features/bootstrap/support.php b/features/bootstrap/support.php index 1e9809dd..7b77eb7d 100644 --- a/features/bootstrap/support.php +++ b/features/bootstrap/support.php @@ -38,6 +38,21 @@ function checkString( $output, $expected, $action ) { } } +function compareTables( $expected_rows, $actual_rows, $output ) { + // the first row is the header and must be present + if ( $expected_rows[0] != $actual_rows[0] ) { + throw new \Exception( $output ); + } + + unset( $actual_rows[0] ); + unset( $expected_rows[0] ); + + $missing_rows = array_diff( $expected_rows, $actual_rows ); + if ( !empty( $missing_rows ) ) { + throw new \Exception( $output ); + } +} + function compareContents( $expected, $actual ) { if ( gettype( $expected ) != gettype( $actual ) ) { return false; diff --git a/features/steps/basic_steps.php b/features/steps/basic_steps.php index d3b267ee..0e1ab488 100644 --- a/features/steps/basic_steps.php +++ b/features/steps/basic_steps.php @@ -168,59 +168,34 @@ $steps->Then( '/^(STDOUT|STDERR) should be a number$/', $steps->Then( '/^STDOUT should be a table containing rows:$/', function ( $world, TableNode $expected ) { - $output = $world->result->STDOUT; - $outputRows = explode( "\n", rtrim( $output, "\n" ) ); + $output = $world->result->STDOUT; + $actual_rows = explode( "\n", rtrim( $output, "\n" ) ); - $expectedRows = array(); + $expected_rows = array(); foreach ( $expected->getRows() as $row ) { - $expectedRows[] = $world->replace_variables( implode( "\t", $row ) ); + $expected_rows[] = $world->replace_variables( implode( "\t", $row ) ); } - // the first row is the header and must be present - if ( $expectedRows[0] != $outputRows[0] ) { - throw new \Exception( $output ); - } - - unset($outputRows[0]); - unset($expectedRows[0]); - $matches = array_intersect( $expectedRows, $outputRows ); - if ( count( $expectedRows ) != count( $matches ) ) { - throw new \Exception( $output ); - } + compareTables( $expected_rows, $actual_rows, $output ); } ); $steps->Then( '/^STDOUT should end with a table containing rows:$/', function ( $world, TableNode $expected ) { - $output = $world->result->STDOUT; - $outputRows = explode( "\n", rtrim( $output, "\n" ) ); + $output = $world->result->STDOUT; + $actual_rows = explode( "\n", rtrim( $output, "\n" ) ); - $expectedRows = array(); + $expected_rows = array(); foreach ( $expected->getRows() as $row ) { - $expectedRows[] = $world->replace_variables( implode( "\t", $row ) ); + $expected_rows[] = $world->replace_variables( implode( "\t", $row ) ); } - $remainingRows = array(); - $start = false; - foreach( $outputRows as $key => $row ) { - - // the first row is the header and must be present - if ( $expectedRows[0] == $row ) - $start = true; + $start = array_search( $expected_rows[0], $actual_rows ); - if ( $start ) - $remainingRows[] = $row; - } - - if ( ! $start ) + if ( false === $start ) throw new \Exception( $output ); - unset($remainingRows[0]); - unset($expectedRows[0]); - $matches = array_intersect( $expectedRows, $remainingRows ); - - if ( count( $expectedRows ) != count( $matches ) ) - throw new \Exception( $output ); + compareTables( $expected_rows, array_slice( $actual_rows, $start ), $output ); } ); @@ -235,17 +210,17 @@ $steps->Then( '/^STDOUT should be JSON containing:$/', }); $steps->Then( '/^STDOUT should be CSV containing:$/', - function( $world, TableNode $expected ) { + function ( $world, TableNode $expected ) { $output = $world->result->STDOUT; - $expectedRows = $expected->getRows(); + $expected_rows = $expected->getRows(); foreach ( $expected as &$row ) { foreach ( $row as &$value ) { $value = $world->replace_variables( $value ); } } - if ( ! checkThatCsvStringContainsValues( $output, $expectedRows ) ) + if ( ! checkThatCsvStringContainsValues( $output, $expected_rows ) ) throw new \Exception( $output ); } );