Skip to content

Module implements Codeception's MultiSession #3

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

Merged
merged 1 commit into from
Jan 28, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/Codeception/Lib/Connector/Yii2.php
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,38 @@ public function restart()
$this->resetApplication();
}

/**
* Return an assoc array with the client context: cookieJar, history.
*
* @return array
*/
public function getContext()
{
return [
'cookieJar' => $this->cookieJar,
'history' => $this->history,
];
}

/**
* Reset the client context: empty cookieJar and history.
*/
public function removeContext()
{
parent::restart();
}

/**
* Set the context, see getContext().
*
* @param array $context
*/
public function setContext(array $context)
{
$this->cookieJar = $context['cookieJar'];
$this->history = $context['history'];
}

/**
* This functions closes the session of the application, if the application exists and has a session.
* @internal
Expand Down
61 changes: 60 additions & 1 deletion src/Codeception/Module/Yii2.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Codeception\Lib\Connector\Yii2 as Yii2Connector;
use Codeception\Lib\Framework;
use Codeception\Lib\Interfaces\ActiveRecord;
use Codeception\Lib\Interfaces\MultiSession;
use Codeception\Lib\Interfaces\PartedModule;
use Codeception\TestInterface;
use Codeception\Util\Debug;
Expand Down Expand Up @@ -178,7 +179,7 @@
*
* @property \Codeception\Lib\Connector\Yii2 $client
*/
class Yii2 extends Framework implements ActiveRecord, PartedModule
class Yii2 extends Framework implements ActiveRecord, MultiSession, PartedModule
{
/**
* Application config file must be set.
Expand Down Expand Up @@ -866,4 +867,62 @@ public function _afterSuite()

$_SERVER = $this->server;
}

/**
* Initialize an empty session. Implements MultiSession.
*/
public function _initializeSession()
{
$this->client->removeContext();
$this->headers = [];
$_SESSION = [];
$_COOKIE = [];
}

/**
* Return the session content for future restoring. Implements MultiSession.
* @return array backup data
*/
public function _backupSession()
{
if (isset(Yii::$app) && Yii::$app->session->useCustomStorage) {
throw new ModuleException("Yii2 MultiSession only supports the default session backend.");
}
return [
'clientContext' => $this->client->getContext(),
'headers' => $this->headers,
'cookie' => $_COOKIE,
'session' => $_SESSION,
];
}

/**
* Restore a session. Implements MultiSession.
* @param array output of _backupSession()
*/
public function _loadSession($session)
{
$this->client->setContext($session['clientContext']);
$this->headers = $session['headers'];
$_SESSION = $session['session'];
$_COOKIE = $session['cookie'];

// reset Yii::$app->user
if (isset(Yii::$app)) {
$definitions = Yii::$app->getComponents(true);
if (Yii::$app->has('user', true)) {
Yii::$app->set('user', $definitions['user']);
}
}
}

/**
* Close and dump a session. Implements MultiSession.
*/
public function _closeSession($session = null)
{
if (!$session) {
$this->_initializeSession();
}
}
}