Passed
Push — master ( 0d5fd3...343c6b )
by Caen
03:59 queued 15s
created

TestView   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Importance

Changes 6
Bugs 0 Features 0
Metric Value
eloc 21
c 6
b 0
f 0
dl 0
loc 92
rs 10
wmc 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A assertDoesNotHaveAttribute() 0 5 1
A dd() 0 9 2
A assertSeeHtmlIgnoringFormatting() 0 5 1
A assertTextIs() 0 5 1
A assertHasAttribute() 0 5 1
A assertSeeHtml() 0 7 2
A trimNewlinesAndIndentation() 0 3 1
A assertAttributeIs() 0 7 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Testing\Support;
6
7
use Hyde\Hyde;
8
use Illuminate\Support\Str;
9
use JetBrains\PhpStorm\NoReturn;
10
use Illuminate\Testing\Assert as PHPUnit;
11
12
class TestView extends \Illuminate\Testing\TestView
13
{
14
    /**
15
     * Assert that the given HTML is contained within the view.
16
     *
17
     * @return $this
18
     */
19
    public function assertSeeHtml(string $value, bool $ignoreFormatting = false): static
20
    {
21
        if ($ignoreFormatting) {
22
            return $this->assertSeeHtmlIgnoringFormatting($value);
23
        }
24
25
        return $this->assertSee($value, false);
26
    }
27
28
    /**
29
     * Assert that the given HTML is contained within the view text, ignoring whitespace and newlines.
30
     *
31
     * @return $this
32
     */
33
    public function assertSeeHtmlIgnoringFormatting(string $value): static
34
    {
35
        PHPUnit::assertStringContainsString($this->trimNewlinesAndIndentation($value), $this->trimNewlinesAndIndentation($this->rendered));
36
37
        return $this;
38
    }
39
40
    /**
41
     * Assert that the HTML attribute value is contained within the view.
42
     *
43
     * @return $this
44
     */
45
    public function assertAttributeIs(string $attributeName, string $expectedValue): static
46
    {
47
        static::assertHasAttribute($attributeName);
0 ignored issues
show
Bug Best Practice introduced by
The method Hyde\Testing\Support\Tes...w::assertHasAttribute() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

47
        static::/** @scrutinizer ignore-call */ 
48
                assertHasAttribute($attributeName);
Loading history...
48
49
        PHPUnit::assertStringContainsString($attributeName.'="'.$expectedValue.'"', $this->rendered, "The attribute '$attributeName' with value '$expectedValue' was not found.");
50
51
        return $this;
52
    }
53
54
    /**
55
     * Assert that the HTML attribute is present within the view.
56
     *
57
     * @return $this
58
     */
59
    public function assertHasAttribute(string $attributeName): static
60
    {
61
        PHPUnit::assertStringContainsString($attributeName.'="', $this->rendered, "The attribute '$attributeName' was not found.");
62
63
        return $this;
64
    }
65
66
    /**
67
     * Assert that the HTML attribute is not present within the view.
68
     *
69
     * @return $this
70
     */
71
    public function assertDoesNotHaveAttribute(string $attributeName): static
72
    {
73
        PHPUnit::assertStringNotContainsString($attributeName.'="', $this->rendered, "The attribute '$attributeName' was found.");
74
75
        return $this;
76
    }
77
78
    /**
79
     * Assert that the given text is equals the view's text content.
80
     *
81
     * @return $this
82
     */
83
    public function assertTextIs(string $value): static
84
    {
85
        PHPUnit::assertSame($value, strip_tags($this->rendered));
86
87
        return $this;
88
    }
89
90
    #[NoReturn]
91
    public function dd(bool $writeHtml = true): void
92
    {
93
        if ($writeHtml) {
94
            $viewName = Str::after(Str::after(basename(class_basename($this->view->getName())), '.'), '.');
95
            file_put_contents(Hyde::path(Str::kebab($viewName.'.html')), $this->rendered);
96
        }
97
98
        exit(trim($this->rendered)."\n\n");
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
99
    }
100
101
    protected function trimNewlinesAndIndentation(string $value): string
102
    {
103
        return str_replace(['    ', "\t", "\n", "\r"], '', $value);
104
    }
105
}
106