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 CrawlerAnySelectorTextContains extends Constraint
18{
19 private bool $hasNode = false;
20
21 public function __construct(
22 private string $selector,
23 private string $expectedText,
24 ) {
25 }
26
27 public function toString(): string
28 {
29 if ($this->hasNode) {
30 return \sprintf('the text of any node matching selector "%s" contains "%s"', $this->selector, $this->expectedText);
31 }
32
33 return \sprintf('the Crawler has a node matching selector "%s"', $this->selector);
34 }
35
36 protected function matches($other): bool
37 {
38 if (!$other instanceof Crawler) {
39 throw new \InvalidArgumentException(\sprintf('"%s" constraint expected an argument of type "%s", got "%s".', self::class, Crawler::class, get_debug_type($other)));
40 }
41
42 $other = $other->filter($this->selector);
43 if (!\count($other)) {
44 $this->hasNode = false;
45
46 return false;
47 }
48
49 $this->hasNode = true;
50
51 $nodes = $other->each(fn (Crawler $node) => $node->text(null, true));
52 $matches = array_filter($nodes, function (string $node): bool {
53 return str_contains($node, $this->expectedText);
54 });
55
56 return 0 < \count($matches);
57 }
58
59 protected function failureDescription($other): string
60 {
61 if (!$other instanceof Crawler) {
62 throw new \InvalidArgumentException(\sprintf('"%s" constraint expected an argument of type "%s", got "%s".', self::class, Crawler::class, get_debug_type($other)));
63 }
64
65 return $this->toString();
66 }
67}