Skip to content

Instantly share code, notes, and snippets.

@schoren
Created November 15, 2011 19:55
Show Gist options
  • Save schoren/1368127 to your computer and use it in GitHub Desktop.
Save schoren/1368127 to your computer and use it in GitHub Desktop.
email bug configuration
<?php
namespace BigContacts\UserBundle\Features\Context;
use Behat\BehatBundle\Context\BehatContext,
Behat\BehatBundle\Context\MinkContext;
use Behat\Behat\Context\ClosuredContextInterface,
Behat\Behat\Context\TranslatedContextInterface,
Behat\Behat\Exception\PendingException;
use Behat\Gherkin\Node\PyStringNode,
Behat\Gherkin\Node\TableNode;
use Symfony\Component\DependencyInjection\Container;
use Behat\Mink\Exception\UnsupportedDriverActionException,
Behat\Mink\Exception\ExpectationException;
use Behat\MinkBundle\Driver\SymfonyDriver;
use PHPUnit_Framework_ExpectationFailedException as AssertException;
use Behat\Behat\Context\Step;
use Behat\Mink\Driver\GoutteDriver;
require_once 'PHPUnit/Autoload.php';
require_once 'PHPUnit/Framework/Assert/Functions.php';
/**
* Feature context.
*/
class FeatureContext extends MinkContext
{
public function getSymfonyProfile()
{
$driver = $this->getSession()->getDriver();
if (!$driver instanceof SymfonyDriver) {
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;
}
/**
* @Given /^there is no "([^"]*)" in database$/
*/
public function thereIsNoInDatabase($entity)
{
$entities = $this->getRepository($entity)->findAll();
foreach ($entities as $eachEntity) {
$this->getEntityManager()->remove($eachEntity);
}
$this->getEntityManager()->flush();
}
/**
* @Then /^I should get an email on "(?P<email>[^"]+)" with:$/
* @todo this isn't working
*/
public function iShouldGetAnEmail($email, PyStringNode $text)
{
$error = sprintf('No message sent to "%s"', $email);
$profile = $this->getSymfonyProfile();
$collector = $profile->getCollector('swiftmailer');
foreach ($collector->getMessages() as $message) {
// Checking the recipient email and the X-Swift-To
// header to handle the RedirectingPlugin.
// If the recipient is not the expected one, check
// the next mail.
$correctRecipient = array_key_exists(
$email, $message->getTo()
);
$headers = $message->getHeaders();
$correctXToHeader = false;
if ($headers->has('X-Swift-To')) {
$correctXToHeader = array_key_exists($email,
$headers->get('X-Swift-To')->getFieldBodyModel()
);
}
if (!$correctRecipient && !$correctXToHeader) {
continue;
}
try {
// checking the content
return assertContains(
$text->getRaw(), $message->getBody()
);
} catch (AssertException $e) {
$error = sprintf(
'An email has been found for "%s" but without ' .
'the text "%s".', $email, $text->getRaw()
);
}
}
throw new ExpectationException($error, $this->getSession());
}
/**
* @Then /^I should have one "([^"]*)" with "([^"]*)" "([^"]*)"$/
*/
public function iShouldHaveOneWith($entity, $field, $value)
{
$rows = $this->getRepository($entity)->findBy(
array(
$field => $value
)
);
$data = explode(':', $entity);
$name = end($data);
$this->{$name} = $rows[0];
assertTrue(count($rows) == 1);
}
/**
* @Then /^User should not be enabled$/
*/
public function userShouldNotBeEnabled()
{
assertFalse($this->User->isEnabled());
}
/**
* @Then /^User should be owner$/
*/
public function userShouldBeOwner()
{
assertTrue($this->User->getIsOwner());
}
/**
* Returns the Doctrine entity manager.
*
* @return Doctrine\ORM\EntityManager
*/
protected function getEntityManager()
{
return $this->getContainer()->get('doctrine')->getEntityManager();
}
/**
* Returns the Doctrine repository manager for a given entity.
*
* @param string $entityName The name of the entity.
*
* @return Doctrine\ORM\EntityRepository
*/
protected function getRepository($entityName)
{
return $this->getEntityManager()->getRepository($entityName);
}
/**
* @Given /^(.*) without redirection$/
*/
public function theRedirectionsAreIntercepted($step)
{
$this->canIntercept();
$this->getSession()->getDriver()->getClient()->followRedirects(false);
return new Step\Given($step);
}
/**
* @When /^I follow the redirection$/
* @Then /^I should be redirected$/
*/
public function iFollowTheRedirection()
{
$this->canIntercept();
$client = $this->getSession()->getDriver()->getClient();
$client->followRedirects(true);
$client->followRedirect();
}
public function canIntercept()
{
$driver = $this->getSession()->getDriver();
if (!$driver instanceof GoutteDriver) {
throw new UnsupportedDriverActionException(
'You need to tag the scenario with ' .
'"@mink:goutte" or "@mink:symfony". ' .
'Intercepting the redirections is not ' .
'supported by %s', $driver
);
}
}
}
Feature: User Signup
In order to use the system
As a Customer
I need to signup
Background: # src/BigContacts/UserBundle/Features/UserRegistration.feature:6
Given there is no "BigContactsUserBundle:User" in database # BigContacts\UserBundle\Features\Context\FeatureContext::thereIsNoInDatabase()
And there is no "BigContactsCustomerBundle:Customer" in database # BigContacts\UserBundle\Features\Context\FeatureContext::thereIsNoInDatabase()
@mink:symfony
Scenario: A user signs up # src/BigContacts/UserBundle/Features/UserRegistration.feature:11
Given I am on "/register" # BigContacts\UserBundle\Features\Context\FeatureContext::visit()
And I fill in the following: # BigContacts\UserBundle\Features\Context\FeatureContext::fillFields()
| Email | [email protected] |
| Password | abc789 |
| Confirm password | abc789 |
| First name | Sebastian |
| Last name | Choren |
| Company | Codigo Austral |
| Phone | 4545-4545 |
When I press "Sign Up" # BigContacts\UserBundle\Features\Context\FeatureContext::pressButton()
Then I should have one "BigContactsCustomerBundle:Customer" with "email" "[email protected]" # BigContacts\UserBundle\Features\Context\FeatureContext::iShouldHaveOneWith()
And I should have one "BigContactsUserBundle:User" with "email" "[email protected]" # BigContacts\UserBundle\Features\Context\FeatureContext::iShouldHaveOneWith()
And User should not be enabled # BigContacts\UserBundle\Features\Context\FeatureContext::userShouldNotBeEnabled()
And User should be owner # BigContacts\UserBundle\Features\Context\FeatureContext::userShouldBeOwner()
And I should get an email on "[email protected]" with: # BigContacts\UserBundle\Features\Context\FeatureContext::iShouldGetAnEmail()
"""
To finish validating your account - please visit
"""
No message sent to "[email protected]"
And I should be redirected # BigContacts\UserBundle\Features\Context\FeatureContext::iFollowTheRedirection()
And I should be on "/register/check-email"
Feature: User Signup
In order to use the system
As a Customer
I need to signup
Background:
Given there is no "BigContactsUserBundle:User" in database
And there is no "BigContactsCustomerBundle:Customer" in database
@mink:symfony
Scenario: A user signs up
Given I am on "/register"
And I fill in the following:
| Email | [email protected] |
| Password | abc789 |
| Confirm password | abc789 |
| First name | Sebastian |
| Last name | Choren |
| Company | Codigo Austral |
| Phone | 4545-4545 |
When I press "Sign Up"
Then I should have one "BigContactsCustomerBundle:Customer" with "email" "[email protected]"
And I should have one "BigContactsUserBundle:User" with "email" "[email protected]"
And User should not be enabled
And User should be owner
And I should get an email on "[email protected]" with:
"""
To finish validating your account - please visit
"""
And I should be redirected
And I should be on "/register/check-email"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment