Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #1232 from suralc/cookie-tests

Tests for CCookieCollection and CHttpCookie
  • Loading branch information...
commit 02584c065c525a378b0bcf37202389916e9fcc9a 2 parents 17479ec + 48e10dc
@samdark samdark authored
View
6 tests/bootstrap.php
@@ -1,8 +1,8 @@
<?php
-define('YII_ENABLE_EXCEPTION_HANDLER',false);
-define('YII_ENABLE_ERROR_HANDLER',false);
-define('YII_DEBUG',true);
+defined('YII_ENABLE_EXCEPTION_HANDLER') or define('YII_ENABLE_EXCEPTION_HANDLER',false);
+defined('YII_ENABLE_ERROR_HANDLER') or define('YII_ENABLE_ERROR_HANDLER',false);
+defined('YII_DEBUG') or define('YII_DEBUG',true);
$_SERVER['SCRIPT_NAME']='/'.basename(__FILE__);
$_SERVER['SCRIPT_FILENAME']=__FILE__;
View
106 tests/framework/web/CCookieCollectionTest.php
@@ -0,0 +1,106 @@
+<?php
+
+class CCookieCollectionTest extends CTestCase
+{
+ protected $request;
+ protected $cookies;
+ protected $testCookies=array(
+ 'testCookieOne'=>'testValue',
+ 'someEmptyCookie'=>'',
+ 'IntegerValue'=>1242,
+ 'cookieWithOptions'=>array(
+ 'value'=>'options',
+ 'httpOnly'=>true,
+ 'expire'=>12422,
+ ),
+ );
+ protected $cookieBefore;
+ public function setUp()
+ {
+ $this->cookieBefore=$_COOKIE;
+ $_COOKIE['testGlobal']='value';
+ $this->request=new TestHttpRequest;
+ $this->cookies=$this->request->cookies;
+ }
+ public function tearDown()
+ {
+ $_COOKIE=$this->cookieBefore;
+ }
+ /**
+ * @covers CCookieCollection::getCookies
+ * @covers CCookieCollection::__construct
+ */
+ public function testConstructorCookieBuilding()
+ {
+ $this->assertTrue($this->cookies->contains('testGlobal'));
+ $this->assertTrue($this->cookies['testGlobal'] instanceof CHttpCookie);
+ $this->assertEquals($_COOKIE['testGlobal'], $this->cookies['testGlobal']->value);
+ }
+ /**
+ * @runInSeparateProcess
+ * @outputBuffering enabled
+ */
+ public function testAdd()
+ {
+ $this->cookies['simple_name']=new CHttpCookie('simple_name', 'simple_value');
+ $this->assertTrue($this->cookies['simple_name'] instanceof CHttpCookie);
+ $this->assertEquals('simple_value',$this->cookies['simple_name']->value);
+ // test if reference is not destroyed
+ $cookie=new CHttpCookie('referenceTest','someValue');
+ $this->cookies[$cookie->name]=$cookie;
+ $this->assertTrue($this->cookies[$cookie->name] instanceof CHttpCookie);
+ $this->assertEquals('someValue',$this->cookies[$cookie->name]->value);
+ $cookie->value='SomeNewValue';
+ $this->assertEquals($cookie->value,$this->cookies[$cookie->name]->value);
+ // test if cookies are added to internal array correctly
+ $this->assertTrue($this->cookies->contains('simple_name'));
+ $this->assertTrue($this->cookies->contains('referenceTest'));
+ $this->assertFalse($this->cookies->contains('someNonExistingCookie'));
+ }
+ /**
+ * @runInSeparateProcess
+ * @outputBuffering enabled
+ */
+ public function testRemove()
+ {
+ // add some cookies to have something to base the tests (remove)
+ foreach($this->testCookies as $name=>$options)
+ {
+ if(is_array($options))
+ {
+ $value=$options['value'];
+ unset($options['value']);
+ $cookie=new CHttpCookie($name,$value,$options);
+ }
+ else
+ $cookie=new CHttpCookie($name,$options);
+ $this->cookies[$cookie->name] = $cookie;
+ }
+ //check if cookies were added, else fail the test, since it makes no sence to do this, if there are no cookies
+ $this->assertTrue($this->cookies->contains('testCookieOne'),'A default cookie is missing! Check the testcase!');
+ $this->assertTrue($this->cookies->contains('someEmptyCookie'),'A default cookie is missing! Check the testcase!');
+ $this->assertTrue($this->cookies->contains('someEmptyCookie'),'A default cookie is missing! Check the testcase!');
+ $this->assertTrue($this->cookies->contains('cookieWithOptions'),'A default cookie is missing! Check the testcase!');
+ // Real tests below:
+ foreach($this->testCookies as $name=>$options)
+ {
+ if(is_array($options))
+ {
+ $value=$options['value'];
+ unset($options['value']);
+ $this->assertTrue($this->cookies->remove($name,$options) instanceof CHttpCookie);
+ $this->assertFalse($this->cookies->contains($name),'Cookie(with options) has not been removed');
+ }
+ else
+ {
+ $this->assertTrue($this->cookies->remove($name) instanceof CHttpCookie);
+ $this->assertFalse($this->cookies->contains($name));
+ }
+ }
+ }
+ public function testGetRequest()
+ {
+ $this->assertTrue($this->cookies->getRequest() instanceof CHttpRequest);
+ $this->assertEquals($this->request,$this->cookies->getRequest(),'The collections does not contain the CHttpRequest instance it is stored in.');
+ }
+}
View
57 tests/framework/web/CHttpCookieTest.php
@@ -0,0 +1,57 @@
+<?php
+
+class CHttpCookieTest extends CTestCase
+{
+ /**
+ * @covers CHttpCookie::configure
+ * @covers CHttpCookie::__construct
+ */
+ public function testConfigure()
+ {
+ //covers construct
+ $cookie=new CHttpCookie('name','value');
+ $this->assertEquals('name',$cookie->name,'Constructor failure. Name should have been set there');
+ $this->assertEquals('value',$cookie->value,'Constructor failure. Value should have been set there');
+ $this->assertEquals('',$cookie->domain,'Default value for CHttpCookie::$domain has been touched');
+ $this->assertEquals(0,$cookie->expire,'Default value for CHttpCookie::$expire has been touched');
+ $this->assertEquals('/',$cookie->path,'Default value for CHttpCookie::$path has been touched');
+ $this->assertFalse($cookie->secure,'Default value for CHttpCookie::$secure has been touched');
+ $this->assertFalse($cookie->httpOnly,'Default value for CHttpCookie::$httpOnly has been touched');
+ $options=array(
+ 'expire'=>123123,
+ 'httpOnly'=>true,
+ );
+ // create cookie with options
+ $cookie2=new CHttpCookie('name2','value2',$options);
+ $this->assertEquals($options['expire'],$cookie2->expire,'Configure inside the Constructor has been failed');
+ $this->assertEquals($options['httpOnly'],$cookie2->httpOnly,'Configure inside the Constructor has been failed');
+ //configure afterwards
+ $cookie->configure($options);
+ $this->assertEquals($options['expire'],$cookie->expire);
+ $this->assertEquals($options['httpOnly'],$cookie->httpOnly);
+ // Set name and value via configure (should have no effect)
+ $name=$cookie->name;
+ $cookie->configure(array('name'=>'someNewName'));
+ $this->assertEquals($name,$cookie->name);
+ $value=$cookie->value;
+ $cookie->configure(array('value'=>'someNewValue'));
+ $this->assertEquals($value,$cookie->value);
+ //new configure should not override already set configuration
+ $this->assertEquals($options['httpOnly'],$cookie->httpOnly);
+ }
+ /**
+ * @covers CHttpCookie::__toString
+ */
+ public function test__ToString()
+ {
+ $cookie=new CHttpCookie('name','someValue');
+ // Note on http://www.php.net/manual/en/language.oop5.magic.php#object.tostring
+ ob_start();
+ echo $cookie;
+ $this->assertEquals(ob_get_clean(),$cookie->value);
+ if(version_compare(PHP_VERSION,'5.2','>='))
+ {
+ $this->assertEquals($cookie->value,(string)$cookie);
+ }
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.