Html::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 4
nc 1
nop 2
1
<?php
2
3
namespace Psr7Unitesting;
4
5
use Psr\Http\Message\StreamInterface;
6
use Symfony\Component\DomCrawler\Crawler;
7
use Closure;
8
9
/**
10
 * Class to execute html related assertions in a StreamInterface instance.
11
 */
12
class Html extends Stream
13
{
14
    /**
15
     * @var Crawler
16
     */
17
    protected $html;
18
19
    /**
20
     * Constructor.
21
     *
22
     * @param StreamInterface     $stream
23
     * @param AbstractAssert|null $previous
24
     */
25
    public function __construct(StreamInterface $stream, Utils\AbstractAssert $previous = null)
26
    {
27
        parent::__construct($stream, $previous);
28
29
        $this->html = new Crawler();
30
        $this->html->addContent($this->string);
31
    }
32
33
    /**
34
     * Asserts the number of elements matching with a selector.
35
     *
36
     * @param string $selector
37
     * @param int    $count
38
     * @param string $message
39
     *
40
     * @return self
41
     */
42
    public function count($selector, $count, $message = '')
43
    {
44
        return $this->assert($this->html, new Html\Count($selector, $count), $message);
45
    }
46
47
    /**
48
     * Asserts that there is (at least) one element matching with a selector.
49
     *
50
     * @param string $selector
51
     * @param string $message
52
     *
53
     * @return self
54
     */
55
    public function has($selector, $message = '')
56
    {
57
        return $this->assert($this->html, new Html\Has($selector), $message);
58
    }
59
60
    /**
61
     * Asserts that there is not elements matching with a selector.
62
     *
63
     * @param string $selector
64
     * @param string $message
65
     *
66
     * @return self
67
     */
68
    public function hasNot($selector, $message = '')
69
    {
70
        return $this->assert($this->html, new Html\HasNot($selector), $message);
71
    }
72
73
    /**
74
     * Asserts that there is (at least) one element matching with a selector and content.
75
     *
76
     * @param string $selector
77
     * @param string $text
78
     * @param string $message
79
     *
80
     * @return self
81
     */
82
    public function contains($selector, $text, $message = '')
83
    {
84
        return $this->assert($this->html, new Html\Contains($selector, $text), $message);
85
    }
86
87
    /**
88
     * Asserts that there is no elements matching with a selector and content.
89
     *
90
     * @param string $selector
91
     * @param string $text
92
     * @param string $message
93
     *
94
     * @return self
95
     */
96
    public function notContains($selector, $text, $message = '')
97
    {
98
        return $this->assert($this->html, new Html\NotContains($selector, $text), $message);
99
    }
100
101
    /**
102
     * Asserts that the html is valid.
103
     *
104
     * @param string $message
105
     *
106
     * @return self
107
     */
108
    public function isValid($message = '')
109
    {
110
        return $this->assert($this->string, new Html\IsValid(), $message);
111
    }
112
113
    /**
114
     * Executes the callback for each element found.
115
     *
116
     * @param string  $selector
117
     * @param Closure $callback
118
     *
119
     * @return self
120
     */
121
    public function map($selector, Closure $callback)
122
    {
123
        $this->html->filter($selector)->each($callback);
124
125
        return $this;
126
    }
127
}
128