friendship ended with social-app. php is my new best friend
1<?php
2
3/*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12namespace Symfony\Component\DomCrawler\Test\Constraint;
13
14use PHPUnit\Framework\Constraint\Constraint;
15use Symfony\Component\DomCrawler\Crawler;
16
17final class CrawlerSelectorTextContains extends Constraint
18{
19 private bool $hasNode = false;
20 private string $nodeText;
21
22 public function __construct(
23 private string $selector,
24 private string $expectedText,
25 ) {
26 }
27
28 public function toString(): string
29 {
30 if ($this->hasNode) {
31 return \sprintf('the text "%s" of the node matching selector "%s" contains "%s"', $this->nodeText, $this->selector, $this->expectedText);
32 }
33
34 return \sprintf('the Crawler has a node matching selector "%s"', $this->selector);
35 }
36
37 /**
38 * @param Crawler $crawler
39 */
40 protected function matches($crawler): bool
41 {
42 $crawler = $crawler->filter($this->selector);
43 if (!\count($crawler)) {
44 $this->hasNode = false;
45
46 return false;
47 }
48
49 $this->hasNode = true;
50 $this->nodeText = $crawler->text(null, true);
51
52 return str_contains($this->nodeText, $this->expectedText);
53 }
54
55 /**
56 * @param Crawler $crawler
57 */
58 protected function failureDescription($crawler): string
59 {
60 return $this->toString();
61 }
62}