From 65747ef1316f42d606a5773d31c6e63417947934 Mon Sep 17 00:00:00 2001 From: everzet Date: Fri, 11 May 2012 10:10:51 +0200 Subject: [PATCH] moved default session configuration and active sessions teardown into initializer --- .../Initializer/MinkAwareInitializer.php | 74 ++++++++++++++++++- .../MinkExtension/Context/MinkContext.php | 25 ------- .../MinkExtension/Context/MinkDictionary.php | 27 ------- src/Behat/MinkExtension/services/mink.xml | 1 + 4 files changed, 73 insertions(+), 54 deletions(-) diff --git a/src/Behat/MinkExtension/Context/Initializer/MinkAwareInitializer.php b/src/Behat/MinkExtension/Context/Initializer/MinkAwareInitializer.php index 061a190..e1fd895 100644 --- a/src/Behat/MinkExtension/Context/Initializer/MinkAwareInitializer.php +++ b/src/Behat/MinkExtension/Context/Initializer/MinkAwareInitializer.php @@ -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 */ -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(); + } } diff --git a/src/Behat/MinkExtension/Context/MinkContext.php b/src/Behat/MinkExtension/Context/MinkContext.php index 269e3a4..0bbf7b6 100644 --- a/src/Behat/MinkExtension/Context/MinkContext.php +++ b/src/Behat/MinkExtension/Context/MinkContext.php @@ -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. * diff --git a/src/Behat/MinkExtension/Context/MinkDictionary.php b/src/Behat/MinkExtension/Context/MinkDictionary.php index 4152a3f..e492a08 100644 --- a/src/Behat/MinkExtension/Context/MinkDictionary.php +++ b/src/Behat/MinkExtension/Context/MinkDictionary.php @@ -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. * diff --git a/src/Behat/MinkExtension/services/mink.xml b/src/Behat/MinkExtension/services/mink.xml index b7961b5..b3ccd46 100644 --- a/src/Behat/MinkExtension/services/mink.xml +++ b/src/Behat/MinkExtension/services/mink.xml @@ -42,6 +42,7 @@ %behat.mink.parameters% +