|
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_CONFIG_KEY = 'provider'; |
|
26
|
|
|
|
|
27
|
|
|
const VALIDATOR_CONFIG_KEY = 'validator'; |
|
28
|
|
|
|
|
29
|
|
|
const FILTER_CONFIG_KEY = 'filter'; |
|
30
|
|
|
|
|
31
|
|
|
const PRINTER_CONFIG_KEY = 'printer'; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* {@inheritDoc} |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $config = array( |
|
37
|
|
|
self::PROVIDER_CONFIG_KEY => array( |
|
38
|
|
|
self::COMPONENT_CLASS_CONFIG_KEY => 'Kolyunya\Codeception\Lib\MarkupValidator\DefaultMarkupProvider', |
|
39
|
|
|
self::COMPONENT_CONFIG_CONFIG_KEY => array(), |
|
40
|
|
|
), |
|
41
|
|
|
self::VALIDATOR_CONFIG_KEY => array( |
|
42
|
|
|
self::COMPONENT_CLASS_CONFIG_KEY => 'Kolyunya\Codeception\Lib\MarkupValidator\W3CMarkupValidator', |
|
43
|
|
|
self::COMPONENT_CONFIG_CONFIG_KEY => array(), |
|
44
|
|
|
), |
|
45
|
|
|
self::FILTER_CONFIG_KEY => array( |
|
46
|
|
|
self::COMPONENT_CLASS_CONFIG_KEY => 'Kolyunya\Codeception\Lib\MarkupValidator\DefaultMessageFilter', |
|
47
|
|
|
self::COMPONENT_CONFIG_CONFIG_KEY => array(), |
|
48
|
|
|
), |
|
49
|
|
|
self::PRINTER_CONFIG_KEY => array( |
|
50
|
|
|
self::COMPONENT_CLASS_CONFIG_KEY => 'Kolyunya\Codeception\Lib\MarkupValidator\DefaultMessagePrinter', |
|
51
|
|
|
self::COMPONENT_CONFIG_CONFIG_KEY => array(), |
|
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
|
|
|
$interface = 'Kolyunya\Codeception\Lib\MarkupValidator\MarkupProviderInterface'; |
|
125
|
|
|
$this->markupProvider = $this->instantiateComponent(self::PROVIDER_CONFIG_KEY, $interface, array( |
|
126
|
|
|
$this->moduleContainer, |
|
127
|
|
|
)); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Initializes markup validator. |
|
132
|
|
|
*/ |
|
133
|
|
|
private function initializeMarkupValidator() |
|
134
|
|
|
{ |
|
135
|
|
|
$interface = 'Kolyunya\Codeception\Lib\MarkupValidator\MarkupValidatorInterface'; |
|
136
|
|
|
$this->markupValidator = $this->instantiateComponent(self::VALIDATOR_CONFIG_KEY, $interface); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Initializes message filter. |
|
141
|
|
|
*/ |
|
142
|
|
|
private function initializeMessageFilter() |
|
143
|
|
|
{ |
|
144
|
|
|
$interface = 'Kolyunya\Codeception\Lib\MarkupValidator\MessageFilterInterface'; |
|
145
|
|
|
$this->messageFilter = $this->instantiateComponent(self::FILTER_CONFIG_KEY, $interface); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Initializes message printer. |
|
150
|
|
|
*/ |
|
151
|
|
|
private function initializeMessagePrinter() |
|
152
|
|
|
{ |
|
153
|
|
|
$interface = 'Kolyunya\Codeception\Lib\MarkupValidator\MessagePrinterInterface'; |
|
154
|
|
|
$this->messagePrinter = $this->instantiateComponent(self::PRINTER_CONFIG_KEY, $interface); |
|
|
|
|
|
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Instantiates and returns a module component. |
|
159
|
|
|
* |
|
160
|
|
|
* @param string $componentName Component name. |
|
161
|
|
|
* @param string $interface An interface component must implement. |
|
162
|
|
|
* @param array $arguments Component's constructor arguments. |
|
163
|
|
|
* |
|
164
|
|
|
* @throws Exception When component does not implement expected interface. |
|
165
|
|
|
* |
|
166
|
|
|
* @return object Instance of a module component. |
|
167
|
|
|
*/ |
|
168
|
|
|
private function instantiateComponent($componentName, $interface, array $arguments = array()) |
|
169
|
|
|
{ |
|
170
|
|
|
$componentClass = $this->getComponentClass($componentName); |
|
171
|
|
|
$componentReflectionClass = new ReflectionClass($componentClass); |
|
172
|
|
|
if ($componentReflectionClass->implementsInterface($interface) === false) { |
|
173
|
|
|
$errorMessageTemplate = 'Invalid class «%s» provided for component «%s». It must implement «%s».'; |
|
174
|
|
|
$errorMessage = sprintf($errorMessageTemplate, $componentClass, $componentName, $interface); |
|
175
|
|
|
throw new Exception($errorMessage); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/* @var $component ComponentInterface */ |
|
179
|
|
|
$component = $componentReflectionClass->newInstanceArgs($arguments); |
|
180
|
|
|
$componentConfiguration = $this->getComponentConfiguration($componentName); |
|
181
|
|
|
$component->setConfiguration($componentConfiguration); |
|
182
|
|
|
|
|
183
|
|
|
return $component; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* Returns component class name. |
|
188
|
|
|
* |
|
189
|
|
|
* @param string $componentName Component name. |
|
190
|
|
|
* |
|
191
|
|
|
* @return string Component class name. |
|
192
|
|
|
*/ |
|
193
|
|
View Code Duplication |
private function getComponentClass($componentName) |
|
|
|
|
|
|
194
|
|
|
{ |
|
195
|
|
|
$componentClassKey = self::COMPONENT_CLASS_CONFIG_KEY; |
|
196
|
|
|
if (isset($this->config[$componentName][$componentClassKey]) === false || |
|
197
|
|
|
is_string($this->config[$componentName][$componentClassKey]) === false |
|
198
|
|
|
) { |
|
199
|
|
|
$errorMessage = sprintf('Invalid class configuration of component «%s».', $componentName); |
|
200
|
|
|
throw new Exception($errorMessage); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
$componentClass = $this->config[$componentName][$componentClassKey]; |
|
204
|
|
|
|
|
205
|
|
|
return $componentClass; |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* Returns component configuration parameters. |
|
210
|
|
|
* |
|
211
|
|
|
* @param string $componentName Component name. |
|
212
|
|
|
* |
|
213
|
|
|
* @return array Component configuration parameters. |
|
214
|
|
|
*/ |
|
215
|
|
View Code Duplication |
private function getComponentConfiguration($componentName) |
|
|
|
|
|
|
216
|
|
|
{ |
|
217
|
|
|
$componentConfigKey = self::COMPONENT_CONFIG_CONFIG_KEY; |
|
218
|
|
|
if (isset($this->config[$componentName][$componentConfigKey]) === false || |
|
219
|
|
|
is_array($this->config[$componentName][$componentConfigKey]) === false |
|
220
|
|
|
) { |
|
221
|
|
|
$errorMessage = sprintf('Invalid configuration of component «%s».', $componentName); |
|
222
|
|
|
throw new Exception($errorMessage); |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
$componentConfig = $this->config[$componentName][$componentConfigKey]; |
|
226
|
|
|
|
|
227
|
|
|
return $componentConfig; |
|
228
|
|
|
} |
|
229
|
|
|
} |
|
230
|
|
|
|
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..