Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

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