Skip to content

Commit ffcc045

Browse files
authored
Add assertContainsElement (#35)
1 parent 7da251b commit ffcc045

File tree

9 files changed

+317
-1
lines changed

9 files changed

+317
-1
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,15 @@ $this->get('/some-route')
192192
});
193193
```
194194

195+
196+
For simple and quick checks, you can use `assertContainsElement`.
197+
This method allows you to verify that a specific element exists on the page you can optionally include an array of expected attributes.
198+
```
199+
$this->get('/some-route')
200+
->assertContainsElement('#content')
201+
->assertContainsElement('div.banner', ['text' => 'Successfully deleted', 'data-status' => 'success']);
202+
```
203+
195204
### Testing forms
196205
Testing forms allows using all the dom asserts from above, but has a few special helpers to help test for forms.
197206
Instead of using `->assertElementExists()` you can use `->assertFormExists()`, or the alias `assertForm()` on the test response.

ide-helper.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ public function assertFormExists($selector = 'form', $callback = null)
3232
/** @var \Illuminate\Testing\TestResponse $instance */
3333
return $instance;
3434
}
35+
36+
public function assertContainsElement($selector, array $attributes = [])
37+
{
38+
/** @var \Illuminate\Testing\TestResponse $instance */
39+
return $instance;
40+
}
3541
}
3642

3743
class TestView
@@ -65,6 +71,12 @@ public function assertFormExists($selector = 'form', $callback = null)
6571
/** @var \Illuminate\Testing\TestResponse $instance */
6672
return $instance;
6773
}
74+
75+
public function assertContainsElement($selector, array $attributes = [])
76+
{
77+
/** @var \Illuminate\Testing\TestResponse $instance */
78+
return $instance;
79+
}
6880
}
6981

7082
class TestComponent
@@ -98,5 +110,11 @@ public function assertFormExists($selector = 'form', $callback = null)
98110
/** @var \Illuminate\Testing\TestResponse $instance */
99111
return $instance;
100112
}
113+
114+
public function assertContainsElement($selector, array $attributes = [])
115+
{
116+
/** @var \Illuminate\Testing\TestComponent $instance */
117+
return $instance;
118+
}
101119
}
102120
}

src/TestComponentMacros.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Sinnbeck\DomAssertions;
66

77
use Closure;
8+
use DOMElement;
89
use DOMException;
910
use Illuminate\Testing\TestComponent;
1011
use PHPUnit\Framework\Assert;
@@ -85,6 +86,74 @@ public function assertElementExists(): Closure
8586
};
8687
}
8788

89+
public function assertContainsElement(): Closure
90+
{
91+
return function (string $selector, array $attributes = []): TestComponent {
92+
/** @var TestComponent $this */
93+
Assert::assertNotEmpty(
94+
(string) $this,
95+
'The component is empty!'
96+
);
97+
98+
try {
99+
$parser = DomParser::new((string) $this);
100+
} catch (DOMException $exception) {
101+
Assert::fail($exception->getMessage());
102+
}
103+
104+
$element = $parser->query($selector);
105+
106+
Assert::assertNotNull(
107+
$element,
108+
sprintf('No element found with selector: %s', $selector)
109+
);
110+
111+
if (! $element instanceof DOMElement) {
112+
Assert::fail('The element found is not a DOMElement!');
113+
}
114+
115+
foreach ($attributes as $attribute => $expected) {
116+
switch ($attribute) {
117+
case 'text':
118+
$actual = trim($element->textContent);
119+
Assert::assertStringContainsString(
120+
$expected,
121+
$actual,
122+
sprintf(
123+
'Failed asserting that element [%s] text contains "%s". Actual: "%s".',
124+
$selector,
125+
$expected,
126+
$actual
127+
)
128+
);
129+
break;
130+
131+
default:
132+
$actual = $element->getAttribute($attribute);
133+
Assert::assertNotEmpty(
134+
$actual,
135+
sprintf('Attribute [%s] not found in element [%s].', $attribute, $selector)
136+
);
137+
138+
Assert::assertStringContainsString(
139+
$expected,
140+
$actual,
141+
sprintf(
142+
'Failed asserting that attribute [%s] of element [%s] contains "%s". Actual: "%s".',
143+
$attribute,
144+
$selector,
145+
$expected,
146+
$actual
147+
)
148+
);
149+
break;
150+
}
151+
}
152+
153+
return $this;
154+
};
155+
}
156+
88157
public function assertForm(): Closure
89158
{
90159
return $this->assertFormExists();

src/TestResponseMacros.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Sinnbeck\DomAssertions;
66

77
use Closure;
8+
use DOMElement;
89
use DOMException;
910
use Illuminate\Testing\TestResponse;
1011
use PHPUnit\Framework\Assert;
@@ -85,6 +86,74 @@ public function assertElementExists(): Closure
8586
};
8687
}
8788

89+
public function assertContainsElement(): Closure
90+
{
91+
return function (string $selector, array $attributes = []): TestResponse {
92+
/** @var TestResponse $this */
93+
Assert::assertNotEmpty(
94+
(string) $this->getContent(),
95+
'The response is empty!'
96+
);
97+
98+
try {
99+
$parser = DomParser::new($this->getContent());
100+
} catch (DOMException $exception) {
101+
Assert::fail($exception->getMessage());
102+
}
103+
104+
$element = $parser->query($selector);
105+
106+
Assert::assertNotNull(
107+
$element,
108+
sprintf('No element found with selector: %s', $selector)
109+
);
110+
111+
if (! $element instanceof DOMElement) {
112+
Assert::fail('The element found is not a DOMElement!');
113+
}
114+
115+
foreach ($attributes as $attribute => $expected) {
116+
switch ($attribute) {
117+
case 'text':
118+
$actual = trim($element->textContent);
119+
Assert::assertStringContainsString(
120+
$expected,
121+
$actual,
122+
sprintf(
123+
'Failed asserting that element [%s] text contains "%s". Actual: "%s".',
124+
$selector,
125+
$expected,
126+
$actual
127+
)
128+
);
129+
break;
130+
131+
default:
132+
$actual = $element->getAttribute($attribute);
133+
Assert::assertNotEmpty(
134+
$actual,
135+
sprintf('Attribute [%s] not found in element [%s].', $attribute, $selector)
136+
);
137+
138+
Assert::assertStringContainsString(
139+
$expected,
140+
$actual,
141+
sprintf(
142+
'Failed asserting that attribute [%s] of element [%s] contains "%s". Actual: "%s".',
143+
$attribute,
144+
$selector,
145+
$expected,
146+
$actual
147+
)
148+
);
149+
break;
150+
}
151+
}
152+
153+
return $this;
154+
};
155+
}
156+
88157
public function assertForm(): Closure
89158
{
90159
return $this->assertFormExists();

src/TestViewMacros.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Sinnbeck\DomAssertions;
66

77
use Closure;
8+
use DOMElement;
89
use DOMException;
910
use Illuminate\Testing\TestView;
1011
use PHPUnit\Framework\Assert;
@@ -85,6 +86,75 @@ public function assertElementExists(): Closure
8586
};
8687
}
8788

89+
public function assertContainsElement(): Closure
90+
{
91+
return function (string $selector, array $attributes = []): TestView {
92+
/** @var TestView $this */
93+
Assert::assertNotEmpty(
94+
(string) $this,
95+
'The view is empty!'
96+
);
97+
98+
try {
99+
$parser = DomParser::new((string) $this);
100+
} catch (DOMException $exception) {
101+
Assert::fail($exception->getMessage());
102+
}
103+
104+
$element = $parser->query($selector);
105+
106+
Assert::assertNotNull(
107+
$element,
108+
sprintf('No element found with selector: %s', $selector)
109+
);
110+
111+
if (! $element instanceof DOMElement) {
112+
Assert::fail('The element found is not a DOMElement!');
113+
}
114+
115+
foreach ($attributes as $attribute => $expected) {
116+
switch ($attribute) {
117+
case 'text':
118+
$actual = trim($element->textContent);
119+
Assert::assertStringContainsString(
120+
$expected,
121+
$actual,
122+
sprintf(
123+
'Failed asserting that element [%s] text contains "%s". Actual: "%s".',
124+
$selector,
125+
$expected,
126+
$actual
127+
)
128+
);
129+
break;
130+
131+
default:
132+
$actual = $element->getAttribute($attribute);
133+
134+
Assert::assertNotEmpty(
135+
$actual,
136+
sprintf('Attribute [%s] not found in element [%s].', $attribute, $selector)
137+
);
138+
139+
Assert::assertStringContainsString(
140+
$expected,
141+
$actual,
142+
sprintf(
143+
'Failed asserting that attribute [%s] of element [%s] contains "%s". Actual: "%s".',
144+
$attribute,
145+
$selector,
146+
$expected,
147+
$actual
148+
)
149+
);
150+
break;
151+
}
152+
}
153+
154+
return $this;
155+
};
156+
}
157+
88158
public function assertForm(): Closure
89159
{
90160
return $this->assertFormExists();

tests/ComponentTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,27 @@
2323
});
2424
});
2525

26+
it('assertContainsElement works as expects', function () {
27+
$this->component(NestedComponent::class)
28+
->assertContainsElement('span.foo', ['text' => 'Foo', 'class' => 'bar foo'])
29+
->assertContainsElement('nav');
30+
});
31+
32+
it('assertContainsElement throws if selector not found', function () {
33+
$this->component(NestedComponent::class)
34+
->assertContainsElement('span.non-existing', ['text' => 'Foo']);
35+
})->throws(AssertionFailedError::class, 'No element found with selector: span.non-existing');
36+
37+
it('assertContainsElement throws if contains text does not exist', function () {
38+
$this->component(NestedComponent::class)
39+
->assertContainsElement('span.foo', ['text' => 'non-existing']);
40+
})->throws(AssertionFailedError::class, 'Failed asserting that element [span.foo] text contains "non-existing". Actual: "Foo"');
41+
42+
it('assertContainsElement throws if contains attribute does not exist', function () {
43+
$this->view('nesting')
44+
->assertContainsElement('span.foo', ['non-existing-attribute' => 'non-existing']);
45+
})->throws(AssertionFailedError::class, 'Attribute [non-existing-attribute] not found in element [span.foo]');
46+
2647
it('can handle an empty component', function () {
2748
$this->component(EmptyComponent::class)
2849
->assertElementExists();

tests/DomTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,27 @@
1010
});
1111
});
1212

13+
it('assertContainsElement works as expects', function () {
14+
$this->get('nesting')
15+
->assertContainsElement('span.foo', ['text' => 'Foo', 'class' => 'bar foo'])
16+
->assertContainsElement('nav');
17+
});
18+
19+
it('assertContainsElement throws if selector not found', function () {
20+
$this->get('nesting')
21+
->assertContainsElement('span.non-existing', ['text' => 'Foo']);
22+
})->throws(AssertionFailedError::class, 'No element found with selector: span.non-existing');
23+
24+
it('assertContainsElement throws if contains text does not exist', function () {
25+
$this->get('nesting')
26+
->assertContainsElement('span.foo', ['text' => 'non-existing']);
27+
})->throws(AssertionFailedError::class, 'Failed asserting that element [span.foo] text contains "non-existing". Actual: "Foo"');
28+
29+
it('assertContainsElement throws if contains attribute does not exist', function () {
30+
$this->view('nesting')
31+
->assertContainsElement('span.foo', ['non-existing-attribute' => 'non-existing']);
32+
})->throws(AssertionFailedError::class, 'Attribute [non-existing-attribute] not found in element [span.foo]');
33+
1334
it('can handle an empty view', function () {
1435
$this->get('empty')
1536
->assertElementExists();

tests/ViewTest.php

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

6+
it('assertContainsElement works as expects', function () {
7+
$this->view('nesting')
8+
->assertContainsElement('span.foo', ['text' => 'Foo'])
9+
->assertContainsElement('nav');
10+
});
11+
12+
it('assertContainsElement throws if selector not found', function () {
13+
$this->view('nesting')
14+
->assertContainsElement('span.non-existing', ['text' => 'Foo']);
15+
})->throws(AssertionFailedError::class, 'No element found with selector: span.non-existing');
16+
17+
it('assertContainsElement throws if contains text does not exist', function () {
18+
$this->view('nesting')
19+
->assertContainsElement('span.foo', ['text' => 'non-existing']);
20+
})->throws(AssertionFailedError::class, 'Failed asserting that element [span.foo] text contains "non-existing". Actual: "Foo"');
21+
22+
it('assertContainsElement throws if contains attribute does not exist', function () {
23+
$this->view('nesting')
24+
->assertContainsElement('span.foo', ['non-existing-attribute' => 'non-existing']);
25+
})->throws(AssertionFailedError::class, 'Attribute [non-existing-attribute] not found in element [span.foo]');
26+
627
it('assertElement alias works for assertElementExists', function () {
728
$this->view('nesting')
829
->assertElement('body', function (AssertElement $assert) {

0 commit comments

Comments
 (0)