Skip to content

Commit 94534fa

Browse files
ziadoztaylorotwell
andauthoredDec 5, 2023
Add attribute does not contain assertion (#1072)
* assert attr does not contain * formatting --------- Co-authored-by: Taylor Otwell <taylor@laravel.com>
1 parent e1ca93d commit 94534fa

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed
 

‎src/Concerns/MakesAssertions.php

+41
Original file line numberDiff line numberDiff line change
@@ -792,6 +792,34 @@ public function assertAttributeContains($selector, $attribute, $value)
792792
return $this;
793793
}
794794

795+
/**
796+
* Assert that the element matching the given selector does not contain the given value in the provided attribute.
797+
*
798+
* @param string $selector
799+
* @param string $attribute
800+
* @param string $value
801+
* @return $this
802+
*/
803+
public function assertAttributeDoesntContain($selector, $attribute, $value)
804+
{
805+
$fullSelector = $this->resolver->format($selector);
806+
807+
$actual = $this->resolver->findOrFail($selector)->getAttribute($attribute);
808+
809+
PHPUnit::assertNotNull(
810+
$actual,
811+
"Did not see expected attribute [{$attribute}] within element [{$fullSelector}]."
812+
);
813+
814+
PHPUnit::assertStringNotContainsString(
815+
$value,
816+
$actual,
817+
"Attribute '$attribute' contains [{$value}]. Full attribute value was [$actual]."
818+
);
819+
820+
return $this;
821+
}
822+
795823
/**
796824
* Assert that the element matching the given selector has the given value in the provided aria attribute.
797825
*
@@ -1087,6 +1115,19 @@ public function assertVueContains($key, $value, $componentSelector = null)
10871115
return $this;
10881116
}
10891117

1118+
/**
1119+
* Assert that a given Vue component data property is an array and does not contain the given value.
1120+
*
1121+
* @param string $key
1122+
* @param string $value
1123+
* @param string|null $componentSelector
1124+
* @return $this
1125+
*/
1126+
public function assertVueDoesntContain($key, $value, $componentSelector = null)
1127+
{
1128+
return $this->assertVueDoesNotContain($key, $value, $componentSelector);
1129+
}
1130+
10901131
/**
10911132
* Assert that a given Vue component data property is an array and does not contain the given value.
10921133
*

‎tests/Unit/MakesAssertionsTest.php

+40
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,46 @@ public function test_assert_attribute_contains()
635635
}
636636
}
637637

638+
public function test_assert_attribute_does_not_contain()
639+
{
640+
$driver = m::mock(stdClass::class);
641+
642+
$element = m::mock(stdClass::class);
643+
$element->shouldReceive('getAttribute')->with('bar')->andReturn(
644+
'class-a class-b',
645+
null,
646+
'class-1 class-2'
647+
);
648+
649+
$resolver = m::mock(stdClass::class);
650+
$resolver->shouldReceive('format')->with('foo')->andReturn('Foo');
651+
$resolver->shouldReceive('findOrFail')->with('foo')->andReturn($element);
652+
653+
$browser = new Browser($driver, $resolver);
654+
655+
$browser->assertAttributeDoesntContain('foo', 'bar', 'class-c');
656+
657+
try {
658+
$browser->assertAttributeDoesntContain('foo', 'bar', 'class-c');
659+
$this->fail();
660+
} catch (ExpectationFailedException $e) {
661+
$this->assertStringContainsString(
662+
'Did not see expected attribute [bar] within element [Foo].',
663+
$e->getMessage()
664+
);
665+
}
666+
667+
try {
668+
$browser->assertAttributeDoesntContain('foo', 'bar', 'class-1');
669+
$this->fail();
670+
} catch (ExpectationFailedException $e) {
671+
$this->assertStringContainsString(
672+
"Attribute 'bar' contains [class-1]. Full attribute value was [class-1 class-2].",
673+
$e->getMessage()
674+
);
675+
}
676+
}
677+
638678
public function test_assert_data_attribute()
639679
{
640680
$driver = m::mock(stdClass::class);

0 commit comments

Comments
 (0)