Skip to content

Commit

Permalink
Merge pull request #1618 from hydephp/improved-view-testing
Browse files Browse the repository at this point in the history
Internal: Normalize view testing helpers hydephp/develop@01d33f1
  • Loading branch information
github-actions committed Mar 18, 2024
1 parent 1c7c40c commit 54520dc
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 12 deletions.
10 changes: 5 additions & 5 deletions tests/Feature/Views/SidebarBrandViewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class SidebarBrandViewTest extends TestCase

public function testSidebarBrandView()
{
$view = $this->test(view('hyde::components.docs.sidebar-brand'));
$view = $this->view(view('hyde::components.docs.sidebar-brand'));

$view->assertSee('HydePHP Docs');
$view->assertSee('theme-toggle-button');
Expand All @@ -27,7 +27,7 @@ public function testSidebarBrandViewWithHomeRoute()
{
Hyde::routes()->addRoute((new DocumentationPage('index'))->getRoute());

$view = $this->test(view('hyde::components.docs.sidebar-brand'));
$view = $this->view(view('hyde::components.docs.sidebar-brand'));

$view->assertSee('HydePHP Docs');
$view->assertSee('theme-toggle-button');
Expand All @@ -38,7 +38,7 @@ public function testSidebarBrandViewWithDefaultHeaderText()
{
config(['docs.sidebar' => []]);

$view = $this->test(view('hyde::components.docs.sidebar-brand'));
$view = $this->view(view('hyde::components.docs.sidebar-brand'));

$view->assertSee('Documentation');
$view->assertDontSee('HydePHP Docs');
Expand All @@ -50,7 +50,7 @@ public function testSidebarBrandViewWithDefaultHeaderTextAndHomeRoute()

config(['docs.sidebar' => []]);

$view = $this->test(view('hyde::components.docs.sidebar-brand'));
$view = $this->view(view('hyde::components.docs.sidebar-brand'));

$view->assertSee('Documentation');
$view->assertSeeHtml('<a href="docs/index.html">Documentation</a>', true);
Expand All @@ -63,7 +63,7 @@ public function testSidebarBrandViewWithoutDarkmodeFeature()
$mock->shouldReceive('hasFeature')->with('darkmode')->andReturn(false);
HydeKernel::setInstance($mock);

$view = $this->test(view('hyde::components.docs.sidebar-brand'));
$view = $this->view(view('hyde::components.docs.sidebar-brand'));

$view->assertSee('HydePHP Docs');
$view->assertDontSee('theme-toggle-button');
Expand Down
6 changes: 3 additions & 3 deletions tests/Feature/Views/SidebarFooterTextViewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class SidebarFooterTextViewTest extends TestCase

public function testSidebarFooterTextViewWithDefaultConfig()
{
$view = $this->test(view('hyde::components.docs.sidebar-footer-text'));
$view = $this->view(view('hyde::components.docs.sidebar-footer-text'));

$view->assertSeeHtml('<a href="index.html">Back to home page</a>');
}
Expand All @@ -23,7 +23,7 @@ public function testSidebarFooterTextViewWhenConfigOptionIsTrue()
{
Config::set('docs.sidebar.footer', true);

$view = $this->test(view('hyde::components.docs.sidebar-footer-text'));
$view = $this->view(view('hyde::components.docs.sidebar-footer-text'));

$view->assertSeeHtml('<a href="index.html">Back to home page</a>');
}
Expand All @@ -32,7 +32,7 @@ public function testSidebarFooterTextViewWhenConfigOptionIsMarkdownString()
{
Config::set('docs.sidebar.footer', 'Your Markdown String Here');

$view = $this->test(view('hyde::components.docs.sidebar-footer-text'));
$view = $this->view(view('hyde::components.docs.sidebar-footer-text'));

$view->assertSeeText('Your Markdown String Here');
}
Expand Down
68 changes: 64 additions & 4 deletions tests/Unit/HtmlTestingSupportMetaTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

/** @noinspection HtmlUnknownAttribute */

declare(strict_types=1);

namespace Hyde\Framework\Testing\Unit;
Expand All @@ -9,6 +11,7 @@
use Hyde\Testing\UnitTestCase;
use Hyde\Testing\TestsBladeViews;
use Illuminate\Support\Collection;
use PHPUnit\Framework\ExpectationFailedException;
use Hyde\Testing\Support\HtmlTesting\TestableHtmlElement;
use Hyde\Testing\Support\HtmlTesting\TestableHtmlDocument;

Expand Down Expand Up @@ -369,7 +372,6 @@ public function testElementAttributes()
{
$this->assertSame([], $this->html('<div>Foo</div>')->getRootElement()->attributes);

/** @noinspection HtmlUnknownAttribute */
$this->assertSame([
'name' => 'test',
'foo' => 'bar',
Expand Down Expand Up @@ -429,21 +431,79 @@ public function testToArrayWithChildren()

public function testToArrayWithAttributes()
{
/** @noinspection HtmlUnknownAttribute */
$this->assertSame(
['id' => 'id', 'tag' => 'div', 'text' => 'Bar', 'classes' => ['class'], 'attributes' => ['name' => 'name']],
$this->html('<div id="id" class="class" name="name">Bar</div>')->getRootElement()->toArray()
);
}

public function testElementAssertHasId()
{
$this->html('<div id="foo">Foo</div>')->getRootElement()->assertHasId('foo');
}

public function testElementAssertDoesNotHaveId()
{
$this->html('<div>Foo</div>')->getRootElement()->assertDoesNotHaveId('foo');
}

public function testElementAssertHasClass()
{
$this->html('<div class="foo">Foo</div>')->getRootElement()->hasClass('foo');
$this->html('<div class="foo">Foo</div>')->getRootElement()->assertHasClass('foo');
}

public function testElementAssertDoesNotHaveClass()
{
$this->html('<div class="foo">Foo</div>')->getRootElement()->doesNotHaveClass('bar');
$this->html('<div class="foo">Foo</div>')->getRootElement()->assertDoesNotHaveClass('bar');
}

public function testElementAssertHasAttribute()
{
$this->html('<div name="foo">Foo</div>')->getRootElement()->assertHasAttribute('name');
}

public function testElementAssertDoesNotHaveAttribute()
{
$this->html('<div name="foo">Foo</div>')->getRootElement()->assertDoesNotHaveAttribute('href');
}

public function testElementAssertHasAttributeWithValue()
{
$this->html('<div name="foo">Foo</div>')->getRootElement()->assertHasAttribute('name', 'foo');
}

public function testElementAssertHasAttributeWithWrongValue()
{
try {
$this->html('<div name="foo">Foo</div>')->getRootElement()->assertHasAttribute('name', 'bar');
} catch (ExpectationFailedException $exception) {
$this->assertSame("The attribute 'name' did not have the expected value.\nFailed asserting that two strings are identical.", $exception->getMessage());
}
}

public function testElementAssertHasAttributeForwardsIdAssertions()
{
$this->html('<div id="foo">Foo</div>')->getRootElement()->assertHasAttribute('id', 'foo');
}

public function testElementAssertHasAttributeForwardsClassAssertions()
{
$this->html('<div class="foo">Foo</div>')->getRootElement()->assertHasAttribute('class', 'foo');
}

public function testAssertionCallsOnDocumentAreForwardedToRootElement()
{
$this->assertInstanceOf(TestableHtmlElement::class,
$this->html('<div id="foo" class="bar">Foo</div>')
->assertSee('Foo')
->assertHasId('foo')
->assertDoesNotHaveId('bar')
->assertHasClass('bar')
->assertDoesNotHaveClass('baz')
->assertHasAttribute('class', 'bar')
->assertDoesNotHaveAttribute('href')
->assertSee('Foo')
);
}

protected function exampleElement(): TestableHtmlElement
Expand Down

0 comments on commit 54520dc

Please sign in to comment.