Skip to content

Commit fc3a63c

Browse files
committed
Merge branch '2.x-dev' into improved-navigation-internals
2 parents be5cb7c + 6586fb4 commit fc3a63c

File tree

6 files changed

+138
-29
lines changed

6 files changed

+138
-29
lines changed

packages/framework/tests/Feature/Views/SidebarBrandViewTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class SidebarBrandViewTest extends TestCase
1717

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

2222
$view->assertSee('HydePHP Docs');
2323
$view->assertSee('theme-toggle-button');
@@ -28,7 +28,7 @@ public function testSidebarBrandViewWithHomeRoute()
2828
{
2929
Hyde::routes()->addRoute((new DocumentationPage('index'))->getRoute());
3030

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

3333
$view->assertSee('HydePHP Docs');
3434
$view->assertSee('theme-toggle-button');
@@ -39,7 +39,7 @@ public function testSidebarBrandViewWithDefaultHeaderText()
3939
{
4040
config(['docs.sidebar' => []]);
4141

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

4444
$view->assertSee('Documentation');
4545
$view->assertDontSee('HydePHP Docs');
@@ -51,7 +51,7 @@ public function testSidebarBrandViewWithDefaultHeaderTextAndHomeRoute()
5151

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

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

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

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

6969
$view->assertSee('HydePHP Docs');
7070
$view->assertDontSee('theme-toggle-button');

packages/framework/tests/Feature/Views/SidebarFooterTextViewTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class SidebarFooterTextViewTest extends TestCase
1515

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

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

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

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

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

3838
$view->assertSeeText('Your Markdown String Here');
3939
}

packages/framework/tests/Unit/HtmlTestingSupportMetaTest.php

Lines changed: 64 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
/** @noinspection HtmlUnknownAttribute */
4+
35
declare(strict_types=1);
46

57
namespace Hyde\Framework\Testing\Unit;
@@ -9,6 +11,7 @@
911
use Hyde\Testing\UnitTestCase;
1012
use Hyde\Testing\TestsBladeViews;
1113
use Illuminate\Support\Collection;
14+
use PHPUnit\Framework\ExpectationFailedException;
1215
use Hyde\Testing\Support\HtmlTesting\TestableHtmlElement;
1316
use Hyde\Testing\Support\HtmlTesting\TestableHtmlDocument;
1417

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

372-
/** @noinspection HtmlUnknownAttribute */
373375
$this->assertSame([
374376
'name' => 'test',
375377
'foo' => 'bar',
@@ -429,21 +431,79 @@ public function testToArrayWithChildren()
429431

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

440+
public function testElementAssertHasId()
441+
{
442+
$this->html('<div id="foo">Foo</div>')->getRootElement()->assertHasId('foo');
443+
}
444+
445+
public function testElementAssertDoesNotHaveId()
446+
{
447+
$this->html('<div>Foo</div>')->getRootElement()->assertDoesNotHaveId('foo');
448+
}
449+
439450
public function testElementAssertHasClass()
440451
{
441-
$this->html('<div class="foo">Foo</div>')->getRootElement()->hasClass('foo');
452+
$this->html('<div class="foo">Foo</div>')->getRootElement()->assertHasClass('foo');
442453
}
443454

444455
public function testElementAssertDoesNotHaveClass()
445456
{
446-
$this->html('<div class="foo">Foo</div>')->getRootElement()->doesNotHaveClass('bar');
457+
$this->html('<div class="foo">Foo</div>')->getRootElement()->assertDoesNotHaveClass('bar');
458+
}
459+
460+
public function testElementAssertHasAttribute()
461+
{
462+
$this->html('<div name="foo">Foo</div>')->getRootElement()->assertHasAttribute('name');
463+
}
464+
465+
public function testElementAssertDoesNotHaveAttribute()
466+
{
467+
$this->html('<div name="foo">Foo</div>')->getRootElement()->assertDoesNotHaveAttribute('href');
468+
}
469+
470+
public function testElementAssertHasAttributeWithValue()
471+
{
472+
$this->html('<div name="foo">Foo</div>')->getRootElement()->assertHasAttribute('name', 'foo');
473+
}
474+
475+
public function testElementAssertHasAttributeWithWrongValue()
476+
{
477+
try {
478+
$this->html('<div name="foo">Foo</div>')->getRootElement()->assertHasAttribute('name', 'bar');
479+
} catch (ExpectationFailedException $exception) {
480+
$this->assertSame("The attribute 'name' did not have the expected value.\nFailed asserting that two strings are identical.", $exception->getMessage());
481+
}
482+
}
483+
484+
public function testElementAssertHasAttributeForwardsIdAssertions()
485+
{
486+
$this->html('<div id="foo">Foo</div>')->getRootElement()->assertHasAttribute('id', 'foo');
487+
}
488+
489+
public function testElementAssertHasAttributeForwardsClassAssertions()
490+
{
491+
$this->html('<div class="foo">Foo</div>')->getRootElement()->assertHasAttribute('class', 'foo');
492+
}
493+
494+
public function testAssertionCallsOnDocumentAreForwardedToRootElement()
495+
{
496+
$this->assertInstanceOf(TestableHtmlElement::class,
497+
$this->html('<div id="foo" class="bar">Foo</div>')
498+
->assertSee('Foo')
499+
->assertHasId('foo')
500+
->assertDoesNotHaveId('bar')
501+
->assertHasClass('bar')
502+
->assertDoesNotHaveClass('baz')
503+
->assertHasAttribute('class', 'bar')
504+
->assertDoesNotHaveAttribute('href')
505+
->assertSee('Foo')
506+
);
447507
}
448508

449509
protected function exampleElement(): TestableHtmlElement

packages/testing/src/Support/HtmlTesting/HtmlTestingAssertions.php

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Hyde\Testing\Support\HtmlTesting;
66

7+
use Closure;
78
use Illuminate\Testing\Assert as PHPUnit;
89

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

36-
protected function doAssert(callable $assertion): static
37+
public function assertHasId(string $id): TestableHtmlElement
38+
{
39+
return $this->doElementAssert(fn () => PHPUnit::assertSame($id, $this->id, 'The id attribute did not have the expected value.'));
40+
}
41+
42+
public function assertDoesNotHaveId(string $id): TestableHtmlElement
43+
{
44+
return $this->doElementAssert(fn () => PHPUnit::assertNotSame($id, $this->id, 'The id attribute had the unexpected value.'));
45+
}
46+
47+
public function assertHasClass(string $class): TestableHtmlElement
48+
{
49+
return $this->doElementAssert(fn () => PHPUnit::assertContains($class, $this->classes, "The class '$class' was not found in the element."));
50+
}
51+
52+
public function assertDoesNotHaveClass(string $class): TestableHtmlElement
53+
{
54+
return $this->doElementAssert(fn () => PHPUnit::assertNotContains($class, $this->classes, "The class '$class' was found in the element."));
55+
}
56+
57+
public function assertHasAttribute(string $attribute, ?string $value = null): TestableHtmlElement
58+
{
59+
if ($attribute === 'id') {
60+
return $this->assertHasId($value);
61+
}
62+
63+
if ($attribute === 'class') {
64+
return $this->assertHasClass($value);
65+
}
66+
67+
$this->doElementAssert(fn () => PHPUnit::assertArrayHasKey($attribute, $this->attributes, "The attribute '$attribute' was not found in the element."));
68+
69+
if ($value) {
70+
return $this->doElementAssert(fn () => PHPUnit::assertSame($value, $this->attributes[$attribute], "The attribute '$attribute' did not have the expected value."));
71+
}
72+
73+
return $this;
74+
}
75+
76+
public function assertDoesNotHaveAttribute(string $attribute): TestableHtmlElement
77+
{
78+
return $this->doElementAssert(fn () => PHPUnit::assertArrayNotHasKey($attribute, $this->attributes, "The attribute '$attribute' was found in the element."));
79+
}
80+
81+
/** @internal */
82+
public function doAssert(Closure $assertion): static
3783
{
3884
$assertion();
3985

4086
return $this;
4187
}
88+
89+
protected function doElementAssert(Closure $assertion): TestableHtmlElement
90+
{
91+
// Proxy to the root element if we're a TestableHtmlDocument.
92+
if ($this instanceof TestableHtmlDocument) {
93+
$rootElement = $this->getRootElement();
94+
95+
// Bind closure to the root element.
96+
$assertion = $assertion->bindTo($rootElement);
97+
98+
return $rootElement->doAssert($assertion);
99+
}
100+
101+
return $this->doAssert($assertion);
102+
}
42103
}

packages/testing/src/Support/HtmlTesting/TestableHtmlElement.php

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@
88
use Illuminate\Support\Arr;
99
use Illuminate\Support\Collection;
1010
use Illuminate\Contracts\Support\Arrayable;
11-
use Illuminate\Testing\Assert as PHPUnit;
1211

1312
use function trim;
14-
use function filled;
1513
use function strlen;
1614
use function explode;
1715
use function preg_match;
@@ -61,7 +59,7 @@ public function __construct(string $html, DOMElement $element, ?TestableHtmlElem
6159
$this->attributes = $this->parseAttributes($element);
6260
}
6361

64-
/** @return array{id: ?string, tag: string, text: string, classes: ?array, attributes: ?array, nodes: \Illuminate\Support\Collection<\Hyde\Testing\Support\HtmlTesting\TestableHtmlElement>} */
62+
/** @return array{id: ?string, tag: string, text: string, classes: ?array, attributes: ?array, nodes: ?\Illuminate\Support\Collection<\Hyde\Testing\Support\HtmlTesting\TestableHtmlElement>} */
6563
public function toArray(): array
6664
{
6765
return array_filter([
@@ -70,18 +68,8 @@ public function toArray(): array
7068
'text' => $this->text,
7169
'classes' => $this->classes,
7270
'attributes' => $this->attributes,
73-
'nodes' => $this->nodes,
74-
], fn ($value): bool => filled($value));
75-
}
76-
77-
public function hasClass(string $class): static
78-
{
79-
return $this->doAssert(fn () => PHPUnit::assertContains($class, $this->classes, "The class '$class' was not found in the element."));
80-
}
81-
82-
public function doesNotHaveClass(string $class): static
83-
{
84-
return $this->doAssert(fn () => PHPUnit::assertNotContains($class, $this->classes, "The class '$class' was found in the element."));
71+
'nodes' => $this->nodes->count() ? $this->nodes : null,
72+
]);
8573
}
8674

8775
protected function parseTag(string $html): string

packages/testing/src/TestsBladeViews.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ trait TestsBladeViews
1616
/**
1717
* Test a Blade view.
1818
*/
19-
protected function test(string|View $view, $data = []): TestView
19+
protected function view(string|View $view, $data = []): TestView
2020
{
2121
$data = array_merge($this->testViewData(), $data);
2222

0 commit comments

Comments
 (0)