Open Journal Systems  3.3.0
ResponseCookieValueSame.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 ResponseCookieValueSame extends Constraint
19 {
20  private $name;
21  private $value;
22  private $path;
23  private $domain;
24 
25  public function __construct(string $name, string $value, string $path = '/', string $domain = null)
26  {
27  $this->name = $name;
28  $this->value = $value;
29  $this->path = $path;
30  $this->domain = $domain;
31  }
32 
36  public function toString(): string
37  {
38  $str = sprintf('has cookie "%s"', $this->name);
39  if ('/' !== $this->path) {
40  $str .= sprintf(' with path "%s"', $this->path);
41  }
42  if ($this->domain) {
43  $str .= sprintf(' for domain "%s"', $this->domain);
44  }
45  $str .= sprintf(' with value "%s"', $this->value);
46 
47  return $str;
48  }
49 
55  protected function matches($response): bool
56  {
57  $cookie = $this->getCookie($response);
58  if (!$cookie) {
59  return false;
60  }
61 
62  return $this->value === $cookie->getValue();
63  }
64 
70  protected function failureDescription($response): string
71  {
72  return 'the Response '.$this->toString();
73  }
74 
75  protected function getCookie(Response $response): ?Cookie
76  {
77  $cookies = $response->headers->getCookies();
78 
79  $filteredCookies = array_filter($cookies, function (Cookie $cookie) {
80  return $cookie->getName() === $this->name && $cookie->getPath() === $this->path && $cookie->getDomain() === $this->domain;
81  });
82 
83  return reset($filteredCookies) ?: null;
84  }
85 }
Symfony\Component\HttpFoundation\Response
Definition: lib/vendor/symfony/http-foundation/Response.php:19
Symfony\Component\HttpFoundation\Test\Constraint\ResponseCookieValueSame\matches
matches($response)
Definition: ResponseCookieValueSame.php:55
Symfony\Component\HttpFoundation\Cookie\getPath
getPath()
Definition: lib/vendor/symfony/http-foundation/Cookie.php:237
Symfony\Component\HttpFoundation\Test\Constraint\ResponseCookieValueSame\__construct
__construct(string $name, string $value, string $path='/', string $domain=null)
Definition: ResponseCookieValueSame.php:25
Symfony\Component\HttpFoundation\Cookie\getDomain
getDomain()
Definition: lib/vendor/symfony/http-foundation/Cookie.php:207
Symfony\Component\HttpFoundation\Test\Constraint\ResponseCookieValueSame
Definition: ResponseCookieValueSame.php:18
Symfony\Component\HttpFoundation\Cookie
Definition: lib/vendor/symfony/http-foundation/Cookie.php:19
Symfony\Component\HttpFoundation\Test\Constraint\ResponseCookieValueSame\toString
toString()
Definition: ResponseCookieValueSame.php:36
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\ResponseCookieValueSame\failureDescription
failureDescription($response)
Definition: ResponseCookieValueSame.php:70
Symfony\Component\HttpFoundation\Test\Constraint\ResponseCookieValueSame\getCookie
getCookie(Response $response)
Definition: ResponseCookieValueSame.php:75