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