CookieTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /*
  3. * This file is part of Chrome PHP.
  4. *
  5. * (c) Soufiane Ghzal <sghzal@gmail.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 HeadlessChromium\Test;
  11. use HeadlessChromium\Browser;
  12. use HeadlessChromium\BrowserFactory;
  13. use HeadlessChromium\Cookies\Cookie;
  14. use HeadlessChromium\Cookies\CookiesCollection;
  15. /**
  16. * @covers \HeadlessChromium\Browser
  17. * @covers \HeadlessChromium\PageUtils\CookiesGetter
  18. * @covers \HeadlessChromium\Page
  19. */
  20. class CookieTest extends HttpEnabledTestCase
  21. {
  22. /**
  23. * @var Browser\ProcessAwareBrowser
  24. */
  25. public static $browser;
  26. public function setUp(): void
  27. {
  28. parent::setUp();
  29. $factory = new BrowserFactory();
  30. self::$browser = $factory->createBrowser();
  31. }
  32. public function tearDown(): void
  33. {
  34. parent::tearDown();
  35. self::$browser->close();
  36. }
  37. private function openSitePage($file)
  38. {
  39. $page = self::$browser->createPage();
  40. $page->navigate(self::sitePath($file))->waitForNavigation();
  41. return $page;
  42. }
  43. public function testReadCookies(): void
  44. {
  45. // initial navigation
  46. $page = $this->openSitePage('cookie.html');
  47. $cookies = $page->getCookies();
  48. self::assertInstanceOf(CookiesCollection::class, $cookies);
  49. self::assertCount(1, $cookies);
  50. self::assertEquals($cookies->getAt(0)->getName(), 'foo');
  51. self::assertEquals($cookies->getAt(0)->getValue(), 'bar');
  52. }
  53. public function testGetAllCookies(): void
  54. {
  55. // initial navigation
  56. $page = $this->openSitePage('cookie.html');
  57. $cookies = $page->getAllCookies();
  58. self::assertInstanceOf(CookiesCollection::class, $cookies);
  59. self::assertCount(1, $cookies);
  60. self::assertEquals($cookies->getAt(0)->getName(), 'foo');
  61. self::assertEquals($cookies->getAt(0)->getValue(), 'bar');
  62. }
  63. public function testSetCookies(): void
  64. {
  65. // initial navigation
  66. $page = self::$browser->createPage();
  67. // set cookie for arbitrary host
  68. $page->setCookies([
  69. Cookie::create('baz', 'qux', [
  70. 'domain' => 'foo.bar',
  71. 'expires' => \time() + 3600,
  72. ]),
  73. ])->await();
  74. $cookies = $page->getAllCookies();
  75. self::assertInstanceOf(CookiesCollection::class, $cookies);
  76. self::assertCount(1, $cookies);
  77. self::assertEquals($cookies->getAt(0)->getName(), 'baz');
  78. self::assertEquals($cookies->getAt(0)->getValue(), 'qux');
  79. self::assertEquals($cookies->getAt(0)->getDomain(), 'foo.bar');
  80. // Set cookie for current page
  81. $page->navigate(self::sitePath('a.html'))->waitForNavigation();
  82. $page->setCookies([
  83. Cookie::create('quux', 'corge'),
  84. ])->await();
  85. $cookies = $page->getAllCookies();
  86. self::assertInstanceOf(CookiesCollection::class, $cookies);
  87. self::assertCount(2, $cookies);
  88. self::assertEquals($cookies->getAt(1)->getName(), 'quux');
  89. self::assertEquals($cookies->getAt(1)->getValue(), 'corge');
  90. self::assertEquals($cookies->getAt(1)->getDomain(), 'localhost');
  91. self::assertEquals($cookies->getAt(0)->getName(), 'baz');
  92. self::assertEquals($cookies->getAt(0)->getValue(), 'qux');
  93. self::assertEquals($cookies->getAt(0)->getDomain(), 'foo.bar');
  94. }
  95. }