moved default session configuration and active sessions teardown into initializer

This commit is contained in:
everzet
2012-05-11 10:10:51 +02:00
parent 74bcde4820
commit 65747ef131
4 changed files with 73 additions and 54 deletions

View File

@@ -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();
}
}

View File

@@ -23,31 +23,6 @@ use Behat\Behat\Context\TranslatedContextInterface,
*/
class MinkContext extends RawMinkContext implements TranslatedContextInterface
{
/**
* @BeforeScenario
*/
public function prepareMinkSessions($event)
{
$scenario = $event instanceof ScenarioEvent ? $event->getScenario() : $event->getOutline();
$session = $this->getMinkParameter('default_session');
foreach ($scenario->getTags() as $tag) {
if ('javascript' === $tag) {
$session = $this->getMinkParameter('javascript_session');
} elseif (preg_match('/^mink\:(.+)/', $tag, $matches)) {
$session = $matches[1];
}
}
if ($scenario->hasTag('insulated')) {
$this->getMink()->stopSessions();
} else {
$this->getMink()->resetSessions();
}
$this->getMink()->setDefaultSessionName($session);
}
/**
* Opens specified page.
*

View File

@@ -4,8 +4,6 @@ namespace Behat\MinkExtension\Context;
use Behat\Gherkin\Node\TableNode;
use Behat\Behat\Event\ScenarioEvent;
use Behat\Mink\Mink,
Behat\Mink\WebAssert;
@@ -93,31 +91,6 @@ trait MinkDictionary
return $this->getMink()->assertSession($name);
}
/**
* @BeforeScenario
*/
public function prepareMinkSessions($event)
{
$scenario = $event instanceof ScenarioEvent ? $event->getScenario() : $event->getOutline();
$session = $this->getMinkParameter('default_session');
foreach ($scenario->getTags() as $tag) {
if ('javascript' === $tag) {
$session = $this->getMinkParameter('javascript_session');
} elseif (preg_match('/^mink\:(.+)/', $tag, $matches)) {
$session = $matches[1];
}
}
if ($scenario->hasTag('insulated')) {
$this->getMink()->stopSessions();
} else {
$this->getMink()->resetSessions();
}
$this->getMink()->setDefaultSessionName($session);
}
/**
* Opens specified page.
*