Skip to content

Commit 7b3beea

Browse files
committed
add doesnt have assertions
1 parent 31dee21 commit 7b3beea

File tree

3 files changed

+69
-1
lines changed

3 files changed

+69
-1
lines changed

src/Asserts/Traits/UsesElementAsserts.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,30 @@ public function has(string $attribute, mixed $value = null): self
3535
return $this;
3636
}
3737

38+
public function doesntHave(string $attribute, mixed $value = null): self
39+
{
40+
41+
if (! $value) {
42+
PHPUnit::assertFalse(
43+
$this->hasAttribute($attribute),
44+
sprintf('Found an attribute "%s"', $attribute)
45+
);
46+
47+
return $this;
48+
}
49+
50+
PHPUnit::assertFalse(
51+
CompareAttributes::compare(
52+
$attribute,
53+
$value,
54+
$this->getAttribute($attribute)
55+
),
56+
sprintf('Found an attribute "%s" with value "%s"', $attribute, $value)
57+
);
58+
59+
return $this;
60+
}
61+
3862
public function find(string $selector, $callback = null): self
3963
{
4064
Assert::assertNotNull(

tests/DomTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,50 @@
7474
});
7575
});
7676

77+
it('can fail finding a class', function () {
78+
$this->get('nesting')
79+
->assertElementExists('body', function (AssertElement $assert) {
80+
$assert->find('#nav', function (AssertElement $element) {
81+
$element->doesntHave('class');
82+
});
83+
});
84+
});
85+
86+
87+
it('can fail finding a href with exact match', function () {
88+
$this->get('nesting')
89+
->assertElementExists('body', function (AssertElement $assert) {
90+
$assert->find('#nav a', function (AssertElement $element) {
91+
$element->has('href')
92+
->doesntHave('href', '/bar');
93+
});
94+
});
95+
});
96+
97+
it('can fail when finding a id that isnt expected', function () {
98+
$this->get('nesting')
99+
->assertElementExists('body', function (AssertElement $assert) {
100+
$assert->find('#nav', function (AssertElement $element) {
101+
$element->doesntHave('id');
102+
});
103+
});
104+
})->throws(
105+
AssertionFailedError::class,
106+
'Found an attribute "id"'
107+
);
108+
109+
it('can fail when finding a href with matching value that isnt expected', function () {
110+
$this->get('nesting')
111+
->assertElementExists('body', function (AssertElement $assert) {
112+
$assert->find('#nav a', function (AssertElement $element) {
113+
$element->doesntHave('href', '/foo');
114+
});
115+
});
116+
})->throws(
117+
AssertionFailedError::class,
118+
'Found an attribute "href" with value "/foo"'
119+
);
120+
77121
it('can find an element by selector', function () {
78122
$this->get('nesting')
79123
->assertElementExists('#nav');

tests/views/nesting.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<title>Nesting</title>
99
</head>
1010
<body>
11-
<nav id="nav"></nav>
11+
<nav id="nav"><a href="/foo">Foo</a></nav>
1212
<div>
1313
<span class="bar foo">Foo</span>
1414
<div class="foobar">

0 commit comments

Comments
 (0)