EmailHtmlBodyContains.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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\Mime\Test\Constraint;
  11. use PHPUnit\Framework\Constraint\Constraint;
  12. use Symfony\Component\Mime\Message;
  13. use Symfony\Component\Mime\RawMessage;
  14. final class EmailHtmlBodyContains extends Constraint
  15. {
  16. private $expectedText;
  17. public function __construct(string $expectedText)
  18. {
  19. $this->expectedText = $expectedText;
  20. }
  21. /**
  22. * {@inheritdoc}
  23. */
  24. public function toString(): string
  25. {
  26. return sprintf('contains "%s"', $this->expectedText);
  27. }
  28. /**
  29. * {@inheritdoc}
  30. *
  31. * @param RawMessage $message
  32. */
  33. protected function matches($message): bool
  34. {
  35. if (RawMessage::class === \get_class($message) || Message::class === \get_class($message)) {
  36. throw new \LogicException('Unable to test a message HTML body on a RawMessage or Message instance.');
  37. }
  38. return false !== mb_strpos($message->getHtmlBody(), $this->expectedText);
  39. }
  40. /**
  41. * {@inheritdoc}
  42. *
  43. * @param RawMessage $message
  44. */
  45. protected function failureDescription($message): string
  46. {
  47. return 'the Email HTML body '.$this->toString();
  48. }
  49. }