From b9048157097876d53f6f7a10f23640882495962d Mon Sep 17 00:00:00 2001 From: u01jmg3 Date: Sun, 10 Oct 2021 15:27:40 +0100 Subject: [PATCH] Add new assertion `assertAttributeContains()` --- src/Concerns/MakesAssertions.php | 28 ++++++++++++++++++++++ tests/MakesAssertionsTest.php | 40 ++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) diff --git a/src/Concerns/MakesAssertions.php b/src/Concerns/MakesAssertions.php index 922cd8650..aeeacdc24 100644 --- a/src/Concerns/MakesAssertions.php +++ b/src/Concerns/MakesAssertions.php @@ -658,6 +658,34 @@ public function assertAttribute($selector, $attribute, $value) return $this; } + /** + * Assert that the element matching the given selector contains the given value in the provided attribute. + * + * @param string $selector + * @param string $attribute + * @param string $value + * @return $this + */ + public function assertAttributeContains($selector, $attribute, $value) + { + $fullSelector = $this->resolver->format($selector); + + $actual = $this->resolver->findOrFail($selector)->getAttribute($attribute); + + PHPUnit::assertNotNull( + $actual, + "Did not see expected attribute [{$attribute}] within element [{$fullSelector}]." + ); + + PHPUnit::assertStringContainsString( + $value, + $actual, + "Expected '$attribute' attribute [{$value}] is not part of the value [$actual]." + ); + + return $this; + } + /** * Assert that the element matching the given selector has the given value in the provided aria attribute. * diff --git a/tests/MakesAssertionsTest.php b/tests/MakesAssertionsTest.php index 277816dd0..22588e3a9 100644 --- a/tests/MakesAssertionsTest.php +++ b/tests/MakesAssertionsTest.php @@ -523,6 +523,46 @@ public function test_assert_attribute() } } + public function test_assert_attribute_contains() + { + $driver = m::mock(stdClass::class); + + $element = m::mock(stdClass::class); + $element->shouldReceive('getAttribute')->with('bar')->andReturn( + 'class-a class-b', + null, + 'class-1 class-2' + ); + + $resolver = m::mock(stdClass::class); + $resolver->shouldReceive('format')->with('foo')->andReturn('Foo'); + $resolver->shouldReceive('findOrFail')->with('foo')->andReturn($element); + + $browser = new Browser($driver, $resolver); + + $browser->assertAttributeContains('foo', 'bar', 'class-b'); + + try { + $browser->assertAttributeContains('foo', 'bar', 'class-b'); + $this->fail(); + } catch (ExpectationFailedException $e) { + $this->assertStringContainsString( + 'Did not see expected attribute [bar] within element [Foo].', + $e->getMessage() + ); + } + + try { + $browser->assertAttributeContains('foo', 'bar', 'class-b'); + $this->fail(); + } catch (ExpectationFailedException $e) { + $this->assertStringContainsString( + "Expected 'bar' attribute [class-b] is not part of the value [class-1 class-2].", + $e->getMessage() + ); + } + } + public function test_assert_data_attribute() { $driver = m::mock(stdClass::class);