1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace whm\Smoke\Rules\Xml; |
4
|
|
|
|
5
|
|
|
use phm\HttpWebdriverClient\Http\Response\DomAwareResponse; |
6
|
|
|
use phm\HttpWebdriverClient\Http\Response\TimeoutAwareResponse; |
7
|
|
|
use Psr\Http\Message\ResponseInterface; |
8
|
|
|
use whm\Smoke\Rules\CheckResult; |
9
|
|
|
use whm\Smoke\Rules\StandardRule; |
10
|
|
|
use whm\Smoke\Rules\ValidationFailedException; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* This rule checks if the found XML is well-formed. |
14
|
|
|
*/ |
15
|
|
|
class XmlCheckRule extends StandardRule |
16
|
|
|
{ |
17
|
|
|
protected $contentTypes = array('text/xml', 'application/xml'); |
18
|
|
|
|
19
|
|
|
public function init() |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param ResponseInterface $response |
26
|
|
|
* @throws ValidationFailedException |
27
|
|
|
*/ |
28
|
|
|
public function doValidation(ResponseInterface $response) |
29
|
|
|
{ |
30
|
|
|
if ($response instanceof DomAwareResponse) { |
31
|
|
|
$body = (string)$response->getHtmlBody(); |
32
|
|
|
} else { |
33
|
|
|
$body = (string)$response->getBody(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
if ($body == "") { |
37
|
|
|
if ($response instanceof TimeoutAwareResponse) { |
38
|
|
|
if ($response->isTimeout()) { |
39
|
|
|
return new CheckResult(CheckResult::STATUS_FAILURE, 'The request timed out and produced an empty XML document.'); |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
return new CheckResult(CheckResult::STATUS_FAILURE, 'The given XML document was empty.'); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$domDocument = new \DOMDocument(); |
46
|
|
|
$success = @$domDocument->loadXML($body); |
47
|
|
|
|
48
|
|
|
if (!$success) { |
49
|
|
|
$lastError = libxml_get_last_error(); |
50
|
|
|
|
51
|
|
|
if ($lastError) { |
52
|
|
|
throw new ValidationFailedException('The xml file ' . $response->getUri() . ' is not well formed (last error: ' . |
|
|
|
|
53
|
|
|
str_replace("\n", '', $lastError->message) . ').'); |
54
|
|
|
} else { |
55
|
|
|
return new CheckResult(CheckResult::STATUS_FAILURE, 'Unknown error occured.'); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: