Added the possibility to define the default session per suite

This commit is contained in:
Christophe Coevoet
2014-04-26 20:23:19 +02:00
parent 1003fb7f79
commit 888ab86a72
4 changed files with 170 additions and 24 deletions

View File

@@ -255,6 +255,7 @@ class Extension implements ExtensionInterface
$container->setParameter('mink.default_session', $defaultSession);
$container->setParameter('mink.javascript_session', $javascriptSession);
$container->setParameter('mink.available_javascript_sessions', $javascriptSessions);
}
private function loadSessionsListener(ContainerBuilder $container)
@@ -263,6 +264,7 @@ class Extension implements ExtensionInterface
new Reference(self::MINK_ID),
'%mink.default_session%',
'%mink.javascript_session%',
'%mink.available_javascript_sessions%',
));
$definition->addTag(EventDispatcherExtension::SUBSCRIBER_TAG, array('priority' => 0));
$container->setDefinition('mink.listener.sessions', $definition);

View File

@@ -16,6 +16,8 @@ use Behat\Behat\EventDispatcher\Event\ScenarioTested;
use Behat\Mink\Mink;
use Behat\Testwork\EventDispatcher\Event\ExerciseCompleted;
use Behat\Testwork\ServiceContainer\Exception\ProcessingException;
use Behat\Testwork\Suite\Exception\SuiteConfigurationException;
use Behat\Testwork\Suite\Suite;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
@@ -30,18 +32,25 @@ class SessionsListener implements EventSubscriberInterface
private $defaultSession;
private $javascriptSession;
/**
* @var string[] The available javascript sessions
*/
private $availableJavascriptSessions;
/**
* Initializes initializer.
*
* @param Mink $mink
* @param string $defaultSession
* @param string|null $javascriptSession
* @param string[] $availableJavascriptSessions
*/
public function __construct(Mink $mink, $defaultSession, $javascriptSession)
public function __construct(Mink $mink, $defaultSession, $javascriptSession, array $availableJavascriptSessions = array())
{
$this->mink = $mink;
$this->defaultSession = $defaultSession;
$this->javascriptSession = $javascriptSession;
$this->availableJavascriptSessions = $availableJavascriptSessions;
}
/**
@@ -75,20 +84,20 @@ class SessionsListener implements EventSubscriberInterface
{
$scenario = $event->getScenario();
$feature = $event->getFeature();
$session = $this->defaultSession;
$session = null;
foreach (array_merge($feature->getTags(), $scenario->getTags()) as $tag) {
if ('javascript' === $tag) {
if (null === $this->javascriptSession) {
throw new ProcessingException('The @javascript tag cannot be used without enabling a javascript session');
}
$session = $this->javascriptSession;
$session = $this->getJavascriptSession($event->getSuite());
} elseif (preg_match('/^mink\:(.+)/', $tag, $matches)) {
$session = $matches[1];
}
}
if (null === $session) {
$session = $this->getDefaultSession($event->getSuite());
}
if ($scenario->hasTag('insulated') || $feature->hasTag('insulated')) {
$this->mink->stopSessions();
} else {
@@ -105,4 +114,64 @@ class SessionsListener implements EventSubscriberInterface
{
$this->mink->stopSessions();
}
private function getDefaultSession(Suite $suite)
{
if (!$suite->hasSetting('mink_session')) {
return $this->defaultSession;
}
$session = $suite->getSetting('mink_session');
if (!is_string($session)) {
throw new SuiteConfigurationException(
sprintf(
'`mink_session` setting of the "%s" suite is expected to be a string, %s given.',
$suite->getName(),
gettype($session)
),
$suite->getName()
);
}
return $session;
}
private function getJavascriptSession(Suite $suite)
{
if (!$suite->hasSetting('mink_javascript_session')) {
if (null === $this->javascriptSession) {
throw new ProcessingException('The @javascript tag cannot be used without enabling a javascript session');
}
return $this->javascriptSession;
}
$session = $suite->getSetting('mink_javascript_session');
if (!is_string($session)) {
throw new SuiteConfigurationException(
sprintf(
'`mink_javascript_session` setting of the "%s" suite is expected to be a string, %s given.',
$suite->getName(),
gettype($session)
),
$suite->getName()
);
}
if (!in_array($session, $this->availableJavascriptSessions)) {
throw new SuiteConfigurationException(
sprintf(
'`mink_javascript_session` setting of the "%s" suite is not a javascript session. %s given but expected one of %s.',
$suite->getName(),
$session,
implode(', ', $this->availableJavascriptSessions)
),
$suite->getName()
);
}
return $session;
}
}