OSDN Git Service

define steps using closures
authorscribu <mail@scribu.net>
Sun, 17 Feb 2013 03:59:26 +0000 (05:59 +0200)
committerscribu <mail@scribu.net>
Sun, 17 Feb 2013 03:59:26 +0000 (05:59 +0200)
features/bootstrap/FeatureContext.php
features/steps/basic_steps.php [new file with mode: 0644]

index 31bff78..7b9be63 100644 (file)
@@ -2,10 +2,7 @@
 
 use Behat\Behat\Context\ClosuredContextInterface,
     Behat\Behat\Context\TranslatedContextInterface,
-    Behat\Behat\Context\BehatContext,
-    Behat\Behat\Exception\PendingException;
-use Behat\Gherkin\Node\PyStringNode,
-    Behat\Gherkin\Node\TableNode;
+    Behat\Behat\Context\BehatContext;
 
 require_once 'PHPUnit/Autoload.php';
 require_once 'PHPUnit/Framework/Assert/Functions.php';
@@ -15,9 +12,9 @@ require_once __DIR__ . '/../../php/utils.php';
 /**
  * Features context.
  */
-class FeatureContext extends BehatContext
+class FeatureContext extends BehatContext implements ClosuredContextInterface
 {
-       private $variables = array();
+       public $variables = array();
 
        /**
         * Initializes context.
@@ -30,188 +27,30 @@ class FeatureContext extends BehatContext
                $this->runner = new WP_CLI_Command_Runner;
        }
 
-       private function replace_variables( &$str )
+       public function getStepDefinitionResources()
        {
-               $str = preg_replace_callback( '/\{(\w+)\}/', array( $this, '_replace_var' ), $str );
-       }
-
-       private function _replace_var( $matches )
-       {
-               $cmd = $matches[0];
-
-               foreach ( array_slice( $matches, 1 ) as $key ) {
-                       $cmd = str_replace( '{' . $key . '}', $this->variables[ $key ], $cmd );
-               }
-
-               return $cmd;
-       }
-
-       /**
-        * @Given /^an empty directory$/
-        */
-       public function anEmptyDirectory()
-       {
-               $this->runner->create_empty_dir();
-       }
-
-       /**
-        * @Given /^WP files$/
-        */
-       public function wordpressFiles()
-       {
-               $this->runner->download_wordpress_files();
-       }
-
-       /**
-        * @Given /^wp-config\.php$/
-        */
-       public function wpConfigPhp()
-       {
-               $this->runner->create_config();
-       }
-
-       /**
-        * @Given /^a database$/
-        */
-       public function aDatabase()
-       {
-               $this->runner->create_db();
-       }
-
-       /**
-        * @Given /^WP install$/
-        */
-       public function wpInstall()
-       {
-               $this->runner->create_db();
-               $this->runner->create_empty_dir();
-               $this->runner->download_wordpress_files();
-               $this->runner->create_config();
-               $this->runner->run_install();
-       }
-
-       /**
-        * @Given /^custom wp-content directory$/
-        */
-       public function customWpContentDirectory()
-       {
-               $this->runner->define_custom_wp_content_dir();
-       }
-
-       /**
-        * @When /^I run `(.+)`$/
-        */
-       public function iRun( $cmd )
-       {
-               $cmd = ltrim( str_replace( 'wp', '', $cmd ) );
-
-               $this->replace_variables( $cmd );
-
-               $this->result = $this->runner->run( $cmd );
-       }
-
-       /**
-        * @When /^I run the previous command again$/
-        */
-       public function iRunThePreviousCommandAgain()
-       {
-               if ( !isset( $this->result ) )
-                       throw new \Exception( 'No previous command.' );
-
-               $this->result = $this->runner->run( $this->result->command );
-       }
-
-       /**
-        * @Given /^save (STDOUT|STDERR) as \{(\w+)\}$/
-        */
-       public function saveStreamAsVariable( $stream, $key )
-       {
-               $this->variables[ $key ] = rtrim( $this->result->$stream, "\n" );
+               return array( __DIR__ . '/../steps/basic_steps.php' );
        }
 
-       /**
-        * @Then /^the return code should be (\d+)$/
-        */
-       public function theReturnCodeShouldBe( $return_code )
+       public function getHookDefinitionResources()
        {
-               assertEquals( $return_code, $this->result->return_code );
+               return array();
        }
 
-       /**
-        * @Then /^it should run without errors$/
-        */
-       public function itShouldRunWithoutErrors()
+       public function replace_variables( &$str )
        {
-               if ( !empty( $this->result->STDERR ) )
-                       throw new \Exception( $this->result->STDERR );
-
-               if ( 0 != $this->result->return_code )
-                       throw new \Exception( "Return code was $this->result->return_code" );
-       }
-
-       /**
-        * @Then /^(STDOUT|STDERR) should be:$/
-        */
-       public function outputShouldBe( $stream, PyStringNode $output )
-       {
-               $this->replace_variables( $output );
-
-               $result = rtrim( $this->result->$stream, "\n" );
-
-               if ( (string) $output != $result ) {
-                       throw new \Exception( $this->result->$stream );
-               }
-       }
-
-       /**
-        * @Then /^(STDOUT|STDERR) should contain:$/
-        */
-       public function outputShouldContain( $stream, PyStringNode $output )
-       {
-               if ( false === strpos( $this->result->$stream, (string) $output ) ) {
-                       throw new \Exception( $this->result->$stream );
-               }
+               $str = preg_replace_callback( '/\{(\w+)\}/', array( $this, '_replace_var' ), $str );
        }
 
-       /**
-        * @Then /^(STDOUT|STDERR) should match \'([^\']+)\'$/
-        */
-       public function outputShouldMatch( $stream, $format )
+       private function _replace_var( $matches )
        {
-               assertStringMatchesFormat( $format, $this->result->$stream );
-       }
+               $cmd = $matches[0];
 
-       /**
-        * @Then /^(STDOUT|STDERR) should be empty$/
-        */
-       public function outputShouldBeEmpty( $stream )
-       {
-               if ( !empty( $this->result->$stream ) ) {
-                       throw new \Exception( $this->result->$stream );
+               foreach ( array_slice( $matches, 1 ) as $key ) {
+                       $cmd = str_replace( '{' . $key . '}', $this->variables[ $key ], $cmd );
                }
-       }
-
-       /**
-        * @Then /^(STDOUT|STDERR) should not be empty$/
-        */
-       public function outputShouldNotBeEmpty( $stream )
-       {
-               assertNotEmpty( rtrim( $this->result->$stream, "\n" ) );
-       }
 
-       /**
-        * @Then /^the (.+) file should exist$/
-        */
-       public function fileShouldExist( $path )
-       {
-               assertFileExists( $this->runner->get_path( $path ) );
-       }
-
-       /**
-        * @Then /^database exists$/
-        */
-       public function databaseExists()
-       {
-               throw new PendingException();
+               return $cmd;
        }
 }
+
diff --git a/features/steps/basic_steps.php b/features/steps/basic_steps.php
new file mode 100644 (file)
index 0000000..cd1651e
--- /dev/null
@@ -0,0 +1,99 @@
+<?php
+
+use Behat\Behat\Exception\PendingException,
+    Behat\Gherkin\Node\PyStringNode,
+    Behat\Gherkin\Node\TableNode;
+
+$steps->Given( '/^an empty directory$/', function( $world ) {
+       $world->runner->create_empty_dir();
+} );
+
+$steps->Given( '/^WP files$/', function ( $world ) {
+       $world->runner->download_wordpress_files();
+} );
+
+$steps->Given( '/^wp-config\.php$/', function ( $world ) {
+       $world->runner->create_config();
+} );
+
+$steps->Given( '/^a database$/', function( $world ) {
+       $world->runner->create_db();
+} );
+
+$steps->Given( '/^WP install$/', function( $world ) {
+       $world->runner->create_db();
+       $world->runner->create_empty_dir();
+       $world->runner->download_wordpress_files();
+       $world->runner->create_config();
+       $world->runner->run_install();
+} );
+
+$steps->Given( '/^custom wp-content directory$/', function( $world ) {
+       $world->runner->define_custom_wp_content_dir();
+} );
+
+$steps->When( '/^I run `(.+)`$/', function( $world, $cmd ) {
+       $cmd = ltrim( str_replace( 'wp', '', $cmd ) );
+
+       $world->replace_variables( $cmd );
+
+       $world->result = $world->runner->run( $cmd );
+} );
+
+$steps->When( '/^I run the previous command again$/', function( $world ) {
+       if ( !isset( $world->result ) )
+               throw new \Exception( 'No previous command.' );
+
+       $world->result = $world->runner->run( $world->result->command );
+} );
+
+$steps->Given( '/^save (STDOUT|STDERR) as \{(\w+)\}$/', function( $world, $stream, $key ) {
+       $world->variables[ $key ] = rtrim( $world->result->$stream, "\n" );
+} );
+
+$steps->Then( '/^the return code should be (\d+)$/', function( $world, $return_code ) {
+       assertEquals( $return_code, $world->result->return_code );
+} );
+
+$steps->Then( '/^it should run without errors$/', function( $world ) {
+       if ( !empty( $world->result->STDERR ) )
+               throw new \Exception( $world->result->STDERR );
+
+       if ( 0 != $world->result->return_code )
+               throw new \Exception( "Return code was $world->result->return_code" );
+} );
+
+$steps->Then( '/^(STDOUT|STDERR) should be:$/', function( $world, $stream, PyStringNode $output ) {
+       $world->replace_variables( $output );
+
+       $result = rtrim( $world->result->$stream, "\n" );
+
+       if ( (string) $output != $result ) {
+               throw new \Exception( $world->result->$stream );
+       }
+} );
+
+$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 );
+       }
+} );
+
+$steps->Then( '/^(STDOUT|STDERR) should match \'([^\']+)\'$/', function( $world, $stream, $format ) {
+       assertStringMatchesFormat( $format, $world->result->$stream );
+} );
+
+$steps->Then( '/^(STDOUT|STDERR) should be empty$/', function( $world, $stream ) {
+       if ( !empty( $world->result->$stream ) ) {
+               throw new \Exception( $world->result->$stream );
+       }
+} );
+
+$steps->Then( '/^(STDOUT|STDERR) should not be empty$/', function( $world, $stream ) {
+       assertNotEmpty( rtrim( $world->result->$stream, "\n" ) );
+} );
+
+$steps->Then( '/^the (.+) file should exist$/', function( $world, $path ) {
+       assertFileExists( $world->runner->get_path( $path ) );
+} );