Skip to content

Commit

Permalink
Added extra definition for resetting to the default base url.
Browse files Browse the repository at this point in the history
  • Loading branch information
kalpaitch committed Dec 22, 2016
1 parent 9fcc355 commit 7977066
Show file tree
Hide file tree
Showing 4 changed files with 187 additions and 148 deletions.
15 changes: 8 additions & 7 deletions src/Behat/DomainExtension/Context/DomainAwareInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
use Behat\Behat\Context\Context;
use Behat\Mink\Mink;

interface DomainAwareInterface extends Context {
interface DomainAwareInterface extends Context
{

/**
* Sets the available domains.
*
* @param array $domains Domain list
*/
public function setDomains(array $domains);
/**
* Sets the available domains.
*
* @param array $domains Domain list
*/
public function setDomains(array $domains);

}
153 changes: 90 additions & 63 deletions src/Behat/DomainExtension/Context/DomainContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,80 +8,107 @@
/**
* Provides pre-built step definitions for interacting with multiple domains.
*/
class DomainContext extends RawMinkContext implements DomainAwareInterface {
class DomainContext extends RawMinkContext implements DomainAwareInterface
{

private $domains;
private $domain_url;
private $domains;
private $domainUrl;

/**
* {@inheritdoc}
*/
public function setDomains(array $domains) {
$this->domains = $domains;
}
/**
* {@inheritdoc}
*/
public function setDomains(array $domains)
{
$this->domains = $domains;
}

/**
* {@inheritdoc}
*/
public function setDomainUrl($url)
{
$this->domainUrl = $url;
}

/**
* {@inheritdoc}
*/
public function setDomainUrl($url) {
$this->domain_url = $url;
}
/**
* {@inheritdoc}
*/
public function getDomains()
{
if (null === $this->domains) {
throw new \RuntimeException(
'Trying to use the domain context with no configured domains.'
);
}

/**
* {@inheritdoc}
*/
public function getDomains() {
if (null === $this->domains) {
throw new \RuntimeException(
'Trying to use the domain context with no configured domains.'
);
return $this->domains;
}

return $this->domains;
}
/**
* {@inheritdoc}
*/
public function getDomainUrl()
{
return $this->domainUrl;
}

/**
* Change the Mink Extension base url to the domain url
* to allow Mink Contexts to read our domain url.
*
* @BeforeStep
*/
public function gatherContexts(BeforeStepScope $scope)
{
$environment = $scope->getEnvironment();

/**
* {@inheritdoc}
*/
public function getDomainUrl() {
return $this->domain_url;
}
foreach ($environment->getContexts() as $context) {
if ($context instanceof RawMinkContext && !empty($this->getDomainUrl())) {
$context->setMinkParameter('base_url', $this->getDomainUrl());
}
}
}

/**
* Change the Mink Extension base url to the domain url
* to allow Mink Contexts to read our domain url.
*
* @BeforeStep
*/
public function gatherContexts(BeforeStepScope $scope) {
$environment = $scope->getEnvironment();
/**
* Opens domain given it's alias.
* Example: Given I am on the domain "Example"
* Example: And I am on the domain "Example"
* Example: When I go to the domain "Example"
*
* @see The domain_map parameter for configuring domain aliases.
*
* @Given I am on the domain :domain
* @When I go to the domain :domain
*/
public function assertValidDomain($domain)
{
$domains = $this->getDomains();

foreach ($environment->getContexts() as $context) {
if ($context instanceof RawMinkContext && !empty($this->getDomainUrl())) {
$context->setMinkParameter('base_url', $this->getDomainUrl());
}
if (!empty($domains) && isset($domains[$domain])) {
$this->setDomainUrl($domains[$domain]);
$this->visitPath($this->getDomainUrl());
return;
}
throw new \Exception(sprintf('The domain "%s" has not been configured', $domain));
}
}

/**
* Opens domain given it's alias.
* Example: Given I am on the domain "Example"
* Example: And I am on the domain "Example"
* Example: When I go to the domain "Example"
*
* @see The domain_map parameter for configuring domain aliases.
*
* @Given I am on the domain :domain
* @When I go to the domain :domain
*/
public function assertValidDomain($domain) {
$domains = $this->getDomains();
/**
* Resets to and opens the default url given by the Mink base_url parameter.
*
* @see The Mink base_url parameter.
*
* @Given I am on the default domain
* @When I go to the default domain
*/
public function assertDefaultDomain()
{
$base_url = $this->getMinkParameter('base_url');

if (!empty($domains) && isset($domains[$domain])) {
$this->setDomainUrl($domains[$domain]);
$this->visitPath($domains[$domain]);
return;
if (!empty($base_url)) {
$this->setDomainUrl($base_url);
$this->visitPath($this->getDomainUrl());
return;
}
throw new \Exception('The Mink base-url has not been configured');
}
throw new \Exception(sprintf('The domain "%s" has not been configured', $domain));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,26 @@
use Behat\Behat\Context\Initializer\ContextInitializer;
use Behat\Behat\Context\Context;

class DomainAwareInitializer implements ContextInitializer {
private $domains;
class DomainAwareInitializer implements ContextInitializer
{
private $domains;

public function __construct($domains) {
$this->domains = $domains;
}

/**
* {@inheritdocs}
*/
public function initializeContext(Context $context) {
// All contexts are passed here, only DomainAwareInterface is allowed.
if (!$context instanceof DomainAwareInterface) {
return;
public function __construct($domains)
{
$this->domains = $domains;
}

$context->setDomains($this->domains);
}
/**
* {@inheritdocs}
*/
public function initializeContext(Context $context)
{
// All contexts are passed here, only DomainAwareInterface is allowed.
if (!$context instanceof DomainAwareInterface) {
return;
}

$context->setDomains($this->domains);
}

}
134 changes: 71 additions & 63 deletions src/Behat/DomainExtension/ServiceContainer/DomainExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,80 +12,88 @@
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;

class DomainExtension implements ExtensionInterface {
class DomainExtension implements ExtensionInterface
{

/**
* Extension configuration ID.
*/
const DOMAIN_ID = 'domain';
/**
* Extension configuration ID.
*/
const DOMAIN_ID = 'domain';

/**
* {@inheritDoc}
*/
public function getConfigKey() {
return self::DOMAIN_ID;
}
/**
* {@inheritDoc}
*/
public function getConfigKey()
{
return self::DOMAIN_ID;
}

/**
* {@inheritDoc}
*/
public function initialize(ExtensionManager $extensionManager) {
}
/**
* {@inheritDoc}
*/
public function initialize(ExtensionManager $extensionManager)
{
}

/**
* {@inheritDoc}
*/
public function load(ContainerBuilder $container, array $config) {
$loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/../config'));
$loader->load('services.yml');
/**
* {@inheritDoc}
*/
public function load(ContainerBuilder $container, array $config)
{
$loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/../config'));
$loader->load('services.yml');

$this->loadParameters($container, $config);
$this->loadContextInitializer($container);
}
$this->loadParameters($container, $config);
$this->loadContextInitializer($container);
}

/**
* {@inheritDoc}
*/
public function process(ContainerBuilder $container) {
}
/**
* {@inheritDoc}
*/
public function process(ContainerBuilder $container)
{
}

/**
* {@inheritDoc}
*/
public function configure(ArrayNodeDefinition $builder) {
$builder->
children()->
/**
* {@inheritDoc}
*/
public function configure(ArrayNodeDefinition $builder)
{
$builder->
children()->
arrayNode('domain_map')->
info("Targeting specific domains can be accomplished once they have been defined." . PHP_EOL
info("Targeting specific domains can be accomplished once they have been defined." . PHP_EOL
. ' Wikipedia: "https://en.wikipedia.org"' . PHP_EOL
. ' Weather dotcom: "https://weather.com/en-GB"'. PHP_EOL
)->
useAttributeAsKey('key')->
prototype('variable')->
end()->
. ' Weather dotcom: "https://weather.com/en-GB"' . PHP_EOL
)->
useAttributeAsKey('key')->
prototype('variable')->
end()->
end()->
end();
}
end()->
end()->
end();
}

private function loadContextInitializer(ContainerBuilder $container) {
$definition = new Definition('Behat\DomainExtension\Context\Initializer\DomainAwareInitializer', array(
'%domain.domain_map%',
));
$definition->addTag(ContextExtension::INITIALIZER_TAG, array('priority' => 0));
$container->setDefinition(self::DOMAIN_ID.'.context_initializer', $definition);
}
private function loadContextInitializer(ContainerBuilder $container)
{
$definition = new Definition('Behat\DomainExtension\Context\Initializer\DomainAwareInitializer', array(
'%domain.domain_map%',
));
$definition->addTag(ContextExtension::INITIALIZER_TAG, array('priority' => 0));
$container->setDefinition(self::DOMAIN_ID . '.context_initializer', $definition);
}

/**
* Load test parameters.
*/
private function loadParameters(ContainerBuilder $container, array $config) {
// Store config in parameters array to be passed into the DomainContext.
$parameters = array();
foreach ($config as $key => $value) {
$parameters[$key] = $value;
/**
* Load test parameters.
*/
private function loadParameters(ContainerBuilder $container, array $config)
{
// Store config in parameters array to be passed into the DomainContext.
$parameters = array();
foreach ($config as $key => $value) {
$parameters[$key] = $value;
}
$container->setParameter('domain.parameters', $parameters);
$container->setParameter('domain.domain_map', $parameters['domain_map']);
}
$container->setParameter('domain.parameters', $parameters);
$container->setParameter('domain.domain_map', $parameters['domain_map']);
}
}

0 comments on commit 7977066

Please sign in to comment.