Skip to content

Commit 843f461

Browse files
authored
Merge pull request #1589 from hydephp/improved-view-testing
Improve and add more view tests
2 parents 1ec4ca5 + f282751 commit 843f461

File tree

3 files changed

+96
-1
lines changed

3 files changed

+96
-1
lines changed

.idea/php-test-framework.xml

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Hyde\Framework\Testing\Feature\Views;
6+
7+
use Hyde\Hyde;
8+
use Hyde\Testing\TestCase;
9+
use Hyde\Foundation\HydeKernel;
10+
use Hyde\Testing\TestsBladeViews;
11+
use Hyde\Pages\DocumentationPage;
12+
13+
class SidebarBrandViewTest extends TestCase
14+
{
15+
use TestsBladeViews;
16+
17+
public function testSidebarBrandView()
18+
{
19+
$view = $this->test(view('hyde::components.docs.sidebar-brand'));
20+
21+
$view->assertSee('HydePHP Docs');
22+
$view->assertSee('theme-toggle-button');
23+
$view->assertDontSee('href');
24+
}
25+
26+
public function testSidebarBrandViewWithHomeRoute()
27+
{
28+
Hyde::routes()->addRoute((new DocumentationPage('index'))->getRoute());
29+
30+
$view = $this->test(view('hyde::components.docs.sidebar-brand'));
31+
32+
$view->assertSee('HydePHP Docs');
33+
$view->assertSee('theme-toggle-button');
34+
$view->assertSeeHtml('<a href="docs/index.html">HydePHP Docs</a>', true);
35+
}
36+
37+
public function testSidebarBrandViewWithDefaultHeaderText()
38+
{
39+
config(['docs.sidebar' => []]);
40+
41+
$view = $this->test(view('hyde::components.docs.sidebar-brand'));
42+
43+
$view->assertSee('Documentation');
44+
$view->assertDontSee('HydePHP Docs');
45+
}
46+
47+
public function testSidebarBrandViewWithDefaultHeaderTextAndHomeRoute()
48+
{
49+
Hyde::routes()->addRoute((new DocumentationPage('index'))->getRoute());
50+
51+
config(['docs.sidebar' => []]);
52+
53+
$view = $this->test(view('hyde::components.docs.sidebar-brand'));
54+
55+
$view->assertSee('Documentation');
56+
$view->assertSeeHtml('<a href="docs/index.html">Documentation</a>', true);
57+
$view->assertDontSee('HydePHP Docs');
58+
}
59+
60+
public function testSidebarBrandViewWithoutDarkmodeFeature()
61+
{
62+
$mock = $this->mock(HydeKernel::class)->makePartial();
63+
$mock->shouldReceive('hasFeature')->with('darkmode')->andReturn(false);
64+
HydeKernel::setInstance($mock);
65+
66+
$view = $this->test(view('hyde::components.docs.sidebar-brand'));
67+
68+
$view->assertSee('HydePHP Docs');
69+
$view->assertDontSee('theme-toggle-button');
70+
}
71+
}

packages/testing/src/Support/TestView.php

+24-1
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,38 @@
44

55
namespace Hyde\Testing\Support;
66

7+
use Illuminate\Testing\Assert as PHPUnit;
8+
79
class TestView extends \Illuminate\Testing\TestView
810
{
911
/**
1012
* Assert that the given HTML is contained within the view.
1113
*
1214
* @return $this
1315
*/
14-
public function assertSeeHtml(string $value): static
16+
public function assertSeeHtml(string $value, bool $ignoreFormatting = false): static
1517
{
18+
if ($ignoreFormatting) {
19+
return $this->assertSeeHtmlIgnoringFormatting($value);
20+
}
21+
1622
return $this->assertSee($value, false);
1723
}
24+
25+
/**
26+
* Assert that the given HTML is contained within the view text, ignoring whitespace and newlines.
27+
*
28+
* @return $this
29+
*/
30+
public function assertSeeHtmlIgnoringFormatting(string $value): static
31+
{
32+
PHPUnit::assertStringContainsString($this->trimNewlinesAndIndentation($value), $this->trimNewlinesAndIndentation($this->rendered));
33+
34+
return $this;
35+
}
36+
37+
protected function trimNewlinesAndIndentation(string $value): string
38+
{
39+
return str_replace([' ', "\t", "\n", "\r"], '', $value);
40+
}
1841
}

0 commit comments

Comments
 (0)