Completed
Push — master ( c69dc2...614661 )
by Dmitry
10:19 queued 07:23
created

CredentialsProvider::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 12
rs 9.8666
cc 1
nc 1
nop 2
1
<?php
2
namespace hipanel\tests\_support\Helper;
3
4
use Codeception\Exception\ModuleException;
5
use Codeception\Lib\ModuleContainer;
6
use Codeception\Module\WebDriver;
7
use hipanel\helpers\Url;
8
9
/**
10
 * Class CredentialsProvider
11
 *
12
 * @author Dmytro Naumenko <[email protected]>
13
 */
14
class CredentialsProvider extends \Codeception\Module
15
{
16
    /**
17
     * @var array
18
     */
19
    private $sessionSnapshots = [];
20
21
    /**
22
     * @var string
23
     */
24
    private $filename = WEBAPP_ROOT_DIR . '/tests/_data/sessionSnapshots.data';
25
26
    public function __construct(ModuleContainer $moduleContainer, $config = null)
27
    {
28
        parent::__construct($moduleContainer, $config);
29
30
        $this->requiredFields = [
31
            // Client
32
            'client.id', 'client.login', 'client.password',
33
            'seller.id', 'seller.login', 'seller.password',
34
            'manager.id', 'manager.login', 'manager.password',
35
            'admin.id', 'admin.login', 'admin.password',
36
        ];
37
    }
38
39
    public function getClientCredentials(): array
40
    {
41
        return [$this->config['client.id'], $this->config['client.login'], $this->config['client.password']];
42
    }
43
44
    public function getSellerCredentials(): array
45
    {
46
        return [$this->config['seller.id'], $this->config['seller.login'], $this->config['seller.password']];
47
    }
48
49
    public function getManagerCredentials(): array
50
    {
51
        return [$this->config['manager.id'], $this->config['manager.login'], $this->config['manager.password']];
52
    }
53
54
    public function getAdminCredentials(): array
55
    {
56
        return [$this->config['admin.id'], $this->config['admin.login'], $this->config['admin.password']];
57
    }
58
59
    public function restartBrowser()
60
    {
61
        /** @var WebDriver $wd */
62
        $wd = $this->getModule('WebDriver');
63
        $wd->_restart();
64
    }
65
66
    public function needPage(string $url): void
67
    {
68
        /** @var WebDriver $wd */
69
        $wd = $this->getModule('WebDriver');
70
        $currentUrl = $wd->grabFromCurrentUrl();
71
        if ($currentUrl !== $url) {
72
            $wd->amOnPage($url);
73
        }
74
    }
75
76
    public function storeSession(string $name): void
77
    {
78
        /** @var WebDriver $wd */
79
        $wd = $this->getModule('WebDriver');
80
        $wd->saveSessionSnapshot($name);
81
82
        $reflection = new \ReflectionObject($wd);
83
        $property = $reflection->getProperty('sessionSnapshots');
84
        $property->setAccessible(true);
85
        $snapshots = $property->getValue($wd);
86
        $property->setAccessible(false);
87
88
        $this->sessionSnapshots[$name] = $snapshots[$name];
89
        $this->persistSessionSnapshots();
90
    }
91
92
    public function retrieveSession(string $name): bool
93
    {
94
        $this->readSessionSnapshots();
95
96
        /** @var WebDriver $wd */
97
        $wd = $this->getModule('WebDriver');
98
99
        try {
100
            $wd->_getCurrentUri();
101
        } catch (ModuleException $e) {
0 ignored issues
show
Bug introduced by
The class Codeception\Exception\ModuleException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
102
            $wd->amOnPage(Url::to('/site/healthcheck'));
103
        }
104
        $reflection = new \ReflectionObject($wd);
105
        $property = $reflection->getProperty('sessionSnapshots');
106
        $property->setAccessible(true);
107
        $property->setValue($wd, array_merge($property->getValue($wd), $this->sessionSnapshots));
108
        $property->setAccessible(false);
109
110
        return $wd->loadSessionSnapshot($name);
111
    }
112
113
    private function persistSessionSnapshots()
114
    {
115
        file_put_contents($this->filename, serialize($this->sessionSnapshots));
116
    }
117
118
    private function readSessionSnapshots()
119
    {
120
        if (!file_exists($this->filename)) {
121
            return;
122
        }
123
124
        $expires = strtotime('+1 day', filemtime($this->filename));
125
        $now = time();
126
        if ($now > $expires) {
127
            unlink($this->filename);
128
        } else {
129
            $this->sessionSnapshots = unserialize(file_get_contents($this->filename));
130
        }
131
    }
132
}
133