BrowserCookieValueSame.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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\Test\Constraint;
  11. use PHPUnit\Framework\Constraint\Constraint;
  12. use Symfony\Component\BrowserKit\AbstractBrowser;
  13. final class BrowserCookieValueSame extends Constraint
  14. {
  15. private $name;
  16. private $value;
  17. private $raw;
  18. private $path;
  19. private $domain;
  20. public function __construct(string $name, string $value, bool $raw = false, string $path = '/', string $domain = null)
  21. {
  22. $this->name = $name;
  23. $this->path = $path;
  24. $this->domain = $domain;
  25. $this->value = $value;
  26. $this->raw = $raw;
  27. }
  28. /**
  29. * {@inheritdoc}
  30. */
  31. public function toString(): string
  32. {
  33. $str = sprintf('has cookie "%s"', $this->name);
  34. if ('/' !== $this->path) {
  35. $str .= sprintf(' with path "%s"', $this->path);
  36. }
  37. if ($this->domain) {
  38. $str .= sprintf(' for domain "%s"', $this->domain);
  39. }
  40. $str .= sprintf(' with %svalue "%s"', $this->raw ? 'raw ' : '', $this->value);
  41. return $str;
  42. }
  43. /**
  44. * @param AbstractBrowser $browser
  45. *
  46. * {@inheritdoc}
  47. */
  48. protected function matches($browser): bool
  49. {
  50. $cookie = $browser->getCookieJar()->get($this->name, $this->path, $this->domain);
  51. if (!$cookie) {
  52. return false;
  53. }
  54. return $this->value === ($this->raw ? $cookie->getRawValue() : $cookie->getValue());
  55. }
  56. /**
  57. * @param AbstractBrowser $browser
  58. *
  59. * {@inheritdoc}
  60. */
  61. protected function failureDescription($browser): string
  62. {
  63. return 'the Browser '.$this->toString();
  64. }
  65. }