Showing posts with label Behat. Show all posts
Showing posts with label Behat. Show all posts

Thursday, September 5, 2013

Symfony2 Behat and Emails

I've been using Behat with my Symfony apps for some time now but recently I needed to include emails in my features. I found tutorials at http://docs.behat.org/cookbook/using_the_profiler_with_minkbundle.html and http://docs.behat.org/cookbook/intercepting_the_redirections.html but they did not work out of the box for me. I guess they are now slightly out of date. After some time searching the docs and source code. I found that I needed to enable the profiler in the test environment:

# app/config/config_test.yml
framework:
    profiler:
        enabled: true

I tried leaving this set to false and enabling the the profiler for the next request with a step but it always said the profiler was diabled

# src/.../...Bundle/Features/Context/FeatureContext.php
    /**
     * @When /^(?:|I )enable the profiler$/
     */
    public function iEnableProfiler()
    {
        $driver = $this->getSession()->getDriver();
        $driver->getClient()->enableProfiler();
    }

Also I needed to change the instance of checks from GoutteDriver and SymfonyDriver to KernelDriver. Also I did not need to tag the scenario @mink:symfony as the default session in behat.yml is set to symfony2. Below is what I ended up with. Hope this helps someone else.

# src/.../...Bundle/Features/Context/FeatureContext.php
    public function getSymfonyProfile()
    {
        $driver = $this->getSession()->getDriver();
        if (!$driver instanceof KernelDriver) {
            throw new UnsupportedDriverActionException(
                'You need to tag the scenario with '.
                '"@mink:symfony". Using the profiler is not '.
                'supported by %s', $driver
            );
        }

        $profile = $driver->getClient()->getProfile();
        if (false === $profile) {
            throw new \RuntimeException(
                'Emails cannot be tested as the profiler is '.
                'disabled.'
            );
        }

        return $profile;
    }

    public function canIntercept()
    {
        $driver = $this->getSession()->getDriver();
        if (!$driver instanceof KernelDriver) {
            throw new UnsupportedDriverActionException(
                'You need to tag the scenario with '.
                '"@mink:goutte" or "@mink:symfony". '.
                'Intercepting the redirections is not '.
                'supported by %s', $driver
            );
        }
    }


# src/.../...Bundle/Features/....feature
  Scenario: Send an email
    Given I am on the homepage
    When I fill in ...
    And I press "Send message" without redirection
    Then I should get an email to "..." with "..."
    And I should be redirected