1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php
namespace Codeception\Lib;
use Codeception\Actor;
use Codeception\Exception\TestRuntimeException;
class Friend
{
protected $name;
protected $actor;
protected $data = [];
protected $multiSessionModules = [];
public function __construct($name, Actor $actor, $modules = [])
{
$this->name = $name;
$this->actor = $actor;
$this->multiSessionModules = array_filter($modules, function ($m) {
return $m instanceof Interfaces\MultiSession;
});
if (empty($this->multiSessionModules)) {
throw new TestRuntimeException("No multisession modules used. Can't instantiate friend");
}
}
public function does($closure)
{
$currentUserData = [];
foreach ($this->multiSessionModules as $module) {
$name = $module->_getName();
$currentUserData[$name] = $module->_backupSession();
if (empty($this->data[$name])) {
$module->_initializeSession();
$this->data[$name] = $module->_backupSession();
continue;
}
$module->_loadSession($this->data[$name]);
};
$this->actor->comment(strtoupper("{$this->name} does ---"));
$ret = $closure($this->actor);
$this->actor->comment(strtoupper("--- {$this->name} finished"));
foreach ($this->multiSessionModules as $module) {
$name = $module->_getName();
$this->data[$name] = $module->_backupSession();
$module->_loadSession($currentUserData[$name]);
};
return $ret;
}
public function isGoingTo($argumentation)
{
$this->actor->amGoingTo($argumentation);
}
public function expects($prediction)
{
$this->actor->expect($prediction);
}
public function expectsTo($prediction)
{
$this->actor->expectTo($prediction);
}
public function leave()
{
foreach ($this->multiSessionModules as $module) {
if (isset($this->data[$module->_getName()])) {
$module->_closeSession($this->data[$module->_getName()]);
}
}
}
}