FileCookieJarTest.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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()
  13. {
  14. $this->file = tempnam('/tmp', 'file-cookies');
  15. }
  16. /**
  17. * @expectedException \RuntimeException
  18. */
  19. public function testValidatesCookieFile()
  20. {
  21. file_put_contents($this->file, 'true');
  22. new FileCookieJar($this->file);
  23. }
  24. public function testLoadsFromFile()
  25. {
  26. $jar = new FileCookieJar($this->file);
  27. $this->assertSame([], $jar->getIterator()->getArrayCopy());
  28. unlink($this->file);
  29. }
  30. /**
  31. * @dataProvider providerPersistsToFileFileParameters
  32. */
  33. public function testPersistsToFile($testSaveSessionCookie = false)
  34. {
  35. $jar = new FileCookieJar($this->file, $testSaveSessionCookie);
  36. $jar->setCookie(new SetCookie([
  37. 'Name' => 'foo',
  38. 'Value' => 'bar',
  39. 'Domain' => 'foo.com',
  40. 'Expires' => time() + 1000
  41. ]));
  42. $jar->setCookie(new SetCookie([
  43. 'Name' => 'baz',
  44. 'Value' => 'bar',
  45. 'Domain' => 'foo.com',
  46. 'Expires' => time() + 1000
  47. ]));
  48. $jar->setCookie(new SetCookie([
  49. 'Name' => 'boo',
  50. 'Value' => 'bar',
  51. 'Domain' => 'foo.com',
  52. ]));
  53. $this->assertCount(3, $jar);
  54. unset($jar);
  55. // Make sure it wrote to the file
  56. $contents = file_get_contents($this->file);
  57. $this->assertNotEmpty($contents);
  58. // Load the cookieJar from the file
  59. $jar = new FileCookieJar($this->file);
  60. if ($testSaveSessionCookie) {
  61. $this->assertCount(3, $jar);
  62. } else {
  63. // Weeds out temporary and session cookies
  64. $this->assertCount(2, $jar);
  65. }
  66. unset($jar);
  67. unlink($this->file);
  68. }
  69. public function providerPersistsToFileFileParameters()
  70. {
  71. return array(
  72. array(false),
  73. array(true)
  74. );
  75. }
  76. }