| 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 ResponseCookieValueSame extends Constraint |
| 19 |
{ |
| 20 |
public function __construct( |
| 21 |
private string $name, |
| 22 |
private string $value, |
| 23 |
private string $path = '/', |
| 24 |
private ?string $domain = null, |
| 25 |
) { |
| 26 |
} |
| 27 |
|
| 28 |
public function toString(): string |
| 29 |
{ |
| 30 |
$str = \sprintf('has cookie "%s"', $this->name); |
| 31 |
if ('/' !== $this->path) { |
| 32 |
$str .= \sprintf(' with path "%s"', $this->path); |
| 33 |
} |
| 34 |
if ($this->domain) { |
| 35 |
$str .= \sprintf(' for domain "%s"', $this->domain); |
| 36 |
} |
| 37 |
|
| 38 |
return $str.\sprintf(' with value "%s"', $this->value); |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
protected function matches($response): bool |
| 45 |
{ |
| 46 |
$cookie = $this->getCookie($response); |
| 47 |
if (!$cookie) { |
| 48 |
return false; |
| 49 |
} |
| 50 |
|
| 51 |
return $this->value === (string) $cookie->getValue(); |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
protected function failureDescription($response): string |
| 58 |
{ |
| 59 |
return 'the Response '.$this->toString(); |
| 60 |
} |
| 61 |
|
| 62 |
protected function getCookie(Response $response): ?Cookie |
| 63 |
{ |
| 64 |
$cookies = $response->headers->getCookies(); |
| 65 |
|
| 66 |
$filteredCookies = array_filter($cookies, fn (Cookie $cookie) => $cookie->getName() === $this->name && $cookie->getPath() === $this->path && $cookie->getDomain() === $this->domain); |
| 67 |
|
| 68 |
return reset($filteredCookies) ?: null; |
| 69 |
} |
| 70 |
} |