BrowserCookieValueSameTest.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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\BrowserKit\Tests\Test\Constraint;
  11. use PHPUnit\Framework\ExpectationFailedException;
  12. use PHPUnit\Framework\TestCase;
  13. use PHPUnit\Framework\TestFailure;
  14. use Symfony\Component\BrowserKit\AbstractBrowser;
  15. use Symfony\Component\BrowserKit\Cookie;
  16. use Symfony\Component\BrowserKit\CookieJar;
  17. use Symfony\Component\BrowserKit\Test\Constraint\BrowserCookieValueSame;
  18. class BrowserCookieValueSameTest extends TestCase
  19. {
  20. public function testConstraint(): void
  21. {
  22. $browser = $this->getBrowser();
  23. $constraint = new BrowserCookieValueSame('foo', 'bar', false, '/path');
  24. $this->assertTrue($constraint->evaluate($browser, '', true));
  25. $constraint = new BrowserCookieValueSame('foo', 'bar', true, '/path');
  26. $this->assertTrue($constraint->evaluate($browser, '', true));
  27. $constraint = new BrowserCookieValueSame('foo', 'babar', false, '/path');
  28. $this->assertFalse($constraint->evaluate($browser, '', true));
  29. try {
  30. $constraint->evaluate($browser);
  31. } catch (ExpectationFailedException $e) {
  32. $this->assertEquals("Failed asserting that the Browser has cookie \"foo\" with path \"/path\" with value \"babar\".\n", TestFailure::exceptionToString($e));
  33. return;
  34. }
  35. $this->fail();
  36. }
  37. private function getBrowser(): AbstractBrowser
  38. {
  39. $browser = $this->createMock(AbstractBrowser::class);
  40. $jar = new CookieJar();
  41. $jar->set(new Cookie('foo', 'bar', null, '/path', 'example.com'));
  42. $browser->expects($this->any())->method('getCookieJar')->willReturn($jar);
  43. return $browser;
  44. }
  45. }