OSDN Git Service

behat: unify and standardize output testing steps:
authorscribu <mail@scribu.net>
Fri, 8 Mar 2013 19:07:42 +0000 (21:07 +0200)
committerscribu <mail@scribu.net>
Fri, 8 Mar 2013 19:09:23 +0000 (21:09 +0200)
* always run replace_variables() and restrict variable names
* add 'not contain' option

features/bootstrap/FeatureContext.php
features/steps/basic_steps.php

index bbdb336..d45f895 100644 (file)
@@ -44,9 +44,9 @@ class FeatureContext extends BehatContext implements ClosuredContextInterface
                return array();
        }
 
-       public function replace_variables( &$str )
+       public function replace_variables( $str )
        {
-               $str = preg_replace_callback( '/\{(\w+)\}/', array( $this, '_replace_var' ), $str );
+               return preg_replace_callback( '/\{([A-Z_]+)\}/', array( $this, '_replace_var' ), $str );
        }
 
        private function _replace_var( $matches )
index 2dbbdf1..28551fb 100644 (file)
@@ -52,9 +52,7 @@ $steps->When( '/^I run `wp`$/',
 
 $steps->When( '/^I run `wp (.+)`$/',
        function ( $world, $cmd ) {
-               $world->replace_variables( $cmd );
-
-               $world->result = $world->run( $cmd );
+               $world->result = $world->run( $world->replace_variables( $cmd ) );
        }
 );
 
@@ -89,22 +87,32 @@ $steps->Then( '/^it should run without errors$/',
        }
 );
 
-$steps->Then( '/^(STDOUT|STDERR) should be:$/',
-       function ( $world, $stream, PyStringNode $output ) {
-               $world->replace_variables( $output );
+$steps->Then( '/^(STDOUT|STDERR) should (be|contain|not contain):$/',
+       function ( $world, $stream, $action, PyStringNode $expected ) {
+               $output = $world->replace_variables( $world->result->$stream );
 
-               $result = rtrim( $world->result->$stream, "\n" );
+               $expected = (string) $expected;
 
-               if ( (string) $output != $result ) {
-                       throw new \Exception( $world->result->$stream );
+               switch ( $action ) {
+
+               case 'be':
+                       $r = $expected === rtrim( $output, "\n" );
+                       break;
+
+               case 'contain':
+                       $r = false !== strpos( $output, $expected );
+                       break;
+
+               case 'not contain':
+                       $r = false === strpos( $output, $expected );
+                       break;
+
+               default:
+                       throw new PendingException();
                }
-       }
-);
 
-$steps->Then( '/^(STDOUT|STDERR) should contain:$/',
-       function ( $world, $stream, PyStringNode $output ) {
-               if ( false === strpos( $world->result->$stream, (string) $output ) ) {
-                       throw new \Exception( $world->result->$stream );
+               if ( !$r ) {
+                       throw new \Exception( $output );
                }
        }
 );