BrowserHasCookie.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 BrowserHasCookie extends Constraint
  14. {
  15. private $name;
  16. private $path;
  17. private $domain;
  18. public function __construct(string $name, string $path = '/', string $domain = null)
  19. {
  20. $this->name = $name;
  21. $this->path = $path;
  22. $this->domain = $domain;
  23. }
  24. /**
  25. * {@inheritdoc}
  26. */
  27. public function toString(): string
  28. {
  29. $str = sprintf('has cookie "%s"', $this->name);
  30. if ('/' !== $this->path) {
  31. $str .= sprintf(' with path "%s"', $this->path);
  32. }
  33. if ($this->domain) {
  34. $str .= sprintf(' for domain "%s"', $this->domain);
  35. }
  36. return $str;
  37. }
  38. /**
  39. * @param AbstractBrowser $browser
  40. *
  41. * {@inheritdoc}
  42. */
  43. protected function matches($browser): bool
  44. {
  45. return null !== $browser->getCookieJar()->get($this->name, $this->path, $this->domain);
  46. }
  47. /**
  48. * @param AbstractBrowser $browser
  49. *
  50. * {@inheritdoc}
  51. */
  52. protected function failureDescription($browser): string
  53. {
  54. return 'the Browser '.$this->toString();
  55. }
  56. }