function WebAssert::pageTextMatchesCount

Same name in other branches
  1. 10 core/tests/Drupal/Tests/WebAssert.php \Drupal\Tests\WebAssert::pageTextMatchesCount()
  2. 11.x 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.

1 call to WebAssert::pageTextMatchesCount()
WebAssert::pageTextContainsOnce in core/tests/Drupal/Tests/WebAssert.php
Checks that current page contains text only once.

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);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.