#2568 Behat context

This commit is contained in:
Arnaud Langlade 2015-06-14 16:53:06 +02:00
parent 52594fc417
commit 0241e6e5c3
6 changed files with 557 additions and 406 deletions

View file

@ -16,7 +16,7 @@ default:
- Sylius\Bundle\AddressingBundle\Behat\AddressingContext
- Sylius\Bundle\CoreBundle\Behat\CoreContext
- Sylius\Bundle\ProductBundle\Behat\ProductContext
- Sylius\Bundle\ResourceBundle\Behat\BaseContext
- Sylius\Bundle\ResourceBundle\Behat\FixtureContext
- Sylius\Bundle\ShippingBundle\Behat\ShippingContext
- Sylius\Bundle\WebBundle\Behat\WebContext
- Sylius\Bundle\MoneyBundle\Behat\MoneyContext
@ -31,7 +31,7 @@ default:
- Sylius\Bundle\AddressingBundle\Behat\AddressingContext
- Sylius\Bundle\AddressingBundle\Behat\FormContext
- Sylius\Bundle\CoreBundle\Behat\CoreContext
- Sylius\Bundle\ResourceBundle\Behat\BaseContext
- Sylius\Bundle\ResourceBundle\Behat\FixtureContext
- Sylius\Bundle\WebBundle\Behat\WebContext
- Sylius\Bundle\MoneyBundle\Behat\MoneyContext
- Sylius\Bundle\ChannelBundle\Behat\ChannelContext
@ -73,7 +73,7 @@ default:
- Sylius\Bundle\CoreBundle\Behat\HookContext
- Sylius\Bundle\CoreBundle\Behat\CoreContext
- Sylius\Bundle\MoneyBundle\Behat\MoneyContext
- Sylius\Bundle\ResourceBundle\Behat\BaseContext
- Sylius\Bundle\ResourceBundle\Behat\FixtureContext
- Sylius\Bundle\WebBundle\Behat\WebContext
- Sylius\Bundle\ContactBundle\Behat\ContactContext
- Sylius\Bundle\ChannelBundle\Behat\ChannelContext
@ -87,7 +87,7 @@ default:
- Sylius\Bundle\CoreBundle\Behat\CoreContext
- Sylius\Bundle\MoneyBundle\Behat\MoneyContext
- Sylius\Bundle\ProductBundle\Behat\ProductContext
- Sylius\Bundle\ResourceBundle\Behat\BaseContext
- Sylius\Bundle\ResourceBundle\Behat\FixtureContext
- Sylius\Bundle\TaxonomyBundle\Behat\TaxonomyContext
- Sylius\Bundle\WebBundle\Behat\WebContext
- Sylius\Bundle\ChannelBundle\Behat\ChannelContext
@ -124,7 +124,7 @@ default:
- Sylius\Bundle\CoreBundle\Behat\HookContext
- Sylius\Bundle\CoreBundle\Behat\CoreContext
- Sylius\Bundle\ProductBundle\Behat\ProductContext
- Sylius\Bundle\ResourceBundle\Behat\BaseContext
- Sylius\Bundle\ResourceBundle\Behat\FixtureContext
- Sylius\Bundle\WebBundle\Behat\WebContext
- Sylius\Bundle\MoneyBundle\Behat\MoneyContext
- Sylius\Bundle\ChannelBundle\Behat\ChannelContext
@ -136,7 +136,7 @@ default:
- Behat\MinkExtension\Context\MinkContext
- Sylius\Bundle\CoreBundle\Behat\HookContext
- Sylius\Bundle\CoreBundle\Behat\CoreContext
- Sylius\Bundle\ResourceBundle\Behat\BaseContext
- Sylius\Bundle\ResourceBundle\Behat\FixtureContext
- Sylius\Bundle\WebBundle\Behat\WebContext
- Sylius\Bundle\MoneyBundle\Behat\MoneyContext
- Sylius\Bundle\ChannelBundle\Behat\ChannelContext
@ -161,7 +161,7 @@ default:
- Sylius\Bundle\AddressingBundle\Behat\AddressingContext
- Sylius\Bundle\CoreBundle\Behat\CoreContext
- Sylius\Bundle\ProductBundle\Behat\ProductContext
- Sylius\Bundle\ResourceBundle\Behat\BaseContext
- Sylius\Bundle\ResourceBundle\Behat\FixtureContext
- Sylius\Bundle\TaxationBundle\Behat\TaxationContext
- Sylius\Bundle\WebBundle\Behat\WebContext
- Sylius\Bundle\MoneyBundle\Behat\MoneyContext
@ -175,7 +175,7 @@ default:
- Sylius\Bundle\CoreBundle\Behat\HookContext
- Sylius\Bundle\CoreBundle\Behat\CoreContext
- Sylius\Bundle\PaymentBundle\Behat\PaymentContext
- Sylius\Bundle\ResourceBundle\Behat\BaseContext
- Sylius\Bundle\ResourceBundle\Behat\FixtureContext
- Sylius\Bundle\WebBundle\Behat\WebContext
- Sylius\Bundle\MoneyBundle\Behat\MoneyContext
- Sylius\Bundle\ChannelBundle\Behat\ChannelContext
@ -204,7 +204,7 @@ default:
- Sylius\Bundle\CoreBundle\Behat\CoreContext
- Sylius\Bundle\MoneyBundle\Behat\MoneyContext
- Sylius\Bundle\ProductBundle\Behat\ProductContext
- Sylius\Bundle\ResourceBundle\Behat\BaseContext
- Sylius\Bundle\ResourceBundle\Behat\FixtureContext
- Sylius\Bundle\TaxationBundle\Behat\TaxationContext
- Sylius\Bundle\TaxonomyBundle\Behat\TaxonomyContext
- Sylius\Bundle\WebBundle\Behat\WebContext
@ -220,7 +220,7 @@ default:
- Sylius\Bundle\CoreBundle\Behat\CoreContext
- Sylius\Bundle\ProductBundle\Behat\ProductContext
- Sylius\Bundle\PromotionBundle\Behat\PromotionContext
- Sylius\Bundle\ResourceBundle\Behat\BaseContext
- Sylius\Bundle\ResourceBundle\Behat\FixtureContext
- Sylius\Bundle\TaxationBundle\Behat\TaxationContext
- Sylius\Bundle\TaxonomyBundle\Behat\TaxonomyContext
- Sylius\Bundle\WebBundle\Behat\WebContext
@ -264,7 +264,7 @@ default:
- Sylius\Bundle\AddressingBundle\Behat\AddressingContext
- Sylius\Bundle\CoreBundle\Behat\CoreContext
- Sylius\Bundle\ProductBundle\Behat\ProductContext
- Sylius\Bundle\ResourceBundle\Behat\BaseContext
- Sylius\Bundle\ResourceBundle\Behat\FixtureContext
- Sylius\Bundle\ShippingBundle\Behat\ShippingContext
- Sylius\Bundle\TaxationBundle\Behat\TaxationContext
- Sylius\Bundle\TaxonomyBundle\Behat\TaxonomyContext
@ -280,7 +280,7 @@ default:
- Sylius\Bundle\CoreBundle\Behat\HookContext
- Sylius\Bundle\AddressingBundle\Behat\AddressingContext
- Sylius\Bundle\CoreBundle\Behat\CoreContext
- Sylius\Bundle\ResourceBundle\Behat\BaseContext
- Sylius\Bundle\ResourceBundle\Behat\FixtureContext
- Sylius\Bundle\TaxationBundle\Behat\TaxationContext
- Sylius\Bundle\WebBundle\Behat\WebContext
- Sylius\Bundle\MoneyBundle\Behat\MoneyContext
@ -293,7 +293,7 @@ default:
- Behat\MinkExtension\Context\MinkContext
- Sylius\Bundle\CoreBundle\Behat\HookContext
- Sylius\Bundle\CoreBundle\Behat\CoreContext
- Sylius\Bundle\ResourceBundle\Behat\BaseContext
- Sylius\Bundle\ResourceBundle\Behat\FixtureContext
- Sylius\Bundle\TaxonomyBundle\Behat\TaxonomyContext
- Sylius\Bundle\WebBundle\Behat\WebContext
- Sylius\Bundle\MoneyBundle\Behat\MoneyContext
@ -324,7 +324,7 @@ default:
- Sylius\Bundle\PaymentBundle\Behat\PaymentContext
- Sylius\Bundle\ProductBundle\Behat\ProductContext
- Sylius\Bundle\PromotionBundle\Behat\PromotionContext
- Sylius\Bundle\ResourceBundle\Behat\BaseContext
- Sylius\Bundle\ResourceBundle\Behat\FixtureContext
- Sylius\Bundle\ShippingBundle\Behat\ShippingContext
- Sylius\Bundle\TaxonomyBundle\Behat\TaxonomyContext
- Sylius\Bundle\TaxationBundle\Behat\TaxationContext
@ -340,7 +340,7 @@ default:
- Sylius\Bundle\CoreBundle\Behat\CoreContext
- Sylius\Bundle\MoneyBundle\Behat\MoneyContext
- Sylius\Bundle\ProductBundle\Behat\ProductContext
- Sylius\Bundle\ResourceBundle\Behat\BaseContext
- Sylius\Bundle\ResourceBundle\Behat\FixtureContext
- Sylius\Bundle\TaxonomyBundle\Behat\TaxonomyContext
- Sylius\Bundle\WebBundle\Behat\WebContext
- Sylius\Bundle\AddressingBundle\Behat\AddressingContext
@ -357,7 +357,7 @@ default:
- Sylius\Bundle\AddressingBundle\Behat\AddressingContext
- Sylius\Bundle\CoreBundle\Behat\CoreContext
- Sylius\Bundle\ProductBundle\Behat\ProductContext
- Sylius\Bundle\ResourceBundle\Behat\BaseContext
- Sylius\Bundle\ResourceBundle\Behat\FixtureContext
- Sylius\Bundle\ShippingBundle\Behat\ShippingContext
- Sylius\Bundle\WebBundle\Behat\WebContext
- Sylius\Bundle\MoneyBundle\Behat\MoneyContext
@ -375,7 +375,7 @@ default:
- Sylius\Bundle\TaxonomyBundle\Behat\TaxonomyContext
- Sylius\Bundle\CoreBundle\Behat\CoreContext
- Sylius\Bundle\ProductBundle\Behat\ProductContext
- Sylius\Bundle\ResourceBundle\Behat\BaseContext
- Sylius\Bundle\ResourceBundle\Behat\FixtureContext
- Sylius\Bundle\ShippingBundle\Behat\ShippingContext
- Sylius\Bundle\WebBundle\Behat\WebContext
- Sylius\Bundle\MoneyBundle\Behat\MoneyContext
@ -387,7 +387,7 @@ default:
- Behat\MinkExtension\Context\MinkContext
- Sylius\Bundle\CoreBundle\Behat\HookContext
- Sylius\Bundle\CoreBundle\Behat\CoreContext
- Sylius\Bundle\ResourceBundle\Behat\BaseContext
- Sylius\Bundle\ResourceBundle\Behat\FixtureContext
- Sylius\Bundle\AddressingBundle\Behat\AddressingContext
- Sylius\Bundle\PromotionBundle\Behat\PromotionContext
- Sylius\Bundle\ShippingBundle\Behat\ShippingContext
@ -405,7 +405,7 @@ default:
- Sylius\Bundle\CoreBundle\Behat\HookContext
- Sylius\Bundle\CoreBundle\Behat\CoreContext
- Sylius\Bundle\MoneyBundle\Behat\MoneyContext
- Sylius\Bundle\ResourceBundle\Behat\BaseContext
- Sylius\Bundle\ResourceBundle\Behat\FixtureContext
- Sylius\Bundle\WebBundle\Behat\WebContext
- Sylius\Bundle\MailerBundle\Behat\MailerContext
- Sylius\Bundle\ChannelBundle\Behat\ChannelContext
@ -419,7 +419,7 @@ default:
- Sylius\Bundle\CoreBundle\Behat\CoreContext
- Sylius\Bundle\ProductBundle\Behat\ProductContext
- Sylius\Bundle\PromotionBundle\Behat\PromotionContext
- Sylius\Bundle\ResourceBundle\Behat\BaseContext
- Sylius\Bundle\ResourceBundle\Behat\FixtureContext
- Sylius\Bundle\TaxationBundle\Behat\TaxationContext
- Sylius\Bundle\TaxonomyBundle\Behat\TaxonomyContext
- Sylius\Bundle\WebBundle\Behat\WebContext

View file

@ -1,67 +0,0 @@
<?php
/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sylius\Bundle\ResourceBundle\Behat;
use Behat\Gherkin\Node\TableNode;
use Symfony\Component\PropertyAccess\StringUtil;
class BaseContext extends DefaultContext
{
/**
* @Given /^there are no ([^"]*)$/
*/
public function thereAreNoResources($type)
{
$type = str_replace(' ', '_', StringUtil::singularify($type));
$type = is_array($type) ? $type[1] : $type; // Hacky hack for multiple singular forms.
$type = $type == 'addresse' ? 'address' : $type; // Hacky hack again because we do not retrieve the right singular with the previous hack...
$manager = $this->getEntityManager();
foreach ($this->getRepository($type)->findAll() as $resource) {
$manager->remove($resource);
}
$manager->flush();
}
/**
* @Given /^([^""]*) with following data should be created:$/
*/
public function objectWithFollowingDataShouldBeCreated($type, TableNode $table)
{
$data = $table->getRowsHash();
$type = str_replace(' ', '_', trim($type));
$object = $this->findOneByName($type, $data['name']);
foreach ($data as $property => $value) {
$objectValue = $object->{'get'.ucfirst($property)}();
if (is_array($objectValue)) {
$objectValue = implode(',', $objectValue);
}
if ($objectValue !== $value) {
throw new \Exception(sprintf('%s object::%s has "%s" value but "%s" expected', $type, $property, $objectValue, is_array($value) ? implode(',', $value) : $value));
}
}
}
/**
* @Given /^I have deleted the ([^"]*) "([^""]*)"/
*/
public function haveDeleted($resource, $name)
{
$manager = $this->getEntityManager();
$manager->remove($this->findOneByName($resource, $name));
$manager->flush();
}
}

View file

@ -12,6 +12,7 @@
namespace Sylius\Bundle\ResourceBundle\Behat;
use Behat\Behat\Context\Context;
use Behat\Mink\Exception\ElementNotFoundException;
use Behat\MinkExtension\Context\RawMinkContext;
use Behat\Symfony2Extension\Context\KernelAwareContext;
use Doctrine\Common\Collections\ArrayCollection;
@ -26,6 +27,11 @@ use Symfony\Component\Security\Core\User\UserInterface;
abstract class DefaultContext extends RawMinkContext implements Context, KernelAwareContext
{
/**
* @var string
*/
protected $applicationName = 'sylius';
/**
* Faker.
*
@ -50,9 +56,13 @@ abstract class DefaultContext extends RawMinkContext implements Context, KernelA
*/
protected $kernel;
public function __construct()
public function __construct($applicationName = null)
{
$this->faker = FakerFactory::create();
if (null !== $applicationName) {
$this->applicationName = $applicationName;
}
}
/**
@ -111,7 +121,7 @@ abstract class DefaultContext extends RawMinkContext implements Context, KernelA
*/
protected function getRepository($resource)
{
return $this->getService('sylius.repository.'.$resource);
return $this->getService($this->applicationName.'.repository.'.$resource);
}
/**
@ -191,7 +201,7 @@ abstract class DefaultContext extends RawMinkContext implements Context, KernelA
/**
* Generate page url.
* This method uses simple convention where page argument is prefixed
* with "sylius_" and used as route name passed to router generate method.
* with the application name and used as route name passed to router generate method.
*
* @param object|string $page
* @param array $parameters
@ -208,11 +218,11 @@ abstract class DefaultContext extends RawMinkContext implements Context, KernelA
$routes = $this->getContainer()->get('router')->getRouteCollection();
if (null === $routes->get($route)) {
$route = 'sylius_'.$route;
$route = $this->applicationName.'_'.$route;
}
if (null === $routes->get($route)) {
$route = str_replace('sylius_', 'sylius_backend_', $route);
$route = str_replace($this->applicationName.'_', $this->applicationName.'_backend_', $route);
}
$route = str_replace(array_keys($this->actions), array_values($this->actions), $route);
@ -265,6 +275,10 @@ abstract class DefaultContext extends RawMinkContext implements Context, KernelA
/**
* Presses button with specified id|name|title|alt|value.
*
* @param string $button
*
* @throws ElementNotFoundException
*/
protected function pressButton($button)
{
@ -273,6 +287,10 @@ abstract class DefaultContext extends RawMinkContext implements Context, KernelA
/**
* Clicks link with specified id|title|alt|text.
*
* @param string $link
*
* @throws ElementNotFoundException
*/
protected function clickLink($link)
{
@ -281,18 +299,34 @@ abstract class DefaultContext extends RawMinkContext implements Context, KernelA
/**
* Fills in form field with specified id|name|label|value.
*
* @param string $field
* @param string $value
*
* @throws ElementNotFoundException
*/
protected function fillField($field, $value)
{
$this->getSession()->getPage()->fillField($this->fixStepArgument($field), $this->fixStepArgument($value));
$this->getSession()->getPage()->fillField(
$this->fixStepArgument($field),
$this->fixStepArgument($value)
);
}
/**
* Selects option in select field with specified id|name|label|value.
*
* @param string $select
* @param string $option
*
* @throws ElementNotFoundException
*/
public function selectOption($select, $option)
protected function selectOption($select, $option)
{
$this->getSession()->getPage()->selectFieldOption($this->fixStepArgument($select), $this->fixStepArgument($option));
$this->getSession()->getPage()->selectFieldOption(
$this->fixStepArgument($select),
$this->fixStepArgument($option)
);
}
/**

View file

@ -0,0 +1,166 @@
<?php
/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sylius\Bundle\ResourceBundle\Behat;
use Behat\Gherkin\Node\TableNode;
use Symfony\Component\PropertyAccess\StringUtil;
class FixtureContext extends DefaultContext
{
/**
* @Given /^there are the following ([^"]*):$/
*/
public function thereAreFollowingResources($resource, TableNode $table)
{
foreach ($table->getHash() as $data) {
$this->thereIsResource($resource, $data, false);
}
$this->getEntityManager()->flush();
}
/**
* @Given /^there is the following "([^"]*)":$/
*/
public function thereIsResource($resource, $additionalData, $flush = true)
{
if ($additionalData instanceof TableNode) {
$additionalData = $additionalData->getHash();
}
$entity = $this->getRepository($resource)->createNew();
if (count($additionalData) > 0) {
$this->setDataToObject($entity, $additionalData);
}
return $this->persistAndFlush($entity, $flush);
}
/**
* @Given /^there are no ([^"]*)$/
*/
public function thereAreNoResources($type)
{
$type = str_replace(' ', '_', StringUtil::singularify($type));
// Hacky hack for multiple singular forms.
$type = is_array($type) ? $type[1] : $type;
// Hacky hack again because we do not retrieve the right singular with the previous hack...
$type = $type == 'addresse' ? 'address' : $type;
$manager = $this->getEntityManager();
foreach ($this->getRepository($type)->findAll() as $resource) {
$manager->remove($resource);
}
$manager->flush();
}
/**
* @Given /^([^""]*) with following data should be created:$/
*/
public function objectWithFollowingDataShouldBeCreated($type, TableNode $table)
{
$data = $table->getRowsHash();
$type = str_replace(' ', '_', trim($type));
$object = $this->findOneByName($type, $data['name']);
foreach ($data as $property => $value) {
$objectValue = $object->{'get'.ucfirst($property)}();
if (is_array($objectValue)) {
$objectValue = implode(',', $objectValue);
}
if ($objectValue !== $value) {
throw new \Exception(sprintf(
'%s object::%s has "%s" value but "%s" expected',
$type,
$property,
$objectValue,
is_array($value) ? implode(',', $value) : $value)
);
}
}
}
/**
* @Given /^I have deleted the ([^"]*) "([^""]*)"/
*/
public function haveDeleted($resource, $name)
{
$manager = $this->getEntityManager();
$manager->remove($this->findOneByName($resource, $name));
$manager->flush();
}
/**
* @Given I deleted :type with :property :value
*/
public function iDeletedResource($type, $property, $value)
{
$user = $this->findOneBy($type, array($property => $value));
$entityManager = $this->getEntityManager();
$entityManager->remove($user);
$entityManager->flush();
}
/**
* Set data to an object.
*
* @param $object
* @param $data
*/
protected function setDataToObject($object, array $data)
{
foreach ($data as $property => $value) {
if (1 === preg_match('/date/', strtolower($property))) {
$value = new \DateTime($value);
}
$propertyName = ucfirst($property);
if (false !== strpos(' ', $property)) {
$propertyName = '';
$propertyParts = explode(' ', $property);
foreach ($propertyParts as $part) {
$part = ucfirst($part);
$propertyName .= $part;
}
}
$method = 'set'.$propertyName;
if (method_exists($object, $method)) {
$object->{'set'.$propertyName}($value);
}
}
}
/**
* Persist and flush $entity.
*
* @param object $entity
* @param bool $flush
*
* @return mixed
*/
protected function persistAndFlush($entity, $flush = true)
{
$this->getEntityManager()->persist($entity);
if ($flush) {
$this->getEntityManager()->flush();
}
return $entity;
}
}

View file

@ -0,0 +1,327 @@
<?php
/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sylius\Bundle\ResourceBundle\Behat;
use Behat\Mink\Exception\UnsupportedDriverActionException;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
class WebContext extends DefaultContext
{
/**
* @Given /^I am on the (.+) (page|step)?$/
* @When /^I go to the (.+) (page|step)?$/
*/
public function iAmOnThePage($page)
{
$this->getSession()->visit($this->generatePageUrl($page));
}
/**
* @Then /^I should be on the (.+) (page|step)$/
* @Then /^I should be redirected to the (.+) (page|step)$/
* @Then /^I should still be on the (.+) (page|step)$/
*/
public function iShouldBeOnThePage($page)
{
$this->assertSession()->addressEquals($this->generatePageUrl($page));
try {
$this->assertStatusCodeEquals(200);
} catch (UnsupportedDriverActionException $e) {
}
}
/**
* @Given /^I am on the page of ([^""]*) with ([^""]*) "([^""]*)"$/
* @Given /^I go to the page of ([^""]*) with ([^""]*) "([^""]*)"$/
*/
public function iAmOnTheResourcePage($type, $property, $value)
{
$type = str_replace(' ', '_', $type);
$entityManager = $this->getEntityManager();
$entityManager->getFilters()->disable('softdeleteable');
$resource = $this->findOneBy($type, array($property => $value));
$entityManager->getFilters()->enable('softdeleteable');
$this->getSession()->visit($this->generatePageUrl(
sprintf('%s_show', $type), array('id' => $resource->getId())
));
}
/**
* @Given /^I am on the page of ([^""(w)]*) "([^""]*)"$/
* @Given /^I go to the page of ([^""(w)]*) "([^""]*)"$/
*/
public function iAmOnTheResourcePageByName($type, $name)
{
$this->iAmOnTheResourcePage($type, 'name', $name);
}
/**
* @Then /^I should be on the page of ([^""]*) with ([^""]*) "([^""]*)"$/
* @Then /^I should still be on the page of ([^""]*) with ([^""]*) "([^""]*)"$/
*/
public function iShouldBeOnTheResourcePage($type, $property, $value)
{
$type = str_replace(' ', '_', $type);
$entityManager = $this->getEntityManager();
$entityManager->getFilters()->disable('softdeleteable');
$resource = $this->findOneBy($type, array($property => $value));
$entityManager->getFilters()->enable('softdeleteable');
$this->assertSession()->addressEquals($this->generatePageUrl(
sprintf('%s_show', $type), array('id' => $resource->getId())
));
$this->assertStatusCodeEquals(200);
}
/**
* @Then /^I should be on the page of ([^""(w)]*) "([^""]*)"$/
* @Then /^I should still be on the page of ([^""(w)]*) "([^""]*)"$/
*/
public function iShouldBeOnTheResourcePageByName($type, $name)
{
$this->iShouldBeOnTheResourcePage($type, 'name', $name);
}
/**
* @Given /^I am (building|viewing|editing) ([^""]*) with ([^""]*) "([^""]*)"$/
*/
public function iAmDoingSomethingWithResource($action, $type, $property, $value)
{
$type = str_replace(' ', '_', $type);
$action = str_replace(array_keys($this->actions), array_values($this->actions), $action);
$resource = $this->findOneBy($type, array($property => $value));
$this->getSession()->visit($this->generatePageUrl(
sprintf('%s_%s', $type, $action), array('id' => $resource->getId())
));
}
/**
* @Given /^I am (building|viewing|editing) ([^""(w)]*) "([^""]*)"$/
*/
public function iAmDoingSomethingWithResourceByName($action, $type, $name)
{
$this->iAmDoingSomethingWithResource($action, $type, 'name', $name);
}
/**
* @Then /^I should be (building|viewing|editing) ([^"]*) with ([^"]*) "([^""]*)"$/
*/
public function iShouldBeDoingSomethingWithResource($action, $type, $property, $value)
{
$type = str_replace(' ', '_', $type);
$action = str_replace(array_keys($this->actions), array_values($this->actions), $action);
$resource = $this->findOneBy($type, array($property => $value));
$this->assertSession()->addressEquals($this->generatePageUrl(
sprintf('%s_%s', $type, $action), array('id' => $resource->getId())
));
$this->assertStatusCodeEquals(200);
}
/**
* @Then /^I should be (building|viewing|editing) ([^""(w)]*) "([^""]*)"$/
*/
public function iShouldBeDoingSomethingWithResourceByName($action, $type, $name)
{
$this->iShouldBeDoingSomethingWithResource($action, $type, 'name', $name);
}
/**
* @Then /^(?:.* )?"([^"]*)" should appear on the page$/
*/
public function textShouldAppearOnThePage($text)
{
$this->assertSession()->pageTextContains($text);
}
/**
* @Then /^(?:.* )?"([^"]*)" should not appear on the page$/
*/
public function textShouldNotAppearOnThePage($text)
{
$this->assertSession()->pageTextNotContains($text);
}
/**
* @When /^I click "([^"]+)"$/
*/
public function iClick($link)
{
$this->clickLink($link);
}
/**
* @Given /^I should see an? "(?P<element>[^"]*)" element near "([^"]*)"$/
*/
public function iShouldSeeAElementNear($element, $value)
{
$tr = $this->assertSession()->elementExists('css', sprintf('table tbody tr:contains("%s")', $value));
$this->assertSession()->elementExists('css', $element, $tr);
}
/**
* @When /^I click "([^"]*)" near "([^"]*)"$/
* @When /^I press "([^"]*)" near "([^"]*)"$/
*/
public function iClickNear($button, $value)
{
$tr = $this->assertSession()->elementExists('css', sprintf('table tbody tr:contains("%s")', $value));
$locator = sprintf('button:contains("%s")', $button);
if ($tr->has('css', $locator)) {
$tr->find('css', $locator)->press();
} else {
$tr->clickLink($button);
}
}
/**
* @Then /^I should see "([^"]*)" field error$/
*/
public function iShouldSeeFieldError($field)
{
$this->assertSession()->elementExists('xpath', sprintf(
"//div[contains(@class, 'error')]//label[text()[contains(., '%s')]]", ucfirst($field)
));
}
/**
* @Given /^I should see (\d+) validation errors$/
*/
public function iShouldSeeFieldsOnError($amount)
{
$this->assertSession()->elementsCount('css', '.form-error', $amount);
}
/**
* @Given /^I leave "([^"]*)" empty$/
* @Given /^I leave "([^"]*)" field blank/
*/
public function iLeaveFieldEmpty($field)
{
$this->fillField($field, '');
}
/**
* For example: I should see product with name "Wine X" in that list.
*
* @Then /^I should see [\w\s]+ with [\w\s]+ "([^""]*)" in (that|the) list$/
*/
public function iShouldSeeResourceWithValueInThatList($value)
{
$this->assertSession()->elementTextContains('css', 'table', $value);
}
/**
* For example: I should not see product with name "Wine X" in that list.
*
* @Then /^I should not see [\w\s]+ with [\w\s]+ "([^""]*)" in (that|the) list$/
*/
public function iShouldNotSeeResourceWithValueInThatList($value)
{
$this->assertSession()->elementTextNotContains('css', 'table', $value);
}
/**
* For example: I should see 10 products in that list.
*
* @Then /^I should see (\d+) ([^""]*) in (that|the) list$/
*/
public function iShouldSeeThatMuchResourcesInTheList($amount, $type)
{
if (1 === count($this->getSession()->getPage()->findAll('css', 'table'))) {
$this->assertSession()->elementsCount('css', 'table tbody > tr', $amount);
} else {
$this->assertSession()->elementsCount(
'css',
sprintf('table#%s tbody > tr', str_replace(' ', '-', $type)),
$amount
);
}
}
/**
* @Then /^I should be logged in$/
*/
public function iShouldBeLoggedIn()
{
if (!$this->getSecurityContext()->isGranted('ROLE_USER')) {
throw new AuthenticationException('User is not authenticated.');
}
}
/**
* @Then /^I should not be logged in$/
*/
public function iShouldNotBeLoggedIn()
{
if ($this->getSecurityContext()->isGranted('ROLE_USER')) {
throw new AuthenticationException('User was not expected to be logged in, but he is.');
}
}
/**
* @Given /^I click "([^"]*)" from the confirmation modal$/
*/
public function iClickOnConfirmationModal($button)
{
$this->assertSession()->elementExists('css', '#confirmation-modal');
$modalContainer = $this->getSession()->getPage()->find('css', '#confirmation-modal');
$primaryButton = $modalContainer->find('css', sprintf('a:contains("%s")' ,$button));
$this->getSession()->wait(100);
if (!preg_match('/in/', $modalContainer->getAttribute('class'))) {
throw new \Exception('The confirmation modal was not opened...');
}
$this->getSession()->wait(100);
$primaryButton->press();
}
/**
* @Given /^I wait (\d+) (seconds|second)$/
*/
public function iWait($time)
{
$this->getSession()->wait($time*1000);
}
/**
* @Then I should have my access denied
*/
public function iShouldHaveMyAccessDenied()
{
$this->assertStatusCodeEquals(403);
}
/**
* Assert that given code equals the current one.
*
* @param integer $code
*/
protected function assertStatusCodeEquals($code)
{
$this->assertSession()->statusCodeEquals($code);
}
}

View file

@ -13,10 +13,8 @@ namespace Sylius\Bundle\WebBundle\Behat;
use Behat\Gherkin\Node\TableNode;
use Behat\Mink\Exception\ElementNotFoundException;
use Behat\Mink\Exception\UnsupportedDriverActionException;
use Sylius\Bundle\ResourceBundle\Behat\DefaultContext;
use Sylius\Bundle\ResourceBundle\Behat\WebContext as BaseWebContext;
use Symfony\Component\Intl\Intl;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Behat\Behat\Context\SnippetAcceptingContext;
/**
@ -24,7 +22,7 @@ use Behat\Behat\Context\SnippetAcceptingContext;
*
* @author Paweł Jędrzejewski <pawel@sylius.org>
*/
class WebContext extends DefaultContext implements SnippetAcceptingContext
class WebContext extends BaseWebContext implements SnippetAcceptingContext
{
/**
* @Given /^go to "([^""]*)" tab$/
@ -50,30 +48,6 @@ class WebContext extends DefaultContext implements SnippetAcceptingContext
$this->getSession()->visit('/');
}
/**
* @Given /^I am on the (.+) (page|step)?$/
* @When /^I go to the (.+) (page|step)?$/
*/
public function iAmOnThePage($page)
{
$this->getSession()->visit($this->generatePageUrl($page));
}
/**
* @Then /^I should be on the (.+) (page|step)$/
* @Then /^I should be redirected to the (.+) (page|step)$/
* @Then /^I should still be on the (.+) (page|step)$/
*/
public function iShouldBeOnThePage($page)
{
$this->assertSession()->addressEquals($this->generatePageUrl($page));
try {
$this->assertStatusCodeEquals(200);
} catch (UnsupportedDriverActionException $e) {
}
}
/**
* @Given /^I should be on the store homepage$/
*/
@ -259,31 +233,6 @@ class WebContext extends DefaultContext implements SnippetAcceptingContext
$this->getSession()->visit($this->generatePageUrl('backend_shipment_show', array('id' => $shipment->getId())));
}
/**
* @Given /^I am on the page of ([^""]*) with ([^""]*) "([^""]*)"$/
* @Given /^I go to the page of ([^""]*) with ([^""]*) "([^""]*)"$/
*/
public function iAmOnTheResourcePage($type, $property, $value)
{
$type = str_replace(' ', '_', $type);
$entityManager = $this->getEntityManager();
$entityManager->getFilters()->disable('softdeleteable');
$resource = $this->findOneBy($type, array($property => $value));
$entityManager->getFilters()->enable('softdeleteable');
$this->getSession()->visit($this->generatePageUrl(sprintf('backend_%s_show', $type), array('id' => $resource->getId())));
}
/**
* @Given /^I am on the page of ([^""(w)]*) "([^""]*)"$/
* @Given /^I go to the page of ([^""(w)]*) "([^""]*)"$/
*/
public function iAmOnTheResourcePageByName($type, $name)
{
$this->iAmOnTheResourcePage($type, 'name', $name);
}
/**
* @Then /^I should be on the shipment page with method "([^"]*)"$/
*/
@ -296,67 +245,6 @@ class WebContext extends DefaultContext implements SnippetAcceptingContext
$this->assertStatusCodeEquals(200);
}
/**
* @Then /^I should be on the page of ([^""]*) with ([^""]*) "([^""]*)"$/
* @Then /^I should still be on the page of ([^""]*) with ([^""]*) "([^""]*)"$/
*/
public function iShouldBeOnTheResourcePage($type, $property, $value)
{
$type = str_replace(' ', '_', $type);
$entityManager = $this->getEntityManager();
$entityManager->getFilters()->disable('softdeleteable');
$resource = $this->findOneBy($type, array($property => $value));
$entityManager->getFilters()->enable('softdeleteable');
$this->assertSession()->addressEquals($this->generatePageUrl(sprintf('backend_%s_show', $type), array('id' => $resource->getId())));
$this->assertStatusCodeEquals(200);
}
/**
* @Then /^I should be on the page of ([^""(w)]*) "([^""]*)"$/
* @Then /^I should still be on the page of ([^""(w)]*) "([^""]*)"$/
*/
public function iShouldBeOnTheResourcePageByName($type, $name)
{
$this->iShouldBeOnTheResourcePage($type, 'name', $name);
}
/**
* @Given /^I am (building|viewing|editing) ([^""]*) with ([^""]*) "([^""]*)"$/
*/
public function iAmDoingSomethingWithResource($action, $type, $property, $value)
{
$type = str_replace(' ', '_', $type);
$action = str_replace(array_keys($this->actions), array_values($this->actions), $action);
$resource = $this->findOneBy($type, array($property => $value));
$this->getSession()->visit($this->generatePageUrl(sprintf('backend_%s_%s', $type, $action), array('id' => $resource->getId())));
}
/**
* @Given /^I am (building|viewing|editing) ([^""(w)]*) "([^""]*)"$/
*/
public function iAmDoingSomethingWithResourceByName($action, $type, $name)
{
$this->iAmDoingSomethingWithResource($action, $type, 'name', $name);
}
/**
* @Then /^I should be (building|viewing|editing) ([^"]*) with ([^"]*) "([^""]*)"$/
*/
public function iShouldBeDoingSomethingWithResource($action, $type, $property, $value)
{
$type = str_replace(' ', '_', $type);
$action = str_replace(array_keys($this->actions), array_values($this->actions), $action);
$resource = $this->findOneBy($type, array($property => $value));
$this->assertSession()->addressEquals($this->generatePageUrl(sprintf('sylius_backend_%s_%s', $type, $action), array('id' => $resource->getId())));
$this->assertStatusCodeEquals(200);
}
/**
* @Given /^I remove property choice number (\d+)$/
*/
@ -370,14 +258,6 @@ class WebContext extends DefaultContext implements SnippetAcceptingContext
;
}
/**
* @Then /^I should be (building|viewing|editing) ([^""(w)]*) "([^""]*)"$/
*/
public function iShouldBeDoingSomethingWithResourceByName($action, $type, $name)
{
$this->iShouldBeDoingSomethingWithResource($action, $type, 'name', $name);
}
/**
* @Given /^I am creating variant of "([^""]*)"$/
*/
@ -408,30 +288,6 @@ class WebContext extends DefaultContext implements SnippetAcceptingContext
$this->pressButton('Add to cart');
}
/**
* @Then /^(?:.* )?"([^"]*)" should appear on the page$/
*/
public function textShouldAppearOnThePage($text)
{
$this->assertSession()->pageTextContains($text);
}
/**
* @Then /^(?:.* )?"([^"]*)" should not appear on the page$/
*/
public function textShouldNotAppearOnThePage($text)
{
$this->assertSession()->pageTextNotContains($text);
}
/**
* @When /^I click "([^"]+)"$/
*/
public function iClick($link)
{
$this->clickLink($link);
}
/**
* @Given /^I fill in province name with "([^"]*)"$/
*/
@ -494,15 +350,6 @@ class WebContext extends DefaultContext implements SnippetAcceptingContext
$this->fillField($radio->getAttribute('name'), $radio->getAttribute('value'));
}
/**
* @Given /^I should see an? "(?P<element>[^"]*)" element near "([^"]*)"$/
*/
public function iShouldSeeAElementNear($element, $value)
{
$tr = $this->assertSession()->elementExists('css', sprintf('table tbody tr:contains("%s")', $value));
$this->assertSession()->elementExists('css', $element, $tr);
}
/**
* @Then I should not see :position in the menu
*/
@ -536,41 +383,6 @@ class WebContext extends DefaultContext implements SnippetAcceptingContext
$this->assertSession()->elementNotExists('css', "#".$table." tr[data-customer='$customer'] form input[value=".strtoupper($button)."]");
}
/**
* @When /^I click "([^"]*)" near "([^"]*)"$/
* @When /^I press "([^"]*)" near "([^"]*)"$/
*/
public function iClickNear($button, $value)
{
$tr = $this->assertSession()->elementExists('css', sprintf('table tbody tr:contains("%s")', $value));
$locator = sprintf('button:contains("%s")', $button);
if ($tr->has('css', $locator)) {
$tr->find('css', $locator)->press();
} else {
$tr->clickLink($button);
}
}
/**
* @Then /^I should see "([^"]*)" field error$/
*/
public function iShouldSeeFieldError($field)
{
$this->assertSession()->elementExists('xpath', sprintf(
"//div[contains(@class, 'error')]//label[text()[contains(., '%s')]]", ucfirst($field)
));
}
/**
* @Given /^I should see (\d+) validation errors$/
*/
public function iShouldSeeFieldsOnError($amount)
{
$this->assertSession()->elementsCount('css', '.form-error', $amount);
}
/**
* @Then /^I should see product prices in "([^"]*)"$/
*/
@ -633,49 +445,6 @@ class WebContext extends DefaultContext implements SnippetAcceptingContext
$this->assertSession()->pageTextContains($text);
}
/**
* @Given /^I leave "([^"]*)" empty$/
* @Given /^I leave "([^"]*)" field blank/
*/
public function iLeaveFieldEmpty($field)
{
$this->fillField($field, '');
}
/**
* For example: I should see product with name "Wine X" in that list.
*
* @Then /^I should see [\w\s]+ with [\w\s]+ "([^""]*)" in (that|the) list$/
*/
public function iShouldSeeResourceWithValueInThatList($value)
{
$this->assertSession()->elementTextContains('css', 'table', $value);
}
/**
* For example: I should not see product with name "Wine X" in that list.
*
* @Then /^I should not see [\w\s]+ with [\w\s]+ "([^""]*)" in (that|the) list$/
*/
public function iShouldNotSeeResourceWithValueInThatList($value)
{
$this->assertSession()->elementTextNotContains('css', 'table', $value);
}
/**
* For example: I should see 10 products in that list.
*
* @Then /^I should see (\d+) ([^""]*) in (that|the) list$/
*/
public function iShouldSeeThatMuchResourcesInTheList($amount, $type)
{
if (1 === count($this->getSession()->getPage()->findAll('css', 'table'))) {
$this->assertSession()->elementsCount('css', 'table tbody > tr', $amount);
} else {
$this->assertSession()->elementsCount('css', sprintf('table#%s tbody > tr', str_replace(' ', '-', $type)), $amount);
}
}
/**
* For example: I should see 10 products.
*
@ -752,26 +521,6 @@ class WebContext extends DefaultContext implements SnippetAcceptingContext
$this->pressButton('Login');
}
/**
* @Then /^I should be logged in$/
*/
public function iShouldBeLoggedIn()
{
if (!$this->getSecurityContext()->isGranted('ROLE_USER')) {
throw new AuthenticationException('User is not authenticated.');
}
}
/**
* @Then /^I should not be logged in$/
*/
public function iShouldNotBeLoggedIn()
{
if ($this->getSecurityContext()->isGranted('ROLE_USER')) {
throw new AuthenticationException('User was not expected to be logged in, but he is.');
}
}
/**
* @Given /^I add following option values:$/
*/
@ -847,56 +596,6 @@ class WebContext extends DefaultContext implements SnippetAcceptingContext
\PHPUnit_Framework_Assert::assertEquals($expectedValue, $cols[$column]->getText());
}
/**
* @Given /^I click "([^"]*)" from the confirmation modal$/
*/
public function iClickOnConfirmationModal($button)
{
$this->assertSession()->elementExists('css', '#confirmation-modal');
$modalContainer = $this->getSession()->getPage()->find('css', '#confirmation-modal');
$primaryButton = $modalContainer->find('css', sprintf('a:contains("%s")' ,$button));
$this->getSession()->wait(100);
if (!preg_match('/in/', $modalContainer->getAttribute('class'))) {
throw new \Exception('The confirmation modal was not opened...');
}
$this->getSession()->wait(100);
$primaryButton->press();
}
/**
* Assert that given code equals the current one.
*
* @param integer $code
*/
protected function assertStatusCodeEquals($code)
{
$this->assertSession()->statusCodeEquals($code);
}
/**
* @Given /^I wait (\d+) (seconds|second)$/
*/
public function iWait($time)
{
$this->getSession()->wait($time*1000);
}
/**
* @Given I deleted :type with :property :value
*/
public function iDeletedResource($type, $property, $value)
{
$user = $this->findOneBy($type, array($property => $value));
$entityManager = $this->getEntityManager();
$entityManager->remove($user);
$entityManager->flush();
}
/**
* @Given I view deleted elements
*/
@ -924,14 +623,6 @@ class WebContext extends DefaultContext implements SnippetAcceptingContext
$this->assertSession()->elementTextContains('css', '#'.$id.' > tbody > tr > td > p', $allSurnames[0]);
}
/**
* @Then I should have my access denied
*/
public function iShouldHaveMyAccessDenied()
{
$this->assertStatusCodeEquals(403);
}
/**
* @Given I registered with email :email and password :password
*/