Open Journal Systems  3.3.0
ResponseHasCookie.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;
17 
18 final class ResponseHasCookie extends Constraint
19 {
20  private $name;
21  private $path;
22  private $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 
34  public function toString(): string
35  {
36  $str = sprintf('has cookie "%s"', $this->name);
37  if ('/' !== $this->path) {
38  $str .= sprintf(' with path "%s"', $this->path);
39  }
40  if ($this->domain) {
41  $str .= sprintf(' for domain "%s"', $this->domain);
42  }
43 
44  return $str;
45  }
46 
52  protected function matches($response): bool
53  {
54  return null !== $this->getCookie($response);
55  }
56 
62  protected function failureDescription($response): string
63  {
64  return 'the Response '.$this->toString();
65  }
66 
67  private function getCookie(Response $response): ?Cookie
68  {
69  $cookies = $response->headers->getCookies();
70 
71  $filteredCookies = array_filter($cookies, function (Cookie $cookie) {
72  return $cookie->getName() === $this->name && $cookie->getPath() === $this->path && $cookie->getDomain() === $this->domain;
73  });
74 
75  return reset($filteredCookies) ?: null;
76  }
77 }
Symfony\Component\HttpFoundation\Response
Definition: lib/vendor/symfony/http-foundation/Response.php:19
Symfony\Component\HttpFoundation\Cookie\getPath
getPath()
Definition: lib/vendor/symfony/http-foundation/Cookie.php:237
Symfony\Component\HttpFoundation\Test\Constraint\ResponseHasCookie\failureDescription
failureDescription($response)
Definition: ResponseHasCookie.php:62
Symfony\Component\HttpFoundation\Cookie\getDomain
getDomain()
Definition: lib/vendor/symfony/http-foundation/Cookie.php:207
Symfony\Component\HttpFoundation\Test\Constraint\ResponseHasCookie
Definition: ResponseHasCookie.php:18
Symfony\Component\HttpFoundation\Test\Constraint\ResponseHasCookie\__construct
__construct(string $name, string $path='/', string $domain=null)
Definition: ResponseHasCookie.php:24
Symfony\Component\HttpFoundation\Test\Constraint\ResponseHasCookie\matches
matches($response)
Definition: ResponseHasCookie.php:52
Symfony\Component\HttpFoundation\Cookie
Definition: lib/vendor/symfony/http-foundation/Cookie.php:19
Symfony\Component\HttpFoundation\Test\Constraint
Definition: RequestAttributeValueSame.php:12
Symfony\Component\HttpFoundation\Cookie\getName
getName()
Definition: lib/vendor/symfony/http-foundation/Cookie.php:187
Symfony\Component\HttpFoundation\Test\Constraint\ResponseHasCookie\toString
toString()
Definition: ResponseHasCookie.php:34