Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new assertion assertAttributeContains() #931

Merged
merged 1 commit into from
Oct 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/Concerns/MakesAssertions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
40 changes: 40 additions & 0 deletions tests/MakesAssertionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down