Skip to content

Commit

Permalink
Merge pull request #1589 from hydephp/improved-view-testing
Browse files Browse the repository at this point in the history
Improve and add more view tests
  • Loading branch information
caendesilva authored Feb 25, 2024
2 parents 1ec4ca5 + f282751 commit 843f461
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 1 deletion.
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);
}
}

0 comments on commit 843f461

Please sign in to comment.