-
Notifications
You must be signed in to change notification settings - Fork 132
/
Copy pathConsole.php
183 lines (159 loc) · 5.13 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
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\FunctionalTestingFramework\Codeception\Subscriber;
use Codeception\Event\StepEvent;
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\Util\TestGenerator;
use Symfony\Component\Console\Formatter\OutputFormatter;
class Console extends \Codeception\Subscriber\Console
{
/**
* Test files cache.
*
* @var array
*/
private $testFiles = [];
/**
* Action group step key.
*
* @var null|string
*/
private $actionGroupStepKey = null;
/**
* 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);
}
/**
* 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;
}
$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)
{
parent::afterStep($e);
if ($e->getStep()->hasFailed()) {
$this->actionGroupStepKey = null;
}
}
/**
* 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;
}
}