ResponseCookieValueSameTest.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\HttpFoundation\Tests\Test\Constraint;
  11. use PHPUnit\Framework\ExpectationFailedException;
  12. use PHPUnit\Framework\TestCase;
  13. use PHPUnit\Framework\TestFailure;
  14. use Symfony\Component\HttpFoundation\Cookie;
  15. use Symfony\Component\HttpFoundation\Response;
  16. use Symfony\Component\HttpFoundation\Test\Constraint\ResponseCookieValueSame;
  17. class ResponseCookieValueSameTest extends TestCase
  18. {
  19. public function testConstraint()
  20. {
  21. $response = new Response();
  22. $response->headers->setCookie(Cookie::create('foo', 'bar', 0, '/path'));
  23. $constraint = new ResponseCookieValueSame('foo', 'bar', '/path');
  24. $this->assertTrue($constraint->evaluate($response, '', true));
  25. $constraint = new ResponseCookieValueSame('foo', 'bar', '/path');
  26. $this->assertTrue($constraint->evaluate($response, '', true));
  27. $constraint = new ResponseCookieValueSame('foo', 'babar', '/path');
  28. $this->assertFalse($constraint->evaluate($response, '', true));
  29. try {
  30. $constraint->evaluate($response);
  31. } catch (ExpectationFailedException $e) {
  32. $this->assertEquals("Failed asserting that the Response has cookie \"foo\" with path \"/path\" with value \"babar\".\n", TestFailure::exceptionToString($e));
  33. return;
  34. }
  35. $this->fail();
  36. }
  37. public function testCookieWithNullValueIsComparedAsEmptyString()
  38. {
  39. $response = new Response();
  40. $response->headers->setCookie(Cookie::create('foo', null, 0, '/path'));
  41. $this->assertTrue((new ResponseCookieValueSame('foo', '', '/path'))->evaluate($response, '', true));
  42. }
  43. }