Subversion Repositories php-qbpwcf

Rev

Rev 3 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
3 liveuser 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 $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
 
31
    /**
32
     * {@inheritdoc}
33
     */
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
 
47
    /**
48
     * @param Response $response
49
     *
50
     * {@inheritdoc}
51
     */
52
    protected function matches($response): bool
53
    {
54
        return null !== $this->getCookie($response);
55
    }
56
 
57
    /**
58
     * @param Response $response
59
     *
60
     * {@inheritdoc}
61
     */
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
}