1
0
mirror of https://github.com/guzzle/guzzle.git synced 2025-02-25 02:22:57 +01:00
guzzle/tests/Cookie/SessionCookieJarTest.php
2014-04-18 23:58:24 +01:00

87 lines
2.4 KiB
PHP

<?php
namespace GuzzleHttp\Tests\CookieJar;
use GuzzleHttp\Cookie\SessionCookieJar;
use GuzzleHttp\Cookie\SetCookie;
/**
* @covers GuzzleHttp\Cookie\SessionCookieJar
*/
class SessionCookieJarTest extends \PHPUnit_Framework_TestCase
{
private $sessionVar;
public function setUp()
{
$this->sessionVar = 'sessionKey';
if (!isset($_SESSION)) {
// If we are run from the command line interface then we do not care
// about headers sent using the session_start.
if (PHP_SAPI === 'cli') {
$_SESSION = array();
} elseif (!headers_sent()) {
if (!session_start()) {
throw new \Exception(__METHOD__ . 'session_start failed.');
}
} else {
throw new Exception(__METHOD__ . 'Session started after headers sent.');
}
}
}
/**
* @expectedException \RuntimeException
*/
public function testValidatesCookieSession()
{
$_SESSION[$this->sessionVar] = 'true';
new SessionCookieJar($this->sessionVar);
}
public function testLoadsFromSession()
{
$jar = new SessionCookieJar($this->sessionVar);
$this->assertEquals([], $jar->getIterator()->getArrayCopy());
unset($_SESSION[$this->sessionVar]);
}
public function testPersistsToSession()
{
$jar = new SessionCookieJar($this->sessionVar);
$jar->setCookie(new SetCookie([
'Name' => 'foo',
'Value' => 'bar',
'Domain' => 'foo.com',
'Expires' => time() + 1000
]));
$jar->setCookie(new SetCookie([
'Name' => 'baz',
'Value' => 'bar',
'Domain' => 'foo.com',
'Expires' => time() + 1000
]));
$jar->setCookie(new SetCookie([
'Name' => 'boo',
'Value' => 'bar',
'Domain' => 'foo.com',
]));
$this->assertEquals(3, count($jar));
unset($jar);
// Make sure it wrote to the sessionVar in $_SESSION
$contents = $_SESSION[$this->sessionVar];
$this->assertNotEmpty($contents);
// Load the cookieJar from the file
$jar = new SessionCookieJar($this->sessionVar);
// Weeds out temporary and session cookies
$this->assertEquals(2, count($jar));
unset($jar);
unset($_SESSION[$this->sessionVar]);
}
}