| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
namespace Symfony\Component\HttpFoundation\Test\Constraint; |
| 13 |
|
| 14 |
use PHPUnit\Framework\Constraint\Constraint; |
| 15 |
use Symfony\Component\HttpFoundation\Cookie; |
| 16 |
use Symfony\Component\HttpFoundation\Response; |
| 17 |
|
| 18 |
final class ResponseHasCookie extends Constraint |
| 19 |
{ |
| 20 |
public function __construct( |
| 21 |
private string $name, |
| 22 |
private string $path = '/', |
| 23 |
private ?string $domain = null, |
| 24 |
) { |
| 25 |
} |
| 26 |
|
| 27 |
public function toString(): string |
| 28 |
{ |
| 29 |
$str = \sprintf('has cookie "%s"', $this->name); |
| 30 |
if ('/' !== $this->path) { |
| 31 |
$str .= \sprintf(' with path "%s"', $this->path); |
| 32 |
} |
| 33 |
if ($this->domain) { |
| 34 |
$str .= \sprintf(' for domain "%s"', $this->domain); |
| 35 |
} |
| 36 |
|
| 37 |
return $str; |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
protected function matches($response): bool |
| 44 |
{ |
| 45 |
return null !== $this->getCookie($response); |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
protected function failureDescription($response): string |
| 52 |
{ |
| 53 |
return 'the Response '.$this->toString(); |
| 54 |
} |
| 55 |
|
| 56 |
private function getCookie(Response $response): ?Cookie |
| 57 |
{ |
| 58 |
$cookies = $response->headers->getCookies(); |
| 59 |
|
| 60 |
$filteredCookies = array_filter($cookies, fn (Cookie $cookie) => $cookie->getName() === $this->name && $cookie->getPath() === $this->path && $cookie->getDomain() === $this->domain); |
| 61 |
|
| 62 |
return reset($filteredCookies) ?: null; |
| 63 |
} |
| 64 |
} |