|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Kolyunya\Codeception\Lib\MarkupValidator; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use Codeception\Util\Shared\Asserts; |
|
7
|
|
|
use Kolyunya\Codeception\Lib\Base\Component; |
|
8
|
|
|
use Kolyunya\Codeception\Lib\MarkupValidator\MarkupReporterInterface; |
|
9
|
|
|
use Kolyunya\Codeception\Lib\MarkupValidator\MarkupValidatorMessageInterface; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Default markup validation message reporter. |
|
13
|
|
|
*/ |
|
14
|
|
|
class DefaultMarkupReporter extends Component implements MarkupReporterInterface |
|
15
|
|
|
{ |
|
16
|
|
|
const ERROR_COUNT_THRESHOLD_KEY = 'errorCountThreshold'; |
|
17
|
|
|
|
|
18
|
|
|
const IGNORE_WARNINGS_CONFIG_KEY = 'ignoreWarnings'; |
|
19
|
|
|
|
|
20
|
|
|
const IGNORED_ERRORS_CONFIG_KEY = 'ignoredErrors'; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Use asserts to report messages. |
|
24
|
|
|
*/ |
|
25
|
|
|
use Asserts; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Configuration parameters. |
|
29
|
|
|
* |
|
30
|
|
|
* @var array |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $configuration = array( |
|
33
|
|
|
self::ERROR_COUNT_THRESHOLD_KEY => 0, |
|
34
|
|
|
self::IGNORE_WARNINGS_CONFIG_KEY => true, |
|
35
|
|
|
self::IGNORED_ERRORS_CONFIG_KEY => array(), |
|
36
|
|
|
); |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* {@inheritDoc} |
|
40
|
|
|
*/ |
|
41
|
|
|
public function __construct(array $configuration = array()) |
|
42
|
|
|
{ |
|
43
|
|
|
parent::__construct($configuration); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* {@inheritDoc} |
|
48
|
|
|
*/ |
|
49
|
|
|
public function report(array $messages) |
|
50
|
|
|
{ |
|
51
|
|
|
$filteredMessages = $this->filterMesages($messages); |
|
52
|
|
|
|
|
53
|
|
|
if ($this->belowErrorCountThreshold($filteredMessages) === true) { |
|
54
|
|
|
return; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
$report = implode("\n", $messages); |
|
58
|
|
|
$this->fail($report); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Filters messages to report. |
|
63
|
|
|
* |
|
64
|
|
|
* @param array $messages Messages to filter. |
|
65
|
|
|
* |
|
66
|
|
|
* @return array Filtered messages. |
|
67
|
|
|
*/ |
|
68
|
|
|
private function filterMesages(array $messages) |
|
69
|
|
|
{ |
|
70
|
|
|
$filteredMessages = array(); |
|
71
|
|
|
|
|
72
|
|
|
foreach ($messages as $message) { |
|
73
|
|
|
/* @var $message MarkupValidatorMessageInterface */ |
|
74
|
|
|
$messageType = $message->getType(); |
|
75
|
|
|
|
|
76
|
|
|
if ($messageType === MarkupValidatorMessageInterface::TYPE_UNDEFINED || |
|
77
|
|
|
$messageType === MarkupValidatorMessageInterface::TYPE_INFO |
|
78
|
|
|
) { |
|
79
|
|
|
continue; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
if ($messageType === MarkupValidatorMessageInterface::TYPE_WARNING && |
|
83
|
|
|
$this->ignoreWarnings() === true |
|
84
|
|
|
) { |
|
85
|
|
|
continue; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
if ($this->ignoreError($message->getSummary()) === true) { |
|
89
|
|
|
continue; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
$filteredMessages[] = $message; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
return $filteredMessages; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Returns a boolean indicating whether messages count |
|
100
|
|
|
* is below the threshold or not. |
|
101
|
|
|
* |
|
102
|
|
|
* @param array $messages Messages to report about. |
|
103
|
|
|
* |
|
104
|
|
|
* @return boolean Whether messages count is below the threshold or not. |
|
105
|
|
|
*/ |
|
106
|
|
|
private function belowErrorCountThreshold(array $messages) |
|
107
|
|
|
{ |
|
108
|
|
|
if (is_int($this->configuration[self::ERROR_COUNT_THRESHOLD_KEY]) === false) { |
|
109
|
|
|
throw new Exception(sprintf('Invalid «%s» config key.', self::ERROR_COUNT_THRESHOLD_KEY)); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
$threshold = $this->configuration[self::ERROR_COUNT_THRESHOLD_KEY]; |
|
113
|
|
|
$belowThreshold = count($messages) <= $threshold; |
|
114
|
|
|
|
|
115
|
|
|
return $belowThreshold; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Returns a boolean indicating whether the reporter ignores warnings or not. |
|
120
|
|
|
* |
|
121
|
|
|
* @return bool Whether the reporter ignores warnings or not. |
|
122
|
|
|
*/ |
|
123
|
|
|
private function ignoreWarnings() |
|
124
|
|
|
{ |
|
125
|
|
|
if (is_bool($this->configuration[self::IGNORE_WARNINGS_CONFIG_KEY]) === false) { |
|
126
|
|
|
throw new Exception(sprintf('Invalid «%s» config key.', self::IGNORE_WARNINGS_CONFIG_KEY)); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/* @var $ignoreWarnings bool */ |
|
130
|
|
|
$ignoreWarnings = $this->configuration[self::IGNORE_WARNINGS_CONFIG_KEY]; |
|
131
|
|
|
|
|
132
|
|
|
return $ignoreWarnings; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Returns a boolean indicating whether an error is ignored or not. |
|
137
|
|
|
* |
|
138
|
|
|
* @param string|null $summary Error summary. |
|
139
|
|
|
* @return boolean Whether an error is ignored or not. |
|
140
|
|
|
*/ |
|
141
|
|
|
private function ignoreError($summary) |
|
142
|
|
|
{ |
|
143
|
|
|
if (is_array($this->configuration[self::IGNORED_ERRORS_CONFIG_KEY]) === false) { |
|
144
|
|
|
throw new Exception(sprintf('Invalid «%s» config key.', self::IGNORED_ERRORS_CONFIG_KEY)); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
$ignoreError = false; |
|
148
|
|
|
|
|
149
|
|
|
if ($summary === null) { |
|
150
|
|
|
return $ignoreError; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
$ignoredErrors = $this->configuration[self::IGNORED_ERRORS_CONFIG_KEY]; |
|
154
|
|
|
foreach ($ignoredErrors as $ignoredError) { |
|
155
|
|
|
$erorIsIgnored = preg_match($ignoredError, $summary) === 1; |
|
156
|
|
|
if ($erorIsIgnored) { |
|
157
|
|
|
$ignoreError = true; |
|
158
|
|
|
break; |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
return $ignoreError; |
|
163
|
|
|
} |
|
164
|
|
|
} |
|
165
|
|
|
|