From 894a865fa461fcc71ee2ead257ee710cff2e9bea Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 13 Mar 2024 17:25:10 +0100 Subject: [PATCH 01/46] Include navigation link as Blade component instead of include Looks more consistent with the dropdown include just above, and we can use attributes --- packages/framework/resources/views/layouts/navigation.blade.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/framework/resources/views/layouts/navigation.blade.php b/packages/framework/resources/views/layouts/navigation.blade.php index b04ccc9df87..663bb8a4567 100644 --- a/packages/framework/resources/views/layouts/navigation.blade.php +++ b/packages/framework/resources/views/layouts/navigation.blade.php @@ -38,7 +38,7 @@ @if($item instanceof \Hyde\Framework\Features\Navigation\NavGroupItem) @else - @include('hyde::components.navigation.navigation-link') + @endif @endforeach From 52f2d0c39b717205207b37a89ac5b381dbf2ca25 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 13 Mar 2024 17:27:35 +0100 Subject: [PATCH 02/46] Fix component name --- packages/framework/resources/views/layouts/navigation.blade.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/framework/resources/views/layouts/navigation.blade.php b/packages/framework/resources/views/layouts/navigation.blade.php index 663bb8a4567..a55e64b5ea1 100644 --- a/packages/framework/resources/views/layouts/navigation.blade.php +++ b/packages/framework/resources/views/layouts/navigation.blade.php @@ -38,7 +38,7 @@ @if($item instanceof \Hyde\Framework\Features\Navigation\NavGroupItem) @else - + @endif @endforeach From 6ba9eaf7006362ddb7b2e3566479725329a212f6 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 13 Mar 2024 17:43:16 +0100 Subject: [PATCH 03/46] Add CSS class hooks for the navigation link states Adds a styling hook, and makes it more readable and testable --- .../navigation/navigation-link.blade.php | 4 ++-- .../tests/Unit/Views/NavigationLinkViewTest.php | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/packages/framework/resources/views/components/navigation/navigation-link.blade.php b/packages/framework/resources/views/components/navigation/navigation-link.blade.php index 048faaa6cba..207172ff4ea 100644 --- a/packages/framework/resources/views/components/navigation/navigation-link.blade.php +++ b/packages/framework/resources/views/components/navigation/navigation-link.blade.php @@ -1,4 +1,4 @@ isActive() ? 'aria-current="page"' : '' !!} @class([ - 'block my-2 md:my-0 md:inline-block py-1 text-gray-700 hover:text-gray-900 dark:text-gray-100', - 'border-l-4 border-indigo-500 md:border-none font-medium -ml-6 pl-5 md:ml-0 md:pl-0 bg-gray-100 dark:bg-gray-800 md:bg-transparent dark:md:bg-transparent' => $item->isActive() + 'navigation-link block my-2 md:my-0 md:inline-block py-1 text-gray-700 hover:text-gray-900 dark:text-gray-100', + 'navigation-link-active border-l-4 border-indigo-500 md:border-none font-medium -ml-6 pl-5 md:ml-0 md:pl-0 bg-gray-100 dark:bg-gray-800 md:bg-transparent dark:md:bg-transparent' => $item->isActive() ])>{{ $item->getLabel() }} \ No newline at end of file diff --git a/packages/framework/tests/Unit/Views/NavigationLinkViewTest.php b/packages/framework/tests/Unit/Views/NavigationLinkViewTest.php index 1018d2ae3c0..53b3a14b553 100644 --- a/packages/framework/tests/Unit/Views/NavigationLinkViewTest.php +++ b/packages/framework/tests/Unit/Views/NavigationLinkViewTest.php @@ -48,4 +48,19 @@ public function testComponentHasAriaCurrentWhenCurrentRouteMatches() $this->mockRoute(Routes::get('index')); $this->assertStringContainsString('aria-current="page"', $this->render(NavItem::forRoute(Routes::get('index'), 'Home'))); } + + public function testComponentDoesNotHaveActiveClassWhenNotActive() + { + $render = $this->render(NavItem::forRoute(Routes::get('index'), 'Home')); + $this->assertStringContainsString('navigation-link ', $render); + $this->assertStringNotContainsString('navigation-link-active', $render); + } + + public function testComponentHasActiveClassWhenActive() + { + $this->mockRoute(Routes::get('index')); + $render = $this->render(NavItem::forRoute(Routes::get('index'), 'Home')); + $this->assertStringContainsString('navigation-link ', $render); + $this->assertStringContainsString('navigation-link-active', $render); + } } From c61eef919422b886f75b78aef654da42da7c397f Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 13 Mar 2024 21:20:35 +0100 Subject: [PATCH 04/46] Include dropdown navigation item as Blade component Normalizes with https://github.com/hydephp/develop/commit/894a865fa461fcc71ee2ead257ee710cff2e9bea --- .../resources/views/components/navigation/dropdown.blade.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/framework/resources/views/components/navigation/dropdown.blade.php b/packages/framework/resources/views/components/navigation/dropdown.blade.php index 52d03fcc9c0..b0c8de89a0d 100644 --- a/packages/framework/resources/views/components/navigation/dropdown.blade.php +++ b/packages/framework/resources/views/components/navigation/dropdown.blade.php @@ -9,7 +9,7 @@ @isset($items) @foreach ($items as $item)
  • - @include('hyde::components.navigation.navigation-link') +
  • @endforeach @else From 759c63a9354b6ff3955fb73fea0ad9e700441286 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 13 Mar 2024 21:23:44 +0100 Subject: [PATCH 05/46] Refactor component to use attribute merging Create attribute bag in test setup Update test to expect cleaner HTML output --- .../views/components/navigation/navigation-link.blade.php | 6 ++++-- .../framework/tests/Unit/Views/NavigationLinkViewTest.php | 2 ++ .../framework/tests/Unit/Views/NavigationMenuViewTest.php | 2 +- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/packages/framework/resources/views/components/navigation/navigation-link.blade.php b/packages/framework/resources/views/components/navigation/navigation-link.blade.php index 207172ff4ea..0460e6fb38f 100644 --- a/packages/framework/resources/views/components/navigation/navigation-link.blade.php +++ b/packages/framework/resources/views/components/navigation/navigation-link.blade.php @@ -1,4 +1,6 @@ -isActive() ? 'aria-current="page"' : '' !!} @class([ +except('item')->class([ 'navigation-link block my-2 md:my-0 md:inline-block py-1 text-gray-700 hover:text-gray-900 dark:text-gray-100', 'navigation-link-active border-l-4 border-indigo-500 md:border-none font-medium -ml-6 pl-5 md:ml-0 md:pl-0 bg-gray-100 dark:bg-gray-800 md:bg-transparent dark:md:bg-transparent' => $item->isActive() -])>{{ $item->getLabel() }} \ No newline at end of file +])->merge([ + 'aria-current' => ($item->isActive() ? 'page' : false), +])) }}>{{ $item->getLabel() }} \ No newline at end of file diff --git a/packages/framework/tests/Unit/Views/NavigationLinkViewTest.php b/packages/framework/tests/Unit/Views/NavigationLinkViewTest.php index 53b3a14b553..14aa79c074a 100644 --- a/packages/framework/tests/Unit/Views/NavigationLinkViewTest.php +++ b/packages/framework/tests/Unit/Views/NavigationLinkViewTest.php @@ -5,6 +5,7 @@ namespace Hyde\Framework\Testing\Unit\Views; use Hyde\Foundation\Facades\Routes; +use Illuminate\View\ComponentAttributeBag; use Hyde\Framework\Features\Navigation\NavItem; use Hyde\Testing\TestCase; @@ -24,6 +25,7 @@ protected function render(?NavItem $item = null): string { return view('hyde::components.navigation.navigation-link', [ 'item' => $item ?? NavItem::forLink('foo.html', 'Foo'), + 'attributes' => new ComponentAttributeBag(), ])->render(); } diff --git a/packages/framework/tests/Unit/Views/NavigationMenuViewTest.php b/packages/framework/tests/Unit/Views/NavigationMenuViewTest.php index a40d6782b33..18d9bfd7cbf 100644 --- a/packages/framework/tests/Unit/Views/NavigationMenuViewTest.php +++ b/packages/framework/tests/Unit/Views/NavigationMenuViewTest.php @@ -73,7 +73,7 @@ public function testNavigationMenuWithRootPages() $contents = $foo->compile(); $this->assertStringContainsString('mockRoute(Routes::get('index')); From 5ffeac4a96f31528c2b4b82a471367eb23a42fd3 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 13 Mar 2024 21:31:45 +0100 Subject: [PATCH 07/46] Remove unnecessary parentheses --- .../views/components/navigation/navigation-link.blade.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/framework/resources/views/components/navigation/navigation-link.blade.php b/packages/framework/resources/views/components/navigation/navigation-link.blade.php index 0460e6fb38f..60ce617081d 100644 --- a/packages/framework/resources/views/components/navigation/navigation-link.blade.php +++ b/packages/framework/resources/views/components/navigation/navigation-link.blade.php @@ -1,6 +1,6 @@ -except('item')->class([ +except('item')->class([ 'navigation-link block my-2 md:my-0 md:inline-block py-1 text-gray-700 hover:text-gray-900 dark:text-gray-100', 'navigation-link-active border-l-4 border-indigo-500 md:border-none font-medium -ml-6 pl-5 md:ml-0 md:pl-0 bg-gray-100 dark:bg-gray-800 md:bg-transparent dark:md:bg-transparent' => $item->isActive() ])->merge([ - 'aria-current' => ($item->isActive() ? 'page' : false), -])) }}>{{ $item->getLabel() }} \ No newline at end of file + 'aria-current' => $item->isActive() ? 'page' : false, +]) }}>{{ $item->getLabel() }} \ No newline at end of file From 6a474091b1afd4211cddf97b15138df1e9749a00 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Thu, 14 Mar 2024 11:39:39 +0100 Subject: [PATCH 08/46] Add testing helper to make NavItem for page --- .../framework/tests/Unit/Views/NavigationLinkViewTest.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/packages/framework/tests/Unit/Views/NavigationLinkViewTest.php b/packages/framework/tests/Unit/Views/NavigationLinkViewTest.php index 93146650502..3d050d2e75a 100644 --- a/packages/framework/tests/Unit/Views/NavigationLinkViewTest.php +++ b/packages/framework/tests/Unit/Views/NavigationLinkViewTest.php @@ -4,6 +4,8 @@ namespace Hyde\Framework\Testing\Unit\Views; +use Hyde\Pages\InMemoryPage; +use Hyde\Support\Models\Route; use Hyde\Testing\TestsBladeViews; use Hyde\Testing\Support\TestView; use Hyde\Foundation\Facades\Routes; @@ -41,6 +43,11 @@ protected function testView(?NavItem $item = null): TestView ])); } + protected function makeNavItemForPage(): NavItem + { + return NavItem::forRoute(new Route(new InMemoryPage('foo')), 'Foo'); + } + public function testComponentLinksToRouteDestination() { $this->testView()->assertAttributeIs('href', 'foo.html'); From 2cd3da2085c5e34cfb64c176457ddbf833629f5c Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Thu, 14 Mar 2024 11:39:54 +0100 Subject: [PATCH 09/46] 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 d5f33f643e321277d0e883998d2602948f96fc8c Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Thu, 14 Mar 2024 11:40:03 +0100 Subject: [PATCH 10/46] Test component resolves relative links for routes --- .../framework/tests/Unit/Views/NavigationLinkViewTest.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/packages/framework/tests/Unit/Views/NavigationLinkViewTest.php b/packages/framework/tests/Unit/Views/NavigationLinkViewTest.php index 3d050d2e75a..6a1bbb0e490 100644 --- a/packages/framework/tests/Unit/Views/NavigationLinkViewTest.php +++ b/packages/framework/tests/Unit/Views/NavigationLinkViewTest.php @@ -53,6 +53,12 @@ public function testComponentLinksToRouteDestination() $this->testView()->assertAttributeIs('href', 'foo.html'); } + public function testComponentResolvesRelativeLinksForRoutes() + { + $this->mockCurrentPage('foo/bar'); + $this->testView($this->makeNavItemForPage())->assertAttributeIs('href', '../foo.html'); + } + public function testComponentUsesTitle() { $this->testView()->assertTextIs('Foo'); From a4c0f9ec0e86134259c2869bf926e09b069167f8 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Thu, 14 Mar 2024 17:19:44 +0100 Subject: [PATCH 11/46] Simplify test data handling --- .../tests/Unit/Views/NavigationLinkViewTest.php | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/packages/framework/tests/Unit/Views/NavigationLinkViewTest.php b/packages/framework/tests/Unit/Views/NavigationLinkViewTest.php index 6a1bbb0e490..0117567081a 100644 --- a/packages/framework/tests/Unit/Views/NavigationLinkViewTest.php +++ b/packages/framework/tests/Unit/Views/NavigationLinkViewTest.php @@ -38,16 +38,11 @@ protected function render(?NavItem $item = null): string protected function testView(?NavItem $item = null): TestView { return $this->test(view('hyde::components.navigation.navigation-link', [ - 'item' => $item ?? NavItem::forLink('foo.html', 'Foo'), + 'item' => $item ?? NavItem::forRoute(new Route(new InMemoryPage('foo')), 'Foo'), 'attributes' => new ComponentAttributeBag(), ])); } - protected function makeNavItemForPage(): NavItem - { - return NavItem::forRoute(new Route(new InMemoryPage('foo')), 'Foo'); - } - public function testComponentLinksToRouteDestination() { $this->testView()->assertAttributeIs('href', 'foo.html'); @@ -56,7 +51,7 @@ public function testComponentLinksToRouteDestination() public function testComponentResolvesRelativeLinksForRoutes() { $this->mockCurrentPage('foo/bar'); - $this->testView($this->makeNavItemForPage())->assertAttributeIs('href', '../foo.html'); + $this->testView()->assertAttributeIs('href', '../foo.html'); } public function testComponentUsesTitle() From c042cf7edcf395930182ba9b85db436f576df2ab Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Thu, 14 Mar 2024 21:06:27 +0100 Subject: [PATCH 12/46] 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 b7fb433c58b0c86ddda81d1613dc6437fa6daeef Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Thu, 14 Mar 2024 21:08:38 +0100 Subject: [PATCH 13/46] Refactor test to use fluent assertions --- .../framework/tests/Unit/Views/NavigationLinkViewTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/framework/tests/Unit/Views/NavigationLinkViewTest.php b/packages/framework/tests/Unit/Views/NavigationLinkViewTest.php index 0117567081a..64914a8934a 100644 --- a/packages/framework/tests/Unit/Views/NavigationLinkViewTest.php +++ b/packages/framework/tests/Unit/Views/NavigationLinkViewTest.php @@ -61,9 +61,9 @@ public function testComponentUsesTitle() public function testComponentDoesNotHaveCurrentAttributesWhenCurrentRouteDoesNotMatch() { - $render = $this->render(NavItem::forRoute(Routes::get('index'), 'Home')); - $this->assertStringNotContainsString('current', $render); - $this->assertStringNotContainsString('aria-current="page"', $render); + $this->testView() + ->assertDontSee('current') + ->assertDoesNotHaveAttribute('aria-current'); } public function testComponentIsCurrentWhenCurrentRouteMatches() From 7b1868e30c7b2f44f40f00eb0421228061c5c799 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Thu, 14 Mar 2024 21:15:04 +0100 Subject: [PATCH 14/46] 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); From 282d1feb4aa648ff3ca58fc5929ca76fdf653268 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Thu, 14 Mar 2024 21:37:32 +0100 Subject: [PATCH 15/46] Refactor test to use fluent assertions --- .../framework/tests/Unit/Views/NavigationLinkViewTest.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/framework/tests/Unit/Views/NavigationLinkViewTest.php b/packages/framework/tests/Unit/Views/NavigationLinkViewTest.php index 64914a8934a..378075c49e7 100644 --- a/packages/framework/tests/Unit/Views/NavigationLinkViewTest.php +++ b/packages/framework/tests/Unit/Views/NavigationLinkViewTest.php @@ -68,8 +68,10 @@ public function testComponentDoesNotHaveCurrentAttributesWhenCurrentRouteDoesNot public function testComponentIsCurrentWhenCurrentRouteMatches() { - $this->mockRoute(Routes::get('index')); - $this->assertStringContainsString('current', $this->render(NavItem::forRoute(Routes::get('index'), 'Home'))); + $this->mockCurrentPage('foo'); + $this->testView() + ->assertSee('current') + ->assertAttributeIs('aria-current', 'page'); } public function testComponentHasAriaCurrentWhenCurrentRouteMatches() From add2390407fbbd41e24f9f680c4b88873fc289d5 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Thu, 14 Mar 2024 21:40:24 +0100 Subject: [PATCH 16/46] Use helper method fluently --- .../framework/tests/Unit/Views/NavigationLinkViewTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/framework/tests/Unit/Views/NavigationLinkViewTest.php b/packages/framework/tests/Unit/Views/NavigationLinkViewTest.php index 378075c49e7..d70e5f78ebd 100644 --- a/packages/framework/tests/Unit/Views/NavigationLinkViewTest.php +++ b/packages/framework/tests/Unit/Views/NavigationLinkViewTest.php @@ -68,8 +68,8 @@ public function testComponentDoesNotHaveCurrentAttributesWhenCurrentRouteDoesNot public function testComponentIsCurrentWhenCurrentRouteMatches() { - $this->mockCurrentPage('foo'); - $this->testView() + $this->mockCurrentPage('foo') + ->testView() ->assertSee('current') ->assertAttributeIs('aria-current', 'page'); } From 68d46fffab7738e034605773e5ec72ec477523de Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 15 Mar 2024 13:38:06 +0100 Subject: [PATCH 17/46] Update testing helper to automatically parse attributes Adds an option to use this with just the full attribute string --- packages/testing/src/Support/TestView.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/packages/testing/src/Support/TestView.php b/packages/testing/src/Support/TestView.php index 428f85cc0b5..924db972f3c 100644 --- a/packages/testing/src/Support/TestView.php +++ b/packages/testing/src/Support/TestView.php @@ -42,8 +42,15 @@ public function assertSeeHtmlIgnoringFormatting(string $value): static * * @return $this */ - public function assertAttributeIs(string $attributeName, string $expectedValue): static + public function assertAttributeIs(string $attribute, ?string $expectedValue = null): static { + if ($expectedValue === null) { + [$attributeName, $expectedValue] = explode('=', $attribute); + $expectedValue = trim($expectedValue, '"'); + } else { + $attributeName = $attribute; + } + static::assertHasAttribute($attributeName); PHPUnit::assertStringContainsString($attributeName.'="'.$expectedValue.'"', $this->rendered, "The attribute '$attributeName' with value '$expectedValue' was not found."); From cd1e567f9208592e21807fce5a3539b6504ec815 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 15 Mar 2024 13:38:19 +0100 Subject: [PATCH 18/46] Use shorthand attribute testing syntax --- packages/framework/tests/Unit/Views/NavigationLinkViewTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/framework/tests/Unit/Views/NavigationLinkViewTest.php b/packages/framework/tests/Unit/Views/NavigationLinkViewTest.php index d70e5f78ebd..b55552c25f3 100644 --- a/packages/framework/tests/Unit/Views/NavigationLinkViewTest.php +++ b/packages/framework/tests/Unit/Views/NavigationLinkViewTest.php @@ -71,7 +71,7 @@ public function testComponentIsCurrentWhenCurrentRouteMatches() $this->mockCurrentPage('foo') ->testView() ->assertSee('current') - ->assertAttributeIs('aria-current', 'page'); + ->assertAttributeIs('aria-current="page"'); } public function testComponentHasAriaCurrentWhenCurrentRouteMatches() From 2a1c9cc270123f4d605b787a801df2c69aa9bef5 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 15 Mar 2024 13:45:43 +0100 Subject: [PATCH 19/46] Add assertion --- packages/framework/tests/Unit/Views/NavigationLinkViewTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/framework/tests/Unit/Views/NavigationLinkViewTest.php b/packages/framework/tests/Unit/Views/NavigationLinkViewTest.php index b55552c25f3..e7c7a534fe8 100644 --- a/packages/framework/tests/Unit/Views/NavigationLinkViewTest.php +++ b/packages/framework/tests/Unit/Views/NavigationLinkViewTest.php @@ -71,6 +71,7 @@ public function testComponentIsCurrentWhenCurrentRouteMatches() $this->mockCurrentPage('foo') ->testView() ->assertSee('current') + ->assertHasAttribute('aria-current') ->assertAttributeIs('aria-current="page"'); } From 322862bb07d10ed18e48b67161204e2909c1bd7f Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 15 Mar 2024 13:45:55 +0100 Subject: [PATCH 20/46] Remove duplicated test case --- .../framework/tests/Unit/Views/NavigationLinkViewTest.php | 6 ------ 1 file changed, 6 deletions(-) diff --git a/packages/framework/tests/Unit/Views/NavigationLinkViewTest.php b/packages/framework/tests/Unit/Views/NavigationLinkViewTest.php index e7c7a534fe8..ebe46e90c88 100644 --- a/packages/framework/tests/Unit/Views/NavigationLinkViewTest.php +++ b/packages/framework/tests/Unit/Views/NavigationLinkViewTest.php @@ -75,12 +75,6 @@ public function testComponentIsCurrentWhenCurrentRouteMatches() ->assertAttributeIs('aria-current="page"'); } - public function testComponentHasAriaCurrentWhenCurrentRouteMatches() - { - $this->mockRoute(Routes::get('index')); - $this->assertStringContainsString('aria-current="page"', $this->render(NavItem::forRoute(Routes::get('index'), 'Home'))); - } - public function testComponentDoesNotHaveActiveClassWhenNotActive() { $render = $this->render(NavItem::forRoute(Routes::get('index'), 'Home')); From cc071eb781b698fde89769078353939a25a9ca97 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 15 Mar 2024 13:49:47 +0100 Subject: [PATCH 21/46] Refactor tests to use fluent assertions --- .../tests/Unit/Views/NavigationLinkViewTest.php | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/packages/framework/tests/Unit/Views/NavigationLinkViewTest.php b/packages/framework/tests/Unit/Views/NavigationLinkViewTest.php index ebe46e90c88..e0f7be81ba6 100644 --- a/packages/framework/tests/Unit/Views/NavigationLinkViewTest.php +++ b/packages/framework/tests/Unit/Views/NavigationLinkViewTest.php @@ -8,7 +8,6 @@ use Hyde\Support\Models\Route; use Hyde\Testing\TestsBladeViews; use Hyde\Testing\Support\TestView; -use Hyde\Foundation\Facades\Routes; use Illuminate\View\ComponentAttributeBag; use Hyde\Framework\Features\Navigation\NavItem; use Hyde\Testing\TestCase; @@ -77,16 +76,16 @@ public function testComponentIsCurrentWhenCurrentRouteMatches() public function testComponentDoesNotHaveActiveClassWhenNotActive() { - $render = $this->render(NavItem::forRoute(Routes::get('index'), 'Home')); - $this->assertStringContainsString('navigation-link ', $render); - $this->assertStringNotContainsString('navigation-link-active', $render); + $this->testView() + ->assertSee('navigation-link ') + ->assertDontSee('navigation-link-active'); } public function testComponentHasActiveClassWhenActive() { - $this->mockRoute(Routes::get('index')); - $render = $this->render(NavItem::forRoute(Routes::get('index'), 'Home')); - $this->assertStringContainsString('navigation-link ', $render); - $this->assertStringContainsString('navigation-link-active', $render); + $this->mockCurrentPage('foo') + ->testView() + ->assertSee('navigation-link ') + ->assertSee('navigation-link-active'); } } From c66c5c89695f6fa8f27d9be470b92dcc0437f39a Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 15 Mar 2024 13:51:11 +0100 Subject: [PATCH 22/46] Remove unused testing helper --- .../framework/tests/Unit/Views/NavigationLinkViewTest.php | 8 -------- 1 file changed, 8 deletions(-) diff --git a/packages/framework/tests/Unit/Views/NavigationLinkViewTest.php b/packages/framework/tests/Unit/Views/NavigationLinkViewTest.php index e0f7be81ba6..db942123aa7 100644 --- a/packages/framework/tests/Unit/Views/NavigationLinkViewTest.php +++ b/packages/framework/tests/Unit/Views/NavigationLinkViewTest.php @@ -26,14 +26,6 @@ protected function setUp(): void $this->mockPage(); } - protected function render(?NavItem $item = null): string - { - return view('hyde::components.navigation.navigation-link', [ - 'item' => $item ?? NavItem::forLink('foo.html', 'Foo'), - 'attributes' => new ComponentAttributeBag(), - ])->render(); - } - protected function testView(?NavItem $item = null): TestView { return $this->test(view('hyde::components.navigation.navigation-link', [ From b451eff6d5e20c188fea772241c1247574e6c4d0 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 15 Mar 2024 13:51:58 +0100 Subject: [PATCH 23/46] Remove unused parameter from testing helper method --- .../framework/tests/Unit/Views/NavigationLinkViewTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/framework/tests/Unit/Views/NavigationLinkViewTest.php b/packages/framework/tests/Unit/Views/NavigationLinkViewTest.php index db942123aa7..d17ec5bd926 100644 --- a/packages/framework/tests/Unit/Views/NavigationLinkViewTest.php +++ b/packages/framework/tests/Unit/Views/NavigationLinkViewTest.php @@ -26,10 +26,10 @@ protected function setUp(): void $this->mockPage(); } - protected function testView(?NavItem $item = null): TestView + protected function testView(): TestView { return $this->test(view('hyde::components.navigation.navigation-link', [ - 'item' => $item ?? NavItem::forRoute(new Route(new InMemoryPage('foo')), 'Foo'), + 'item' => NavItem::forRoute(new Route(new InMemoryPage('foo')), 'Foo'), 'attributes' => new ComponentAttributeBag(), ])); } From fbb5bfdbda3d202b734dd7af885553976a29ea5e Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 15 Mar 2024 13:57:13 +0100 Subject: [PATCH 24/46] Add testing helpers to assert CSS class states --- packages/testing/src/Support/TestView.php | 32 +++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/packages/testing/src/Support/TestView.php b/packages/testing/src/Support/TestView.php index 924db972f3c..92aa98c331c 100644 --- a/packages/testing/src/Support/TestView.php +++ b/packages/testing/src/Support/TestView.php @@ -82,6 +82,30 @@ public function assertDoesNotHaveAttribute(string $attributeName): static return $this; } + /** + * Assert that the given CSS class is contained within the view. + * + * @return $this + */ + public function assertHasClass(string $class): static + { + PHPUnit::assertContains($class, $this->findClasses(), "The class '$class' was not found."); + + return $this; + } + + /** + * Assert that the given CSS class is not contained within the view. + * + * @return $this + */ + public function assertDoesNotHaveClass(string $class): static + { + PHPUnit::assertNotContains($class, $this->findClasses(), "The class '$class' was found."); + + return $this; + } + /** * Assert that the given text is equals the view's text content. * @@ -109,4 +133,12 @@ protected function trimNewlinesAndIndentation(string $value): string { return str_replace([' ', "\t", "\n", "\r"], '', $value); } + + /** @return array */ + protected function findClasses(): array + { + preg_match_all('/class="([^"]+)"/', $this->rendered, $matches); + + return explode(' ', $matches[1][0]); + } } From d5411eb91af90a5bd05f678301e225db96abfe10 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 15 Mar 2024 14:00:46 +0100 Subject: [PATCH 25/46] Use new class state helpers --- .../framework/tests/Unit/Views/NavigationLinkViewTest.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/framework/tests/Unit/Views/NavigationLinkViewTest.php b/packages/framework/tests/Unit/Views/NavigationLinkViewTest.php index d17ec5bd926..b2a2b01c0e5 100644 --- a/packages/framework/tests/Unit/Views/NavigationLinkViewTest.php +++ b/packages/framework/tests/Unit/Views/NavigationLinkViewTest.php @@ -69,15 +69,15 @@ public function testComponentIsCurrentWhenCurrentRouteMatches() public function testComponentDoesNotHaveActiveClassWhenNotActive() { $this->testView() - ->assertSee('navigation-link ') - ->assertDontSee('navigation-link-active'); + ->assertHasClass('navigation-link') + ->assertDoesNotHaveClass('navigation-link-active'); } public function testComponentHasActiveClassWhenActive() { $this->mockCurrentPage('foo') ->testView() - ->assertSee('navigation-link ') - ->assertSee('navigation-link-active'); + ->assertHasClass('navigation-link') + ->assertHasClass('navigation-link-active'); } } From fd1c68a5eb5c5a57f829e637542c1aa82fb803a4 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 15 Mar 2024 14:03:05 +0100 Subject: [PATCH 26/46] Clean up and normalize test semantics --- .../tests/Unit/Views/NavigationLinkViewTest.php | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/packages/framework/tests/Unit/Views/NavigationLinkViewTest.php b/packages/framework/tests/Unit/Views/NavigationLinkViewTest.php index b2a2b01c0e5..10febbd13cf 100644 --- a/packages/framework/tests/Unit/Views/NavigationLinkViewTest.php +++ b/packages/framework/tests/Unit/Views/NavigationLinkViewTest.php @@ -36,13 +36,14 @@ protected function testView(): TestView public function testComponentLinksToRouteDestination() { - $this->testView()->assertAttributeIs('href', 'foo.html'); + $this->testView()->assertAttributeIs('href="foo.html"'); } public function testComponentResolvesRelativeLinksForRoutes() { $this->mockCurrentPage('foo/bar'); - $this->testView()->assertAttributeIs('href', '../foo.html'); + + $this->testView()->assertAttributeIs('href="../foo.html"'); } public function testComponentUsesTitle() @@ -59,8 +60,9 @@ public function testComponentDoesNotHaveCurrentAttributesWhenCurrentRouteDoesNot public function testComponentIsCurrentWhenCurrentRouteMatches() { - $this->mockCurrentPage('foo') - ->testView() + $this->mockCurrentPage('foo'); + + $this->testView() ->assertSee('current') ->assertHasAttribute('aria-current') ->assertAttributeIs('aria-current="page"'); @@ -75,8 +77,9 @@ public function testComponentDoesNotHaveActiveClassWhenNotActive() public function testComponentHasActiveClassWhenActive() { - $this->mockCurrentPage('foo') - ->testView() + $this->mockCurrentPage('foo'); + + $this->testView() ->assertHasClass('navigation-link') ->assertHasClass('navigation-link-active'); } From b0d4331f7698228d62a439571fcb5ebcf71402b7 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 15 Mar 2024 14:09:53 +0100 Subject: [PATCH 27/46] Add testing helper to assert element 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 92aa98c331c..1867c2f0b64 100644 --- a/packages/testing/src/Support/TestView.php +++ b/packages/testing/src/Support/TestView.php @@ -37,6 +37,20 @@ public function assertSeeHtmlIgnoringFormatting(string $value): static return $this; } + /** + * Assert that the given HTML element is contained within the view. + * + * @return $this + */ + public function assertHasElement(string $element): static + { + $element = trim($element, ''); + + PHPUnit::assertStringContainsString("<$element", $this->rendered, "The element '$element' was not found."); + + return $this; + } + /** * Assert that the HTML attribute value is contained within the view. * From 8449237f8ef57bf279e6ff517b5cb34fe9d2e59f Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 15 Mar 2024 14:10:01 +0100 Subject: [PATCH 28/46] Test component renders --- .../framework/tests/Unit/Views/NavigationLinkViewTest.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/framework/tests/Unit/Views/NavigationLinkViewTest.php b/packages/framework/tests/Unit/Views/NavigationLinkViewTest.php index 10febbd13cf..8609cdb82c7 100644 --- a/packages/framework/tests/Unit/Views/NavigationLinkViewTest.php +++ b/packages/framework/tests/Unit/Views/NavigationLinkViewTest.php @@ -34,6 +34,11 @@ protected function testView(): TestView ])); } + public function testComponentRenders() + { + $this->testView()->assertHasElement(''); + } + public function testComponentLinksToRouteDestination() { $this->testView()->assertAttributeIs('href="foo.html"'); From 2439b721776d550c340ca7e2c65aa216330d3dfc Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 15 Mar 2024 21:44:28 +0100 Subject: [PATCH 29/46] Create SidebarItemsViewTest.php --- .../tests/Unit/Views/SidebarItemsViewTest.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 packages/framework/tests/Unit/Views/SidebarItemsViewTest.php diff --git a/packages/framework/tests/Unit/Views/SidebarItemsViewTest.php b/packages/framework/tests/Unit/Views/SidebarItemsViewTest.php new file mode 100644 index 00000000000..c2deb985986 --- /dev/null +++ b/packages/framework/tests/Unit/Views/SidebarItemsViewTest.php @@ -0,0 +1,18 @@ + Date: Fri, 15 Mar 2024 21:49:40 +0100 Subject: [PATCH 30/46] Add testing helper to assert if an HTML ID is present --- 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 1867c2f0b64..ed697e6b1fc 100644 --- a/packages/testing/src/Support/TestView.php +++ b/packages/testing/src/Support/TestView.php @@ -96,6 +96,20 @@ public function assertDoesNotHaveAttribute(string $attributeName): static return $this; } + /** + * Assert that the given HTML ID is contained within the view. + * + * @return $this + */ + public function assertHasId(string $id): static + { + $id = trim($id, '#'); + + PHPUnit::assertStringContainsString("id=\"$id\"", $this->rendered, "The id '$id' was not found."); + + return $this; + } + /** * Assert that the given CSS class is contained within the view. * From 1d5e1f77a43d8c682366a229362d2492c95ebe13 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 15 Mar 2024 21:50:06 +0100 Subject: [PATCH 31/46] Update testing helper to fluently direct assertions --- packages/testing/src/Support/TestView.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/testing/src/Support/TestView.php b/packages/testing/src/Support/TestView.php index ed697e6b1fc..22f0e4dac2e 100644 --- a/packages/testing/src/Support/TestView.php +++ b/packages/testing/src/Support/TestView.php @@ -46,6 +46,10 @@ public function assertHasElement(string $element): static { $element = trim($element, ''); + if (str_starts_with($element, '#')) { + return $this->assertHasId($element); + } + PHPUnit::assertStringContainsString("<$element", $this->rendered, "The element '$element' was not found."); return $this; From 4fa8deab206ee06022acf5b9a842ebb6a15858ce Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 15 Mar 2024 21:52:29 +0100 Subject: [PATCH 32/46] Set up testing helper --- .../tests/Unit/Views/SidebarItemsViewTest.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/packages/framework/tests/Unit/Views/SidebarItemsViewTest.php b/packages/framework/tests/Unit/Views/SidebarItemsViewTest.php index c2deb985986..cbbbff820cc 100644 --- a/packages/framework/tests/Unit/Views/SidebarItemsViewTest.php +++ b/packages/framework/tests/Unit/Views/SidebarItemsViewTest.php @@ -6,6 +6,9 @@ use Hyde\Testing\TestCase; use Hyde\Testing\TestsBladeViews; +use Hyde\Testing\Support\TestView; +use Hyde\Framework\Features\Navigation\DocumentationSidebar; +use Hyde\Framework\Features\Navigation\NavigationMenuGenerator; /** * @see resources/views/components/docs/sidebar-items.blade.php @@ -14,5 +17,10 @@ class SidebarItemsViewTest extends TestCase { use TestsBladeViews; - // + protected function testView(): TestView + { + return $this->test(view('hyde::components.docs.sidebar-items', [ + 'sidebar' => NavigationMenuGenerator::handle(DocumentationSidebar::class), + ])); + } } From 2d9ae2aff3f0deb5b4477b4b2a5197fbe9d470cf Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 15 Mar 2024 21:54:07 +0100 Subject: [PATCH 33/46] Test component renders --- packages/framework/tests/Unit/Views/SidebarItemsViewTest.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/framework/tests/Unit/Views/SidebarItemsViewTest.php b/packages/framework/tests/Unit/Views/SidebarItemsViewTest.php index cbbbff820cc..f95afd79b97 100644 --- a/packages/framework/tests/Unit/Views/SidebarItemsViewTest.php +++ b/packages/framework/tests/Unit/Views/SidebarItemsViewTest.php @@ -23,4 +23,9 @@ protected function testView(): TestView 'sidebar' => NavigationMenuGenerator::handle(DocumentationSidebar::class), ])); } + + public function testComponentRenders() + { + $this->testView()->assertHasElement('#sidebar-items'); + } } From 3ebd20f8260d63f5c10511f6ed61cf583895a4d8 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 15 Mar 2024 21:59:00 +0100 Subject: [PATCH 34/46] Mock route --- packages/framework/tests/Unit/Views/SidebarItemsViewTest.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/framework/tests/Unit/Views/SidebarItemsViewTest.php b/packages/framework/tests/Unit/Views/SidebarItemsViewTest.php index f95afd79b97..e06fc42cff1 100644 --- a/packages/framework/tests/Unit/Views/SidebarItemsViewTest.php +++ b/packages/framework/tests/Unit/Views/SidebarItemsViewTest.php @@ -19,6 +19,8 @@ class SidebarItemsViewTest extends TestCase protected function testView(): TestView { + $this->mockRoute(); + return $this->test(view('hyde::components.docs.sidebar-items', [ 'sidebar' => NavigationMenuGenerator::handle(DocumentationSidebar::class), ])); From 2eeed5f01a279c382920d37792d14ba0126412ba Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 15 Mar 2024 21:59:39 +0100 Subject: [PATCH 35/46] Add a route to populate testing sidebar --- packages/framework/tests/Unit/Views/SidebarItemsViewTest.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/framework/tests/Unit/Views/SidebarItemsViewTest.php b/packages/framework/tests/Unit/Views/SidebarItemsViewTest.php index e06fc42cff1..43978cc5d11 100644 --- a/packages/framework/tests/Unit/Views/SidebarItemsViewTest.php +++ b/packages/framework/tests/Unit/Views/SidebarItemsViewTest.php @@ -4,8 +4,11 @@ namespace Hyde\Framework\Testing\Unit\Views; +use Hyde\Hyde; use Hyde\Testing\TestCase; +use Hyde\Support\Models\Route; use Hyde\Testing\TestsBladeViews; +use Hyde\Pages\DocumentationPage; use Hyde\Testing\Support\TestView; use Hyde\Framework\Features\Navigation\DocumentationSidebar; use Hyde\Framework\Features\Navigation\NavigationMenuGenerator; @@ -21,6 +24,8 @@ protected function testView(): TestView { $this->mockRoute(); + Hyde::routes()->addRoute(new Route(new DocumentationPage('foo'))); + return $this->test(view('hyde::components.docs.sidebar-items', [ 'sidebar' => NavigationMenuGenerator::handle(DocumentationSidebar::class), ])); From a0c0726c63abfa204a6527a0c3d31fe02befcb94 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 15 Mar 2024 22:01:39 +0100 Subject: [PATCH 36/46] Add type annotation to view --- .../resources/views/components/docs/sidebar-items.blade.php | 1 + packages/framework/tests/Unit/Views/SidebarItemsViewTest.php | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/packages/framework/resources/views/components/docs/sidebar-items.blade.php b/packages/framework/resources/views/components/docs/sidebar-items.blade.php index e06a90e94c5..f0d4cb553f0 100644 --- a/packages/framework/resources/views/components/docs/sidebar-items.blade.php +++ b/packages/framework/resources/views/components/docs/sidebar-items.blade.php @@ -6,6 +6,7 @@ @else