Skip to content

Commit d5370d7

Browse files
committed
Refactor module
1 parent 237ed8f commit d5370d7

File tree

2 files changed

+122
-58
lines changed

2 files changed

+122
-58
lines changed

sources/Module/MarkupValidator.php

Lines changed: 72 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -89,59 +89,101 @@ public function validateMarkup()
8989
*/
9090
private function initializeProvider()
9191
{
92-
$name = 'provider';
93-
$interface = 'Kolyunya\Codeception\Lib\MarkupValidator\MarkupProviderInterface';
94-
$componentClass = $this->config[$name]['class'];
95-
$componentConfig = $this->config[$name]['config'];
96-
$component = new $componentClass($this->moduleContainer, $componentConfig);
97-
if (($component instanceof $interface) === false) {
98-
$errorMessage = sprintf('Invalid class «%s» provided for component «%s».', $componentClass, $name);
99-
throw new Exception($errorMessage);
100-
}
101-
102-
$this->provider = $component;
92+
$providerName = 'provider';
93+
$providerClass = $this->getComponentClass($providerName);
94+
$providerConfig = $this->getComponentConfig($providerName);
95+
$this->provider = new $providerClass($this->moduleContainer, $providerConfig);
96+
$providerInterface = 'Kolyunya\Codeception\Lib\MarkupValidator\MarkupProviderInterface';
97+
$this->validateComponentInstance($this->provider, $providerInterface, $providerName);
10398
}
10499

105100
/**
106101
* Initializes markup validator.
107102
*/
108103
private function initializeValidator()
109104
{
110-
$this->validator = $this->makeComponent(
111-
'validator',
112-
'Kolyunya\Codeception\Lib\MarkupValidator\MarkupValidatorInterface'
113-
);
105+
$validatorName = 'validator';
106+
$validatorClass = $this->getComponentClass($validatorName);
107+
$validatorConfig = $this->getComponentConfig($validatorName);
108+
$this->validator = new $validatorClass($validatorConfig);
109+
$validatorInterface = 'Kolyunya\Codeception\Lib\MarkupValidator\MarkupValidatorInterface';
110+
$this->validateComponentInstance($this->validator, $validatorInterface, $validatorName);
114111
}
115112

116113
/**
117114
* Initializes markup validator.
118115
*/
119116
private function initializeReporter()
120117
{
121-
$this->reporter = $this->makeComponent(
122-
'reporter',
123-
'Kolyunya\Codeception\Lib\MarkupValidator\MarkupReporterInterface'
124-
);
118+
$reporterName = 'reporter';
119+
$reporterClass = $this->getComponentClass($reporterName);
120+
$reporterConfig = $this->getComponentConfig($reporterName);
121+
$this->reporter = new $reporterClass($reporterConfig);
122+
$reporterInterface = 'Kolyunya\Codeception\Lib\MarkupValidator\MarkupReporterInterface';
123+
$this->validateComponentInstance($this->reporter, $reporterInterface, $reporterName);
125124
}
126125

127126
/**
128-
* Constructs a validator component.
127+
* Returns component class name.
129128
*
130-
* @param string $name Component name.
131-
* @param string $interface Component interface.
129+
* @param string $componentName Component name.
132130
*
133-
* @return object Component instance.
131+
* @return string Component class name.
134132
*/
135-
private function makeComponent($name, $interface)
133+
private function getComponentClass($componentName)
136134
{
137-
$componentClass = $this->config[$name]['class'];
138-
$componentConfig = $this->config[$name]['config'];
139-
$component = new $componentClass($componentConfig);
140-
if (($component instanceof $interface) === false) {
141-
$errorMessage = sprintf('Invalid class «%s» provided for component «%s».', $componentClass, $name);
135+
$componentClassKey = 'class';
136+
if (isset($this->config[$componentName][$componentClassKey]) === false ||
137+
is_string($this->config[$componentName][$componentClassKey]) === false
138+
) {
139+
$errorMessage = sprintf('Invalid class configuration of component «%s».', $componentName);
142140
throw new Exception($errorMessage);
143141
}
144142

145-
return $component;
143+
$componentClass = $this->config[$componentName][$componentClassKey];
144+
145+
return $componentClass;
146+
}
147+
148+
/**
149+
* Returns component configuration parameters.
150+
*
151+
* @param string $componentName Component name.
152+
*
153+
* @return string Component configuration parameters.
154+
*/
155+
private function getComponentConfig($componentName)
156+
{
157+
$componentConfig = array();
158+
159+
$componentConfigKey = 'config';
160+
if (isset($this->config[$componentName][$componentConfigKey]) === true &&
161+
is_array($this->config[$componentName][$componentConfigKey]) === true
162+
) {
163+
$componentConfig = $this->config[$componentName][$componentConfigKey];
164+
}
165+
166+
return $componentConfig;
167+
}
168+
169+
/**
170+
* Ensures that a component is an instance of a specifi interface.
171+
*
172+
* @param object $component Component instance to validate.
173+
* @param string $interface Interface to validate component instance against.
174+
* @param string $componentName Component name. User for error logging.
175+
*
176+
* @throws Exception When `component` is not an instance of the `interface`.
177+
*/
178+
private function validateComponentInstance($component, $interface, $componentName)
179+
{
180+
if (($component instanceof $interface) === false) {
181+
$componentClass = get_class($component);
182+
$errorMessage = vsprintf('Invalid class «%s» provided for component «%s».', array(
183+
$componentClass,
184+
$componentName,
185+
));
186+
throw new Exception($errorMessage);
187+
}
146188
}
147189
}

tests/Module/MarkupValidatorTest.php

Lines changed: 50 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ public function testInvalidProvider()
5959
$this->module = new MarkupValidator($this->moduleContainer, array(
6060
'provider' => array(
6161
'class' => 'stdClass',
62-
'config' => array(),
6362
),
6463
));
6564
}
@@ -71,7 +70,6 @@ public function testInvalidValidator()
7170
$this->module = new MarkupValidator($this->moduleContainer, array(
7271
'validator' => array(
7372
'class' => 'stdClass',
74-
'config' => array(),
7573
),
7674
));
7775
}
@@ -83,7 +81,6 @@ public function testInvalidReporter()
8381
$this->module = new MarkupValidator($this->moduleContainer, array(
8482
'reporter' => array(
8583
'class' => 'stdClass',
86-
'config' => array(),
8784
),
8885
));
8986
}
@@ -93,31 +90,7 @@ public function testInvalidReporter()
9390
*/
9491
public function testValidateMarkup($markup, $valid)
9592
{
96-
$phpBrowser = $this
97-
->getMockBuilder('Codeception\Module\PhpBrowser')
98-
->disableOriginalConstructor()
99-
->setMethods(array(
100-
'_getResponseContent',
101-
))
102-
->getMock()
103-
;
104-
$phpBrowser
105-
->method('_getResponseContent')
106-
->will($this->returnValue($markup))
107-
;
108-
109-
$this->moduleContainer
110-
->method('hasModule')
111-
->will($this->returnValueMap(array(
112-
array('PhpBrowser', true),
113-
)))
114-
;
115-
$this->moduleContainer
116-
->method('getModule')
117-
->will($this->returnValueMap(array(
118-
array('PhpBrowser', $phpBrowser),
119-
)))
120-
;
93+
$this->mockMarkup($markup);
12194

12295
if ($valid === true) {
12396
$this->module->validateMarkup($markup);
@@ -176,10 +149,59 @@ public function testValidateMarkupDataProvider()
176149
</form>
177150
</body>
178151
</html>
152+
HTML
153+
,
154+
false,
155+
),
156+
array(
157+
<<<HTML
158+
<!DOCTYPE HTML>
159+
<html>
160+
<head>
161+
<title>
162+
A page with a warning.
163+
</title>
164+
</head>
165+
<body>
166+
<form>
167+
<button role="button">
168+
</button>
169+
</form>
170+
</body>
171+
</html>
179172
HTML
180173
,
181174
false,
182175
),
183176
);
184177
}
178+
179+
private function mockMarkup($markup)
180+
{
181+
$phpBrowser = $this
182+
->getMockBuilder('Codeception\Module\PhpBrowser')
183+
->disableOriginalConstructor()
184+
->setMethods(array(
185+
'_getResponseContent',
186+
))
187+
->getMock()
188+
;
189+
$phpBrowser
190+
->method('_getResponseContent')
191+
->will($this->returnValue($markup))
192+
;
193+
194+
$this->moduleContainer
195+
->method('hasModule')
196+
->will($this->returnValueMap(array(
197+
array('PhpBrowser', true),
198+
)))
199+
;
200+
$this->moduleContainer
201+
->method('getModule')
202+
->will($this->returnValueMap(array(
203+
array('PhpBrowser', $phpBrowser),
204+
)))
205+
;
206+
}
185207
}

0 commit comments

Comments
 (0)