Skip to content

Instantly share code, notes, and snippets.

@everzet
Created January 26, 2012 16:28
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save everzet/1683634 to your computer and use it in GitHub Desktop.
Save everzet/1683634 to your computer and use it in GitHub Desktop.
Describing your Symfony2 console commands with BehatBundle
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 "/.../"
<?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());
}
}
@phreaknerd
Copy link

Even if not proposed, this would work for static functions too - I guess ;-)

@tPl0ch
Copy link

tPl0ch commented Sep 26, 2013

Some more sophisiticated approach here: https://gist.github.com/tPl0ch/6706427

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment