Completed
Pull Request — master (#85)
by
unknown
02:50
created

XpathExistsRule::doValidation()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 11
rs 9.4285
cc 2
eloc 6
nc 2
nop 1
1
<?php
2
3
namespace whm\Smoke\Rules\Html;
4
5
use whm\Smoke\Http\Response;
6
use whm\Smoke\Rules\StandardRule;
7
8
/**
9
 * This rule checks if xpath is found in a html document.
10
 */
11
class XpathExistsRule extends StandardRule
12
{
13
    protected $contentTypes = ['text/html'];
14
15
    private $xPaths;
16
17
    public function init(array $xPaths)
18
    {
19
        $this->xPaths = $xPaths;
20
    }
21
22
    public function doValidation(Response $response)
23
    {
24
        $domDocument = new \DOMDocument();
25
        @$domDocument->loadHTML((string) $response->getBody());
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
26
27
        $domXPath = new \DOMXPath($domDocument);
28
29
        foreach ($this->xPaths as $xpath) {
30
            $this->assert($domXPath->query($xpath)->length > 0, 'The xpath ' . $xpath . ' was not found.');
31
        }
32
    }
33
}
34