Completed
Push — master ( 3c1969...5b5792 )
by Schlaefer
03:16 queued 10s
created

AssertTrait::assertXPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 3
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types = 1);
4
5
/**
6
 * Saito - The Threaded Web Forum
7
 *
8
 * @copyright Copyright (c) the Saito Project Developers
9
 * @link https://github.com/Schlaefer/Saito
10
 * @license http://opensource.org/licenses/MIT
11
 */
12
13
namespace Saito\Test;
14
15
use Symfony\Component\DomCrawler\Crawler;
16
17
/**
18
 * Non-depended assumptions
19
 */
20
trait AssertTrait
21
{
22
    /**
23
     * assert contains tag
24
     *
25
     * @param array $expected expected
26
     * @param string $result HTML
27
     * @return void
28
     */
29
    public function assertContainsTag($expected, $result)
30
    {
31
        do {
32
            $crawler = new Crawler;
33
            $crawler->addHtmlContent($result);
34
            $selector = key($expected);
35
            $node = $crawler->filter($selector);
36
            $this->assertEquals(
0 ignored issues
show
Bug introduced by
It seems like assertEquals() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
37
                1,
38
                $node->count(),
39
                "Selector '$selector' not found."
40
            );
41
42
            if (isset($expected[$selector]['attributes'])) {
43
                foreach ($expected[$selector]['attributes'] as $attribute => $value) {
44
                    $this->assertEquals($value, $node->attr($attribute));
0 ignored issues
show
Bug introduced by
It seems like assertEquals() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
45
                }
46
            }
47
        } while (next($expected));
48
    }
49
50
    /**
51
     * tests if XPath exists in HTML Source
52
     *
53
     * @param string $html HTML
54
     * @param string $path XPath
55
     * @param int $count how many times should XPath exist in HTML
56
     * @return mixed
57
     */
58
    public function assertXPath($html, $path, $count = 1)
59
    {
60
        $xpath = $this->_getDOMXPath($html);
61
        $length = $xpath->query($path)->length;
62
63
        return $this->assertEquals(
0 ignored issues
show
Bug introduced by
It seems like assertEquals() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
64
            $count,
65
            $length,
66
            "Failed XPath. Expected '$path' to be found $count times instead of $length."
67
        );
68
    }
69
70
    /**
71
     * assert not xpath
72
     *
73
     * @param string $html path
74
     * @param string $path path
75
     * @return bool
76
     */
77
    public function assertNotXPath($html, $path)
78
    {
79
        return !$this->assertXPath($html, $path, 0);
80
    }
81
82
    /**
83
     * get dom xpath
84
     *
85
     * @param string $html HTML
86
     * @return \DOMXPath
87
     */
88
    protected function _getDOMXPath($html)
89
    {
90
        $document = new \DOMDocument;
91
        libxml_use_internal_errors(true);
92
        $document->loadHTML('<!DOCTYPE html>' . $html);
93
        $xpath = new \DOMXPath($document);
94
        libxml_clear_errors();
95
96
        return $xpath;
97
    }
98
}
99