EmailHeaderSame.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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\RawMessage;
  13. final class EmailHeaderSame extends Constraint
  14. {
  15. private $headerName;
  16. private $expectedValue;
  17. public function __construct(string $headerName, string $expectedValue)
  18. {
  19. $this->headerName = $headerName;
  20. $this->expectedValue = $expectedValue;
  21. }
  22. /**
  23. * {@inheritdoc}
  24. */
  25. public function toString(): string
  26. {
  27. return sprintf('has header "%s" with value "%s"', $this->headerName, $this->expectedValue);
  28. }
  29. /**
  30. * @param RawMessage $message
  31. *
  32. * {@inheritdoc}
  33. */
  34. protected function matches($message): bool
  35. {
  36. if (RawMessage::class === \get_class($message)) {
  37. throw new \LogicException('Unable to test a message header on a RawMessage instance.');
  38. }
  39. return $this->expectedValue === $message->getHeaders()->get($this->headerName)->getBodyAsString();
  40. }
  41. /**
  42. * @param RawMessage $message
  43. *
  44. * {@inheritdoc}
  45. */
  46. protected function failureDescription($message): string
  47. {
  48. return sprintf('the Email %s (value is %s)', $this->toString(), $message->getHeaders()->get($this->headerName)->getBodyAsString());
  49. }
  50. }