| 148 |
lars |
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 |
|
|
|
12 |
namespace Symfony\Component\Mime\Test\Constraint;
|
|
|
13 |
|
|
|
14 |
use PHPUnit\Framework\Constraint\Constraint;
|
|
|
15 |
use Symfony\Component\Mime\Header\MailboxHeader;
|
|
|
16 |
use Symfony\Component\Mime\Header\MailboxListHeader;
|
|
|
17 |
use Symfony\Component\Mime\RawMessage;
|
|
|
18 |
|
|
|
19 |
final class EmailAddressContains extends Constraint
|
|
|
20 |
{
|
|
|
21 |
private string $headerName;
|
|
|
22 |
private string $expectedValue;
|
|
|
23 |
|
|
|
24 |
public function __construct(string $headerName, string $expectedValue)
|
|
|
25 |
{
|
|
|
26 |
$this->headerName = $headerName;
|
|
|
27 |
$this->expectedValue = $expectedValue;
|
|
|
28 |
}
|
|
|
29 |
|
|
|
30 |
public function toString(): string
|
|
|
31 |
{
|
|
|
32 |
return sprintf('contains address "%s" with value "%s"', $this->headerName, $this->expectedValue);
|
|
|
33 |
}
|
|
|
34 |
|
|
|
35 |
/**
|
|
|
36 |
* @param RawMessage $message
|
|
|
37 |
*/
|
|
|
38 |
protected function matches($message): bool
|
|
|
39 |
{
|
|
|
40 |
if (RawMessage::class === $message::class) {
|
|
|
41 |
throw new \LogicException('Unable to test a message address on a RawMessage instance.');
|
|
|
42 |
}
|
|
|
43 |
|
|
|
44 |
$header = $message->getHeaders()->get($this->headerName);
|
|
|
45 |
if ($header instanceof MailboxHeader) {
|
|
|
46 |
return $this->expectedValue === $header->getAddress()->getAddress();
|
|
|
47 |
} elseif ($header instanceof MailboxListHeader) {
|
|
|
48 |
foreach ($header->getAddresses() as $address) {
|
|
|
49 |
if ($this->expectedValue === $address->getAddress()) {
|
|
|
50 |
return true;
|
|
|
51 |
}
|
|
|
52 |
}
|
|
|
53 |
|
|
|
54 |
return false;
|
|
|
55 |
}
|
|
|
56 |
|
|
|
57 |
throw new \LogicException('Unable to test a message address on a non-address header.');
|
|
|
58 |
}
|
|
|
59 |
|
|
|
60 |
/**
|
|
|
61 |
* @param RawMessage $message
|
|
|
62 |
*/
|
|
|
63 |
protected function failureDescription($message): string
|
|
|
64 |
{
|
|
|
65 |
return sprintf('the Email %s (value is %s)', $this->toString(), $message->getHeaders()->get($this->headerName)->getBodyAsString());
|
|
|
66 |
}
|
|
|
67 |
}
|