EmailHtmlBodyContains.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. public function __construct(
  17. private string $expectedText,
  18. ) {
  19. }
  20. public function toString(): string
  21. {
  22. return sprintf('contains "%s"', $this->expectedText);
  23. }
  24. /**
  25. * @param RawMessage $message
  26. */
  27. protected function matches($message): bool
  28. {
  29. if (RawMessage::class === $message::class || Message::class === $message::class) {
  30. throw new \LogicException('Unable to test a message HTML body on a RawMessage or Message instance.');
  31. }
  32. return str_contains($message->getHtmlBody(), $this->expectedText);
  33. }
  34. /**
  35. * @param RawMessage $message
  36. */
  37. protected function failureDescription($message): string
  38. {
  39. return 'the Email HTML body '.$this->toString();
  40. }
  41. }