Skip to content

Commit 343c6b7

Browse files
authored
Merge pull request #1609 from hydephp/refactor-and-improve-testing-internals
Internal: Improve and refactor testing helpers
2 parents 0d5fd3b + 91bae19 commit 343c6b7

File tree

2 files changed

+46
-5
lines changed

2 files changed

+46
-5
lines changed

packages/testing/src/InteractsWithPages.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,27 @@
44

55
namespace Hyde\Testing;
66

7+
use Hyde\Pages\InMemoryPage;
78
use Hyde\Pages\Concerns\HydePage;
8-
use Hyde\Pages\MarkdownPage;
99
use Hyde\Support\Facades\Render;
1010
use Hyde\Support\Models\Route;
1111

1212
trait InteractsWithPages
1313
{
1414
protected function mockRoute(?Route $route = null): void
1515
{
16-
Render::share('route', $route ?? (new Route(new MarkdownPage())));
16+
Render::share('route', $route ?? (new Route(new InMemoryPage())));
1717
}
1818

1919
protected function mockPage(?HydePage $page = null, ?string $currentPage = null): void
2020
{
21-
Render::share('page', $page ?? new MarkdownPage());
22-
Render::share('routeKey', $currentPage ?? 'PHPUnit');
21+
Render::share('page', $page ?? new InMemoryPage());
22+
Render::share('routeKey', $currentPage ?? 'foo');
2323
}
2424

2525
protected function mockCurrentPage(string $currentPage): void
2626
{
2727
Render::share('routeKey', $currentPage);
28+
Render::share('route', new Route(new InMemoryPage($currentPage)));
2829
}
2930
}

packages/testing/src/Support/TestView.php

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
namespace Hyde\Testing\Support;
66

7+
use Hyde\Hyde;
8+
use Illuminate\Support\Str;
9+
use JetBrains\PhpStorm\NoReturn;
710
use Illuminate\Testing\Assert as PHPUnit;
811

912
class TestView extends \Illuminate\Testing\TestView
@@ -41,7 +44,33 @@ public function assertSeeHtmlIgnoringFormatting(string $value): static
4144
*/
4245
public function assertAttributeIs(string $attributeName, string $expectedValue): static
4346
{
44-
PHPUnit::assertStringContainsString($attributeName.'="'.$expectedValue.'"', $this->rendered);
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.");
4574

4675
return $this;
4776
}
@@ -58,6 +87,17 @@ public function assertTextIs(string $value): static
5887
return $this;
5988
}
6089

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+
61101
protected function trimNewlinesAndIndentation(string $value): string
62102
{
63103
return str_replace([' ', "\t", "\n", "\r"], '', $value);

0 commit comments

Comments
 (0)