From 84db14b33e71577632b21916c2d95ad19f73de61 Mon Sep 17 00:00:00 2001 From: Jack Date: Fri, 19 Sep 2025 21:23:57 +0100 Subject: [PATCH 1/8] Aliases --- src/TestComponentMacros.php | 10 ++++++++++ src/TestResponseMacros.php | 10 ++++++++++ src/TestViewMacros.php | 10 ++++++++++ 3 files changed, 30 insertions(+) diff --git a/src/TestComponentMacros.php b/src/TestComponentMacros.php index 3156cd4..d3b696a 100644 --- a/src/TestComponentMacros.php +++ b/src/TestComponentMacros.php @@ -44,6 +44,11 @@ public function assertHtml5(): Closure }; } + public function assertElement(): Closure + { + return $this->assertElementExists(); + } + public function assertElementExists(): Closure { return function ($selector = 'body', $callback = null): TestComponent { @@ -80,6 +85,11 @@ public function assertElementExists(): Closure }; } + public function assertForm(): Closure + { + return $this->assertFormExists(); + } + public function assertFormExists(): Closure { return function ($selector = 'form', $callback = null): TestComponent { diff --git a/src/TestResponseMacros.php b/src/TestResponseMacros.php index d87a92c..2b4938b 100644 --- a/src/TestResponseMacros.php +++ b/src/TestResponseMacros.php @@ -44,6 +44,11 @@ public function assertHtml5(): Closure }; } + public function assertElement(): Closure + { + return $this->assertElementExists(); + } + public function assertElementExists(): Closure { return function ($selector = 'body', $callback = null): TestResponse { @@ -80,6 +85,11 @@ public function assertElementExists(): Closure }; } + public function assertForm(): Closure + { + return $this->assertFormExists(); + } + public function assertFormExists(): Closure { return function ($selector = 'form', $callback = null): TestResponse { diff --git a/src/TestViewMacros.php b/src/TestViewMacros.php index 79fa246..7092fd1 100644 --- a/src/TestViewMacros.php +++ b/src/TestViewMacros.php @@ -44,6 +44,11 @@ public function assertHtml5(): Closure }; } + public function assertElement(): Closure + { + return $this->assertElementExists(); + } + public function assertElementExists(): Closure { return function ($selector = 'body', $callback = null): TestView { @@ -80,6 +85,11 @@ public function assertElementExists(): Closure }; } + public function assertForm(): Closure + { + return $this->assertFormExists(); + } + public function assertFormExists(): Closure { return function ($selector = 'form', $callback = null): TestView { From ae3c984695a9eb8d9284d9543a633be766b51980 Mon Sep 17 00:00:00 2001 From: Jack Date: Fri, 19 Sep 2025 21:24:15 +0100 Subject: [PATCH 2/8] Update ide-helper.php --- ide-helper.php | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/ide-helper.php b/ide-helper.php index cfeb5f8..1bfb3d0 100644 --- a/ide-helper.php +++ b/ide-helper.php @@ -9,12 +9,24 @@ public function assertHtml5() return $instance; } + public function assertElement($selector = 'body', $callback = null) + { + /** @var \Illuminate\Testing\TestResponse $instance */ + return $instance; + } + public function assertElementExists($selector = 'body', $callback = null) { /** @var \Illuminate\Testing\TestResponse $instance */ return $instance; } + public function assertForm($selector = 'body', $callback = null) + { + /** @var \Illuminate\Testing\TestResponse $instance */ + return $instance; + } + public function assertFormExists($selector = 'form', $callback = null) { /** @var \Illuminate\Testing\TestResponse $instance */ @@ -30,12 +42,24 @@ public function assertHtml5() return $instance; } + public function assertElement($selector = 'body', $callback = null) + { + /** @var \Illuminate\Testing\TestResponse $instance */ + return $instance; + } + public function assertElementExists($selector = 'body', $callback = null) { /** @var \Illuminate\Testing\TestResponse $instance */ return $instance; } + public function assertForm($selector = 'form', $callback = null) + { + /** @var \Illuminate\Testing\TestResponse $instance */ + return $instance; + } + public function assertFormExists($selector = 'form', $callback = null) { /** @var \Illuminate\Testing\TestResponse $instance */ From 44348e2336f61cbfe3f035c0bfff7b727ad049eb Mon Sep 17 00:00:00 2001 From: Jack Date: Fri, 19 Sep 2025 21:33:40 +0100 Subject: [PATCH 3/8] tests n ide --- ide-helper.php | 33 +++++++++++++++++++++++++++++++++ tests/ComponentTest.php | 5 +++++ tests/DomTest.php | 5 +++++ tests/FormTest.php | 5 +++++ tests/ViewTest.php | 4 ++++ 5 files changed, 52 insertions(+) diff --git a/ide-helper.php b/ide-helper.php index 1bfb3d0..71ad6ea 100644 --- a/ide-helper.php +++ b/ide-helper.php @@ -66,4 +66,37 @@ public function assertFormExists($selector = 'form', $callback = null) return $instance; } } + + class TestComponent + { + public function assertHtml5() + { + /** @var \Illuminate\Testing\TestResponse $instance */ + return $instance; + } + + public function assertElement($selector = 'body', $callback = null) + { + /** @var \Illuminate\Testing\TestResponse $instance */ + return $instance; + } + + public function assertElementExists($selector = 'body', $callback = null) + { + /** @var \Illuminate\Testing\TestResponse $instance */ + return $instance; + } + + public function assertForm($selector = 'form', $callback = null) + { + /** @var \Illuminate\Testing\TestResponse $instance */ + return $instance; + } + + public function assertFormExists($selector = 'form', $callback = null) + { + /** @var \Illuminate\Testing\TestResponse $instance */ + return $instance; + } + } } diff --git a/tests/ComponentTest.php b/tests/ComponentTest.php index bfd02e0..3d45d7f 100644 --- a/tests/ComponentTest.php +++ b/tests/ComponentTest.php @@ -18,6 +18,11 @@ } }); +it('assertElement alias works for assertElementExists', function () { + $this->component(NestedComponent::class) + ->assertElement(); +}); + it('can handle an empty component', function () { $this->component(EmptyComponent::class) ->assertElementExists(); diff --git a/tests/DomTest.php b/tests/DomTest.php index 3c29c46..f0be5c8 100644 --- a/tests/DomTest.php +++ b/tests/DomTest.php @@ -3,6 +3,11 @@ use PHPUnit\Framework\AssertionFailedError; use Sinnbeck\DomAssertions\Asserts\AssertElement; +it('assertAlias alias works for assertElementExists', function () { + $this->get('nesting') + ->assertElement(); +}); + it('can handle an empty view', function () { $this->get('empty') ->assertElementExists(); diff --git a/tests/FormTest.php b/tests/FormTest.php index a375104..dc4410a 100644 --- a/tests/FormTest.php +++ b/tests/FormTest.php @@ -6,6 +6,11 @@ use Sinnbeck\DomAssertions\Asserts\AssertForm; use Sinnbeck\DomAssertions\Asserts\AssertSelect; +it('assertForm alias works for assertFormExists', function () { + $this->get('form') + ->assertForm(); +}); + it('can find a form by default', function () { $this->get('form') ->assertFormExists(); diff --git a/tests/ViewTest.php b/tests/ViewTest.php index e58e422..7770f51 100644 --- a/tests/ViewTest.php +++ b/tests/ViewTest.php @@ -3,6 +3,10 @@ use PHPUnit\Framework\AssertionFailedError; use Sinnbeck\DomAssertions\Asserts\AssertElement; +it('assertElement alias works for assertElementExists', function () { + $this->view('nesting') + ->assertElement(); +}); it('can handle an empty view', function () { $this->view('empty') ->assertElementExists(); From 7e0364a93264bc72bd2ab9b475418a23eef5617d Mon Sep 17 00:00:00 2001 From: Jack Date: Fri, 19 Sep 2025 22:11:17 +0100 Subject: [PATCH 4/8] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 551b4bb..1fbb635 100644 --- a/README.md +++ b/README.md @@ -62,7 +62,7 @@ $this->get(route('about')) ## Usage ### Testing the DOM -When calling a route in a test you might want to make sure that the view contains certain elements. To test this you can use the `->assertElementExists()` method on the test response. +When calling a route in a test you might want to make sure that the view contains certain elements. To test this, you can use the `->assertElementExists()` method on the test response or the alias `assertElement()`. The following will ensure that there is a body tag in the parsed response. Be aware that this package assumes a proper html structure and will wrap your html in a html, head and body tag if they are missing! ```php $this->get('/some-route') @@ -194,7 +194,7 @@ $this->get('/some-route') ### Testing forms Testing forms allows using all the dom asserts from above, but has a few special helpers to help test for forms. -Instead of using `->assertElementExists()` we will use `->assertFormExists()` method on the test response. +Instead of using `->assertElementExists()` you can use `->assertFormExists()`, or the alias `assertForm()` on the test response. ```php $this->get('/some-route') ->assertFormExists(); From e03ce410df90b810eff307efab988845653cbce2 Mon Sep 17 00:00:00 2001 From: Jack Date: Fri, 19 Sep 2025 22:25:09 +0100 Subject: [PATCH 5/8] complex tests to prove it works --- tests/ComponentTest.php | 5 ++++- tests/FormTest.php | 4 +++- tests/ViewTest.php | 4 +++- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/tests/ComponentTest.php b/tests/ComponentTest.php index 3d45d7f..8648ba7 100644 --- a/tests/ComponentTest.php +++ b/tests/ComponentTest.php @@ -20,7 +20,10 @@ it('assertElement alias works for assertElementExists', function () { $this->component(NestedComponent::class) - ->assertElement(); + ->assertElement('body', function (AssertElement $assert) { + dd($assert); + $assert->is('body'); + }); }); it('can handle an empty component', function () { diff --git a/tests/FormTest.php b/tests/FormTest.php index dc4410a..c8a2c12 100644 --- a/tests/FormTest.php +++ b/tests/FormTest.php @@ -8,7 +8,9 @@ it('assertForm alias works for assertFormExists', function () { $this->get('form') - ->assertForm(); + ->assertForm('form:nth-child(2)', function (AssertForm $form) { + $form->hasAction('form'); + }); }); it('can find a form by default', function () { diff --git a/tests/ViewTest.php b/tests/ViewTest.php index 7770f51..228900a 100644 --- a/tests/ViewTest.php +++ b/tests/ViewTest.php @@ -5,7 +5,9 @@ it('assertElement alias works for assertElementExists', function () { $this->view('nesting') - ->assertElement(); + ->assertElement('body', function (AssertElement $assert) { + $assert->is('body'); + }); }); it('can handle an empty view', function () { $this->view('empty') From 9e9972047545d72025a1b24d510619b8dd736187 Mon Sep 17 00:00:00 2001 From: Jack Date: Fri, 19 Sep 2025 22:26:59 +0100 Subject: [PATCH 6/8] oopsie --- tests/ComponentTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/ComponentTest.php b/tests/ComponentTest.php index 8648ba7..6ca6583 100644 --- a/tests/ComponentTest.php +++ b/tests/ComponentTest.php @@ -21,7 +21,6 @@ it('assertElement alias works for assertElementExists', function () { $this->component(NestedComponent::class) ->assertElement('body', function (AssertElement $assert) { - dd($assert); $assert->is('body'); }); }); From fb22303451366d351249099fa5368f849636ea3d Mon Sep 17 00:00:00 2001 From: Jack Date: Fri, 19 Sep 2025 22:32:54 +0100 Subject: [PATCH 7/8] Update DomTest.php --- tests/DomTest.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/DomTest.php b/tests/DomTest.php index f0be5c8..9d8bc41 100644 --- a/tests/DomTest.php +++ b/tests/DomTest.php @@ -5,7 +5,9 @@ it('assertAlias alias works for assertElementExists', function () { $this->get('nesting') - ->assertElement(); + ->assertElementExists('body', function (AssertElement $assert) { + $assert->is('body'); + }); }); it('can handle an empty view', function () { From b6f8aaf5a0a68e06b67c191dae7c01f6746f1daa Mon Sep 17 00:00:00 2001 From: Jack Date: Fri, 19 Sep 2025 22:41:59 +0100 Subject: [PATCH 8/8] Update ViewTest.php --- tests/ViewTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/ViewTest.php b/tests/ViewTest.php index 228900a..5478da6 100644 --- a/tests/ViewTest.php +++ b/tests/ViewTest.php @@ -9,6 +9,7 @@ $assert->is('body'); }); }); + it('can handle an empty view', function () { $this->view('empty') ->assertElementExists();