Skip to content

Commit

Permalink
[PhpBrowser] Ensure sessions have independent cookies, fixes Codecept…
Browse files Browse the repository at this point in the history
  • Loading branch information
insightfuls committed Jan 17, 2017
1 parent 4f6e2ed commit 80dd745
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 7 deletions.
43 changes: 40 additions & 3 deletions src/Codeception/Coverage/Subscriber/LocalServer.php
Expand Up @@ -7,6 +7,7 @@
use Codeception\Event\SuiteEvent;
use Codeception\Event\TestEvent;
use Codeception\Events;
use Codeception\Exception\ModuleException;
use Codeception\Exception\RemoteException;

/**
Expand Down Expand Up @@ -148,18 +149,54 @@ function ($h) {

protected function startCoverageCollection($testName)
{
$cookie = [
$value = [
'CodeCoverage' => $testName,
'CodeCoverage_Suite' => $this->suiteName,
'CodeCoverage_Config' => $this->settings['remote_config']
];
$value = json_encode($value);

$this->module->amOnPage('/');
$this->module->setCookie(self::COVERAGE_COOKIE, json_encode($cookie));
$this->module->setCookie(self::COVERAGE_COOKIE, $value);

// putting in configuration ensures the cookie is used for all sessions of a MultiSession test

$cookies = $this->module->_getConfig('cookies');
if (!$cookies || !is_array($cookies)) {
$cookies = [];
}

$found = false;
foreach ($cookies as &$cookie) {
if (!is_array($cookie) || !array_key_exists('Name', $cookie) || !array_key_exists('Value', $cookie)) {
// \Codeception\Lib\InnerBrowser will complain about this
continue;
}
if ($cookie['Name'] === self::COVERAGE_COOKIE) {
$found = true;
$cookie['Value'] = $value;
break;
}
}
if (!$found) {
$cookies[] = [
'Name' => self::COVERAGE_COOKIE,
'Value' => $value
];
}

$this->module->_setConfig(['cookies' => $cookies]);
}

protected function fetchErrors()
{
$error = $this->module->grabCookie(self::COVERAGE_COOKIE_ERROR);
try {
$error = $this->module->grabCookie(self::COVERAGE_COOKIE_ERROR);
} catch (ModuleException $e) {
// when a new session is started we can't get cookies because there is no
// current page, but there can be no code coverage error either
$error = null;
}
if (!empty($error)) {
$this->module->resetCookie(self::COVERAGE_COOKIE_ERROR);
throw new RemoteException($error);
Expand Down
12 changes: 9 additions & 3 deletions src/Codeception/Module/PhpBrowser.php
Expand Up @@ -131,7 +131,6 @@ public function _requires()

public function _initialize()
{
$this->client = $this->guessGuzzleConnector();
$this->_initializeSession();
}

Expand All @@ -150,7 +149,7 @@ public function _before(TestInterface $test)
if (!$this->client) {
$this->client = $this->guessGuzzleConnector();
}
$this->_initializeSession();
$this->_prepareSession();
}

public function _getUrl()
Expand Down Expand Up @@ -193,7 +192,7 @@ public function amOnSubdomain($subdomain)

protected function onReconfigure()
{
$this->_initializeSession();
$this->_prepareSession();
}

/**
Expand Down Expand Up @@ -227,6 +226,13 @@ public function _getResponseCode()
}

public function _initializeSession()
{
// independent sessions need independent cookies
$this->client = $this->guessGuzzleConnector();
$this->_prepareSession();
}

public function _prepareSession()
{
$defaults = array_intersect_key($this->config, array_flip($this->guzzleConfigFields));
$curlOptions = [];
Expand Down
6 changes: 5 additions & 1 deletion tests/data/claypit/tests/remote/DemoCept.php
@@ -1,4 +1,8 @@
<?php
$I = new OtherGuy($scenario);
$I->amOnPage('/');
$I->see('Welcome to test app');
$I->see('Welcome to test app');
$I->haveFriend("friend")->does(function(OtherGuy $I) {
$I->amOnPage('/info');
$I->see('Lots of valuable data');
});
6 changes: 6 additions & 0 deletions tests/support/CoverGuy.php
Expand Up @@ -29,6 +29,12 @@ public function seeCoverageStatsNotEmpty()
<<<EOF
index
Methods: 50.00% ( 1/ 2) Lines: 50.00% ( 2/ 4)
EOF
);
$this->seeInShellOutput(
<<<EOF
info
Methods: 100.00% ( 1/ 1) Lines: 100.00% ( 4/ 4)
EOF
);
}
Expand Down
22 changes: 22 additions & 0 deletions tests/unit/Codeception/Module/PhpBrowserTest.php
Expand Up @@ -101,6 +101,28 @@ public function testSetMultipleCookies()
$this->module->dontSeeCookie($cookie_name_2);
}

public function testSessionsHaveIndependentCookies()
{
$this->module->amOnPage('/');
$cookie_name_1 = 'test_cookie';
$cookie_value_1 = 'this is a test';
$this->module->setCookie($cookie_name_1, $cookie_value_1);

$session = $this->module->_backupSession();
$this->module->_initializeSession();

$this->module->dontSeeCookie($cookie_name_1);

$cookie_name_2 = '2_test_cookie';
$cookie_value_2 = '2 this is a test';
$this->module->setCookie($cookie_name_2, $cookie_value_2);

$this->module->_loadSession($session);

$this->module->dontSeeCookie($cookie_name_2);
$this->module->seeCookie($cookie_name_1);
}

public function testSubmitFormGet()
{
$I = $this->module;
Expand Down

0 comments on commit 80dd745

Please sign in to comment.