Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve and add more view tests #1589

Merged
merged 5 commits into from
Feb 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .idea/php-test-framework.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

71 changes: 71 additions & 0 deletions packages/framework/tests/Feature/Views/SidebarBrandViewTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

declare(strict_types=1);

namespace Hyde\Framework\Testing\Feature\Views;

use Hyde\Hyde;
use Hyde\Testing\TestCase;
use Hyde\Foundation\HydeKernel;
use Hyde\Testing\TestsBladeViews;
use Hyde\Pages\DocumentationPage;

class SidebarBrandViewTest extends TestCase
{
use TestsBladeViews;

public function testSidebarBrandView()
{
$view = $this->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('<a href="docs/index.html">HydePHP Docs</a>', 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('<a href="docs/index.html">Documentation</a>', 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');
}
}
25 changes: 24 additions & 1 deletion packages/testing/src/Support/TestView.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,38 @@

namespace Hyde\Testing\Support;

use Illuminate\Testing\Assert as PHPUnit;

class TestView extends \Illuminate\Testing\TestView
{
/**
* Assert that the given HTML is contained within the view.
*
* @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);
}
}
Loading