EmailSubjectContains.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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\Email;
  13. final class EmailSubjectContains extends Constraint
  14. {
  15. public function __construct(
  16. private readonly string $expectedSubjectValue,
  17. ) {
  18. }
  19. public function toString(): string
  20. {
  21. return sprintf('contains subject with value "%s"', $this->expectedSubjectValue);
  22. }
  23. protected function matches($other): bool
  24. {
  25. if (!$other instanceof Email) {
  26. throw new \LogicException('Can only test a message subject on an Email instance.');
  27. }
  28. return str_contains((string) $other->getSubject(), $this->expectedSubjectValue);
  29. }
  30. protected function failureDescription($other): string
  31. {
  32. $message = 'The email subject '.$this->toString();
  33. if ($other instanceof Email) {
  34. $message .= sprintf('. The subject was: "%s"', $other->getSubject() ?? '<empty>');
  35. }
  36. return $message;
  37. }
  38. }