CrawlerSelectorTextSame.php 1.4 KB

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