1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace espend\Behat\PlaceholderExtension\Context; |
5
|
|
|
|
6
|
|
|
use Behat\Behat\Context\Context; |
7
|
|
|
use espend\Behat\PlaceholderExtension\PlaceholderBagInterface; |
8
|
|
|
use espend\Behat\PlaceholderExtension\Utils\PlaceholderUtil; |
9
|
|
|
use PHPUnit\Framework\Assert as Assertions; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @author Daniel Espendiller <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class PlaceholderContext implements Context, PlaceholderBagAwareContextInterface |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
private $randomizedMail = 'behat-%random%@example.com'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var PlaceholderBagInterface |
23
|
|
|
*/ |
24
|
|
|
private $placeholderBag; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param string $placeholder |
28
|
|
|
* @param string $value |
29
|
|
|
* @Given /^set a placeholder "([^"]*)" with value "([^"]*)"$/ |
30
|
|
|
*/ |
31
|
7 |
|
public function iSetAPlaceholderWithValue(string $placeholder, string $value) |
32
|
|
|
{ |
33
|
7 |
|
$this->validatePlaceholder($placeholder); |
34
|
|
|
|
35
|
6 |
|
$this->placeholderBag->add($placeholder, $value); |
36
|
6 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param string $placeholder |
40
|
|
|
* @Given /^set a random mail in "([^"]*)" placeholder/ |
41
|
|
|
*/ |
42
|
2 |
|
public function iCreateARandomMailPlaceholder(string $placeholder) |
43
|
|
|
{ |
44
|
2 |
|
$this->iSetAPlaceholderWithValue( |
45
|
2 |
|
$placeholder, |
46
|
2 |
|
str_replace('%random%', substr(md5(uniqid()), 0, 6), $this->randomizedMail) |
47
|
|
|
); |
48
|
2 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param string $placeholder |
52
|
|
|
* @Given /^set a random password in "([^"]*)" placeholder/ |
53
|
|
|
*/ |
54
|
1 |
|
public function iCreateARandomPasswordPlaceholder(string $placeholder) |
55
|
|
|
{ |
56
|
|
|
// some special cars |
57
|
1 |
|
$input = ['#', '/', '-', '~', '[', ']']; |
58
|
1 |
|
array_shift($input); |
59
|
|
|
|
60
|
1 |
|
$this->iSetAPlaceholderWithValue( |
61
|
1 |
|
$placeholder, |
62
|
1 |
|
substr(base64_encode(md5(uniqid())), 5, 15) . implode('', array_slice($input, 2)) |
63
|
|
|
); |
64
|
1 |
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param string $format |
68
|
|
|
* @param string $placeholder |
69
|
|
|
* @Given /^set current date as "([^"]*)" format in "([^"]*)" placeholder/ |
70
|
|
|
*/ |
71
|
1 |
|
public function iSetCurrentDatetimeAsFormatInPlaceholder(string $format, string $placeholder) |
72
|
|
|
{ |
73
|
1 |
|
$this->iSetAPlaceholderWithValue( |
74
|
1 |
|
$placeholder, |
75
|
1 |
|
(new \DateTime())->format($format) |
76
|
|
|
); |
77
|
1 |
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param string $length |
81
|
|
|
* @param string $placeholder |
82
|
|
|
* @Given /^set a random text with length "(\d+)" in "([^"]*)" placeholder/ |
83
|
|
|
*/ |
84
|
1 |
|
public function iSetARandomTextWithLengthInPlaceholder(string $length, string $placeholder) |
85
|
|
|
{ |
86
|
1 |
|
Assertions::assertTrue(is_numeric($length), 'Invalid length given need integer'); |
87
|
|
|
|
88
|
|
|
// randomized char whitelist a-z, A-Z, 0-9 |
89
|
1 |
|
$randomChars = implode('', array_merge(range('a', 'z'), range('A', 'Z'), range(0, 9))); |
90
|
|
|
|
91
|
|
|
// randomize text with length |
92
|
1 |
|
$value = substr(str_shuffle(str_repeat($randomChars, mt_rand(1, 5))), 1, (int)$length); |
93
|
|
|
|
94
|
1 |
|
$this->iSetAPlaceholderWithValue($placeholder, $value); |
95
|
1 |
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param string $placeholder |
99
|
|
|
* @Given /^print placeholder value of "([^"]*)"/ |
100
|
|
|
*/ |
101
|
2 |
|
public function printPlaceholderValueOf(string $placeholder) |
102
|
|
|
{ |
103
|
2 |
|
$placeholders = $this->placeholderBag->all(); |
104
|
2 |
|
echo sprintf('Placeholder "%s": "%s"', $placeholder, $placeholders[$placeholder] ?? 'not set'); |
105
|
2 |
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @Given /^print all placeholder values/ |
109
|
|
|
*/ |
110
|
1 |
|
public function printAllPlaceholder() |
111
|
|
|
{ |
112
|
1 |
|
foreach (array_keys($this->placeholderBag->all()) as $key) { |
113
|
1 |
|
$this->printPlaceholderValueOf($key); |
114
|
|
|
} |
115
|
1 |
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param string $placeholder |
119
|
|
|
*/ |
120
|
7 |
|
private function validatePlaceholder(string $placeholder) |
121
|
|
|
{ |
122
|
7 |
|
PlaceholderUtil::isValidPlaceholderOrThrowException($placeholder); |
123
|
6 |
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param string $randomizedMail |
127
|
|
|
*/ |
128
|
2 |
|
public function setRandomizedMail(string $randomizedMail) |
129
|
|
|
{ |
130
|
2 |
|
if (false === strpos($randomizedMail, '%random%')) { |
131
|
1 |
|
throw new \RuntimeException('Please provide a %random% placeholder'); |
132
|
|
|
} |
133
|
|
|
|
134
|
1 |
|
$this->randomizedMail = $randomizedMail; |
135
|
1 |
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* {@inheritdoc} |
139
|
|
|
*/ |
140
|
9 |
|
public function setPlaceholderBag(PlaceholderBagInterface $placeholderBag) |
141
|
|
|
{ |
142
|
9 |
|
$this->placeholderBag = $placeholderBag; |
143
|
9 |
|
} |
144
|
|
|
} |
145
|
|
|
|