/core/Test/Cage/Cookie.php

https://github.com/robtuley/knotwerk · PHP · 153 lines · 137 code · 16 blank · 0 comment · 0 complexity · b5923b3478e32411c7d16ef3d967f9d2 MD5 · raw file

  1. <?php
  2. class T_Test_Cage_Cookie extends T_Unit_Case
  3. {
  4. function testDataSetInConstructor()
  5. {
  6. $data = array(1,'b'=>'a string');
  7. $cookie = new T_Cage_Cookie($data);
  8. $this->assertSame($data,$cookie->uncage());
  9. }
  10. function testCanDeleteCookie()
  11. {
  12. $cookie = new T_Test_Cage_CookieStub(array('key'=>'value'));
  13. $cookie->delete('key');
  14. $this->assertSame(array(),$cookie->uncage());
  15. $this->assertSame(1,$cookie->getNumberCookiesSet());
  16. $cookie = $cookie->getSetCookieArgArray(0);
  17. $this->assertSame($cookie['name'],'key');
  18. $this->assertTrue($cookie['expires']<time());
  19. }
  20. function testCookieSetPassesDataToSetCookie()
  21. {
  22. $cookie = new T_Test_Cage_CookieStub(array());
  23. $expiry = time()+60;
  24. $cookie->set('key','value',$expiry,'/some/path/','.domain.com',true);
  25. $this->assertSame(array( 'name' => 'key',
  26. 'value' => 'value',
  27. 'expires' => $expiry,
  28. 'path' => '/some/path/',
  29. 'domain' => '.domain.com',
  30. 'secure' => true ),
  31. $cookie->getSetCookieArgArray(0) );
  32. }
  33. function testCookieSetPopulatesCurrentCookieInstanceData()
  34. {
  35. $cookie = new T_Test_Cage_CookieStub(array());
  36. $cookie->set('key','value');
  37. $this->assertSame('value',$cookie->asScalar('key')->uncage());
  38. }
  39. function testCookieSetWithPassedExpiryResultsInNullCookieBeingSet()
  40. {
  41. $cookie = new T_Test_Cage_CookieStub(array('key'=>'value'));
  42. $expiry = time()-200;
  43. $cookie->set('key','anyvalue',$expiry);
  44. $this->assertSame(array( 'name' => 'key',
  45. 'value' => null,
  46. 'expires' => $expiry,
  47. 'path' => null,
  48. 'domain' => null,
  49. 'secure' => null ),
  50. $cookie->getSetCookieArgArray(0) );
  51. $this->assertFalse($cookie->exists('key'));
  52. }
  53. function testCookieSetHasAFluentInterface()
  54. {
  55. $cookie = new T_Test_Cage_CookieStub(array());
  56. $test = $cookie->set('key','value');
  57. $this->assertSame($cookie,$test);
  58. }
  59. function testCookieGivenCustomDomainMaintainedWhenServerAppRootIsSet()
  60. {
  61. $url = new T_Url('http','example.com',array('path'));
  62. $cookie = new T_Test_Cage_CookieStub(array());
  63. $this->assertSame($cookie,$cookie->setRootUrl($url));
  64. $cookie->set('key','value',null,null,'.domain.com');
  65. $test = $cookie->getSetCookieArgArray(0);
  66. $this->assertSame('.domain.com',$test['domain']);
  67. $this->assertSame(null,$test['path']);
  68. }
  69. function testCookieGivenCustomPathMaintainedWhenServerAppRootIsSet()
  70. {
  71. $url = new T_Url('http','example.com',array('path'));
  72. $cookie = new T_Test_Cage_CookieStub(array());
  73. $cookie->setRootUrl($url);
  74. $cookie->set('key','value',null,'/some/path/');
  75. $test = $cookie->getSetCookieArgArray(0);
  76. $this->assertSame(null,$test['domain']);
  77. $this->assertSame('/some/path/',$test['path']);
  78. }
  79. function testPathAndDomainSetFromServerAppRootByDefault()
  80. {
  81. $url = new T_Url('http','example.com',array('some','path'));
  82. $cookie = new T_Test_Cage_CookieStub(array());
  83. $cookie->setRootUrl($url);
  84. $cookie->set('key','value');
  85. $test = $cookie->getSetCookieArgArray(0);
  86. $this->assertSame('.example.com',$test['domain']);
  87. $this->assertSame('/some/path/',$test['path']);
  88. }
  89. function testDomainNotSetFromServerAppRootWhenIsTopLevelDomain()
  90. {
  91. $url = new T_Url('http','localhost',array('some','path'));
  92. $cookie = new T_Test_Cage_CookieStub(array());
  93. $cookie->setRootUrl($url);
  94. $cookie->set('key','value');
  95. $test = $cookie->getSetCookieArgArray(0);
  96. $this->assertSame(null,$test['domain']);
  97. $this->assertSame('/some/path/',$test['path']);
  98. }
  99. function testSetFailureWhenDomainIsTopLevelDomain()
  100. {
  101. $cookie = new T_Test_Cage_CookieStub(array());
  102. try {
  103. $cookie->set('key','value',null,null,'localhost');
  104. $this->fail();
  105. } catch (InvalidArgumentException $e) {
  106. $this->assertContains('localhost',$e->getMessage());
  107. }
  108. }
  109. function testEmptyPathFromAppRootSetAsSingleForwardSlash()
  110. {
  111. $url = new T_Url('http','example.com',array());
  112. $cookie = new T_Test_Cage_CookieStub(array());
  113. $cookie->setRootUrl($url);
  114. $cookie->set('key','value');
  115. $test = $cookie->getSetCookieArgArray(0);
  116. $this->assertSame('.example.com',$test['domain']);
  117. $this->assertSame('/',$test['path']);
  118. }
  119. function testWwwPrefixStrippedFromAppRootDomain()
  120. {
  121. $url = new T_Url('http','www.example.com',array());
  122. $cookie = new T_Test_Cage_CookieStub(array());
  123. $cookie->setRootUrl($url);
  124. $cookie->set('key','value');
  125. $test = $cookie->getSetCookieArgArray(0);
  126. $this->assertSame('.example.com',$test['domain']);
  127. }
  128. function testAnyPortInformationStrippedFromAppRootDomain()
  129. {
  130. $url = new T_Url('http','www.example.com:413',array());
  131. $cookie = new T_Test_Cage_CookieStub(array());
  132. $cookie->setRootUrl($url);
  133. $cookie->set('key','value');
  134. $test = $cookie->getSetCookieArgArray(0);
  135. $this->assertSame('.example.com',$test['domain']);
  136. }
  137. }