Skip to content

Commit 13de4ad

Browse files
authored
Add assertElement and assertForm aliases (#29)
* Add assertElement and assertForm aliases
1 parent 7fa158d commit 13de4ad

File tree

9 files changed

+117
-2
lines changed

9 files changed

+117
-2
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ $this->get(route('about'))
6262
## Usage
6363

6464
### Testing the DOM
65-
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.
65+
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()`.
6666
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!
6767
```php
6868
$this->get('/some-route')
@@ -194,7 +194,7 @@ $this->get('/some-route')
194194

195195
### Testing forms
196196
Testing forms allows using all the dom asserts from above, but has a few special helpers to help test for forms.
197-
Instead of using `->assertElementExists()` we will use `->assertFormExists()` method on the test response.
197+
Instead of using `->assertElementExists()` you can use `->assertFormExists()`, or the alias `assertForm()` on the test response.
198198
```php
199199
$this->get('/some-route')
200200
->assertFormExists();

ide-helper.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,24 @@ public function assertHtml5()
99
return $instance;
1010
}
1111

12+
public function assertElement($selector = 'body', $callback = null)
13+
{
14+
/** @var \Illuminate\Testing\TestResponse $instance */
15+
return $instance;
16+
}
17+
1218
public function assertElementExists($selector = 'body', $callback = null)
1319
{
1420
/** @var \Illuminate\Testing\TestResponse $instance */
1521
return $instance;
1622
}
1723

24+
public function assertForm($selector = 'body', $callback = null)
25+
{
26+
/** @var \Illuminate\Testing\TestResponse $instance */
27+
return $instance;
28+
}
29+
1830
public function assertFormExists($selector = 'form', $callback = null)
1931
{
2032
/** @var \Illuminate\Testing\TestResponse $instance */
@@ -30,12 +42,57 @@ public function assertHtml5()
3042
return $instance;
3143
}
3244

45+
public function assertElement($selector = 'body', $callback = null)
46+
{
47+
/** @var \Illuminate\Testing\TestResponse $instance */
48+
return $instance;
49+
}
50+
51+
public function assertElementExists($selector = 'body', $callback = null)
52+
{
53+
/** @var \Illuminate\Testing\TestResponse $instance */
54+
return $instance;
55+
}
56+
57+
public function assertForm($selector = 'form', $callback = null)
58+
{
59+
/** @var \Illuminate\Testing\TestResponse $instance */
60+
return $instance;
61+
}
62+
63+
public function assertFormExists($selector = 'form', $callback = null)
64+
{
65+
/** @var \Illuminate\Testing\TestResponse $instance */
66+
return $instance;
67+
}
68+
}
69+
70+
class TestComponent
71+
{
72+
public function assertHtml5()
73+
{
74+
/** @var \Illuminate\Testing\TestResponse $instance */
75+
return $instance;
76+
}
77+
78+
public function assertElement($selector = 'body', $callback = null)
79+
{
80+
/** @var \Illuminate\Testing\TestResponse $instance */
81+
return $instance;
82+
}
83+
3384
public function assertElementExists($selector = 'body', $callback = null)
3485
{
3586
/** @var \Illuminate\Testing\TestResponse $instance */
3687
return $instance;
3788
}
3889

90+
public function assertForm($selector = 'form', $callback = null)
91+
{
92+
/** @var \Illuminate\Testing\TestResponse $instance */
93+
return $instance;
94+
}
95+
3996
public function assertFormExists($selector = 'form', $callback = null)
4097
{
4198
/** @var \Illuminate\Testing\TestResponse $instance */

src/TestComponentMacros.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ public function assertHtml5(): Closure
4444
};
4545
}
4646

47+
public function assertElement(): Closure
48+
{
49+
return $this->assertElementExists();
50+
}
51+
4752
public function assertElementExists(): Closure
4853
{
4954
return function ($selector = 'body', $callback = null): TestComponent {
@@ -80,6 +85,11 @@ public function assertElementExists(): Closure
8085
};
8186
}
8287

88+
public function assertForm(): Closure
89+
{
90+
return $this->assertFormExists();
91+
}
92+
8393
public function assertFormExists(): Closure
8494
{
8595
return function ($selector = 'form', $callback = null): TestComponent {

src/TestResponseMacros.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ public function assertHtml5(): Closure
4444
};
4545
}
4646

47+
public function assertElement(): Closure
48+
{
49+
return $this->assertElementExists();
50+
}
51+
4752
public function assertElementExists(): Closure
4853
{
4954
return function ($selector = 'body', $callback = null): TestResponse {
@@ -80,6 +85,11 @@ public function assertElementExists(): Closure
8085
};
8186
}
8287

88+
public function assertForm(): Closure
89+
{
90+
return $this->assertFormExists();
91+
}
92+
8393
public function assertFormExists(): Closure
8494
{
8595
return function ($selector = 'form', $callback = null): TestResponse {

src/TestViewMacros.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ public function assertHtml5(): Closure
4444
};
4545
}
4646

47+
public function assertElement(): Closure
48+
{
49+
return $this->assertElementExists();
50+
}
51+
4752
public function assertElementExists(): Closure
4853
{
4954
return function ($selector = 'body', $callback = null): TestView {
@@ -80,6 +85,11 @@ public function assertElementExists(): Closure
8085
};
8186
}
8287

88+
public function assertForm(): Closure
89+
{
90+
return $this->assertFormExists();
91+
}
92+
8393
public function assertFormExists(): Closure
8494
{
8595
return function ($selector = 'form', $callback = null): TestView {

tests/ComponentTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@
1616
}
1717
});
1818

19+
it('assertElement alias works for assertElementExists', function () {
20+
$this->component(NestedComponent::class)
21+
->assertElement('body', function (AssertElement $assert) {
22+
$assert->is('body');
23+
});
24+
});
25+
1926
it('can handle an empty component', function () {
2027
$this->component(EmptyComponent::class)
2128
->assertElementExists();

tests/DomTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@
33
use PHPUnit\Framework\AssertionFailedError;
44
use Sinnbeck\DomAssertions\Asserts\AssertElement;
55

6+
it('assertAlias alias works for assertElementExists', function () {
7+
$this->get('nesting')
8+
->assertElementExists('body', function (AssertElement $assert) {
9+
$assert->is('body');
10+
});
11+
});
12+
613
it('can handle an empty view', function () {
714
$this->get('empty')
815
->assertElementExists();

tests/FormTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@
66
use Sinnbeck\DomAssertions\Asserts\AssertForm;
77
use Sinnbeck\DomAssertions\Asserts\AssertSelect;
88

9+
it('assertForm alias works for assertFormExists', function () {
10+
$this->get('form')
11+
->assertForm('form:nth-child(2)', function (AssertForm $form) {
12+
$form->hasAction('form');
13+
});
14+
});
15+
916
it('can find a form by default', function () {
1017
$this->get('form')
1118
->assertFormExists();

tests/ViewTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@
33
use PHPUnit\Framework\AssertionFailedError;
44
use Sinnbeck\DomAssertions\Asserts\AssertElement;
55

6+
it('assertElement alias works for assertElementExists', function () {
7+
$this->view('nesting')
8+
->assertElement('body', function (AssertElement $assert) {
9+
$assert->is('body');
10+
});
11+
});
12+
613
it('can handle an empty view', function () {
714
$this->view('empty')
815
->assertElementExists();

0 commit comments

Comments
 (0)