EmailTextBodyContains.php 1.3 KB

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