Skip to content

Commit 140d2c6

Browse files
author
Alexander Obuhovich
committed
Session sharing wasn't working, when non-shared sessions were present
1 parent 7d5e31d commit 140d2c6

9 files changed

Lines changed: 215 additions & 55 deletions

library/aik099/PHPUnit/Session/IsolatedSessionStrategy.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,27 @@ public function session(BrowserConfiguration $browser)
9090
*/
9191
public function onTestEnd(TestEvent $event)
9292
{
93+
if ( !$this->_isEventForMe($event) ) {
94+
return;
95+
}
96+
9397
$session = $event->getSession();
9498

9599
if ( $session !== null && $session->isStarted() ) {
96100
$session->stop();
97101
}
98102
}
99103

104+
/**
105+
* Checks, that event can be handled by this class.
106+
*
107+
* @param TestEvent $event Test event.
108+
*
109+
* @return boolean
110+
*/
111+
private function _isEventForMe(TestEvent $event)
112+
{
113+
return $event->getTestCase()->getSessionStrategy() instanceof self;
114+
}
115+
100116
}

library/aik099/PHPUnit/Session/SharedSessionStrategy.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,11 +159,27 @@ public function onTestFailed(TestFailedEvent $event)
159159
*/
160160
public function onTestSuiteEnd(TestEvent $event)
161161
{
162+
if ( !$this->_isEventForMe($event) ) {
163+
return;
164+
}
165+
162166
$session = $event->getSession();
163167

164168
if ( $session !== null && $session->isStarted() ) {
165169
$session->stop();
166170
}
167171
}
168172

173+
/**
174+
* Checks, that event can be handled by this class.
175+
*
176+
* @param TestEvent $event Test event.
177+
*
178+
* @return boolean
179+
*/
180+
private function _isEventForMe(TestEvent $event)
181+
{
182+
return $event->getTestCase()->getSessionStrategy() instanceof self;
183+
}
184+
169185
}

tests/aik099/PHPUnit/Fixture/SetupEventFixture.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ public function testEvents()
9696
$session->shouldReceive('getDriver')->once()->andReturn($driver);
9797

9898
// For IsolatedSessionStrategy::onTestEnd (twice per each browser because
99-
// we have 2 strategies listening for test end).
100-
$session->shouldReceive('stop')->times(4);
99+
// we have 2 strategies listening for test end + IsolatedSessionStrategyTest with 2 tests).
100+
$session->shouldReceive('stop')->times(6);
101101
$session->shouldReceive('isStarted')->andReturn(true);
102102

103103
$this->_setSession($session);

tests/aik099/PHPUnit/Integration/DataProviderTest.php

Lines changed: 1 addition & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,9 @@
1111
namespace tests\aik099\PHPUnit\Integration;
1212

1313

14-
use aik099\PHPUnit\BrowserTestCase;
15-
use Mockery as m;
16-
17-
class DataProviderTest extends BrowserTestCase
14+
class DataProviderTest extends SauceLabsAwareTestCase
1815
{
1916

20-
/**
21-
* Browser list to be used in tests.
22-
*
23-
* @var array
24-
*/
25-
public static $browsers = array(
26-
array('alias' => 'saucelabs'),
27-
// array('alias' => 'browserstack'),
28-
);
29-
3017
public function sampleDataProvider()
3118
{
3219
return array(
@@ -50,46 +37,9 @@ public function testDataProvider($case)
5037
}
5138
}
5239

53-
/**
54-
* Whatever or not code coverage information should be gathered.
55-
*
56-
* @return boolean
57-
* @throws \RuntimeException When used before test is started.
58-
*/
59-
public function getCollectCodeCoverageInformation()
60-
{
61-
// FIXME: Workaround for https://github.com/minkphp/phpunit-mink/issues/35 bug.
62-
return false;
63-
}
64-
6540
protected function customMethod()
6641
{
6742
return 5;
6843
}
6944

70-
/**
71-
* Gets browser configuration aliases.
72-
*
73-
* Allows to decouple actual test server connection details from test cases.
74-
*
75-
* @return array
76-
*/
77-
public function getBrowserAliases()
78-
{
79-
return array(
80-
'saucelabs' => array(
81-
'type' => 'saucelabs',
82-
'apiUsername' => getenv('SAUCE_USERNAME'),
83-
'apiKey' => getenv('SAUCE_ACCESS_KEY'),
84-
85-
'browserName' => 'chrome',
86-
'desiredCapabilities' => array('version' => 28),
87-
'baseUrl' => 'http://www.google.com',
88-
),
89-
/*'browserstack' => array(
90-
'type' => 'browserstack',
91-
),*/
92-
);
93-
}
94-
9545
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
/**
3+
* This file is part of the phpunit-mink library.
4+
* For the full copyright and license information, please view
5+
* the LICENSE file that was distributed with this source code.
6+
*
7+
* @copyright Alexander Obuhovich <aik.bold@gmail.com>
8+
* @link https://github.com/aik099/phpunit-mink
9+
*/
10+
11+
namespace tests\aik099\PHPUnit\Integration;
12+
13+
14+
class IsolatedSessionStrategyTest extends SauceLabsAwareTestCase
15+
{
16+
17+
/**
18+
* Browser list to be used in tests.
19+
*
20+
* @var array
21+
*/
22+
public static $browsers = array(
23+
array(
24+
'alias' => 'saucelabs',
25+
'sessionStrategy' => 'isolated',
26+
),
27+
);
28+
29+
public function testOne()
30+
{
31+
$session = $this->getSession();
32+
$session->visit('https://www.google.com');
33+
34+
$this->assertTrue(true);
35+
}
36+
37+
/**
38+
* @depends testOne
39+
*/
40+
public function testTwo()
41+
{
42+
$session = $this->getSession();
43+
$url = $session->getCurrentUrl();
44+
45+
$this->assertNotContains('https://www.google.com', $url);
46+
}
47+
48+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
/**
3+
* This file is part of the phpunit-mink library.
4+
* For the full copyright and license information, please view
5+
* the LICENSE file that was distributed with this source code.
6+
*
7+
* @copyright Alexander Obuhovich <aik.bold@gmail.com>
8+
* @link https://github.com/aik099/phpunit-mink
9+
*/
10+
11+
namespace tests\aik099\PHPUnit\Integration;
12+
13+
14+
use aik099\PHPUnit\BrowserTestCase;
15+
16+
abstract class SauceLabsAwareTestCase extends BrowserTestCase
17+
{
18+
19+
/**
20+
* Browser list to be used in tests.
21+
*
22+
* @var array
23+
*/
24+
public static $browsers = array(
25+
array('alias' => 'saucelabs'),
26+
);
27+
28+
/**
29+
* Set session meta-info for "Sauce Labs".
30+
*
31+
* @return void
32+
*/
33+
protected function setUp()
34+
{
35+
if ( !getenv('SAUCE_USERNAME') || !getenv('SAUCE_ACCESS_KEY') ) {
36+
$this->markTestSkipped('SauceLabs integration is not configured');
37+
}
38+
39+
parent::setUp();
40+
}
41+
42+
/**
43+
* Whatever or not code coverage information should be gathered.
44+
*
45+
* @return boolean
46+
* @throws \RuntimeException When used before test is started.
47+
*/
48+
public function getCollectCodeCoverageInformation()
49+
{
50+
// FIXME: Workaround for https://github.com/minkphp/phpunit-mink/issues/35 bug.
51+
return false;
52+
}
53+
54+
/**
55+
* Gets browser configuration aliases.
56+
*
57+
* Allows to decouple actual test server connection details from test cases.
58+
*
59+
* @return array
60+
*/
61+
public function getBrowserAliases()
62+
{
63+
return array(
64+
'saucelabs' => array(
65+
'type' => 'saucelabs',
66+
'apiUsername' => getenv('SAUCE_USERNAME'),
67+
'apiKey' => getenv('SAUCE_ACCESS_KEY'),
68+
69+
'browserName' => 'chrome',
70+
'desiredCapabilities' => array('version' => 28),
71+
'baseUrl' => 'http://www.google.com',
72+
),
73+
);
74+
}
75+
76+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
/**
3+
* This file is part of the phpunit-mink library.
4+
* For the full copyright and license information, please view
5+
* the LICENSE file that was distributed with this source code.
6+
*
7+
* @copyright Alexander Obuhovich <aik.bold@gmail.com>
8+
* @link https://github.com/aik099/phpunit-mink
9+
*/
10+
11+
namespace tests\aik099\PHPUnit\Integration;
12+
13+
14+
class SharedSessionStrategyTest extends SauceLabsAwareTestCase
15+
{
16+
17+
/**
18+
* Browser list to be used in tests.
19+
*
20+
* @var array
21+
*/
22+
public static $browsers = array(
23+
array(
24+
'alias' => 'saucelabs',
25+
'sessionStrategy' => 'shared',
26+
),
27+
);
28+
29+
public function testOne()
30+
{
31+
$session = $this->getSession();
32+
$session->visit('https://www.google.com');
33+
34+
$this->assertTrue(true);
35+
}
36+
37+
/**
38+
* @depends testOne
39+
*/
40+
public function testTwo()
41+
{
42+
$session = $this->getSession();
43+
$url = $session->getCurrentUrl();
44+
45+
$this->assertContains('https://www.google.com', $url);
46+
}
47+
48+
}

tests/aik099/PHPUnit/Session/IsolatedSessionStrategyTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,13 @@ public function testOnTestEnd()
7777
$session->shouldReceive('stop')->once();
7878
$session->shouldReceive('isStarted')->once()->andReturn(true);
7979

80+
$test_case = m::mock(self::TEST_CASE_CLASS);
81+
$test_case->shouldReceive('getSessionStrategy')->once()->andReturn($this->strategy);
82+
8083
$event = $this->eventDispatcher->dispatch(
8184
BrowserTestCase::TEST_ENDED_EVENT,
8285
new TestEndedEvent(
83-
m::mock(self::TEST_CASE_CLASS),
86+
$test_case,
8487
m::mock('PHPUnit_Framework_TestResult'),
8588
$session
8689
)

tests/aik099/PHPUnit/Session/SharedSessionStrategyTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,12 @@ public function testEndOfTestCaseWithSession()
169169
$session->shouldReceive('stop')->withNoArgs()->once();
170170
$session->shouldReceive('isStarted')->once()->andReturn(true);
171171

172+
$test_case = m::mock(self::TEST_CASE_CLASS);
173+
$test_case->shouldReceive('getSessionStrategy')->once()->andReturn($this->strategy);
174+
172175
$event = $this->eventDispatcher->dispatch(
173176
BrowserTestCase::TEST_SUITE_ENDED_EVENT,
174-
new TestEvent(m::mock(self::TEST_CASE_CLASS), $session)
177+
new TestEvent($test_case, $session)
175178
);
176179

177180
$this->assertInstanceOf('aik099\\PHPUnit\\Event\\TestEvent', $event);

0 commit comments

Comments
 (0)