1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- <?php
- /*
- * This file is part of the Symfony package.
- *
- * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Symfony\Tests\Components\BrowserKit;
- use Symfony\Components\BrowserKit\CookieJar;
- use Symfony\Components\BrowserKit\Cookie;
- use Symfony\Components\BrowserKit\Response;
- class CookieJarTest extends \PHPUnit_Framework_TestCase
- {
- public function testSetGet()
- {
- $cookieJar = new CookieJar();
- $cookieJar->set($cookie = new Cookie('foo', 'bar'));
- $this->assertEquals($cookie, $cookieJar->get('foo'), '->set() sets a cookie');
- $this->assertNull($cookieJar->get('foobar'), '->get() returns null if the cookie does not exist');
- $cookieJar->set($cookie = new Cookie('foo', 'bar', time() - 86400));
- $this->assertNull($cookieJar->get('foo'), '->get() returns null if the cookie is expired');
- }
- public function testExpire()
- {
- $cookieJar = new CookieJar();
- $cookieJar->set($cookie = new Cookie('foo', 'bar'));
- $cookieJar->expire('foo');
- $this->assertNull($cookieJar->get('foo'), '->get() returns null if the cookie is expired');
- }
- public function testAll()
- {
- $cookieJar = new CookieJar();
- $cookieJar->set($cookie1 = new Cookie('foo', 'bar'));
- $cookieJar->set($cookie2 = new Cookie('bar', 'foo'));
- $this->assertEquals(array('foo' => $cookie1, 'bar' => $cookie2), $cookieJar->all(), '->all() returns all cookies in the jar');
- }
- public function testClear()
- {
- $cookieJar = new CookieJar();
- $cookieJar->set($cookie1 = new Cookie('foo', 'bar'));
- $cookieJar->set($cookie2 = new Cookie('bar', 'foo'));
- $cookieJar->clear();
- $this->assertEquals(array(), $cookieJar->all(), '->clear() expires all cookies');
- }
- public function testUpdateFromResponse()
- {
- $response = new Response('', 200, array(), array('foo' => array('value' => 'foo')));
- $cookieJar = new CookieJar();
- $cookieJar->set(new Cookie('bar', 'bar'));
- $cookieJar->updateFromResponse($response);
- $this->assertEquals('foo', $cookieJar->get('foo')->getValue(), '->updateFromResponse() updates cookies from a Response objects');
- $this->assertEquals('bar', $cookieJar->get('bar')->getValue(), '->updateFromResponse() keeps existing cookies');
- }
- /**
- * @dataProvider provideGetValuesValues
- */
- public function testGetValues($uri, $values)
- {
- $cookieJar = new CookieJar();
- $cookieJar->set($cookie1 = new Cookie('foo_nothing', 'foo'));
- $cookieJar->set($cookie2 = new Cookie('foo_expired', 'foo', time() - 86400));
- $cookieJar->set($cookie3 = new Cookie('foo_path', 'foo', 0, '/foo'));
- $cookieJar->set($cookie4 = new Cookie('foo_domain', 'foo', 0, '/', 'example.com'));
- $cookieJar->set($cookie5 = new Cookie('foo_secure', 'foo', 0, '/', '', true));
- $this->assertEquals($values, array_keys($cookieJar->getValues($uri)), '->getValues() returns the cookie for a given URI');
- }
- public function provideGetValuesValues()
- {
- return array(
- array('http://www.example.com/', array('foo_nothing', 'foo_domain')),
- array('http://foo.example.com/', array('foo_nothing', 'foo_domain')),
- array('http://foo.example1.com/', array('foo_nothing')),
- array('https://foo.example.com/', array('foo_nothing', 'foo_domain', 'foo_secure')),
- array('http://www.example.com/foo/bar', array('foo_nothing', 'foo_path', 'foo_domain')),
- );
- }
- }
|