|
8 | 8 | use Codeception\Lib\Connector\Yii2 as Yii2Connector;
|
9 | 9 | use Codeception\Lib\Framework;
|
10 | 10 | use Codeception\Lib\Interfaces\ActiveRecord;
|
| 11 | +use Codeception\Lib\Interfaces\MultiSession; |
11 | 12 | use Codeception\Lib\Interfaces\PartedModule;
|
12 | 13 | use Codeception\TestInterface;
|
13 | 14 | use Codeception\Util\Debug;
|
|
178 | 179 | *
|
179 | 180 | * @property \Codeception\Lib\Connector\Yii2 $client
|
180 | 181 | */
|
181 |
| -class Yii2 extends Framework implements ActiveRecord, PartedModule |
| 182 | +class Yii2 extends Framework implements ActiveRecord, MultiSession, PartedModule |
182 | 183 | {
|
183 | 184 | /**
|
184 | 185 | * Application config file must be set.
|
@@ -866,4 +867,62 @@ public function _afterSuite()
|
866 | 867 |
|
867 | 868 | $_SERVER = $this->server;
|
868 | 869 | }
|
| 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 | + } |
869 | 928 | }
|
0 commit comments