EmailAttachmentCount.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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 EmailAttachmentCount extends Constraint
  15. {
  16. public function __construct(
  17. private int $expectedValue,
  18. private ?string $transport = null,
  19. ) {
  20. }
  21. public function toString(): string
  22. {
  23. return sprintf('has sent "%d" attachment(s)', $this->expectedValue);
  24. }
  25. /**
  26. * @param RawMessage $message
  27. */
  28. protected function matches($message): bool
  29. {
  30. if (RawMessage::class === $message::class || Message::class === $message::class) {
  31. throw new \LogicException('Unable to test a message attachment on a RawMessage or Message instance.');
  32. }
  33. return $this->expectedValue === \count($message->getAttachments());
  34. }
  35. /**
  36. * @param RawMessage $message
  37. */
  38. protected function failureDescription($message): string
  39. {
  40. return 'the Email '.$this->toString();
  41. }
  42. }