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(); diff --git a/ide-helper.php b/ide-helper.php index cfeb5f8..71ad6ea 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,57 @@ 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 */ + 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 */ 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 { diff --git a/tests/ComponentTest.php b/tests/ComponentTest.php index bfd02e0..6ca6583 100644 --- a/tests/ComponentTest.php +++ b/tests/ComponentTest.php @@ -18,6 +18,13 @@ } }); +it('assertElement alias works for assertElementExists', function () { + $this->component(NestedComponent::class) + ->assertElement('body', function (AssertElement $assert) { + $assert->is('body'); + }); +}); + it('can handle an empty component', function () { $this->component(EmptyComponent::class) ->assertElementExists(); diff --git a/tests/DomTest.php b/tests/DomTest.php index 3c29c46..9d8bc41 100644 --- a/tests/DomTest.php +++ b/tests/DomTest.php @@ -3,6 +3,13 @@ use PHPUnit\Framework\AssertionFailedError; use Sinnbeck\DomAssertions\Asserts\AssertElement; +it('assertAlias alias works for assertElementExists', function () { + $this->get('nesting') + ->assertElementExists('body', function (AssertElement $assert) { + $assert->is('body'); + }); +}); + it('can handle an empty view', function () { $this->get('empty') ->assertElementExists(); diff --git a/tests/FormTest.php b/tests/FormTest.php index a375104..c8a2c12 100644 --- a/tests/FormTest.php +++ b/tests/FormTest.php @@ -6,6 +6,13 @@ use Sinnbeck\DomAssertions\Asserts\AssertForm; use Sinnbeck\DomAssertions\Asserts\AssertSelect; +it('assertForm alias works for assertFormExists', function () { + $this->get('form') + ->assertForm('form:nth-child(2)', function (AssertForm $form) { + $form->hasAction('form'); + }); +}); + it('can find a form by default', function () { $this->get('form') ->assertFormExists(); diff --git a/tests/ViewTest.php b/tests/ViewTest.php index e58e422..5478da6 100644 --- a/tests/ViewTest.php +++ b/tests/ViewTest.php @@ -3,6 +3,13 @@ use PHPUnit\Framework\AssertionFailedError; use Sinnbeck\DomAssertions\Asserts\AssertElement; +it('assertElement alias works for assertElementExists', function () { + $this->view('nesting') + ->assertElement('body', function (AssertElement $assert) { + $assert->is('body'); + }); +}); + it('can handle an empty view', function () { $this->view('empty') ->assertElementExists();