Skip to content
This repository
Browse code

Merge branch 'release/0.3.0'

  • Loading branch information...
commit 4912e72ac0ed25fb1f1713d961d4380353b845e8 2 parents faa9007 + 72e1a54
Konstantin Kudryashov everzet authored
5 CHANGES.md
Source Rendered
... ... @@ -1,3 +1,8 @@
  1 +0.3.0 / 2011-05-17
  2 +
  3 + * Rewritten from scratch Mink drivers handler. Now it's sessions handler. And Mink now
  4 + sessions-centric tool. See examples in readme. Much cleaner API now.
  5 +
1 6 0.2.4 / 2011-05-12
2 7 ==================
3 8
36 README.md
Source Rendered
@@ -10,32 +10,38 @@ Usage
10 10 <?php
11 11
12 12 use Behat\Mink\Mink,
  13 + Behat\Mink\Session,
13 14 Behat\Mink\Driver\GoutteDriver,
14 15 Behat\Mink\Driver\SahiDriver;
15 16
16 17 $startUrl = 'http://example.com';
17 18
18 19 // init Mink and register drivers
19   -$mink = new Mink();
20   -$mink->registerDriver('goutte', new GoutteDriver($startUrl), true); // last argument === isDefault
21   -$mink->registerDriver('javascript', new SahiDriver($startUrl, 'firefox'));
22   -$mink->registerDriver('symfony2', new GoutteDriver($startUrl, $container->get('client')));
23   -$mink->registerDriver('custom', new MyCustomDriver($startUrl));
24   -
25   -// run in default driver ("goutte" is default driver - last argument to registerDriver())
26   -$mink->switchToDefaultDriver();
  20 +$mink = new Mink(
  21 + 'goutte1' => new Session(new GoutteDriver($startUrl)),
  22 + 'goutte2' => new Session(new GoutteDriver($startUrl)),
  23 + 'javascript' => new Session(new SahiDriver($startUrl, 'firefox')),
  24 + 'custom' => new Session(new MyCustomDriver($startUrl))
  25 +);
  26 +
  27 +// set active session name
  28 +$mink->setActiveSessionName('goutte2');
  29 +
  30 +// call getSession without argument will always return active session if has one (goutte2 here)
27 31 $mink->getSession()->getPage()->findLink('Downloads')->click();
28 32 echo $mink->getSession()->getPage()->getContent();
29 33
30 34 // run in javascript (Sahi) driver
31   -$mink->switchToDriver('javascript');
32   -$mink->getSession()->getPage()->findLink('Downloads')->click();
33   -echo $mink->getSession()->getPage()->getContent();
  35 +$mink->getSession('javascript')->getPage()->findLink('Downloads')->click();
  36 +echo $mink->getSession('javascript')->getPage()->getContent();
34 37
35 38 // run in custom driver
36   -$mink->switchToDriver('custom');
37   -$mink->getSession()->getPage()->findLink('Downloads')->click();
38   -echo $mink->getSession()->getPage()->getContent();
  39 +$mink->getSession('custom')->getPage()->findLink('Downloads')->click();
  40 +echo $mink->getSession('custom')->getPage()->getContent();
  41 +
  42 +// mix sessions
  43 +$mink->getSession('goutte1')->getPage()->findLink('Chat')->click();
  44 +$mink->getSession('goutte2')->getPage()->findLink('Chat')->click();
39 45 ```
40 46
41 47 Existing Sahi session usage
@@ -47,7 +53,7 @@ Everytime Mink inits SahiDriver - it tries to connect to the browser with specif
47 53 <?php
48 54
49 55 $client = new \Behat\SahiClient\Client(new \Behat\SahiClient\Connection('SAHI_SID'));
50   -$mink->registerDriver('javascript', new SahiDriver($startUrl, 'firefox', $client));
  56 +$mink->registerSession('javascript', new Session(new SahiDriver($startUrl, 'firefox', $client)));
51 57 ```
52 58
53 59 `SAHI_SID` could be any unique string.
28 src/Behat/Mink/Integration/MinkEnvironment.php
@@ -5,6 +5,7 @@
5 5 use Behat\Behat\Environment\Environment;
6 6
7 7 use Behat\Mink\Mink,
  8 + Behat\Mink\Session,
8 9 Behat\Mink\Driver\GoutteDriver,
9 10 Behat\Mink\Driver\SahiDriver;
10 11
@@ -35,7 +36,7 @@ public function __construct()
35 36 {
36 37 $world = $this;
37 38
38   - $world->drivers = array();
  39 + $world->sessions = array();
39 40 $world->getPathTo = function($path) use($world) {
40 41 $startUrl = rtrim($world->getParameter('start_url'), '/') . '/';
41 42
@@ -54,7 +55,7 @@ public function getMink()
54 55
55 56 if (null === $mink) {
56 57 $mink = new Mink();
57   - $this->registerMinkDrivers($mink);
  58 + $this->registerMinkSessions($mink);
58 59 }
59 60
60 61 return $mink;
@@ -63,33 +64,34 @@ public function getMink()
63 64 /**
64 65 * Returns current Mink session.
65 66 *
  67 + * @param string|null name of the session OR active session will be used
  68 + *
66 69 * @return Behat\Mink\Session
67 70 */
68   - public function getSession()
  71 + public function getSession($name = null)
69 72 {
70   - return $this->getMink()->getSession();
  73 + return $this->getMink()->getSession($name);
71 74 }
72 75
73 76 /**
74   - * Registers drivers on mink.
  77 + * Registers sessions on mink.
75 78 *
76 79 * @param Behat\Mink\Mink $mink
77 80 */
78   - private function registerMinkDrivers(Mink $mink)
  81 + private function registerMinkSessions(Mink $mink)
79 82 {
80 83 if (null === $this->getParameter('start_url')) {
81 84 throw new \InvalidArgumentException('Specify start_url environment parameter');
82 85 }
83   - $startUrl = $this->getParameter('start_url');
84   - $defaultDriver = $this->getParameter('default_driver') ?: 'goutte';
85   - $browser = $this->getParameter('browser') ?: 'firefox';
  86 + $startUrl = $this->getParameter('start_url');
  87 + $browser = $this->getParameter('browser') ?: 'firefox';
86 88
87 89 $config = $this->getParameter('goutte', array());
88 90 $goutte = new GoutteClient(
89 91 isset($config['zend_config']) ? $config['zend_config'] : array(),
90 92 isset($config['server_parameters']) ? $config['server_parameters'] : array()
91 93 );
92   - $mink->registerDriver('goutte', new GoutteDriver($startUrl, $goutte), 'goutte' === $defaultDriver);
  94 + $mink->registerSession('goutte', new Session(new GoutteDriver($startUrl, $goutte)));
93 95
94 96 $config = $this->getParameter('sahi', array());
95 97 $client = new SahiClient(new SahiConnection(
@@ -97,11 +99,11 @@ private function registerMinkDrivers(Mink $mink)
97 99 isset($config['host']) ? $config['host'] : 'localhost',
98 100 isset($config['port']) ? $config['port'] : 9999
99 101 ));
100   - $mink->registerDriver('sahi', new SahiDriver($startUrl, $browser, $client), 'sahi' === $defaultDriver);
  102 + $mink->registerSession('sahi', new Session(new SahiDriver($startUrl, $browser, $client)));
101 103
102 104
103   - foreach ($this->drivers as $alias => $driver) {
104   - $mink->registerDriver($alias, $driver, $driver === $this->getDefaultDriverName());
  105 + foreach ($this->sessions as $name => $session) {
  106 + $mink->registerSession($name, $session);
105 107 }
106 108 }
107 109 }
19 src/Behat/Mink/Integration/support/hooks.php
@@ -14,19 +14,20 @@
14 14 $scenario = $event instanceof ScenarioEvent ? $event->getScenario() : $event->getOutline();
15 15 $environment = $event->getEnvironment();
16 16
17   - $driver = null;
  17 + $session = null;
18 18 foreach ($scenario->getTags() as $tag) {
19 19 if ('javascript' === $tag) {
20   - $driver = 'sahi';
  20 + $session = 'sahi';
21 21 } elseif (preg_match('/^mink\:([^\n]+)/', $tag, $matches)) {
22   - $driver = $matches[1];
  22 + $session = $matches[1];
23 23 }
24 24 }
25   - if (null !== $driver) {
26   - $environment->getMink()->switchToDriver($driver);
27   - } else {
28   - $environment->getMink()->switchToDefaultDriver();
29   - }
30 25
31   - $environment->getMink()->resetDriver();
  26 + $environment->getMink()->setActiveSessionName(
  27 + $session ?: ($environment->getParameter('default_driver') ?: 'goutte')
  28 + );
  29 +
  30 + if ($environment->getSession()->isStarted()) {
  31 + $environment->getSession()->reset();
  32 + }
32 33 });
129 src/Behat/Mink/Mink.php
@@ -20,134 +20,119 @@
20 20 */
21 21 class Mink
22 22 {
23   - private $selectorsHandler;
24   - private $defaultDriverName;
25   - private $currentDriverName;
26   - private $drivers = array();
  23 + private $activeSessionName;
  24 + private $sessions = array();
27 25
28 26 /**
29 27 * Initializes mink.
30 28 *
31 29 * @param Behat\Mink\Selector\SelectorsHandler $selectorsHandler
32 30 */
33   - public function __construct(SelectorsHandler $selectorsHandler = null)
  31 + public function __construct(array $sessions = array())
34 32 {
35   - if (null === $selectorsHandler) {
36   - $selectorsHandler = new SelectorsHandler();
  33 + foreach ($sessions as $name => $session) {
  34 + $this->registerSession($name, $session);
37 35 }
38   -
39   - $this->selectorsHandler = $selectorsHandler;
40 36 }
41 37
42 38 /**
43   - * Stops all started drivers.
  39 + * Stops all started sessions.
44 40 */
45 41 public function __destruct()
46 42 {
47   - $this->stopDrivers();
  43 + $this->stopSessions();
48 44 }
49 45
50 46 /**
51   - * Registers new driver.
  47 + * Registers new session.
52 48 *
53   - * @param string $name driver alias name
54   - * @param Behat\Mink\Driver\DriverInterface $driver driver instance
55   - * @param Boolean $isDefault should this driver be the default one?
  49 + * @param string $name session alias name
  50 + * @param Behat\Mink\Session $session session instance
56 51 */
57   - public function registerDriver($name, DriverInterface $driver, $isDefault = false)
  52 + public function registerSession($name, Session $session)
58 53 {
59 54 $name = strtolower($name);
60 55
61   - $this->drivers[$name] = $driver;
62   -
63   - if ($isDefault) {
64   - $this->defaultDriverName = $name;
65   - }
  56 + $this->sessions[$name] = $session;
66 57 }
67 58
68 59 /**
69   - * Switches mink to specific driver.
  60 + * Checks whether session with specified name is registered.
  61 + *
  62 + * @param string $name
70 63 *
71   - * @param string $name driver alias name
  64 + * @return Boolean
72 65 */
73   - public function switchToDriver($name)
  66 + public function hasSession($name)
74 67 {
75   - $name = strtolower($name);
76   -
77   - if (!isset($this->drivers[$name])) {
78   - throw new \InvalidArgumentException(sprintf('Driver "%s" is not registered.', $name));
79   - }
80   -
81   - if (null !== $this->currentDriverName && $name === $this->currentDriverName) {
82   - return;
83   - }
84   -
85   - $this->currentDriverName = $name;
  68 + return isset($this->sessions[strtolower($name)]);
86 69 }
87 70
88 71 /**
89   - * Switches mink to default driver.
  72 + * Sets active session name to use.
  73 + *
  74 + * @param null|string $name name of the registered session
90 75 */
91   - public function switchToDefaultDriver()
  76 + public function setActiveSessionName($name)
92 77 {
93   - if (null === $this->defaultDriverName) {
94   - throw new \RuntimeException('Default driver is not defined.');
95   - }
  78 + $name = strtolower($name);
96 79
97   - $this->switchToDriver($this->defaultDriverName);
98   - }
  80 + if (!isset($this->sessions[$name])) {
  81 + throw new \InvalidArgumentException(sprintf('session "%s" is not registered.', $name));
  82 + }
99 83
100   - /**
101   - * Resets driver (between tests for example).
102   - */
103   - public function resetDriver()
104   - {
105   - $this->getDriver()->reset();
  84 + $this->activeSessionName = $name;
106 85 }
107 86
108 87 /**
109   - * Stop all started drivers.
  88 + * Returns currently active session name or null if none.
  89 + *
  90 + * @return null|string
110 91 */
111   - public function stopDrivers()
  92 + public function getActiveSessionName()
112 93 {
113   - foreach ($this->drivers as $driver) {
114   - if ($driver->isStarted()) {
115   - $driver->stop();
116   - }
117   - }
  94 + return $this->activeSessionName;
118 95 }
119 96
120 97 /**
121   - * Returns mink session, initialized with current (or default) driver.
  98 + * Returns registered session by it's name or active one.
  99 + *
  100 + * @param string $name session name
122 101 *
123 102 * @return Behat\Mink\Session
124 103 */
125   - public function getSession()
  104 + public function getSession($name = null)
126 105 {
127   - static $session;
  106 + $name = strtolower($name) ?: $this->activeSessionName;
  107 +
  108 + if (null === $name) {
  109 + throw new \InvalidArgumentException('specify session name to get');
  110 + }
128 111
129   - if (null === $session || $session->getDriver() !== $this->getDriver()) {
130   - $session = new Session($this->getDriver(), $this->selectorsHandler);
  112 + if (!isset($this->sessions[$name])) {
  113 + throw new \InvalidArgumentException(sprintf('session "%s" is not registered.', $name));
  114 + }
  115 +
  116 + $session = $this->sessions[$name];
  117 +
  118 + // start session if needed
  119 + if (!$session->isStarted()) {
  120 + $session->start();
  121 + $this->activeSessionName = $name;
131 122 }
132 123
133 124 return $session;
134 125 }
135 126
136 127 /**
137   - * Returns current (or default) driver.
138   - *
139   - * @return Behat\Mink\Driver\DriverInterface
  128 + * Stop all started sessions.
140 129 */
141   - private function getDriver()
  130 + public function stopSessions()
142 131 {
143   - if (null === $this->currentDriverName) {
144   - $this->switchToDefaultDriver();
145   - }
146   -
147   - if (!$this->drivers[$this->currentDriverName]->isStarted()) {
148   - $this->drivers[$this->currentDriverName]->start();
  132 + foreach ($this->sessions as $session) {
  133 + if ($session->isStarted()) {
  134 + $session->stop();
  135 + }
149 136 }
150   -
151   - return $this->drivers[$this->currentDriverName];
152 137 }
153 138 }
48 src/Behat/Mink/Session.php
@@ -31,16 +31,54 @@ class Session
31 31 * @param Behat\Mink\Driver\DriverInterface $driver driver instance
32 32 * @param Behat\Mink\Selector\SelectorsHandler $selectorsHandler selectors handler
33 33 */
34   - public function __construct(DriverInterface $driver, SelectorsHandler $selectorsHandler)
  34 + public function __construct(DriverInterface $driver, SelectorsHandler $selectorsHandler = null)
35 35 {
36 36 $driver->setSession($this);
37 37
  38 + if (null === $selectorsHandler) {
  39 + $selectorsHandler = new SelectorsHandler();
  40 + }
  41 +
38 42 $this->driver = $driver;
39 43 $this->page = new DocumentElement($this);
40 44 $this->selectorsHandler = $selectorsHandler;
41 45 }
42 46
43 47 /**
  48 + * Checks whether session (driver) was started.
  49 + *
  50 + * @return Boolean
  51 + */
  52 + public function isStarted()
  53 + {
  54 + return $this->driver->isStarted();
  55 + }
  56 +
  57 + /**
  58 + * Starts session driver.
  59 + */
  60 + public function start()
  61 + {
  62 + $this->driver->start();
  63 + }
  64 +
  65 + /**
  66 + * Stops session driver.
  67 + */
  68 + public function stop()
  69 + {
  70 + $this->driver->stop();
  71 + }
  72 +
  73 + /**
  74 + * Reset session driver.
  75 + */
  76 + public function reset()
  77 + {
  78 + $this->driver->reset();
  79 + }
  80 +
  81 + /**
44 82 * Returns session driver.
45 83 *
46 84 * @return Behat\Mink\Driver\DriverInterface
@@ -81,14 +119,6 @@ public function visit($url)
81 119 }
82 120
83 121 /**
84   - * Reset driver.
85   - */
86   - public function reset()
87   - {
88   - $this->driver->reset();
89   - }
90   -
91   - /**
92 122 * Returns all response headers.
93 123 *
94 124 * @return array
151 tests/Behat/Mink/MinkTest.php
@@ -11,87 +11,132 @@ protected function setUp()
11 11 $this->mink = new Mink();
12 12 }
13 13
14   - public function testSwitchToNotStartedDriver()
  14 + public function testRegisterSession()
15 15 {
16   - $driver = $this->getMockBuilder('Behat\Mink\Driver\DriverInterface')->getMock();
17   - $driver
  16 + $session = $this->getSessionMock();
  17 +
  18 + $this->assertFalse($this->mink->hasSession('not_registered'));
  19 + $this->assertFalse($this->mink->hasSession('js'));
  20 + $this->assertFalse($this->mink->hasSession('my'));
  21 +
  22 + $this->mink->registerSession('my', $session);
  23 +
  24 + $this->assertTrue($this->mink->hasSession('my'));
  25 + $this->assertFalse($this->mink->hasSession('not_registered'));
  26 + $this->assertFalse($this->mink->hasSession('js'));
  27 + }
  28 +
  29 + public function testSessionAutostop()
  30 + {
  31 + $session1 = $this->getSessionMock();
  32 + $session2 = $this->getSessionMock();
  33 + $this->mink->registerSession('my1', $session1);
  34 + $this->mink->registerSession('my2', $session2);
  35 +
  36 + $session1
  37 + ->expects($this->once())
  38 + ->method('isStarted')
  39 + ->will($this->returnValue(true));
  40 + $session1
  41 + ->expects($this->once())
  42 + ->method('stop');
  43 + $session2
  44 + ->expects($this->once())
  45 + ->method('isStarted')
  46 + ->will($this->returnValue(false));
  47 + $session2
  48 + ->expects($this->never())
  49 + ->method('stop');
  50 +
  51 + unset($this->mink);
  52 + }
  53 +
  54 + public function testNotStartedSession()
  55 + {
  56 + $session = $this->getSessionMock();
  57 +
  58 + $session
18 59 ->expects($this->once())
19 60 ->method('isStarted')
20 61 ->will($this->returnValue(false));
21   - $driver
  62 + $session
22 63 ->expects($this->once())
23 64 ->method('start');
24   - $driver
25   - ->expects($this->once())
26   - ->method('reset');
27 65
28   - $this->mink->registerDriver('everzet', $driver);
29   - $this->mink->switchToDriver('everzet');
30   - $this->mink->resetDriver();
  66 + $this->mink->registerSession('mock_session', $session);
  67 + $this->assertSame($session, $this->mink->getSession('mock_session'));
31 68
32 69 $this->setExpectedException('InvalidArgumentException');
33   - $this->mink->switchToDriver('undefined');
  70 +
  71 + $this->mink->getSession('not_registered');
34 72 }
35 73
36   - public function testSwitchToStartedDriver()
  74 + public function testGetAlreadyStartedSession()
37 75 {
38   - $driver = $this->getMockBuilder('Behat\Mink\Driver\DriverInterface')->getMock();
39   - $driver
  76 + $session = $this->getSessionMock();
  77 +
  78 + $session
40 79 ->expects($this->once())
41 80 ->method('isStarted')
42 81 ->will($this->returnValue(true));
43   - $driver
  82 + $session
44 83 ->expects($this->never())
45 84 ->method('start');
46   - $driver
47   - ->expects($this->once())
48   - ->method('reset');
49 85
50   - $this->mink->registerDriver('everzet', $driver);
51   - $this->mink->switchToDriver('everzet');
52   - $this->mink->resetDriver();
  86 + $this->mink->registerSession('mock_session', $session);
  87 + $this->assertSame($session, $this->mink->getSession('mock_session'));
  88 + }
  89 +
  90 + public function testSetActiveSessionName()
  91 + {
  92 + $this->assertNull($this->mink->getActiveSessionName());
  93 +
  94 + $session = $this->getSessionMock();
  95 + $this->mink->registerSession('session_name', $session);
  96 + $this->mink->setActiveSessionName('session_name');
  97 +
  98 + $this->assertEquals('session_name', $this->mink->getActiveSessionName());
  99 +
  100 + $this->setExpectedException('InvalidArgumentException');
53 101
54   - $this->setExpectedException('RuntimeException');
55   - $this->mink->switchToDefaultDriver();
  102 + $this->mink->setActiveSessionName('not_registered');
56 103 }
57 104
58   - public function testSwitchToDefaultDriver()
  105 + public function testGetActiveSession()
59 106 {
60   - $driver = $this->getMockBuilder('Behat\Mink\Driver\DriverInterface')->getMock();
61   - $driver
62   - ->expects($this->once())
63   - ->method('isStarted')
64   - ->will($this->returnValue(true));
65   - $driver
66   - ->expects($this->once())
67   - ->method('reset');
  107 + $session1 = $this->getSessionMock();
  108 + $session2 = $this->getSessionMock();
  109 +
  110 + $this->assertNotSame($session1, $session2);
68 111
69   - $this->mink->registerDriver('everzet', $driver, true);
70   - $this->mink->resetDriver();
  112 + $this->mink->registerSession('session_1', $session1);
  113 + $this->mink->registerSession('session_2', $session2);
  114 + $this->mink->setActiveSessionName('session_2');
  115 +
  116 + $this->assertSame($session1, $this->mink->getSession('session_1'));
  117 + $this->assertSame($session2, $this->mink->getSession('session_2'));
  118 + $this->assertSame($session2, $this->mink->getSession());
  119 +
  120 + $this->mink->setActiveSessionName('session_1');
  121 +
  122 + $this->assertSame($session1, $this->mink->getSession());
71 123 }
72 124
73   - public function testSessionCreation()
  125 + public function testGetNoActiveSession()
74 126 {
75   - $driver1 = $this->getMockBuilder('Behat\Mink\Driver\DriverInterface')->getMock();
76   - $driver1
77   - ->expects($this->exactly(2))
78   - ->method('isStarted')
79   - ->will($this->returnValue(true));
80   - $driver2 = $this->getMockBuilder('Behat\Mink\Driver\DriverInterface')->getMock();
81   - $driver2
82   - ->expects($this->exactly(4))
83   - ->method('isStarted')
84   - ->will($this->returnValue(true));
  127 + $session1 = $this->getSessionMock();
85 128
86   - $this->mink->registerDriver('driver1', $driver1, true);
87   - $this->mink->registerDriver('driver2', $driver2);
  129 + $this->mink->registerSession('session_1', $session1);
88 130
89   - $session = $this->mink->getSession();
90   - $this->assertInstanceOf('Behat\Mink\Session', $session);
91   - $this->assertSame($session, $this->mink->getSession());
  131 + $this->setExpectedException('InvalidArgumentException');
92 132
93   - $this->mink->switchToDriver('driver2');
94   - $this->assertNotSame($session, $this->mink->getSession());
95   - $this->assertSame($this->mink->getSession(), $this->mink->getSession());
  133 + $this->mink->getSession();
  134 + }
  135 +
  136 + private function getSessionMock()
  137 + {
  138 + return $this->getMockBuilder('Behat\Mink\Session')
  139 + ->disableOriginalConstructor()
  140 + ->getMock();
96 141 }
97 142 }
2  vendor/SahiClient
... ... @@ -1 +1 @@
1   -Subproject commit 466d47de6a39f16b17319f287d721d1f40710528
  1 +Subproject commit 861e1afa53e694a010776cf78ac61ad267aeff2c

0 comments on commit 4912e72

Please sign in to comment.
Something went wrong with that request. Please try again.