Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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();
Expand Down
57 changes: 57 additions & 0 deletions ide-helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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 */
Expand Down
10 changes: 10 additions & 0 deletions src/TestComponentMacros.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
10 changes: 10 additions & 0 deletions src/TestResponseMacros.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
10 changes: 10 additions & 0 deletions src/TestViewMacros.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
7 changes: 7 additions & 0 deletions tests/ComponentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
7 changes: 7 additions & 0 deletions tests/DomTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
7 changes: 7 additions & 0 deletions tests/FormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
7 changes: 7 additions & 0 deletions tests/ViewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Loading