ResponseHasCookie example

use PHPUnit\Framework\TestFailure;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Test\Constraint\ResponseHasCookie;

class ResponseHasCookieTest extends TestCase
{
    public function testConstraint()
    {
        $response = new Response();
        $response->headers->setCookie(Cookie::create('foo', 'bar'));
        $constraint = new ResponseHasCookie('foo');
        $this->assertTrue($constraint->evaluate($response, '', true));
        $constraint = new ResponseHasCookie('bar');
        $this->assertFalse($constraint->evaluate($response, '', true));

        try {
            $constraint->evaluate($response);
        } catch (ExpectationFailedException $e) {
            $this->assertEquals("Failed asserting that the Response has cookie \"bar\".\n", TestFailure::exceptionToString($e));

            return;
        }

        

        self::assertThatForResponse(new ResponseConstraint\ResponseHeaderSame($headerName$expectedValue)$message);
    }

    public static function assertResponseHeaderNotSame(string $headerName, string $expectedValue, string $message = ''): void
    {
        self::assertThatForResponse(new LogicalNot(new ResponseConstraint\ResponseHeaderSame($headerName$expectedValue))$message);
    }

    public static function assertResponseHasCookie(string $name, string $path = '/', string $domain = null, string $message = ''): void
    {
        self::assertThatForResponse(new ResponseConstraint\ResponseHasCookie($name$path$domain)$message);
    }

    public static function assertResponseNotHasCookie(string $name, string $path = '/', string $domain = null, string $message = ''): void
    {
        self::assertThatForResponse(new LogicalNot(new ResponseConstraint\ResponseHasCookie($name$path$domain))$message);
    }

    public static function assertResponseCookieValueSame(string $name, string $expectedValue, string $path = '/', string $domain = null, string $message = ''): void
    {
        self::assertThatForResponse(LogicalAnd::fromConstraints(
            new ResponseConstraint\ResponseHasCookie($name$path$domain),
            
Home | Imprint | This part of the site doesn't use cookies.