Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to determine session freshness state #128

Merged
merged 1 commit into from Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -6,6 +6,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
### Added
- Specify failed PHPUnit assertion text to the BrowserStack ("reason" field)/SauceLabs("custom-data" field) test.
- Added the `$auto_create` parameter to the `BrowserTestCase::getSession` method, which allows to verify is session is already started.
- 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.

### Changed
- Bumped minimum PHP version to 5.6.
Expand Down
4 changes: 2 additions & 2 deletions docs/configuration.rst
Expand Up @@ -29,13 +29,13 @@ in that test case class.
Browser Session Sharing
^^^^^^^^^^^^^^^^^^^^^^^
As a benefit of the shared (per test case) browser configuration, that was described above is an ability
to not only to share the browser configuration, that is used to create `Mink`_ session, but to actually share
to not only share the browser configuration, that is used to create `Mink`_ session, but to actually share
created sessions between all tests in a single test case. This can be done by adding the ``sessionStrategy``
option (line 15) to the browser configuration.

.. literalinclude:: examples/configuration/per_test_case_browser_config.php
:linenos:
:emphasize-lines: 15
:emphasize-lines: 15,26,48

Selecting the Mink Driver
^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
38 changes: 38 additions & 0 deletions docs/examples/configuration/per_test_case_browser_config.php
Expand Up @@ -16,4 +16,42 @@ class CommonBrowserConfigTest extends BrowserTestCase
),
);

/**
* @before
*/
public function setUpTest()
{
parent::setUpTest();

if ( $this->getSessionStrategy()->isFreshSession() ) {
// login once before any of the tests was started
}
}

public function testOne()
{
// user will be already logged-in regardless
// of the test execution order/filtering
}

public function testTwo()
{
// user will be already logged-in regardless
// of the test execution order/filtering
}

/**
* @inheritDoc
*/
public function onTestSuiteEnded()
{
$session = $this->getSession(false);

if ( $session !== null && $session->isStarted() ) {
// logout once after all the tests were finished
}

return parent::onTestSuiteEnded();
}

}
74 changes: 74 additions & 0 deletions library/aik099/PHPUnit/Session/AbstractSessionStrategy.php
@@ -0,0 +1,74 @@
<?php
/**
* This file is part of the phpunit-mink library.
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*
* @copyright Alexander Obuhovich <aik.bold@gmail.com>
* @link https://github.com/aik099/phpunit-mink
*/

namespace aik099\PHPUnit\Session;


use aik099\PHPUnit\BrowserTestCase;
use Behat\Mink\Session;

abstract class AbstractSessionStrategy implements ISessionStrategy
{

/**
* Determines if the session was just started.
*
* @var boolean|null
*/
protected $isFreshSession;

/**
* @inheritDoc
*/
public function isFreshSession()
{
return $this->isFreshSession;
}

/**
* @inheritDoc
*/
public function onTestEnded(BrowserTestCase $test_case)
{

}

/**
* @inheritDoc
*/
public function onTestFailed(BrowserTestCase $test_case, $exception)
{

}

/**
* @inheritDoc
*/
public function onTestSuiteEnded(BrowserTestCase $test_case)
{

}

/**
* Stops the session.
*
* @param Session|null $session Session.
*
* @return void
*/
protected function stopSession(Session $session = null)
{
if ( $session !== null && $session->isStarted() ) {
$session->stop();
$this->isFreshSession = null;
}
}

}
7 changes: 7 additions & 0 deletions library/aik099/PHPUnit/Session/ISessionStrategy.php
Expand Up @@ -32,6 +32,13 @@ interface ISessionStrategy
*/
public function session(BrowserConfiguration $browser);

/**
* Determines if the session was just started.
*
* @return boolean|null
*/
public function isFreshSession();

/**
* Hook, called from "BrowserTestCase::tearDownTest" method.
*
Expand Down
33 changes: 7 additions & 26 deletions library/aik099/PHPUnit/Session/IsolatedSessionStrategy.php
Expand Up @@ -20,7 +20,7 @@
*
* @method \Mockery\Expectation shouldReceive(string $name)
*/
class IsolatedSessionStrategy implements ISessionStrategy
class IsolatedSessionStrategy extends AbstractSessionStrategy
{

/**
Expand All @@ -41,15 +41,14 @@ public function __construct(ISessionFactory $session_factory)
}

/**
* Returns Mink session with given browser configuration.
*
* @param BrowserConfiguration $browser Browser configuration for a session.
*
* @return Session
* @inheritDoc
*/
public function session(BrowserConfiguration $browser)
{
return $this->_sessionFactory->createSession($browser);
$session = $this->_sessionFactory->createSession($browser);
$this->isFreshSession = true;

return $session;
}

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

if ( $session !== null && $session->isStarted() ) {
$session->stop();
}
}

/**
* @inheritDoc
*/
public function onTestFailed(BrowserTestCase $test_case, $exception)
{

}

/**
* @inheritDoc
*/
public function onTestSuiteEnded(BrowserTestCase $test_case)
{

$this->stopSession($session);
}

}
36 changes: 11 additions & 25 deletions library/aik099/PHPUnit/Session/SharedSessionStrategy.php
Expand Up @@ -22,7 +22,7 @@
*
* @method \Mockery\Expectation shouldReceive(string $name)
*/
class SharedSessionStrategy implements ISessionStrategy
class SharedSessionStrategy extends AbstractSessionStrategy
{

/**
Expand Down Expand Up @@ -57,42 +57,38 @@ public function __construct(ISessionStrategy $original_strategy)
}

/**
* Returns Mink session with given browser configuration.
*
* @param BrowserConfiguration $browser Browser configuration for a session.
*
* @return Session
* @inheritDoc
*/
public function session(BrowserConfiguration $browser)
{
if ( $this->_lastTestFailed ) {
$this->stopSession();
$this->stopAndForgetSession();
$this->_lastTestFailed = false;
}

if ( $this->_session === null ) {
$this->_session = $this->_originalStrategy->session($browser);
$this->isFreshSession = $this->_originalStrategy->isFreshSession();
}
else {
$this->isFreshSession = false;
$this->_switchToMainWindow();
}

return $this->_session;
}

/**
* Stops session.
* Stops and forgets a session.
*
* @return void
*/
protected function stopSession()
protected function stopAndForgetSession()
{
if ( $this->_session === null ) {
return;
if ( $this->_session !== null ) {
$this->stopSession($this->_session);
$this->_session = null;
}

$this->_session->stop();
$this->_session = null;
}

/**
Expand All @@ -105,14 +101,6 @@ private function _switchToMainWindow()
$this->_session->switchToWindow(null);
}

/**
* @inheritDoc
*/
public function onTestEnded(BrowserTestCase $test_case)
{

}

/**
* @inheritDoc
*/
Expand All @@ -132,9 +120,7 @@ public function onTestSuiteEnded(BrowserTestCase $test_case)
{
$session = $test_case->getSession(false);

if ( $session !== null && $session->isStarted() ) {
$session->stop();
}
$this->stopSession($session);
}

}
Expand Up @@ -17,7 +17,7 @@
use Behat\Mink\Session;
use aik099\PHPUnit\BrowserTestCase;

class SessionStrategyTestCase extends AbstractTestCase
abstract class AbstractSessionStrategyTestCase extends AbstractTestCase
{

const BROWSER_CLASS = BrowserConfiguration::class;
Expand All @@ -33,4 +33,9 @@ class SessionStrategyTestCase extends AbstractTestCase
*/
protected $strategy;

public function testUnknownSessionFreshnessStateUntilItsStarted()
{
$this->assertNull($this->strategy->isFreshSession());
}

}
14 changes: 13 additions & 1 deletion tests/aik099/PHPUnit/Session/IsolatedSessionStrategyTest.php
Expand Up @@ -16,7 +16,7 @@
use Mockery as m;
use Mockery\MockInterface;

class IsolatedSessionStrategyTest extends SessionStrategyTestCase
class IsolatedSessionStrategyTest extends AbstractSessionStrategyTestCase
{

/**
Expand Down Expand Up @@ -57,6 +57,18 @@ public function testSession()
$this->assertEquals($session2, $this->strategy->session($browser));
}

public function testIsFreshSessionAfterSessionIsStarted()
{
$browser = m::mock(self::BROWSER_CLASS);
$session = m::mock(self::SESSION_CLASS);

$this->_factory->shouldReceive('createSession')->with($browser)->once()->andReturn($session);

$this->strategy->session($browser);

$this->assertTrue($this->strategy->isFreshSession());
}

/**
* Test description.
*
Expand Down