Passed
Push — master ( 9a2ed4...0d5fd3 )
by Caen
03:42 queued 18s
created

TestView::assertAttributeIs()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 5
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Testing\Support;
6
7
use Illuminate\Testing\Assert as PHPUnit;
8
9
class TestView extends \Illuminate\Testing\TestView
10
{
11
    /**
12
     * Assert that the given HTML is contained within the view.
13
     *
14
     * @return $this
15
     */
16
    public function assertSeeHtml(string $value, bool $ignoreFormatting = false): static
17
    {
18
        if ($ignoreFormatting) {
19
            return $this->assertSeeHtmlIgnoringFormatting($value);
20
        }
21
22
        return $this->assertSee($value, false);
23
    }
24
25
    /**
26
     * Assert that the given HTML is contained within the view text, ignoring whitespace and newlines.
27
     *
28
     * @return $this
29
     */
30
    public function assertSeeHtmlIgnoringFormatting(string $value): static
31
    {
32
        PHPUnit::assertStringContainsString($this->trimNewlinesAndIndentation($value), $this->trimNewlinesAndIndentation($this->rendered));
33
34
        return $this;
35
    }
36
37
    /**
38
     * Assert that the HTML attribute value is contained within the view.
39
     *
40
     * @return $this
41
     */
42
    public function assertAttributeIs(string $attributeName, string $expectedValue): static
43
    {
44
        PHPUnit::assertStringContainsString($attributeName.'="'.$expectedValue.'"', $this->rendered);
45
46
        return $this;
47
    }
48
49
    /**
50
     * Assert that the given text is equals the view's text content.
51
     *
52
     * @return $this
53
     */
54
    public function assertTextIs(string $value): static
55
    {
56
        PHPUnit::assertSame($value, strip_tags($this->rendered));
57
58
        return $this;
59
    }
60
61
    protected function trimNewlinesAndIndentation(string $value): string
62
    {
63
        return str_replace(['    ', "\t", "\n", "\r"], '', $value);
64
    }
65
}
66