diff --git a/.idea/php-test-framework.xml b/.idea/php-test-framework.xml index f990e0a96bb..8ecfa3b3072 100644 --- a/.idea/php-test-framework.xml +++ b/.idea/php-test-framework.xml @@ -17,6 +17,7 @@ + diff --git a/packages/framework/tests/Feature/Views/SidebarBrandViewTest.php b/packages/framework/tests/Feature/Views/SidebarBrandViewTest.php new file mode 100644 index 00000000000..10b840bf5e4 --- /dev/null +++ b/packages/framework/tests/Feature/Views/SidebarBrandViewTest.php @@ -0,0 +1,71 @@ +test(view('hyde::components.docs.sidebar-brand')); + + $view->assertSee('HydePHP Docs'); + $view->assertSee('theme-toggle-button'); + $view->assertDontSee('href'); + } + + public function testSidebarBrandViewWithHomeRoute() + { + Hyde::routes()->addRoute((new DocumentationPage('index'))->getRoute()); + + $view = $this->test(view('hyde::components.docs.sidebar-brand')); + + $view->assertSee('HydePHP Docs'); + $view->assertSee('theme-toggle-button'); + $view->assertSeeHtml('HydePHP Docs', true); + } + + public function testSidebarBrandViewWithDefaultHeaderText() + { + config(['docs.sidebar' => []]); + + $view = $this->test(view('hyde::components.docs.sidebar-brand')); + + $view->assertSee('Documentation'); + $view->assertDontSee('HydePHP Docs'); + } + + public function testSidebarBrandViewWithDefaultHeaderTextAndHomeRoute() + { + Hyde::routes()->addRoute((new DocumentationPage('index'))->getRoute()); + + config(['docs.sidebar' => []]); + + $view = $this->test(view('hyde::components.docs.sidebar-brand')); + + $view->assertSee('Documentation'); + $view->assertSeeHtml('Documentation', true); + $view->assertDontSee('HydePHP Docs'); + } + + public function testSidebarBrandViewWithoutDarkmodeFeature() + { + $mock = $this->mock(HydeKernel::class)->makePartial(); + $mock->shouldReceive('hasFeature')->with('darkmode')->andReturn(false); + HydeKernel::setInstance($mock); + + $view = $this->test(view('hyde::components.docs.sidebar-brand')); + + $view->assertSee('HydePHP Docs'); + $view->assertDontSee('theme-toggle-button'); + } +} diff --git a/packages/testing/src/Support/TestView.php b/packages/testing/src/Support/TestView.php index c29267f1c85..8bb446c3ebc 100644 --- a/packages/testing/src/Support/TestView.php +++ b/packages/testing/src/Support/TestView.php @@ -4,6 +4,8 @@ namespace Hyde\Testing\Support; +use Illuminate\Testing\Assert as PHPUnit; + class TestView extends \Illuminate\Testing\TestView { /** @@ -11,8 +13,29 @@ class TestView extends \Illuminate\Testing\TestView * * @return $this */ - public function assertSeeHtml(string $value): static + public function assertSeeHtml(string $value, bool $ignoreFormatting = false): static { + if ($ignoreFormatting) { + return $this->assertSeeHtmlIgnoringFormatting($value); + } + return $this->assertSee($value, false); } + + /** + * Assert that the given HTML is contained within the view text, ignoring whitespace and newlines. + * + * @return $this + */ + public function assertSeeHtmlIgnoringFormatting(string $value): static + { + PHPUnit::assertStringContainsString($this->trimNewlinesAndIndentation($value), $this->trimNewlinesAndIndentation($this->rendered)); + + return $this; + } + + protected function trimNewlinesAndIndentation(string $value): string + { + return str_replace([' ', "\t", "\n", "\r"], '', $value); + } }