AuthenticationTrait::enterIdentity()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Webino (http://webino.sk/)
4
 *
5
 * @link        https://github.com/webino/WebinoDev/ for the canonical source repository
6
 * @copyright   Copyright (c) 2014-2017 Webino, s. r. o. (http://webino.sk/)
7
 * @license     BSD-3-Clause
8
 */
9
10
namespace WebinoDev\Test\Selenium;
11
12
use RuntimeException;
13
14
/**
15
 * WebDriver test authentication trait
16
 *
17
 * Use this trait when you want a WebDriver test authentication handling.
18
 */
19
trait AuthenticationTrait
20
{
21
    /**
22
     * Key to resolve identity from an environment
23
     */
24
    public static $identityEnv = 'IDENTITY';
25
26
    /**
27
     * Key to resolve credential from an environment
28
     */
29
    public static $credentialEnv = 'CREDENTIAL';
30
31
    /**
32
     * Login
33
     *
34
     * @var string
35
     */
36
    protected $identity;
37
38
    /**
39
     * Password
40
     */
41
    protected $credential;
42
43
    /**
44
     * Login input name
45
     *
46
     * @var string
47
     */
48
    public $identityName = 'identity';
49
50
    /**
51
     * Password input name
52
     *
53
     * @var string
54
     */
55
    public $credentialName = 'credential';
56
57
    /**
58
     * CSS selector of element to assert after successful authentication
59
     *
60
     * @var string
61
     */
62
    protected $authSuccessLocator;
63
64
    /**
65
     * Enters the input value
66
     *
67
     * @param string $name
68
     * @param string $value
69
     * @param callable|false|null $callback
70
     * @return $this
71
     */
72
    abstract protected function enterInput($name, $value, $callback = null);
73
74
    /**
75
     * Wait for something, then do something else
76
     *
77
     * @param callable $action
78
     * @param callable $callback
79
     * @return $this
80
     */
81
    abstract protected function waitFor(callable $action, callable $callback = null);
82
83
    /**
84
     * Returns element by CSS selector
85
     *
86
     * @param string $selector
87
     * @return \PHPWebDriver_WebDriverElement
88
     */
89
    abstract protected function elementByCssSelector($selector);
90
91
    /**
92
     * Resolve test identity from an environment variable
93
     *
94
     * @return string
95
     */
96
    protected function getIdentity()
97
    {
98
        if (!$this->identity) {
99
            $identity = getenv($this::$identityEnv);
100
            if (empty($identity)) {
101
                throw new RuntimeException(sprintf('Expected %s env', $this::$identityEnv));
102
            }
103
            $this->setIdentity($identity);
104
        }
105
106
        return $this->identity;
107
    }
108
109
    /**
110
     * Resolve test credential from an environment variable
111
     *
112
     * @return string
113
     */
114
    protected function getCredential()
115
    {
116
        if (!$this->credential) {
117
            $credential = getenv($this::$credentialEnv);
118
            if (empty($credential)) {
119
                throw new RuntimeException(sprintf('Expected %s env', $this::$credentialEnv));
120
            }
121
            $this->setCredential($credential);
122
        }
123
124
        return $this->credential;
125
    }
126
127
    /**
128
     * @return string
129
     * @throws RuntimeException Use setAuthSuccessLocator() first
130
     */
131
    protected function getAuthSuccessLocator()
132
    {
133
        if (empty($this->authSuccessLocator)) {
134
            throw new RuntimeException('You have to call setAuthSuccessLocator() first');
135
        }
136
        return $this->authSuccessLocator;
137
    }
138
139
    /**
140
     * Set login for authentication
141
     *
142
     * @param string $identity
143
     * @return $this
144
     */
145
    protected function setIdentity($identity)
146
    {
147
        $this->identity = (string) $identity;
148
        return $this;
149
    }
150
151
    /**
152
     * Set password for authentication
153
     *
154
     * @param string $credential
155
     * @return $this
156
     */
157
    protected function setCredential($credential)
158
    {
159
        $this->credential = (string) $credential;
160
        return $this;
161
    }
162
163
    /**
164
     * @param string $locator CSS selector
165
     * @return $this
166
     */
167
    protected function setAuthSuccessLocator($locator)
168
    {
169
        $this->authSuccessLocator = (string) $locator;
170
        return $this;
171
    }
172
173
    /**
174
     * Authenticate and check for success
175
     *
176
     * @return $this
177
     */
178
    protected function authenticate(callable $callback = null)
179
    {
180
        $this
181
            ->enterIdentity()
182
            ->enterCredential(function ($elm) {
183
                $elm->submit();
184
            })
185
            ->waitToBeAuthenticated($callback);
186
187
        return $this;
188
    }
189
190
    /**
191
     * Enters the password into input
192
     *
193
     * @param callable $callback
194
     * @return $this
195
     */
196
    protected function enterIdentity(callable $callback = null)
197
    {
198
        $this->enterInput($this->identityName, $this->getIdentity(), $callback);
199
        return $this;
200
    }
201
202
    /**
203
     * Enters the password into input
204
     *
205
     * @param callable $callback
206
     * @return $this
207
     */
208
    protected function enterCredential(callable $callback = null)
209
    {
210
        $this->enterInput($this->credentialName, $this->getCredential(), $callback);
211
        return $this;
212
    }
213
214
    /**
215
     * Check that we are authenticated successfuly
216
     *
217
     * @param callable $callback
218
     * @return $this
219
     */
220
    protected function waitToBeAuthenticated(callable $callback = null)
221
    {
222
        $this->waitFor(
223
            function () {
224
                 return $this->elementByCssSelector($this->getAuthSuccessLocator());
225
            },
226
            $callback
227
        );
228
        return $this;
229
    }
230
}
231