diff --git a/src/Codeception/Lib/Connector/Yii2.php b/src/Codeception/Lib/Connector/Yii2.php index a0ae874..2e9e419 100644 --- a/src/Codeception/Lib/Connector/Yii2.php +++ b/src/Codeception/Lib/Connector/Yii2.php @@ -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 diff --git a/src/Codeception/Module/Yii2.php b/src/Codeception/Module/Yii2.php index 969b75f..4d1f0d6 100644 --- a/src/Codeception/Module/Yii2.php +++ b/src/Codeception/Module/Yii2.php @@ -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; @@ -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. @@ -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(); + } + } }