1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace whm\Smoke\Rules\Html; |
4
|
|
|
|
5
|
|
|
use Psr\Http\Message\ResponseInterface; |
6
|
|
|
use whm\Smoke\Rules\StandardRule; |
7
|
|
|
use whm\Smoke\Rules\ValidationFailedException; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* This rule will analyze any html document and checks if a given string is contained. |
11
|
|
|
*/ |
12
|
|
|
class RegExExistsRule extends StandardRule |
13
|
|
|
{ |
14
|
|
|
private $regExs; |
15
|
|
|
|
16
|
|
|
protected $contentTypes = array('text/html', 'application/json', 'application/xml'); |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @param int $string The string that the document must contain |
|
|
|
|
20
|
|
|
*/ |
21
|
|
|
public function init(array $regExs) |
22
|
|
|
{ |
23
|
|
|
$regExArray = array(); |
24
|
|
|
|
25
|
|
|
foreach ($regExs as $regEx) { |
26
|
|
|
if (array_key_exists('regex', $regEx)) { |
27
|
|
|
$isRegex = $regEx['isRegex'] == 'on'; |
28
|
|
|
$regExArray[] = ['pattern' => $regEx['regex'], 'isRegEx' => $isRegex]; |
29
|
|
|
} else { |
30
|
|
|
$regExArray[] = $regEx; |
31
|
|
|
} |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
$this->regExs = $regExArray; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param ResponseInterface $response |
39
|
|
|
* @throws ValidationFailedException |
40
|
|
|
*/ |
41
|
|
|
protected function doValidation(ResponseInterface $response) |
42
|
|
|
{ |
43
|
|
|
$errors = []; |
44
|
|
|
|
45
|
|
|
foreach ($this->regExs as $regEx) { |
46
|
|
|
if ($regEx['isRegEx']) { |
47
|
|
|
$pattern = str_replace('', '\\~', $regEx['pattern']); |
48
|
|
|
|
49
|
|
View Code Duplication |
if (preg_match('~' . $pattern . '~', (string)$response->getBody()) === 0) { |
|
|
|
|
50
|
|
|
$errors[] = 'Regular expression: ' . $regEx['pattern']; |
51
|
|
|
} |
52
|
|
View Code Duplication |
} else { |
|
|
|
|
53
|
|
|
if (preg_match('^' . preg_quote($regEx['pattern']) . '^', (string)$response->getBody()) === 0) { |
54
|
|
|
$errors[] = 'Text: ' . $regEx['pattern']; |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
if (count($errors) > 0) { |
60
|
|
|
$errorString = 'The following text elements were not found: <ul>'; |
61
|
|
|
|
62
|
|
|
foreach ($errors as $error) { |
63
|
|
|
$errorString .= '<li>' . $error . '</li>'; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$errorString .= '</ul>'; |
67
|
|
|
|
68
|
|
|
throw new ValidationFailedException($errorString); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.