2

I have a project for which i have written features/scenarios in Behat, which is almost completed now. I have to test the email functionality on the site for which symfony comes in handy. But, i could not find any tutorial that helps me configure symfony from within Behat. Most of the sites provide Behat in Symfony and not the other way.

This is the article i found that has some information on configuration but it is not complete. http://extensions.behat.org/symfony2

This article http://docs.behat.org/cookbook/using_the_profiler_with_minkbundle.html gives code to check email functionality, but it does not say how to configure symfony in Behat. I have symfony extension installed.

This is my composer.json contents:

{
    "require": {
        "behat/behat": "*",
        "behat/mink": "1.4.0",
        "behat/mink-goutte-driver": "*",
        "behat/mink-selenium-driver": "*",
        "behat/mink-selenium2-driver": "*",
        "behat/mink-sahi-driver": "*",
        "behat/mink-zombie-driver": "*",
        "drupal/drupal-extension": "*",
        "symfony/process": "*",
        "behat/symfony2-extension": "*",
        "symfony/form": "*",
        "symfony/validator": "*",
        "behat/mink-extension": "*",
        "symfony/http-kernel": "*",
        "fabpot/goutte": "dev-master#5f7fd00"
    },
    "minimum-stability": "dev",
    "config": {
      "bin-dir": "bin/"
    }
}

Can anyone please guide me here?

Moe Far
  • 2,742
  • 2
  • 23
  • 41
Sundararajan KS
  • 699
  • 1
  • 7
  • 8
  • What do you want to configure in Symfony from Behat? Could you elaborate more on what you mean by that? – Jakub Zalas Oct 04 '12 at 10:18
  • You should use behat.yml file to configure behat. If you have configured behat properly with symfony2extension, then you have access to symfony DIC and you can do such stuff https://github.com/Behat/CommonContexts/blob/master/Behat/CommonContexts/SymfonyMailerContext.php – l3l0 Oct 04 '12 at 17:23
  • if you think one of our replies is the answer you were looking for, can you please accept it? – Francesco Casula Jun 11 '14 at 09:39

3 Answers3

10

In a Symfony 2(.2) configuration, you have to put your behat.yml file in your root folder, so the same folder where the composer.json is.

app/
bin/
src/
vendor/
web/
behat.yml
composer.json

This is an example of a working behat.yml:

default:
    # ...
    extensions:
        Behat\Symfony2Extension\Extension: ~
        Behat\MinkExtension\Extension:
            goutte:    ~
            selenium2: ~

Now, you have to launch the behat init command specifying your desired Bundle:

php bin/behat @AcmeDemoBundle --init

The above command will create the Features folder with a FeatureContext class inside it. Put in this class the methods of your scenario. Below the official hello world example:

/**
 * @Given /^I am in a directory "([^"]*)"$/
 */
public function iAmInADirectory($dir)
{
    if (!file_exists($dir))
        mkdir($dir);

    chdir($dir);
}

/**
 * @Given /^I have a file named "([^"]*)"$/
 */
public function iHaveAFileNamed($file)
{
    touch($file);
}

/**
 * @When /^I run "([^"]*)"$/
 */
public function iRun($command)
{
    exec($command, $output);
    $this->output = trim(implode("\n", $output));
}

/**
 * @Then /^I should get:$/
 */
public function iShouldGet(PyStringNode $string)
{
    if ((string) $string !== $this->output)
        throw new \Exception("Actual output is:\n" . $this->output);
}

Now you have to create the feature file (ls.feature from the same example) inside your Features folder:

Feature: ls
    In order to see the directory structure
    As a UNIX user
    I need to be able to list the current directory's contents

    Scenario: List 2 files in a directory
        Given I am in a directory "test"
        And I have a file named "foo"
        And I have a file named "bar"
        When I run "ls"
        Then I should get:
            """
            bar
            foo
            """

So, your Features folder will look like the structure below:

Acme\DemoBundle\Features
    |- Context /
       |- FeatureContext.php
    ls.feature

Finally, launch behat and enjoy!

php bin/behat @AcmeDemoBundle
Francesco Casula
  • 26,184
  • 15
  • 132
  • 131
1

The dependencies needed to run behat with symfony are behat, symfony2-extension, mink and drivers, and you may need the formatter to get the desired output.

Then you need to configure the behat.yml for drivers, session and browser and to name test suites.

Once this is done you need to add your feature and FeatureContext to write the test scenarios and to define custom steps.

I have added a details configurations with my symfony installation with behat+mink+selenium+symfony here. Please check if it works for you.

Community
  • 1
  • 1
aniruddha
  • 689
  • 8
  • 29
0

I'm not sure if I completely understand what you're trying to achieve, but the fact that you're trying to achieve a different configuration of your Symfony2 project makes me think that you should probably make use of a custom environment. See this cookbook article on how to go about doing it.

For example you may want to setup a "behat" environment, which would have it's own configuration file (/app/config/config_behat.yml) and would be accessed using /app_behat.php/ from your behat tests.

RobMasters
  • 4,108
  • 2
  • 27
  • 34
  • I am not running symfony here. I am running Behat in which i have symfony2 as an extension. All i want is a tutorial that will configure this symfony2 extension in Behat. – Sundararajan KS Oct 08 '12 at 11:47
  • Here's the documentation of Symfony2 extension where you'll find everything about it: http://extensions.behat.org/symfony2/ I don't see why you need behat extension for Symfony since you're not running Symfony though... – Jakub Zalas Oct 14 '12 at 14:19