1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace whm\Smoke\Rules\Xml; |
4
|
|
|
|
5
|
|
|
use phm\HttpWebdriverClient\Http\Request\TimeoutAwareRequest; |
6
|
|
|
use phm\HttpWebdriverClient\Http\Response\DomAwareResponse; |
7
|
|
|
use phm\HttpWebdriverClient\Http\Response\TimeoutAwareResponse; |
8
|
|
|
use Psr\Http\Message\ResponseInterface; |
9
|
|
|
use whm\Smoke\Rules\CheckResult; |
10
|
|
|
use whm\Smoke\Rules\StandardRule; |
11
|
|
|
use whm\Smoke\Rules\ValidationFailedException; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* This rule checks if the found XML is well-formed. |
15
|
|
|
*/ |
16
|
|
|
class XmlCheckRule extends StandardRule |
17
|
|
|
{ |
18
|
|
|
protected $contentTypes = array('text/xml', 'application/xml'); |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param ResponseInterface $response |
22
|
|
|
* @throws ValidationFailedException |
23
|
|
|
*/ |
24
|
|
|
public function doValidation(ResponseInterface $response) |
25
|
|
|
{ |
26
|
|
|
if ($response instanceof DomAwareResponse) { |
27
|
|
|
$body = (string)$response->getHtmlBody(); |
28
|
|
|
} else { |
29
|
|
|
$body = (string)$response->getBody(); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
if ($body == "") { |
33
|
|
|
if ($response instanceof TimeoutAwareResponse) { |
34
|
|
|
if ($response->isTimeout()) { |
35
|
|
|
return new CheckResult(CheckResult::STATUS_FAILURE, 'The request timed out and produced an empty XML document.'); |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
return new CheckResult(CheckResult::STATUS_FAILURE, 'The given XML document was empty.'); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
$domDocument = new \DOMDocument(); |
42
|
|
|
$success = @$domDocument->loadXML($body); |
43
|
|
|
|
44
|
|
|
if (!$success) { |
45
|
|
|
$lastError = libxml_get_last_error(); |
46
|
|
|
|
47
|
|
|
if ($lastError) { |
48
|
|
|
throw new ValidationFailedException('The xml file ' . $response->getUri() . ' is not well formed (last error: ' . |
|
|
|
|
49
|
|
|
str_replace("\n", '', $lastError->message) . ').'); |
50
|
|
|
} else { |
51
|
|
|
return new CheckResult(CheckResult::STATUS_FAILURE, 'Unknown error occured.'); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
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: