Skip to content

Commit fe5911c

Browse files
authored
Merge pull request #128 from aik099/session-tracking-feat
Ability to determine session freshness state
2 parents 4dac104 + 8999227 commit fe5911c

10 files changed

Lines changed: 185 additions & 65 deletions

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
66
### Added
77
- Specify failed PHPUnit assertion text to the BrowserStack ("reason" field)/SauceLabs("custom-data" field) test.
88
- Added the `$auto_create` parameter to the `BrowserTestCase::getSession` method, which allows to verify is session is already started.
9+
- Added the `ISessionStrategy::isFreshSession` method to indicate fact, that previous `ISessionStrategy::session` call have created a new session instead of reusing a previously created one. Can be used to perform a login once per a test case class.
910

1011
### Changed
1112
- Bumped minimum PHP version to 5.6.

docs/configuration.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ in that test case class.
2929
Browser Session Sharing
3030
^^^^^^^^^^^^^^^^^^^^^^^
3131
As a benefit of the shared (per test case) browser configuration, that was described above is an ability
32-
to not only to share the browser configuration, that is used to create `Mink`_ session, but to actually share
32+
to not only share the browser configuration, that is used to create `Mink`_ session, but to actually share
3333
created sessions between all tests in a single test case. This can be done by adding the ``sessionStrategy``
3434
option (line 15) to the browser configuration.
3535

3636
.. literalinclude:: examples/configuration/per_test_case_browser_config.php
3737
:linenos:
38-
:emphasize-lines: 15
38+
:emphasize-lines: 15,26,48
3939

4040
Selecting the Mink Driver
4141
^^^^^^^^^^^^^^^^^^^^^^^^^

docs/examples/configuration/per_test_case_browser_config.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,42 @@ class CommonBrowserConfigTest extends BrowserTestCase
1616
),
1717
);
1818

19+
/**
20+
* @before
21+
*/
22+
public function setUpTest()
23+
{
24+
parent::setUpTest();
25+
26+
if ( $this->getSessionStrategy()->isFreshSession() ) {
27+
// login once before any of the tests was started
28+
}
29+
}
30+
31+
public function testOne()
32+
{
33+
// user will be already logged-in regardless
34+
// of the test execution order/filtering
35+
}
36+
37+
public function testTwo()
38+
{
39+
// user will be already logged-in regardless
40+
// of the test execution order/filtering
41+
}
42+
43+
/**
44+
* @inheritDoc
45+
*/
46+
public function onTestSuiteEnded()
47+
{
48+
$session = $this->getSession(false);
49+
50+
if ( $session !== null && $session->isStarted() ) {
51+
// logout once after all the tests were finished
52+
}
53+
54+
return parent::onTestSuiteEnded();
55+
}
56+
1957
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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 aik099\PHPUnit\Session;
12+
13+
14+
use aik099\PHPUnit\BrowserTestCase;
15+
use Behat\Mink\Session;
16+
17+
abstract class AbstractSessionStrategy implements ISessionStrategy
18+
{
19+
20+
/**
21+
* Determines if the session was just started.
22+
*
23+
* @var boolean|null
24+
*/
25+
protected $isFreshSession;
26+
27+
/**
28+
* @inheritDoc
29+
*/
30+
public function isFreshSession()
31+
{
32+
return $this->isFreshSession;
33+
}
34+
35+
/**
36+
* @inheritDoc
37+
*/
38+
public function onTestEnded(BrowserTestCase $test_case)
39+
{
40+
41+
}
42+
43+
/**
44+
* @inheritDoc
45+
*/
46+
public function onTestFailed(BrowserTestCase $test_case, $exception)
47+
{
48+
49+
}
50+
51+
/**
52+
* @inheritDoc
53+
*/
54+
public function onTestSuiteEnded(BrowserTestCase $test_case)
55+
{
56+
57+
}
58+
59+
/**
60+
* Stops the session.
61+
*
62+
* @param Session|null $session Session.
63+
*
64+
* @return void
65+
*/
66+
protected function stopSession(Session $session = null)
67+
{
68+
if ( $session !== null && $session->isStarted() ) {
69+
$session->stop();
70+
$this->isFreshSession = null;
71+
}
72+
}
73+
74+
}

library/aik099/PHPUnit/Session/ISessionStrategy.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ interface ISessionStrategy
3232
*/
3333
public function session(BrowserConfiguration $browser);
3434

35+
/**
36+
* Determines if the session was just started.
37+
*
38+
* @return boolean|null
39+
*/
40+
public function isFreshSession();
41+
3542
/**
3643
* Hook, called from "BrowserTestCase::tearDownTest" method.
3744
*

library/aik099/PHPUnit/Session/IsolatedSessionStrategy.php

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
*
2121
* @method \Mockery\Expectation shouldReceive(string $name)
2222
*/
23-
class IsolatedSessionStrategy implements ISessionStrategy
23+
class IsolatedSessionStrategy extends AbstractSessionStrategy
2424
{
2525

2626
/**
@@ -41,15 +41,14 @@ public function __construct(ISessionFactory $session_factory)
4141
}
4242

4343
/**
44-
* Returns Mink session with given browser configuration.
45-
*
46-
* @param BrowserConfiguration $browser Browser configuration for a session.
47-
*
48-
* @return Session
44+
* @inheritDoc
4945
*/
5046
public function session(BrowserConfiguration $browser)
5147
{
52-
return $this->_sessionFactory->createSession($browser);
48+
$session = $this->_sessionFactory->createSession($browser);
49+
$this->isFreshSession = true;
50+
51+
return $session;
5352
}
5453

5554
/**
@@ -59,25 +58,7 @@ public function onTestEnded(BrowserTestCase $test_case)
5958
{
6059
$session = $test_case->getSession(false);
6160

62-
if ( $session !== null && $session->isStarted() ) {
63-
$session->stop();
64-
}
65-
}
66-
67-
/**
68-
* @inheritDoc
69-
*/
70-
public function onTestFailed(BrowserTestCase $test_case, $exception)
71-
{
72-
73-
}
74-
75-
/**
76-
* @inheritDoc
77-
*/
78-
public function onTestSuiteEnded(BrowserTestCase $test_case)
79-
{
80-
61+
$this->stopSession($session);
8162
}
8263

8364
}

library/aik099/PHPUnit/Session/SharedSessionStrategy.php

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
*
2323
* @method \Mockery\Expectation shouldReceive(string $name)
2424
*/
25-
class SharedSessionStrategy implements ISessionStrategy
25+
class SharedSessionStrategy extends AbstractSessionStrategy
2626
{
2727

2828
/**
@@ -57,42 +57,38 @@ public function __construct(ISessionStrategy $original_strategy)
5757
}
5858

5959
/**
60-
* Returns Mink session with given browser configuration.
61-
*
62-
* @param BrowserConfiguration $browser Browser configuration for a session.
63-
*
64-
* @return Session
60+
* @inheritDoc
6561
*/
6662
public function session(BrowserConfiguration $browser)
6763
{
6864
if ( $this->_lastTestFailed ) {
69-
$this->stopSession();
65+
$this->stopAndForgetSession();
7066
$this->_lastTestFailed = false;
7167
}
7268

7369
if ( $this->_session === null ) {
7470
$this->_session = $this->_originalStrategy->session($browser);
71+
$this->isFreshSession = $this->_originalStrategy->isFreshSession();
7572
}
7673
else {
74+
$this->isFreshSession = false;
7775
$this->_switchToMainWindow();
7876
}
7977

8078
return $this->_session;
8179
}
8280

8381
/**
84-
* Stops session.
82+
* Stops and forgets a session.
8583
*
8684
* @return void
8785
*/
88-
protected function stopSession()
86+
protected function stopAndForgetSession()
8987
{
90-
if ( $this->_session === null ) {
91-
return;
88+
if ( $this->_session !== null ) {
89+
$this->stopSession($this->_session);
90+
$this->_session = null;
9291
}
93-
94-
$this->_session->stop();
95-
$this->_session = null;
9692
}
9793

9894
/**
@@ -105,14 +101,6 @@ private function _switchToMainWindow()
105101
$this->_session->switchToWindow(null);
106102
}
107103

108-
/**
109-
* @inheritDoc
110-
*/
111-
public function onTestEnded(BrowserTestCase $test_case)
112-
{
113-
114-
}
115-
116104
/**
117105
* @inheritDoc
118106
*/
@@ -132,9 +120,7 @@ public function onTestSuiteEnded(BrowserTestCase $test_case)
132120
{
133121
$session = $test_case->getSession(false);
134122

135-
if ( $session !== null && $session->isStarted() ) {
136-
$session->stop();
137-
}
123+
$this->stopSession($session);
138124
}
139125

140126
}

tests/aik099/PHPUnit/Session/SessionStrategyTestCase.php renamed to tests/aik099/PHPUnit/Session/AbstractSessionStrategyTestCase.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
use Behat\Mink\Session;
1818
use aik099\PHPUnit\BrowserTestCase;
1919

20-
class SessionStrategyTestCase extends AbstractTestCase
20+
abstract class AbstractSessionStrategyTestCase extends AbstractTestCase
2121
{
2222

2323
const BROWSER_CLASS = BrowserConfiguration::class;
@@ -33,4 +33,9 @@ class SessionStrategyTestCase extends AbstractTestCase
3333
*/
3434
protected $strategy;
3535

36+
public function testUnknownSessionFreshnessStateUntilItsStarted()
37+
{
38+
$this->assertNull($this->strategy->isFreshSession());
39+
}
40+
3641
}

tests/aik099/PHPUnit/Session/IsolatedSessionStrategyTest.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
use Mockery as m;
1717
use Mockery\MockInterface;
1818

19-
class IsolatedSessionStrategyTest extends SessionStrategyTestCase
19+
class IsolatedSessionStrategyTest extends AbstractSessionStrategyTestCase
2020
{
2121

2222
/**
@@ -57,6 +57,18 @@ public function testSession()
5757
$this->assertEquals($session2, $this->strategy->session($browser));
5858
}
5959

60+
public function testIsFreshSessionAfterSessionIsStarted()
61+
{
62+
$browser = m::mock(self::BROWSER_CLASS);
63+
$session = m::mock(self::SESSION_CLASS);
64+
65+
$this->_factory->shouldReceive('createSession')->with($browser)->once()->andReturn($session);
66+
67+
$this->strategy->session($browser);
68+
69+
$this->assertTrue($this->strategy->isFreshSession());
70+
}
71+
6072
/**
6173
* Test description.
6274
*

0 commit comments

Comments
 (0)