Created
January 26, 2012 16:28
-
-
Save everzet/1683634 to your computer and use it in GitHub Desktop.
Describing your Symfony2 console commands with BehatBundle
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Feature: Demo greetings command | |
In order to show how to describe commands in Behat | |
As a Behat developer | |
I need to show simple scenario based on http://symfony.com/doc/2.0/components/console.html#testing-commands | |
Scenario: Running demo:greet command | |
When I run "demo:greet" command | |
Then I should see "/.../" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Acme\DemoBundle\Features\Context; | |
use Behat\BehatBundle\Context\BehatContext, | |
Behat\Behat\Context\TranslatedContextInterface, | |
Behat\Behat\Exception\PendingException; | |
use Behat\Gherkin\Node\PyStringNode, | |
Behat\Gherkin\Node\TableNode; | |
use Symfony\Component\Console\Tester\CommandTester; | |
use Symfony\Bundle\FrameworkBundle\Console\Application; | |
use Acme\DemoBundle\Command\GreetCommand; | |
require_once 'PHPUnit/Autoload.php'; | |
require_once 'PHPUnit/Framework/Assert/Functions.php'; | |
/** | |
* Feature context. | |
*/ | |
class FeatureContext extends BehatContext | |
{ | |
private $application; | |
private $tester; | |
public function __construct($kernel) | |
{ | |
$this->application = new Application($kernel); | |
$this->application->add(new GreetCommand()); | |
// add other commands if needed | |
} | |
/** | |
* @When /^I run "([^"]*)" command$/ | |
*/ | |
public function iRunCommand($name) | |
{ | |
$command = $this->application->find($name); | |
$this->tester = new CommandTester($command); | |
$this->tester->execute(array('command' => $command->getName())); | |
} | |
/** | |
* @Then /^I should see "([^"]*)"$/ | |
*/ | |
public function iShouldSee($regexp) | |
{ | |
assertRegExp($regexp, $this->tester->getDisplay()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Even if not proposed, this would work for static functions too - I guess ;-)