BrowserHasCookieTest.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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\BrowserHasCookie;
  18. class BrowserHasCookieTest extends TestCase
  19. {
  20. public function testConstraint(): void
  21. {
  22. $browser = $this->getBrowser();
  23. $constraint = new BrowserHasCookie('foo', '/path');
  24. $this->assertTrue($constraint->evaluate($browser, '', true));
  25. $constraint = new BrowserHasCookie('foo', '/path', 'example.com');
  26. $this->assertTrue($constraint->evaluate($browser, '', true));
  27. $constraint = new BrowserHasCookie('bar');
  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 \"bar\".\n", TestFailure::exceptionToString($e));
  33. return;
  34. }
  35. $this->fail();
  36. }
  37. public function testConstraintWithWrongPath(): void
  38. {
  39. $browser = $this->getBrowser();
  40. $constraint = new BrowserHasCookie('foo', '/other');
  41. try {
  42. $constraint->evaluate($browser);
  43. } catch (ExpectationFailedException $e) {
  44. $this->assertEquals("Failed asserting that the Browser has cookie \"foo\" with path \"/other\".\n", TestFailure::exceptionToString($e));
  45. return;
  46. }
  47. $this->fail();
  48. }
  49. public function testConstraintWithWrongDomain(): void
  50. {
  51. $browser = $this->getBrowser();
  52. $constraint = new BrowserHasCookie('foo', '/path', 'example.org');
  53. try {
  54. $constraint->evaluate($browser);
  55. } catch (ExpectationFailedException $e) {
  56. $this->assertEquals("Failed asserting that the Browser has cookie \"foo\" with path \"/path\" for domain \"example.org\".\n", TestFailure::exceptionToString($e));
  57. return;
  58. }
  59. $this->fail();
  60. }
  61. private function getBrowser(): AbstractBrowser
  62. {
  63. $browser = $this->createMock(AbstractBrowser::class);
  64. $jar = new CookieJar();
  65. $jar->set(new Cookie('foo', 'bar', null, '/path', 'example.com'));
  66. $browser->expects($this->any())->method('getCookieJar')->willReturn($jar);
  67. return $browser;
  68. }
  69. }