Open Journal Systems  3.3.0
EmailAddressContains.php
1 <?php
2 
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11 
13 
14 use PHPUnit\Framework\Constraint\Constraint;
18 
19 final class EmailAddressContains extends Constraint
20 {
21  private $headerName;
22  private $expectedValue;
23 
24  public function __construct(string $headerName, string $expectedValue)
25  {
26  $this->headerName = $headerName;
27  $this->expectedValue = $expectedValue;
28  }
29 
33  public function toString(): string
34  {
35  return sprintf('contains address "%s" with value "%s"', $this->headerName, $this->expectedValue);
36  }
37 
43  protected function matches($message): bool
44  {
45  if (RawMessage::class === \get_class($message)) {
46  throw new \LogicException('Unable to test a message address on a RawMessage instance.');
47  }
48 
49  $header = $message->getHeaders()->get($this->headerName);
50  if ($header instanceof MailboxHeader) {
51  return $this->expectedValue === $header->getAddress()->getAddress();
52  } elseif ($header instanceof MailboxListHeader) {
53  foreach ($header->getAddresses() as $address) {
54  if ($this->expectedValue === $address->getAddress()) {
55  return true;
56  }
57  }
58 
59  return false;
60  }
61 
62  throw new \LogicException(sprintf('Unable to test a message address on a non-address header.'));
63  }
64 
70  protected function failureDescription($message): string
71  {
72  return sprintf('the Email %s (value is %s)', $this->toString(), $message->getHeaders()->get($this->headerName)->getBodyAsString());
73  }
74 }
Symfony\Component\Mime\Test\Constraint
Definition: EmailAddressContains.php:12
Symfony\Component\Mime\Test\Constraint\EmailAddressContains
Definition: EmailAddressContains.php:19
Symfony\Component\Mime\Test\Constraint\EmailAddressContains\toString
toString()
Definition: EmailAddressContains.php:33
Symfony\Component\Mime\RawMessage
Definition: RawMessage.php:19
Symfony\Component\Mime\Test\Constraint\EmailAddressContains\failureDescription
failureDescription($message)
Definition: EmailAddressContains.php:70
Symfony\Component\Mime\Test\Constraint\EmailAddressContains\matches
matches($message)
Definition: EmailAddressContains.php:43
Symfony\Component\Mime\Header\MailboxHeader
Definition: MailboxHeader.php:22
Symfony\Component\Mime\Test\Constraint\EmailAddressContains\__construct
__construct(string $headerName, string $expectedValue)
Definition: EmailAddressContains.php:24
Symfony\Component\Mime\Header\MailboxListHeader
Definition: MailboxListHeader.php:22