Passed
Push — master ( 1302b5...85aea0 )
by Caen
03:56
created

TestView::trimNewlinesAndIndentation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
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 given HTML element is contained within the view.
42
     *
43
     * @return $this
44
     */
45
    public function assertHasElement(string $element): static
46
    {
47
        $element = trim($element, '</>');
48
49
        PHPUnit::assertStringContainsString("<$element", $this->rendered, "The element '$element' was not found.");
50
51
        return $this;
52
    }
53
54
    /**
55
     * Assert that the HTML attribute value is contained within the view.
56
     *
57
     * @return $this
58
     */
59
    public function assertAttributeIs(string $attribute, ?string $expectedValue = null): static
60
    {
61
        if ($expectedValue === null) {
62
            [$attributeName, $expectedValue] = explode('=', $attribute);
63
            $expectedValue = trim($expectedValue, '"');
64
        } else {
65
            $attributeName = $attribute;
66
        }
67
68
        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

68
        static::/** @scrutinizer ignore-call */ 
69
                assertHasAttribute($attributeName);
Loading history...
69
70
        PHPUnit::assertStringContainsString($attributeName.'="'.$expectedValue.'"', $this->rendered, "The attribute '$attributeName' with value '$expectedValue' was not found.");
71
72
        return $this;
73
    }
74
75
    /**
76
     * Assert that the HTML attribute is present within the view.
77
     *
78
     * @return $this
79
     */
80
    public function assertHasAttribute(string $attributeName): static
81
    {
82
        PHPUnit::assertStringContainsString($attributeName.'="', $this->rendered, "The attribute '$attributeName' was not found.");
83
84
        return $this;
85
    }
86
87
    /**
88
     * Assert that the HTML attribute is not present within the view.
89
     *
90
     * @return $this
91
     */
92
    public function assertDoesNotHaveAttribute(string $attributeName): static
93
    {
94
        PHPUnit::assertStringNotContainsString($attributeName.'="', $this->rendered, "The attribute '$attributeName' was found.");
95
96
        return $this;
97
    }
98
99
    /**
100
     * Assert that the given CSS class is contained within the view.
101
     *
102
     * @return $this
103
     */
104
    public function assertHasClass(string $class): static
105
    {
106
        PHPUnit::assertContains($class, $this->findClasses(), "The class '$class' was not found.");
107
108
        return $this;
109
    }
110
111
    /**
112
     * Assert that the given CSS class is not contained within the view.
113
     *
114
     * @return $this
115
     */
116
    public function assertDoesNotHaveClass(string $class): static
117
    {
118
        PHPUnit::assertNotContains($class, $this->findClasses(), "The class '$class' was found.");
119
120
        return $this;
121
    }
122
123
    /**
124
     * Assert that the given text is equals the view's text content.
125
     *
126
     * @return $this
127
     */
128
    public function assertTextIs(string $value): static
129
    {
130
        PHPUnit::assertSame($value, strip_tags($this->rendered));
131
132
        return $this;
133
    }
134
135
    #[NoReturn]
136
    public function dd(bool $writeHtml = true): void
137
    {
138
        if ($writeHtml) {
139
            $viewName = Str::after(Str::after(basename(class_basename($this->view->getName())), '.'), '.');
140
            file_put_contents(Hyde::path(Str::kebab($viewName.'.html')), $this->rendered);
141
        }
142
143
        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...
144
    }
145
146
    protected function trimNewlinesAndIndentation(string $value): string
147
    {
148
        return str_replace(['    ', "\t", "\n", "\r"], '', $value);
149
    }
150
151
    /** @return array<string> */
152
    protected function findClasses(): array
153
    {
154
        preg_match_all('/class="([^"]+)"/', $this->rendered, $matches);
155
156
        return explode(' ', $matches[1][0]);
157
    }
158
}
159