1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kolyunya\Codeception\Module; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use ReflectionClass; |
7
|
|
|
use Codeception\Lib\ModuleContainer; |
8
|
|
|
use Codeception\Module; |
9
|
|
|
use Kolyunya\Codeception\Lib\Base\ComponentInterface; |
10
|
|
|
use Kolyunya\Codeception\Lib\MarkupValidator\MarkupProviderInterface; |
11
|
|
|
use Kolyunya\Codeception\Lib\MarkupValidator\MarkupValidatorInterface; |
12
|
|
|
use Kolyunya\Codeception\Lib\MarkupValidator\MessageFilterInterface; |
13
|
|
|
use Kolyunya\Codeception\Lib\MarkupValidator\MessagePrinterInterface; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* A module which validates page markup via a markup validator. |
17
|
|
|
*/ |
18
|
|
|
class MarkupValidator extends Module |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
const COMPONENT_CLASS_CONFIG_KEY = 'class'; |
22
|
|
|
|
23
|
|
|
const COMPONENT_CONFIG_CONFIG_KEY = 'config'; |
24
|
|
|
|
25
|
|
|
const PROVIDER_INTERFACE = 'Kolyunya\Codeception\Lib\MarkupValidator\MarkupProviderInterface'; |
26
|
|
|
const PROVIDER_CONFIG_KEY = 'provider'; |
27
|
|
|
|
28
|
|
|
const VALIDATOR_INTERFACE = 'Kolyunya\Codeception\Lib\MarkupValidator\MarkupValidatorInterface'; |
29
|
|
|
const VALIDATOR_CONFIG_KEY = 'validator'; |
30
|
|
|
|
31
|
|
|
const FILTER_INTERFACE = 'Kolyunya\Codeception\Lib\MarkupValidator\MessageFilterInterface'; |
32
|
|
|
const FILTER_CONFIG_KEY = 'filter'; |
33
|
|
|
|
34
|
|
|
const PRINTER_INTERFACE = 'Kolyunya\Codeception\Lib\MarkupValidator\MessagePrinterInterface'; |
35
|
|
|
const PRINTER_CONFIG_KEY = 'printer'; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* {@inheritDoc} |
39
|
|
|
*/ |
40
|
|
|
protected $config = array( |
41
|
|
|
self::PROVIDER_CONFIG_KEY => array( |
42
|
|
|
self::COMPONENT_CLASS_CONFIG_KEY => 'Kolyunya\Codeception\Lib\MarkupValidator\DefaultMarkupProvider', |
43
|
|
|
), |
44
|
|
|
self::VALIDATOR_CONFIG_KEY => array( |
45
|
|
|
self::COMPONENT_CLASS_CONFIG_KEY => 'Kolyunya\Codeception\Lib\MarkupValidator\W3CMarkupValidator', |
46
|
|
|
), |
47
|
|
|
self::FILTER_CONFIG_KEY => array( |
48
|
|
|
self::COMPONENT_CLASS_CONFIG_KEY => 'Kolyunya\Codeception\Lib\MarkupValidator\DefaultMessageFilter', |
49
|
|
|
), |
50
|
|
|
self::PRINTER_CONFIG_KEY => array( |
51
|
|
|
self::COMPONENT_CLASS_CONFIG_KEY => 'Kolyunya\Codeception\Lib\MarkupValidator\DefaultMessagePrinter', |
52
|
|
|
), |
53
|
|
|
); |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Markup provider. |
57
|
|
|
* |
58
|
|
|
* @var MarkupProviderInterface |
59
|
|
|
*/ |
60
|
|
|
private $markupProvider; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Markup validator. |
64
|
|
|
* |
65
|
|
|
* @var MarkupValidatorInterface |
66
|
|
|
*/ |
67
|
|
|
private $markupValidator; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Message filter. |
71
|
|
|
* |
72
|
|
|
* @var MessageFilterInterface |
73
|
|
|
*/ |
74
|
|
|
private $messageFilter; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Message printer. |
78
|
|
|
* |
79
|
|
|
* @var MessagePrinterInterface |
80
|
|
|
*/ |
81
|
|
|
private $messagePrinter; |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* {@inheritDoc} |
85
|
|
|
*/ |
86
|
|
|
public function __construct(ModuleContainer $moduleContainer, $config = null) |
87
|
|
|
{ |
88
|
|
|
parent::__construct($moduleContainer, $config); |
89
|
|
|
|
90
|
|
|
$this->initializeMarkupProvider(); |
91
|
|
|
$this->initializeMarkupValidator(); |
92
|
|
|
$this->initializeMessageFilter(); |
93
|
|
|
$this->initializeMessagePrinter(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Validates page markup via a markup validator. |
98
|
|
|
* Allows to recongigure message filter component. |
99
|
|
|
* |
100
|
|
|
* @param array $messageFilterConfiguration Message filter configuration. |
101
|
|
|
*/ |
102
|
|
|
public function validateMarkup(array $messageFilterConfiguration = array()) |
103
|
|
|
{ |
104
|
|
|
$markup = $this->markupProvider->getMarkup(); |
105
|
|
|
$messages = $this->markupValidator->validate($markup); |
106
|
|
|
|
107
|
|
|
$this->messageFilter->setConfiguration($messageFilterConfiguration); |
108
|
|
|
$filteredMessages = $this->messageFilter->filterMessages($messages); |
109
|
|
|
|
110
|
|
|
if (empty($filteredMessages) === false) { |
111
|
|
|
$messagesString = $this->messagePrinter->getMessagesString($filteredMessages); |
112
|
|
|
$this->fail($messagesString); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
// Validation succeeded. |
116
|
|
|
$this->assertTrue(true); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Initializes markup provider. |
121
|
|
|
*/ |
122
|
|
|
private function initializeMarkupProvider() |
123
|
|
|
{ |
124
|
|
|
$this->markupProvider = $this->instantiateComponent( |
125
|
|
|
self::PROVIDER_CONFIG_KEY, |
126
|
|
|
self::PROVIDER_INTERFACE, |
127
|
|
|
array( |
128
|
|
|
$this->moduleContainer, |
129
|
|
|
) |
130
|
|
|
); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Initializes markup validator. |
135
|
|
|
*/ |
136
|
|
|
private function initializeMarkupValidator() |
137
|
|
|
{ |
138
|
|
|
$this->markupValidator = $this->instantiateComponent( |
139
|
|
|
self::VALIDATOR_CONFIG_KEY, |
140
|
|
|
self::VALIDATOR_INTERFACE |
141
|
|
|
); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Initializes message filter. |
146
|
|
|
*/ |
147
|
|
|
private function initializeMessageFilter() |
148
|
|
|
{ |
149
|
|
|
$this->messageFilter = $this->instantiateComponent( |
150
|
|
|
self::FILTER_CONFIG_KEY, |
151
|
|
|
self::FILTER_INTERFACE |
152
|
|
|
); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Initializes message printer. |
157
|
|
|
*/ |
158
|
|
|
private function initializeMessagePrinter() |
159
|
|
|
{ |
160
|
|
|
$this->messagePrinter = $this->instantiateComponent( |
|
|
|
|
161
|
|
|
self::PRINTER_CONFIG_KEY, |
162
|
|
|
self::PRINTER_INTERFACE |
163
|
|
|
); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Instantiates and returns a module component. |
168
|
|
|
* |
169
|
|
|
* @param string $componentName Component name. |
170
|
|
|
* @param string $interface An interface component must implement. |
171
|
|
|
* @param array $arguments Component's constructor arguments. |
172
|
|
|
* |
173
|
|
|
* @throws Exception When component does not implement expected interface. |
174
|
|
|
* |
175
|
|
|
* @return object Instance of a module component. |
176
|
|
|
*/ |
177
|
|
|
private function instantiateComponent($componentName, $interface, array $arguments = array()) |
178
|
|
|
{ |
179
|
|
|
$componentClass = $this->getComponentClass($componentName); |
180
|
|
|
$componentReflectionClass = new ReflectionClass($componentClass); |
181
|
|
|
if ($componentReflectionClass->implementsInterface($interface) === false) { |
182
|
|
|
$errorMessageTemplate = 'Invalid class «%s» provided for component «%s». It must implement «%s».'; |
183
|
|
|
$errorMessage = sprintf($errorMessageTemplate, $componentClass, $componentName, $interface); |
184
|
|
|
throw new Exception($errorMessage); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/* @var $component ComponentInterface */ |
188
|
|
|
$component = $componentReflectionClass->newInstanceArgs($arguments); |
189
|
|
|
$componentConfiguration = $this->getComponentConfiguration($componentName); |
190
|
|
|
$component->setConfiguration($componentConfiguration); |
191
|
|
|
|
192
|
|
|
return $component; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Returns component class name. |
197
|
|
|
* |
198
|
|
|
* @param string $componentName Component name. |
199
|
|
|
* |
200
|
|
|
* @return string Component class name. |
201
|
|
|
*/ |
202
|
|
View Code Duplication |
private function getComponentClass($componentName) |
|
|
|
|
203
|
|
|
{ |
204
|
|
|
$componentClassKey = self::COMPONENT_CLASS_CONFIG_KEY; |
205
|
|
|
if (isset($this->config[$componentName][$componentClassKey]) === false || |
206
|
|
|
is_string($this->config[$componentName][$componentClassKey]) === false |
207
|
|
|
) { |
208
|
|
|
$errorMessage = sprintf('Invalid class configuration of component «%s».', $componentName); |
209
|
|
|
throw new Exception($errorMessage); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
$componentClass = $this->config[$componentName][$componentClassKey]; |
213
|
|
|
|
214
|
|
|
return $componentClass; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Returns component configuration parameters. |
219
|
|
|
* |
220
|
|
|
* @param string $componentName Component name. |
221
|
|
|
* |
222
|
|
|
* @return array Component configuration parameters. |
223
|
|
|
*/ |
224
|
|
View Code Duplication |
private function getComponentConfiguration($componentName) |
|
|
|
|
225
|
|
|
{ |
226
|
|
|
$componentConfig = array(); |
227
|
|
|
|
228
|
|
|
$componentConfigKey = self::COMPONENT_CONFIG_CONFIG_KEY; |
229
|
|
|
if (isset($this->config[$componentName][$componentConfigKey]) === true) { |
230
|
|
|
if (is_array($this->config[$componentName][$componentConfigKey]) === true) { |
231
|
|
|
$componentConfig = $this->config[$componentName][$componentConfigKey]; |
232
|
|
|
} else { |
233
|
|
|
$errorMessage = sprintf('Invalid configuration of component «%s».', $componentName); |
234
|
|
|
throw new Exception($errorMessage); |
235
|
|
|
} |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
return $componentConfig; |
239
|
|
|
} |
240
|
|
|
} |
241
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..