CrawlerSelectorAttributeValueSame.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 CrawlerSelectorAttributeValueSame extends Constraint
  14. {
  15. private $selector;
  16. private $attribute;
  17. private $expectedText;
  18. public function __construct(string $selector, string $attribute, string $expectedText)
  19. {
  20. $this->selector = $selector;
  21. $this->attribute = $attribute;
  22. $this->expectedText = $expectedText;
  23. }
  24. /**
  25. * {@inheritdoc}
  26. */
  27. public function toString(): string
  28. {
  29. return sprintf('has a node matching selector "%s" with attribute "%s" of value "%s"', $this->selector, $this->attribute, $this->expectedText);
  30. }
  31. /**
  32. * @param Crawler $crawler
  33. *
  34. * {@inheritdoc}
  35. */
  36. protected function matches($crawler): bool
  37. {
  38. $crawler = $crawler->filter($this->selector);
  39. if (!\count($crawler)) {
  40. return false;
  41. }
  42. return $this->expectedText === trim($crawler->attr($this->attribute));
  43. }
  44. /**
  45. * @param Crawler $crawler
  46. *
  47. * {@inheritdoc}
  48. */
  49. protected function failureDescription($crawler): string
  50. {
  51. return 'the Crawler '.$this->toString();
  52. }
  53. }