SessionHandlerTest.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Service_WindowsAzure
  17. * @subpackage UnitTests
  18. * @version $Id$
  19. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  20. * @license http://framework.zend.com/license/new-bsd New BSD License
  21. */
  22. /**
  23. * Test helpers
  24. */
  25. require_once dirname(__FILE__) . '/../../../TestHelper.php';
  26. /** Zend_Service_WindowsAzure_SessionHandler */
  27. require_once 'Zend/Service/WindowsAzure/SessionHandler.php';
  28. /** Zend_Service_WindowsAzure_Storage_Table */
  29. require_once 'Zend/Service/WindowsAzure/Storage/Table.php';
  30. /**
  31. * @category Zend
  32. * @package Zend_Service_WindowsAzure
  33. * @subpackage UnitTests
  34. * @version $Id$
  35. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  36. * @license http://framework.zend.com/license/new-bsd New BSD License
  37. */
  38. class Zend_Service_WindowsAzure_SessionHandlerTest extends PHPUnit_Framework_TestCase
  39. {
  40. protected static $uniqId = 0;
  41. public function __construct()
  42. {
  43. }
  44. /**
  45. * Test setup
  46. */
  47. protected function setUp()
  48. {
  49. if (!TESTS_ZEND_SERVICE_WINDOWSAZURE_SESSIONHANDLER_RUNTESTS) {
  50. $this->markTestSkipped('This test case requires TESTS_ZEND_SERVICE_WINDOWSAZURE_SESSIONHANDLER_RUNTESTS to be enabled in TestConfiguration.php');
  51. }
  52. }
  53. /**
  54. * Test teardown
  55. */
  56. protected function tearDown()
  57. {
  58. if ($this->status == PHPUnit_Runner_BaseTestRunner::STATUS_SKIPPED) {
  59. return;
  60. }
  61. $storageClient = $this->createStorageInstance();
  62. for ($i = 1; $i <= self::$uniqId; $i++)
  63. {
  64. try { $storageClient->deleteTable(TESTS_ZEND_SERVICE_WINDOWSAZURE_SESSIONHANDLER_TABLENAME_PREFIX . $i); } catch (Exception $e) { }
  65. }
  66. }
  67. protected function createStorageInstance()
  68. {
  69. $storageClient = null;
  70. if (TESTS_ZEND_SERVICE_WINDOWSAZURE_SESSIONHANDLER_RUNONPROD) {
  71. $storageClient = new Zend_Service_WindowsAzure_Storage_Table(TESTS_ZEND_SERVICE_WINDOWSAZURE_TABLE_HOST_PROD, TESTS_ZEND_SERVICE_WINDOWSAZURE_STORAGE_ACCOUNT_PROD, TESTS_ZEND_SERVICE_WINDOWSAZURE_STORAGE_KEY_PROD, false, Zend_Service_WindowsAzure_RetryPolicy_RetryPolicyAbstract::retryN(10, 250));
  72. } else {
  73. $storageClient = new Zend_Service_WindowsAzure_Storage_Table(TESTS_ZEND_SERVICE_WINDOWSAZURE_TABLE_HOST_DEV, TESTS_ZEND_SERVICE_WINDOWSAZURE_STORAGE_ACCOUNT_DEV, TESTS_ZEND_SERVICE_WINDOWSAZURE_STORAGE_KEY_DEV, true, Zend_Service_WindowsAzure_RetryPolicy_RetryPolicyAbstract::retryN(10, 250));
  74. }
  75. if (TESTS_ZEND_SERVICE_WINDOWSAZURE_STORAGE_USEPROXY) {
  76. $storageClient->setProxy(TESTS_ZEND_SERVICE_WINDOWSAZURE_STORAGE_USEPROXY, TESTS_ZEND_SERVICE_WINDOWSAZURE_STORAGE_PROXY, TESTS_ZEND_SERVICE_WINDOWSAZURE_STORAGE_PROXY_PORT, TESTS_ZEND_SERVICE_WINDOWSAZURE_STORAGE_PROXY_CREDENTIALS);
  77. }
  78. return $storageClient;
  79. }
  80. protected function createSessionHandler($storageInstance, $tableName)
  81. {
  82. $sessionHandler = new Zend_Service_WindowsAzure_SessionHandler(
  83. $storageInstance,
  84. $tableName
  85. );
  86. return $sessionHandler;
  87. }
  88. protected function generateName()
  89. {
  90. self::$uniqId++;
  91. return TESTS_ZEND_SERVICE_WINDOWSAZURE_SESSIONHANDLER_TABLENAME_PREFIX . self::$uniqId;
  92. }
  93. /**
  94. * Test register
  95. */
  96. public function testRegister()
  97. {
  98. $storageClient = $this->createStorageInstance();
  99. $tableName = $this->generateName();
  100. $sessionHandler = $this->createSessionHandler($storageClient, $tableName);
  101. $result = $sessionHandler->register();
  102. $this->assertTrue($result);
  103. }
  104. /**
  105. * Test open
  106. */
  107. public function testOpen()
  108. {
  109. $storageClient = $this->createStorageInstance();
  110. $tableName = $this->generateName();
  111. $sessionHandler = $this->createSessionHandler($storageClient, $tableName);
  112. $result = $sessionHandler->open();
  113. $this->assertTrue($result);
  114. $verifyResult = $storageClient->listTables();
  115. $this->assertEquals($tableName, $verifyResult[0]->Name);
  116. }
  117. /**
  118. * Test close
  119. */
  120. public function testClose()
  121. {
  122. $storageClient = $this->createStorageInstance();
  123. $tableName = $this->generateName();
  124. $sessionHandler = $this->createSessionHandler($storageClient, $tableName);
  125. $sessionHandler->open();
  126. $result = $sessionHandler->close();
  127. $this->assertTrue($result);
  128. }
  129. /**
  130. * Test read
  131. */
  132. public function testRead()
  133. {
  134. $storageClient = $this->createStorageInstance();
  135. $tableName = $this->generateName();
  136. $sessionHandler = $this->createSessionHandler($storageClient, $tableName);
  137. $sessionHandler->open();
  138. $sessionId = $this->session_id();
  139. $sessionData = serialize( 'PHPAzure' );
  140. $sessionHandler->write($sessionId, $sessionData);
  141. $result = unserialize( $sessionHandler->read($sessionId) );
  142. $this->assertEquals('PHPAzure', $result);
  143. }
  144. /**
  145. * Test write
  146. */
  147. public function testWrite()
  148. {
  149. if (TESTS_ZEND_SERVICE_WINDOWSAZURE_SESSIONHANDLER_RUNTESTS) {
  150. $storageClient = $this->createStorageInstance();
  151. $tableName = $this->generateName();
  152. $sessionHandler = $this->createSessionHandler($storageClient, $tableName);
  153. $sessionHandler->open();
  154. $sessionId = $this->session_id();
  155. $sessionData = serialize( 'PHPAzure' );
  156. $sessionHandler->write($sessionId, $sessionData);
  157. $verifyResult = $storageClient->retrieveEntities($tableName);
  158. $this->assertEquals(1, count($verifyResult));
  159. }
  160. }
  161. /**
  162. * Test destroy
  163. */
  164. public function testDestroy()
  165. {
  166. $storageClient = $this->createStorageInstance();
  167. $tableName = $this->generateName();
  168. $sessionHandler = $this->createSessionHandler($storageClient, $tableName);
  169. $sessionHandler->open();
  170. $sessionId = $this->session_id();
  171. $sessionData = serialize( 'PHPAzure' );
  172. $sessionHandler->write($sessionId, $sessionData);
  173. $result = $sessionHandler->destroy($sessionId);
  174. $this->assertTrue($result);
  175. $verifyResult = $storageClient->retrieveEntities($tableName);
  176. $this->assertEquals(0, count($verifyResult));
  177. }
  178. /**
  179. * Test gc
  180. */
  181. public function testGc()
  182. {
  183. $storageClient = $this->createStorageInstance();
  184. $tableName = $this->generateName();
  185. $sessionHandler = $this->createSessionHandler($storageClient, $tableName);
  186. $sessionHandler->open();
  187. $sessionId = $this->session_id();
  188. $sessionData = serialize( 'PHPAzure' );
  189. $sessionHandler->write($sessionId, $sessionData);
  190. sleep(1); // let time() tick
  191. $result = $sessionHandler->gc(0);
  192. $this->assertTrue($result);
  193. $verifyResult = $storageClient->retrieveEntities($tableName);
  194. $this->assertEquals(0, count($verifyResult));
  195. }
  196. protected function session_id()
  197. {
  198. return md5(self::$uniqId);
  199. }
  200. }