Skip to content

Commit

Permalink
Merge branch '2.x-dev' into improved-navigation-internals
Browse files Browse the repository at this point in the history
  • Loading branch information
caendesilva committed Mar 18, 2024
2 parents be5cb7c + 6586fb4 commit fc3a63c
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 29 deletions.
10 changes: 5 additions & 5 deletions packages/framework/tests/Feature/Views/SidebarBrandViewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,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 @@ -28,7 +28,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 @@ -39,7 +39,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 @@ -51,7 +51,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 @@ -64,7 +64,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
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class SidebarFooterTextViewTest extends TestCase

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

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

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

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

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

$view->assertSeeText('Your Markdown String Here');
}
Expand Down
68 changes: 64 additions & 4 deletions packages/framework/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
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Hyde\Testing\Support\HtmlTesting;

use Closure;
use Illuminate\Testing\Assert as PHPUnit;

trait HtmlTestingAssertions
Expand Down Expand Up @@ -33,10 +34,70 @@ public function assertDontSeeEscaped(string $value): static
return $this->doAssert(fn () => PHPUnit::assertStringNotContainsString(e($value), $this->html, "The escaped string '$value' was found in the HTML."));
}

protected function doAssert(callable $assertion): static
public function assertHasId(string $id): TestableHtmlElement
{
return $this->doElementAssert(fn () => PHPUnit::assertSame($id, $this->id, 'The id attribute did not have the expected value.'));
}

public function assertDoesNotHaveId(string $id): TestableHtmlElement
{
return $this->doElementAssert(fn () => PHPUnit::assertNotSame($id, $this->id, 'The id attribute had the unexpected value.'));
}

public function assertHasClass(string $class): TestableHtmlElement
{
return $this->doElementAssert(fn () => PHPUnit::assertContains($class, $this->classes, "The class '$class' was not found in the element."));
}

public function assertDoesNotHaveClass(string $class): TestableHtmlElement
{
return $this->doElementAssert(fn () => PHPUnit::assertNotContains($class, $this->classes, "The class '$class' was found in the element."));
}

public function assertHasAttribute(string $attribute, ?string $value = null): TestableHtmlElement
{
if ($attribute === 'id') {
return $this->assertHasId($value);
}

if ($attribute === 'class') {
return $this->assertHasClass($value);
}

$this->doElementAssert(fn () => PHPUnit::assertArrayHasKey($attribute, $this->attributes, "The attribute '$attribute' was not found in the element."));

if ($value) {
return $this->doElementAssert(fn () => PHPUnit::assertSame($value, $this->attributes[$attribute], "The attribute '$attribute' did not have the expected value."));
}

return $this;
}

public function assertDoesNotHaveAttribute(string $attribute): TestableHtmlElement
{
return $this->doElementAssert(fn () => PHPUnit::assertArrayNotHasKey($attribute, $this->attributes, "The attribute '$attribute' was found in the element."));
}

/** @internal */
public function doAssert(Closure $assertion): static
{
$assertion();

return $this;
}

protected function doElementAssert(Closure $assertion): TestableHtmlElement
{
// Proxy to the root element if we're a TestableHtmlDocument.
if ($this instanceof TestableHtmlDocument) {
$rootElement = $this->getRootElement();

// Bind closure to the root element.
$assertion = $assertion->bindTo($rootElement);

return $rootElement->doAssert($assertion);
}

return $this->doAssert($assertion);
}
}
18 changes: 3 additions & 15 deletions packages/testing/src/Support/HtmlTesting/TestableHtmlElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Testing\Assert as PHPUnit;

use function trim;
use function filled;
use function strlen;
use function explode;
use function preg_match;
Expand Down Expand Up @@ -61,7 +59,7 @@ public function __construct(string $html, DOMElement $element, ?TestableHtmlElem
$this->attributes = $this->parseAttributes($element);
}

/** @return array{id: ?string, tag: string, text: string, classes: ?array, attributes: ?array, nodes: \Illuminate\Support\Collection<\Hyde\Testing\Support\HtmlTesting\TestableHtmlElement>} */
/** @return array{id: ?string, tag: string, text: string, classes: ?array, attributes: ?array, nodes: ?\Illuminate\Support\Collection<\Hyde\Testing\Support\HtmlTesting\TestableHtmlElement>} */
public function toArray(): array
{
return array_filter([
Expand All @@ -70,18 +68,8 @@ public function toArray(): array
'text' => $this->text,
'classes' => $this->classes,
'attributes' => $this->attributes,
'nodes' => $this->nodes,
], fn ($value): bool => filled($value));
}

public function hasClass(string $class): static
{
return $this->doAssert(fn () => PHPUnit::assertContains($class, $this->classes, "The class '$class' was not found in the element."));
}

public function doesNotHaveClass(string $class): static
{
return $this->doAssert(fn () => PHPUnit::assertNotContains($class, $this->classes, "The class '$class' was found in the element."));
'nodes' => $this->nodes->count() ? $this->nodes : null,
]);
}

protected function parseTag(string $html): string
Expand Down
2 changes: 1 addition & 1 deletion packages/testing/src/TestsBladeViews.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ trait TestsBladeViews
/**
* Test a Blade view.
*/
protected function test(string|View $view, $data = []): TestView
protected function view(string|View $view, $data = []): TestView
{
$data = array_merge($this->testViewData(), $data);

Expand Down

0 comments on commit fc3a63c

Please sign in to comment.