From e861b46e25e833183b8e46e423659ad0359ca67a Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Thu, 14 Mar 2024 21:22:07 +0100 Subject: [PATCH 1/6] Share a new route instance when mocking current page Should improve it's accuracy and scope --- packages/testing/src/InteractsWithPages.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/testing/src/InteractsWithPages.php b/packages/testing/src/InteractsWithPages.php index ed4162bdc6d..a5f0e9cf9d1 100644 --- a/packages/testing/src/InteractsWithPages.php +++ b/packages/testing/src/InteractsWithPages.php @@ -4,6 +4,7 @@ namespace Hyde\Testing; +use Hyde\Pages\InMemoryPage; use Hyde\Pages\Concerns\HydePage; use Hyde\Pages\MarkdownPage; use Hyde\Support\Facades\Render; @@ -25,5 +26,6 @@ protected function mockPage(?HydePage $page = null, ?string $currentPage = null) protected function mockCurrentPage(string $currentPage): void { Render::share('routeKey', $currentPage); + Render::share('route', new Route(new InMemoryPage($currentPage))); } } From 7160e705010f025d068d3a7740fd8fda7a68d8c5 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Thu, 14 Mar 2024 21:25:04 +0100 Subject: [PATCH 2/6] Use in memory pages for mocking helpers Makes sense to use the most generic page here --- packages/testing/src/InteractsWithPages.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/testing/src/InteractsWithPages.php b/packages/testing/src/InteractsWithPages.php index a5f0e9cf9d1..6a37b763d8c 100644 --- a/packages/testing/src/InteractsWithPages.php +++ b/packages/testing/src/InteractsWithPages.php @@ -6,7 +6,6 @@ use Hyde\Pages\InMemoryPage; use Hyde\Pages\Concerns\HydePage; -use Hyde\Pages\MarkdownPage; use Hyde\Support\Facades\Render; use Hyde\Support\Models\Route; @@ -14,12 +13,12 @@ trait InteractsWithPages { protected function mockRoute(?Route $route = null): void { - Render::share('route', $route ?? (new Route(new MarkdownPage()))); + Render::share('route', $route ?? (new Route(new InMemoryPage()))); } protected function mockPage(?HydePage $page = null, ?string $currentPage = null): void { - Render::share('page', $page ?? new MarkdownPage()); + Render::share('page', $page ?? new InMemoryPage()); Render::share('routeKey', $currentPage ?? 'PHPUnit'); } From 8430003b53943eb9ac98fd3a2b76256e24de8724 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Thu, 14 Mar 2024 21:26:21 +0100 Subject: [PATCH 3/6] Change default testing helper data value --- packages/testing/src/InteractsWithPages.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/testing/src/InteractsWithPages.php b/packages/testing/src/InteractsWithPages.php index 6a37b763d8c..b50dddcdff2 100644 --- a/packages/testing/src/InteractsWithPages.php +++ b/packages/testing/src/InteractsWithPages.php @@ -19,7 +19,7 @@ protected function mockRoute(?Route $route = null): void protected function mockPage(?HydePage $page = null, ?string $currentPage = null): void { Render::share('page', $page ?? new InMemoryPage()); - Render::share('routeKey', $currentPage ?? 'PHPUnit'); + Render::share('routeKey', $currentPage ?? 'foo'); } protected function mockCurrentPage(string $currentPage): void From 49dd6ab31f43ed9bcc7ad76b776b5db2ee49f84a Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Thu, 14 Mar 2024 11:39:54 +0100 Subject: [PATCH 4/6] Add improved assertion output --- packages/testing/src/Support/TestView.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/testing/src/Support/TestView.php b/packages/testing/src/Support/TestView.php index 4e04a247a7d..d75cea1a980 100644 --- a/packages/testing/src/Support/TestView.php +++ b/packages/testing/src/Support/TestView.php @@ -41,7 +41,7 @@ public function assertSeeHtmlIgnoringFormatting(string $value): static */ public function assertAttributeIs(string $attributeName, string $expectedValue): static { - PHPUnit::assertStringContainsString($attributeName.'="'.$expectedValue.'"', $this->rendered); + PHPUnit::assertStringContainsString($attributeName.'="'.$expectedValue.'"', $this->rendered, "The attribute '$attributeName' with value '$expectedValue' was not found."); return $this; } From eb6582dc04a1ccf360277ef5d42229754dde4ef3 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Thu, 14 Mar 2024 21:06:27 +0100 Subject: [PATCH 5/6] Add more attribute assertion helpers --- packages/testing/src/Support/TestView.php | 26 +++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/packages/testing/src/Support/TestView.php b/packages/testing/src/Support/TestView.php index d75cea1a980..c0ac4979328 100644 --- a/packages/testing/src/Support/TestView.php +++ b/packages/testing/src/Support/TestView.php @@ -41,11 +41,37 @@ public function assertSeeHtmlIgnoringFormatting(string $value): static */ public function assertAttributeIs(string $attributeName, string $expectedValue): static { + static::assertHasAttribute($attributeName); + PHPUnit::assertStringContainsString($attributeName.'="'.$expectedValue.'"', $this->rendered, "The attribute '$attributeName' with value '$expectedValue' was not found."); return $this; } + /** + * Assert that the HTML attribute is present within the view. + * + * @return $this + */ + public function assertHasAttribute(string $attributeName): static + { + PHPUnit::assertStringContainsString($attributeName.'="', $this->rendered, "The attribute '$attributeName' was not found."); + + return $this; + } + + /** + * Assert that the HTML attribute is not present within the view. + * + * @return $this + */ + public function assertDoesNotHaveAttribute(string $attributeName): static + { + PHPUnit::assertStringNotContainsString($attributeName.'="', $this->rendered, "The attribute '$attributeName' was found."); + + return $this; + } + /** * Assert that the given text is equals the view's text content. * From 91bae19597d4ac2fd605eef218ecf8d86a51d4fc Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Thu, 14 Mar 2024 21:15:04 +0100 Subject: [PATCH 6/6] Add testing helper to dump and die with HTML state --- packages/testing/src/Support/TestView.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/packages/testing/src/Support/TestView.php b/packages/testing/src/Support/TestView.php index c0ac4979328..428f85cc0b5 100644 --- a/packages/testing/src/Support/TestView.php +++ b/packages/testing/src/Support/TestView.php @@ -4,6 +4,9 @@ namespace Hyde\Testing\Support; +use Hyde\Hyde; +use Illuminate\Support\Str; +use JetBrains\PhpStorm\NoReturn; use Illuminate\Testing\Assert as PHPUnit; class TestView extends \Illuminate\Testing\TestView @@ -84,6 +87,17 @@ public function assertTextIs(string $value): static return $this; } + #[NoReturn] + public function dd(bool $writeHtml = true): void + { + if ($writeHtml) { + $viewName = Str::after(Str::after(basename(class_basename($this->view->getName())), '.'), '.'); + file_put_contents(Hyde::path(Str::kebab($viewName.'.html')), $this->rendered); + } + + exit(trim($this->rendered)."\n\n"); + } + protected function trimNewlinesAndIndentation(string $value): string { return str_replace([' ', "\t", "\n", "\r"], '', $value);