1, 'cookie2' => 'cookies', 'cookie3' => 'test', ); $cookieJar = new CookieJar($defaultCookies); //make sure all the "recieved" cookies are as expected $this->assertEquals($defaultCookies, $cookieJar->getAll(false)); //make sure there are no "phantom" cookies $this->assertEquals($defaultCookies, $cookieJar->getAll(false)); //check an empty array is accepted $cookieJar = new CookieJar(array()); $this->assertEmpty($cookieJar->getAll(false)); //check no argument is accepted $cookieJar = new CookieJar(); $this->assertEmpty($cookieJar->getAll(false)); } /** * Test that we can set and get cookies */ public function testSetAndGet() { $cookieJar = new CookieJar(); $this->assertEmpty($cookieJar->get('testCookie')); //set a test cookie $cookieJar->set('testCookie', 'testVal'); //make sure it was set $this->assertEquals('testVal', $cookieJar->get('testCookie')); //make sure we can distinguise it from ones that were "existing" $this->assertEmpty($cookieJar->get('testCookie', false)); } /** * Test that we can distinguish between vars that were loaded on instantiation * and those added later */ public function testExistingVersusNew() { //load with a cookie $cookieJar = new CookieJar(array( 'cookieExisting' => 'i woz here', )); //set a new cookie $cookieJar->set('cookieNew', 'i am new'); //check we can fetch new and old cookie values $this->assertEquals('i woz here', $cookieJar->get('cookieExisting')); $this->assertEquals('i woz here', $cookieJar->get('cookieExisting', false)); $this->assertEquals('i am new', $cookieJar->get('cookieNew')); //there should be no original value for the new cookie $this->assertEmpty($cookieJar->get('cookieNew', false)); //change the existing cookie, can we fetch the new and old value $cookieJar->set('cookieExisting', 'i woz changed'); $this->assertEquals('i woz changed', $cookieJar->get('cookieExisting')); $this->assertEquals('i woz here', $cookieJar->get('cookieExisting', false)); //check we can get all cookies $this->assertEquals(array( 'cookieExisting' => 'i woz changed', 'cookieNew' => 'i am new', ), $cookieJar->getAll()); //check we can get all original cookies $this->assertEquals(array( 'cookieExisting' => 'i woz here', ), $cookieJar->getAll(false)); } /** * Check we can remove cookies and we can access their original values */ public function testForceExpiry() { //load an existing cookie $cookieJar = new CookieJar(array( 'cookieExisting' => 'i woz here', )); //make sure it's available $this->assertEquals('i woz here', $cookieJar->get('cookieExisting')); //remove the cookie $cookieJar->forceExpiry('cookieExisting'); //check it's gone $this->assertEmpty($cookieJar->get('cookieExisting')); //check we can get it's original value $this->assertEquals('i woz here', $cookieJar->get('cookieExisting', false)); //check we can add a new cookie and remove it and it doesn't leave any phantom values $cookieJar->set('newCookie', 'i am new'); //check it's set by not recieved $this->assertEquals('i am new', $cookieJar->get('newCookie')); $this->assertEmpty($cookieJar->get('newCookie', false)); //remove it $cookieJar->forceExpiry('newCookie'); //check it's neither set nor reveived $this->assertEmpty($cookieJar->get('newCookie')); $this->assertEmpty($cookieJar->get('newCookie', false)); } }