-
Notifications
You must be signed in to change notification settings - Fork 132
/
Copy pathConsole.php
249 lines (217 loc) · 7.25 KB
/
Console.php
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\FunctionalTestingFramework\Codeception\Subscriber;
use Codeception\Event\StepEvent;
use Codeception\Event\TestEvent;
use Codeception\Lib\Console\Message;
use Codeception\Step;
use Codeception\Step\Comment;
use Codeception\Test\Interfaces\ScenarioDriven;
use Magento\FunctionalTestingFramework\Test\Objects\ActionGroupObject;
use Magento\FunctionalTestingFramework\Test\Objects\ActionObject;
use Magento\FunctionalTestingFramework\Util\Logger\LoggingUtil;
use Magento\FunctionalTestingFramework\Util\TestGenerator;
use Symfony\Component\Console\Formatter\OutputFormatter;
/**
* @SuppressWarnings(PHPMD)
*/
class Console extends \Codeception\Subscriber\Console
{
/**
* Regular expresion to find deprecated notices.
*/
const DEPRECATED_NOTICE = '/<li>(?<deprecatedMessage>.*?)<\/li>/m';
/**
* Test files cache.
*
* @var array
*/
private $testFiles = [];
/**
* Action group step key.
*
* @var null|string
*/
private $actionGroupStepKey = null;
/**
* Boolean value to indicate if steps are invisible steps
*
* @var boolean
*/
private $atInvisibleSteps = false;
/**
* Console constructor. Parent constructor requires codeception CLI options, and does not have its own configs.
* Constructor is only different than parent due to the way Codeception instantiates Extensions.
*
* @param array $extensionOptions
* @param array $options
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function __construct($extensionOptions = [], $options = [])
{
parent::__construct($options);
}
/**
* Triggered event before each test.
*
* @param TestEvent $e
* @return void
* @throws \Exception
*/
public function startTest(TestEvent $e)
{
$test = $e->getTest()->getTestClass();
try {
$testReflection = new \ReflectionClass($test);
$isDeprecated = preg_match_all(self::DEPRECATED_NOTICE, $testReflection->getDocComment(), $match);
if ($isDeprecated) {
$this->message('DEPRECATION NOTICE(S): ')
->style('debug')
->writeln();
foreach ($match['deprecatedMessage'] as $deprecatedMessage) {
$this->message(' - ' . $deprecatedMessage)
->style('debug')
->writeln();
}
}
} catch (\ReflectionException $e) {
LoggingUtil::getInstance()->getLogger(self::class)->error($e->getMessage(), $e->getTrace());
}
parent::startTest($e);
}
/**
* Printing stepKey in before step action.
*
* @param StepEvent $e
* @return void
*/
public function beforeStep(StepEvent $e)
{
if ($this->silent or !$this->steps or !$e->getTest() instanceof ScenarioDriven) {
return;
}
$stepAction = $e->getStep()->getAction();
// Set atInvisibleSteps flag and return if step is in INVISIBLE_STEP_ACTIONS
if (in_array($stepAction, ActionObject::INVISIBLE_STEP_ACTIONS)) {
$this->atInvisibleSteps = true;
return;
}
// Set back atInvisibleSteps flag
if ($this->atInvisibleSteps && !in_array($stepAction, ActionObject::INVISIBLE_STEP_ACTIONS)) {
$this->atInvisibleSteps = false;
}
$metaStep = $e->getStep()->getMetaStep();
if ($metaStep and $this->metaStep != $metaStep) {
$this->message(' ' . $metaStep->getPrefix())
->style('bold')
->append($metaStep->__toString())
->writeln();
}
$this->metaStep = $metaStep;
$this->printStepKeys($e->getStep());
}
/**
* If step failed we move back from action group to test scope
*
* @param StepEvent $e
* @return void
*/
public function afterStep(StepEvent $e)
{
// Do usual after step if step is not INVISIBLE_STEP_ACTIONS
if (!$this->atInvisibleSteps) {
parent::afterStep($e);
}
if ($e->getStep()->hasFailed()) {
$this->actionGroupStepKey = null;
$this->atInvisibleSteps = false;
}
}
/**
* Print output to cli with stepKey.
*
* @param Step $step
* @return void
* @SuppressWarnings(PHPMD)
*/
private function printStepKeys(Step $step)
{
if ($step instanceof Comment and $step->__toString() == '') {
return; // don't print empty comments
}
$stepKey = $this->retrieveStepKey($step->getLine());
$isActionGroup = (strpos($step->__toString(), ActionGroupObject::ACTION_GROUP_CONTEXT_START) !== false);
if ($isActionGroup) {
preg_match(TestGenerator::ACTION_GROUP_STEP_KEY_REGEX, $step->__toString(), $matches);
if (!empty($matches['actionGroupStepKey'])) {
$this->actionGroupStepKey = ucfirst($matches['actionGroupStepKey']);
}
}
if (strpos($step->__toString(), ActionGroupObject::ACTION_GROUP_CONTEXT_END) !== false) {
$this->actionGroupStepKey = null;
return;
}
$msg = $this->message();
if ($this->metaStep || ($this->actionGroupStepKey !== null && !$isActionGroup)) {
$msg->append(' ');
}
if ($stepKey !== null) {
$msg->append(OutputFormatter::escape("[" . $stepKey . "] "));
$msg->style('bold');
}
if (!$this->metaStep) {
$msg->style('bold');
}
$stepString = str_replace(
[ActionGroupObject::ACTION_GROUP_CONTEXT_START, ActionGroupObject::ACTION_GROUP_CONTEXT_END],
'',
$step->toString(150)
);
$msg->append(OutputFormatter::escape($stepString));
if ($isActionGroup) {
$msg->style('comment');
}
if ($this->metaStep || ($this->actionGroupStepKey !== null && !$isActionGroup)) {
$msg->style('info');
}
$msg->writeln();
}
/**
* Message instance.
*
* @param string $string
* @return Message
*/
private function message($string = '')
{
return $this->messageFactory->message($string);
}
/**
* Reading stepKey from file.
*
* @param string $stepLine
* @return string|null
*/
private function retrieveStepKey($stepLine)
{
$stepKey = null;
list($filePath, $stepLine) = explode(":", $stepLine);
$stepLine = $stepLine - 1;
if (!array_key_exists($filePath, $this->testFiles)) {
$this->testFiles[$filePath] = explode(PHP_EOL, file_get_contents($filePath));
}
preg_match(TestGenerator::ACTION_STEP_KEY_REGEX, $this->testFiles[$filePath][$stepLine], $matches);
if (!empty($matches['stepKey'])) {
$stepKey = $matches['stepKey'];
}
if ($this->actionGroupStepKey !== null) {
$stepKey = str_replace($this->actionGroupStepKey, '', $stepKey);
}
$stepKey = $stepKey === '[]' ? null : $stepKey;
return $stepKey;
}
}