OSDN Git Service

behat: extract compareTables() helper
authorscribu <mail@scribu.net>
Tue, 13 Aug 2013 18:20:52 +0000 (21:20 +0300)
committerscribu <mail@scribu.net>
Tue, 13 Aug 2013 18:21:46 +0000 (21:21 +0300)
features/bootstrap/support.php
features/steps/basic_steps.php

index 1e9809d..7b77eb7 100644 (file)
@@ -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;
index d3b267e..0e1ab48 100644 (file)
@@ -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 );
        }
 );