Skip to content

Commit

Permalink
Merge pull request #54 from aik099/session-sharing-isnt-working
Browse files Browse the repository at this point in the history
Session sharing not working
  • Loading branch information
Alexander Obuhovich committed Aug 1, 2015
2 parents 7d5e31d + 140d2c6 commit 6880698
Show file tree
Hide file tree
Showing 9 changed files with 215 additions and 55 deletions.
16 changes: 16 additions & 0 deletions library/aik099/PHPUnit/Session/IsolatedSessionStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,27 @@ public function session(BrowserConfiguration $browser)
*/
public function onTestEnd(TestEvent $event)
{
if ( !$this->_isEventForMe($event) ) {
return;
}

$session = $event->getSession();

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

/**
* Checks, that event can be handled by this class.
*
* @param TestEvent $event Test event.
*
* @return boolean
*/
private function _isEventForMe(TestEvent $event)
{
return $event->getTestCase()->getSessionStrategy() instanceof self;
}

}
16 changes: 16 additions & 0 deletions library/aik099/PHPUnit/Session/SharedSessionStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,27 @@ public function onTestFailed(TestFailedEvent $event)
*/
public function onTestSuiteEnd(TestEvent $event)
{
if ( !$this->_isEventForMe($event) ) {
return;
}

$session = $event->getSession();

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

/**
* Checks, that event can be handled by this class.
*
* @param TestEvent $event Test event.
*
* @return boolean
*/
private function _isEventForMe(TestEvent $event)
{
return $event->getTestCase()->getSessionStrategy() instanceof self;
}

}
4 changes: 2 additions & 2 deletions tests/aik099/PHPUnit/Fixture/SetupEventFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ public function testEvents()
$session->shouldReceive('getDriver')->once()->andReturn($driver);

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

$this->_setSession($session);
Expand Down
52 changes: 1 addition & 51 deletions tests/aik099/PHPUnit/Integration/DataProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,9 @@
namespace tests\aik099\PHPUnit\Integration;


use aik099\PHPUnit\BrowserTestCase;
use Mockery as m;

class DataProviderTest extends BrowserTestCase
class DataProviderTest extends SauceLabsAwareTestCase
{

/**
* Browser list to be used in tests.
*
* @var array
*/
public static $browsers = array(
array('alias' => 'saucelabs'),
// array('alias' => 'browserstack'),
);

public function sampleDataProvider()
{
return array(
Expand All @@ -50,46 +37,9 @@ public function testDataProvider($case)
}
}

/**
* Whatever or not code coverage information should be gathered.
*
* @return boolean
* @throws \RuntimeException When used before test is started.
*/
public function getCollectCodeCoverageInformation()
{
// FIXME: Workaround for https://github.com/minkphp/phpunit-mink/issues/35 bug.
return false;
}

protected function customMethod()
{
return 5;
}

/**
* Gets browser configuration aliases.
*
* Allows to decouple actual test server connection details from test cases.
*
* @return array
*/
public function getBrowserAliases()
{
return array(
'saucelabs' => array(
'type' => 'saucelabs',
'apiUsername' => getenv('SAUCE_USERNAME'),
'apiKey' => getenv('SAUCE_ACCESS_KEY'),

'browserName' => 'chrome',
'desiredCapabilities' => array('version' => 28),
'baseUrl' => 'http://www.google.com',
),
/*'browserstack' => array(
'type' => 'browserstack',
),*/
);
}

}
48 changes: 48 additions & 0 deletions tests/aik099/PHPUnit/Integration/IsolatedSessionStrategyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?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 <[email protected]>
* @link https://github.com/aik099/phpunit-mink
*/

namespace tests\aik099\PHPUnit\Integration;


class IsolatedSessionStrategyTest extends SauceLabsAwareTestCase
{

/**
* Browser list to be used in tests.
*
* @var array
*/
public static $browsers = array(
array(
'alias' => 'saucelabs',
'sessionStrategy' => 'isolated',
),
);

public function testOne()
{
$session = $this->getSession();
$session->visit('https://www.google.com');

$this->assertTrue(true);
}

/**
* @depends testOne
*/
public function testTwo()
{
$session = $this->getSession();
$url = $session->getCurrentUrl();

$this->assertNotContains('https://www.google.com', $url);
}

}
76 changes: 76 additions & 0 deletions tests/aik099/PHPUnit/Integration/SauceLabsAwareTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?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 <[email protected]>
* @link https://github.com/aik099/phpunit-mink
*/

namespace tests\aik099\PHPUnit\Integration;


use aik099\PHPUnit\BrowserTestCase;

abstract class SauceLabsAwareTestCase extends BrowserTestCase
{

/**
* Browser list to be used in tests.
*
* @var array
*/
public static $browsers = array(
array('alias' => 'saucelabs'),
);

/**
* Set session meta-info for "Sauce Labs".
*
* @return void
*/
protected function setUp()
{
if ( !getenv('SAUCE_USERNAME') || !getenv('SAUCE_ACCESS_KEY') ) {
$this->markTestSkipped('SauceLabs integration is not configured');
}

parent::setUp();
}

/**
* Whatever or not code coverage information should be gathered.
*
* @return boolean
* @throws \RuntimeException When used before test is started.
*/
public function getCollectCodeCoverageInformation()
{
// FIXME: Workaround for https://github.com/minkphp/phpunit-mink/issues/35 bug.
return false;
}

/**
* Gets browser configuration aliases.
*
* Allows to decouple actual test server connection details from test cases.
*
* @return array
*/
public function getBrowserAliases()
{
return array(
'saucelabs' => array(
'type' => 'saucelabs',
'apiUsername' => getenv('SAUCE_USERNAME'),
'apiKey' => getenv('SAUCE_ACCESS_KEY'),

'browserName' => 'chrome',
'desiredCapabilities' => array('version' => 28),
'baseUrl' => 'http://www.google.com',
),
);
}

}
48 changes: 48 additions & 0 deletions tests/aik099/PHPUnit/Integration/SharedSessionStrategyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?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 <[email protected]>
* @link https://github.com/aik099/phpunit-mink
*/

namespace tests\aik099\PHPUnit\Integration;


class SharedSessionStrategyTest extends SauceLabsAwareTestCase
{

/**
* Browser list to be used in tests.
*
* @var array
*/
public static $browsers = array(
array(
'alias' => 'saucelabs',
'sessionStrategy' => 'shared',
),
);

public function testOne()
{
$session = $this->getSession();
$session->visit('https://www.google.com');

$this->assertTrue(true);
}

/**
* @depends testOne
*/
public function testTwo()
{
$session = $this->getSession();
$url = $session->getCurrentUrl();

$this->assertContains('https://www.google.com', $url);
}

}
5 changes: 4 additions & 1 deletion tests/aik099/PHPUnit/Session/IsolatedSessionStrategyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,13 @@ public function testOnTestEnd()
$session->shouldReceive('stop')->once();
$session->shouldReceive('isStarted')->once()->andReturn(true);

$test_case = m::mock(self::TEST_CASE_CLASS);
$test_case->shouldReceive('getSessionStrategy')->once()->andReturn($this->strategy);

$event = $this->eventDispatcher->dispatch(
BrowserTestCase::TEST_ENDED_EVENT,
new TestEndedEvent(
m::mock(self::TEST_CASE_CLASS),
$test_case,
m::mock('PHPUnit_Framework_TestResult'),
$session
)
Expand Down
5 changes: 4 additions & 1 deletion tests/aik099/PHPUnit/Session/SharedSessionStrategyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,12 @@ public function testEndOfTestCaseWithSession()
$session->shouldReceive('stop')->withNoArgs()->once();
$session->shouldReceive('isStarted')->once()->andReturn(true);

$test_case = m::mock(self::TEST_CASE_CLASS);
$test_case->shouldReceive('getSessionStrategy')->once()->andReturn($this->strategy);

$event = $this->eventDispatcher->dispatch(
BrowserTestCase::TEST_SUITE_ENDED_EVENT,
new TestEvent(m::mock(self::TEST_CASE_CLASS), $session)
new TestEvent($test_case, $session)
);

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

0 comments on commit 6880698

Please sign in to comment.