moved default session configuration and active sessions teardown into initializer
This commit is contained in:
@@ -2,8 +2,12 @@
|
||||
|
||||
namespace Behat\MinkExtension\Context\Initializer;
|
||||
|
||||
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
||||
|
||||
use Behat\Behat\Context\Initializer\InitializerInterface,
|
||||
Behat\Behat\Context\ContextInterface;
|
||||
Behat\Behat\Context\ContextInterface,
|
||||
Behat\Behat\Event\ScenarioEvent,
|
||||
Behat\Behat\Event\OutlineEvent;
|
||||
|
||||
use Behat\Mink\Mink;
|
||||
|
||||
@@ -23,7 +27,7 @@ use Behat\MinkExtension\Context\MinkAwareContextInterface;
|
||||
*
|
||||
* @author Konstantin Kudryashov <ever.zet@gmail.com>
|
||||
*/
|
||||
class MinkAwareInitializer implements InitializerInterface
|
||||
class MinkAwareInitializer implements InitializerInterface, EventSubscriberInterface
|
||||
{
|
||||
private $mink;
|
||||
private $parameters;
|
||||
@@ -40,6 +44,32 @@ class MinkAwareInitializer implements InitializerInterface
|
||||
$this->parameters = $parameters;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array of event names this subscriber wants to listen to.
|
||||
*
|
||||
* The array keys are event names and the value can be:
|
||||
*
|
||||
* * The method name to call (priority defaults to 0)
|
||||
* * An array composed of the method name to call and the priority
|
||||
* * An array of arrays composed of the method names to call and respective
|
||||
* priorities, or 0 if unset
|
||||
*
|
||||
* For instance:
|
||||
*
|
||||
* * array('eventName' => 'methodName')
|
||||
* * array('eventName' => array('methodName', $priority))
|
||||
* * array('eventName' => array(array('methodName1', $priority), array('methodName2'))
|
||||
*
|
||||
* @return array The event names to listen to
|
||||
*/
|
||||
static function getSubscribedEvents()
|
||||
{
|
||||
return array(
|
||||
'beforeScenario' => 'prepareDefaultMinkSession',
|
||||
'afterSuite' => 'tearDownMinkSessions'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if initializer supports provided context.
|
||||
*
|
||||
@@ -75,4 +105,44 @@ class MinkAwareInitializer implements InitializerInterface
|
||||
$context->setMink($this->mink);
|
||||
$context->setMinkParameters($this->parameters);
|
||||
}
|
||||
|
||||
/**
|
||||
* Configures default Mink session before each scenario.
|
||||
* Configuration is based on scenario tags.
|
||||
*
|
||||
* `@javascript` tagged scenarios will get `javascript_session` as default session
|
||||
* `@mink:CUSTOM_NAME tagged scenarios will get `CUSTOM_NAME` as default session
|
||||
* Other scenarios get `default_session` as default session
|
||||
*
|
||||
* @param ScenarioEvent|OutlineEvent $event
|
||||
*/
|
||||
public function prepareDefaultMinkSession($event)
|
||||
{
|
||||
$scenario = $event instanceof ScenarioEvent ? $event->getScenario() : $event->getOutline();
|
||||
$session = $this->parameters['default_session'];
|
||||
|
||||
foreach ($scenario->getTags() as $tag) {
|
||||
if ('javascript' === $tag) {
|
||||
$session = $this->parameters['javascript_session'];
|
||||
} elseif (preg_match('/^mink\:(.+)/', $tag, $matches)) {
|
||||
$session = $matches[1];
|
||||
}
|
||||
}
|
||||
|
||||
if ($scenario->hasTag('insulated')) {
|
||||
$this->mink->stopSessions();
|
||||
} else {
|
||||
$this->mink->resetSessions();
|
||||
}
|
||||
|
||||
$this->mink->setDefaultSessionName($session);
|
||||
}
|
||||
|
||||
/**
|
||||
* Stops all started Mink sessions.
|
||||
*/
|
||||
public function tearDownMinkSessions()
|
||||
{
|
||||
$this->mink->stopSessions();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user