You are here

public function WebAssert::pageTextMatchesCount in Drupal 10

Same name and namespace in other branches
  1. 9 core/tests/Drupal/Tests/WebAssert.php \Drupal\Tests\WebAssert::pageTextMatchesCount()

Asserts that the current page text matches regex a number of times.

Parameters

int $count: The number of times the pattern is expected to be present.

string $regex: The regex pattern.

string $message: (Optional) the failure message.

File

core/tests/Drupal/Tests/WebAssert.php, line 116

Class

WebAssert
Defines a class with methods for asserting presence of elements during tests.

Namespace

Drupal\Tests

Code

public function pageTextMatchesCount(int $count, string $regex, string $message = '') : void {
  $actual = preg_replace('/\\s+/u', ' ', $this->session
    ->getPage()
    ->getText());
  $matches = preg_match_all($regex, $actual);
  if ($message === '') {
    $message = "Failed asserting that the page matches the pattern '{$regex}' {$count} time(s), {$matches} found.";
  }
  $constraint = new IsIdentical($count);
  Assert::assertThat($matches, $constraint, $message);
}