FileCookieJarTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. namespace GuzzleHttp\Tests\CookieJar;
  3. use GuzzleHttp\Cookie\FileCookieJar;
  4. use GuzzleHttp\Cookie\SetCookie;
  5. use PHPUnit\Framework\TestCase;
  6. /**
  7. * @covers \GuzzleHttp\Cookie\FileCookieJar
  8. */
  9. class FileCookieJarTest extends TestCase
  10. {
  11. private $file;
  12. public function setUp(): void
  13. {
  14. $this->file = \tempnam(\sys_get_temp_dir(), 'file-cookies');
  15. }
  16. /**
  17. * @dataProvider invalidCookieJarContent
  18. */
  19. public function testValidatesCookieFile($invalidCookieJarContent)
  20. {
  21. \file_put_contents($this->file, json_encode($invalidCookieJarContent));
  22. $this->expectException(\RuntimeException::class);
  23. new FileCookieJar($this->file);
  24. }
  25. public function testLoadsFromFile()
  26. {
  27. $jar = new FileCookieJar($this->file);
  28. self::assertSame([], $jar->getIterator()->getArrayCopy());
  29. \unlink($this->file);
  30. }
  31. /**
  32. * @dataProvider providerPersistsToFileFileParameters
  33. */
  34. public function testPersistsToFile($testSaveSessionCookie = false)
  35. {
  36. $jar = new FileCookieJar($this->file, $testSaveSessionCookie);
  37. $jar->setCookie(new SetCookie([
  38. 'Name' => 'foo',
  39. 'Value' => 'bar',
  40. 'Domain' => 'foo.com',
  41. 'Expires' => \time() + 1000,
  42. ]));
  43. $jar->setCookie(new SetCookie([
  44. 'Name' => 'baz',
  45. 'Value' => 'bar',
  46. 'Domain' => 'foo.com',
  47. 'Expires' => \time() + 1000,
  48. ]));
  49. $jar->setCookie(new SetCookie([
  50. 'Name' => 'boo',
  51. 'Value' => 'bar',
  52. 'Domain' => 'foo.com',
  53. ]));
  54. self::assertCount(3, $jar);
  55. unset($jar);
  56. // Make sure it wrote to the file
  57. $contents = \file_get_contents($this->file);
  58. self::assertNotEmpty($contents);
  59. // Load the cookieJar from the file
  60. $jar = new FileCookieJar($this->file);
  61. if ($testSaveSessionCookie) {
  62. self::assertCount(3, $jar);
  63. } else {
  64. // Weeds out temporary and session cookies
  65. self::assertCount(2, $jar);
  66. }
  67. unset($jar);
  68. \unlink($this->file);
  69. }
  70. public function testRemovesCookie()
  71. {
  72. $jar = new FileCookieJar($this->file);
  73. $jar->setCookie(new SetCookie([
  74. 'Name' => 'foo',
  75. 'Value' => 'bar',
  76. 'Domain' => 'foo.com',
  77. 'Expires' => \time() + 1000,
  78. ]));
  79. self::assertCount(1, $jar);
  80. // Remove the cookie.
  81. $jar->clear('foo.com', '/', 'foo');
  82. // Confirm that the cookie was removed.
  83. self::assertCount(0, $jar);
  84. \unlink($this->file);
  85. }
  86. public function testUpdatesCookie()
  87. {
  88. $jar = new FileCookieJar($this->file);
  89. $jar->setCookie(new SetCookie([
  90. 'Name' => 'foo',
  91. 'Value' => 'bar',
  92. 'Domain' => 'foo.com',
  93. 'Expires' => \time() + 1000,
  94. ]));
  95. self::assertCount(1, $jar);
  96. // Update the cookie value.
  97. $jar->setCookie(new SetCookie([
  98. 'Name' => 'foo',
  99. 'Value' => 'new_value',
  100. 'Domain' => 'foo.com',
  101. 'Expires' => \time() + 1000,
  102. ]));
  103. $cookies = $jar->getIterator()->getArrayCopy();
  104. // Confirm that the cookie was updated.
  105. self::assertEquals('new_value', $cookies[0]->getValue());
  106. \unlink($this->file);
  107. }
  108. public function providerPersistsToFileFileParameters()
  109. {
  110. return [
  111. [false],
  112. [true],
  113. ];
  114. }
  115. public function invalidCookieJarContent(): array
  116. {
  117. return [
  118. [true],
  119. ['invalid-data'],
  120. ];
  121. }
  122. }