Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 89ec2b3

Browse files
authoredJan 28, 2020
Merge pull request #3 from mytskine/2-multisession
Module implements Codeception's MultiSession
2 parents 9796e9a + 2422215 commit 89ec2b3

File tree

2 files changed

+92
-1
lines changed

2 files changed

+92
-1
lines changed
 

‎src/Codeception/Lib/Connector/Yii2.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,38 @@ public function restart()
455455
$this->resetApplication();
456456
}
457457

458+
/**
459+
* Return an assoc array with the client context: cookieJar, history.
460+
*
461+
* @return array
462+
*/
463+
public function getContext()
464+
{
465+
return [
466+
'cookieJar' => $this->cookieJar,
467+
'history' => $this->history,
468+
];
469+
}
470+
471+
/**
472+
* Reset the client context: empty cookieJar and history.
473+
*/
474+
public function removeContext()
475+
{
476+
parent::restart();
477+
}
478+
479+
/**
480+
* Set the context, see getContext().
481+
*
482+
* @param array $context
483+
*/
484+
public function setContext(array $context)
485+
{
486+
$this->cookieJar = $context['cookieJar'];
487+
$this->history = $context['history'];
488+
}
489+
458490
/**
459491
* This functions closes the session of the application, if the application exists and has a session.
460492
* @internal

‎src/Codeception/Module/Yii2.php

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Codeception\Lib\Connector\Yii2 as Yii2Connector;
99
use Codeception\Lib\Framework;
1010
use Codeception\Lib\Interfaces\ActiveRecord;
11+
use Codeception\Lib\Interfaces\MultiSession;
1112
use Codeception\Lib\Interfaces\PartedModule;
1213
use Codeception\TestInterface;
1314
use Codeception\Util\Debug;
@@ -178,7 +179,7 @@
178179
*
179180
* @property \Codeception\Lib\Connector\Yii2 $client
180181
*/
181-
class Yii2 extends Framework implements ActiveRecord, PartedModule
182+
class Yii2 extends Framework implements ActiveRecord, MultiSession, PartedModule
182183
{
183184
/**
184185
* Application config file must be set.
@@ -866,4 +867,62 @@ public function _afterSuite()
866867

867868
$_SERVER = $this->server;
868869
}
870+
871+
/**
872+
* Initialize an empty session. Implements MultiSession.
873+
*/
874+
public function _initializeSession()
875+
{
876+
$this->client->removeContext();
877+
$this->headers = [];
878+
$_SESSION = [];
879+
$_COOKIE = [];
880+
}
881+
882+
/**
883+
* Return the session content for future restoring. Implements MultiSession.
884+
* @return array backup data
885+
*/
886+
public function _backupSession()
887+
{
888+
if (isset(Yii::$app) && Yii::$app->session->useCustomStorage) {
889+
throw new ModuleException("Yii2 MultiSession only supports the default session backend.");
890+
}
891+
return [
892+
'clientContext' => $this->client->getContext(),
893+
'headers' => $this->headers,
894+
'cookie' => $_COOKIE,
895+
'session' => $_SESSION,
896+
];
897+
}
898+
899+
/**
900+
* Restore a session. Implements MultiSession.
901+
* @param array output of _backupSession()
902+
*/
903+
public function _loadSession($session)
904+
{
905+
$this->client->setContext($session['clientContext']);
906+
$this->headers = $session['headers'];
907+
$_SESSION = $session['session'];
908+
$_COOKIE = $session['cookie'];
909+
910+
// reset Yii::$app->user
911+
if (isset(Yii::$app)) {
912+
$definitions = Yii::$app->getComponents(true);
913+
if (Yii::$app->has('user', true)) {
914+
Yii::$app->set('user', $definitions['user']);
915+
}
916+
}
917+
}
918+
919+
/**
920+
* Close and dump a session. Implements MultiSession.
921+
*/
922+
public function _closeSession($session = null)
923+
{
924+
if (!$session) {
925+
$this->_initializeSession();
926+
}
927+
}
869928
}

0 commit comments

Comments
 (0)
Please sign in to comment.