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