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); |
|
|
|
|
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"); |
|
|
|
|
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
protected function trimNewlinesAndIndentation(string $value): string |
102
|
|
|
{ |
103
|
|
|
return str_replace([' ', "\t", "\n", "\r"], '', $value); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|