SessionHandlerTest.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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-2015 Zend Technologies USA Inc. (http://www.zend.com)
  20. * @license http://framework.zend.com/license/new-bsd New BSD License
  21. */
  22. /** Zend_Service_WindowsAzure_SessionHandler */
  23. require_once 'Zend/Service/WindowsAzure/SessionHandler.php';
  24. /** Zend_Service_WindowsAzure_Storage_Table */
  25. require_once 'Zend/Service/WindowsAzure/Storage/Table.php';
  26. /**
  27. * @category Zend
  28. * @package Zend_Service_WindowsAzure
  29. * @subpackage UnitTests
  30. * @version $Id$
  31. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  32. * @license http://framework.zend.com/license/new-bsd New BSD License
  33. */
  34. class Zend_Service_WindowsAzure_SessionHandlerTest extends PHPUnit_Framework_TestCase
  35. {
  36. protected static $uniqId = 0;
  37. public function __construct()
  38. {
  39. }
  40. /**
  41. * Test setup
  42. */
  43. protected function setUp()
  44. {
  45. if (!TESTS_ZEND_SERVICE_WINDOWSAZURE_SESSIONHANDLER_RUNTESTS) {
  46. $this->markTestSkipped('This test case requires TESTS_ZEND_SERVICE_WINDOWSAZURE_SESSIONHANDLER_RUNTESTS to be enabled in TestConfiguration.php');
  47. }
  48. }
  49. /**
  50. * Test teardown
  51. */
  52. protected function tearDown()
  53. {
  54. if ($this->status == PHPUnit_Runner_BaseTestRunner::STATUS_SKIPPED) {
  55. return;
  56. }
  57. $storageClient = $this->createStorageInstance();
  58. for ($i = 1; $i <= self::$uniqId; $i++)
  59. {
  60. try { $storageClient->deleteTable(TESTS_ZEND_SERVICE_WINDOWSAZURE_SESSIONHANDLER_TABLENAME_PREFIX . $i); } catch (Exception $e) { }
  61. }
  62. }
  63. protected function createStorageInstance()
  64. {
  65. $storageClient = null;
  66. if (TESTS_ZEND_SERVICE_WINDOWSAZURE_SESSIONHANDLER_RUNONPROD) {
  67. $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));
  68. } else {
  69. $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));
  70. }
  71. if (TESTS_ZEND_SERVICE_WINDOWSAZURE_STORAGE_USEPROXY) {
  72. $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);
  73. }
  74. return $storageClient;
  75. }
  76. protected function createSessionHandler($storageInstance, $tableName)
  77. {
  78. $sessionHandler = new Zend_Service_WindowsAzure_SessionHandler(
  79. $storageInstance,
  80. $tableName
  81. );
  82. return $sessionHandler;
  83. }
  84. protected function generateName()
  85. {
  86. self::$uniqId++;
  87. return TESTS_ZEND_SERVICE_WINDOWSAZURE_SESSIONHANDLER_TABLENAME_PREFIX . self::$uniqId;
  88. }
  89. /**
  90. * Test register
  91. */
  92. public function testRegister()
  93. {
  94. $storageClient = $this->createStorageInstance();
  95. $tableName = $this->generateName();
  96. $sessionHandler = $this->createSessionHandler($storageClient, $tableName);
  97. $result = $sessionHandler->register();
  98. $this->assertTrue($result);
  99. }
  100. /**
  101. * Test open
  102. */
  103. public function testOpen()
  104. {
  105. $storageClient = $this->createStorageInstance();
  106. $tableName = $this->generateName();
  107. $sessionHandler = $this->createSessionHandler($storageClient, $tableName);
  108. $result = $sessionHandler->open();
  109. $this->assertTrue($result);
  110. $verifyResult = $storageClient->listTables();
  111. $this->assertEquals($tableName, $verifyResult[0]->Name);
  112. }
  113. /**
  114. * Test close
  115. */
  116. public function testClose()
  117. {
  118. $storageClient = $this->createStorageInstance();
  119. $tableName = $this->generateName();
  120. $sessionHandler = $this->createSessionHandler($storageClient, $tableName);
  121. $sessionHandler->open();
  122. $result = $sessionHandler->close();
  123. $this->assertTrue($result);
  124. }
  125. /**
  126. * Test read
  127. */
  128. public function testRead()
  129. {
  130. $storageClient = $this->createStorageInstance();
  131. $tableName = $this->generateName();
  132. $sessionHandler = $this->createSessionHandler($storageClient, $tableName);
  133. $sessionHandler->open();
  134. $sessionId = $this->session_id();
  135. $sessionData = serialize( 'PHPAzure' );
  136. $sessionHandler->write($sessionId, $sessionData);
  137. $result = unserialize( $sessionHandler->read($sessionId) );
  138. $this->assertEquals('PHPAzure', $result);
  139. }
  140. /**
  141. * Test write
  142. */
  143. public function testWrite()
  144. {
  145. if (TESTS_ZEND_SERVICE_WINDOWSAZURE_SESSIONHANDLER_RUNTESTS) {
  146. $storageClient = $this->createStorageInstance();
  147. $tableName = $this->generateName();
  148. $sessionHandler = $this->createSessionHandler($storageClient, $tableName);
  149. $sessionHandler->open();
  150. $sessionId = $this->session_id();
  151. $sessionData = serialize( 'PHPAzure' );
  152. $sessionHandler->write($sessionId, $sessionData);
  153. $verifyResult = $storageClient->retrieveEntities($tableName);
  154. $this->assertEquals(1, count($verifyResult));
  155. }
  156. }
  157. /**
  158. * Test destroy
  159. */
  160. public function testDestroy()
  161. {
  162. $storageClient = $this->createStorageInstance();
  163. $tableName = $this->generateName();
  164. $sessionHandler = $this->createSessionHandler($storageClient, $tableName);
  165. $sessionHandler->open();
  166. $sessionId = $this->session_id();
  167. $sessionData = serialize( 'PHPAzure' );
  168. $sessionHandler->write($sessionId, $sessionData);
  169. $result = $sessionHandler->destroy($sessionId);
  170. $this->assertTrue($result);
  171. $verifyResult = $storageClient->retrieveEntities($tableName);
  172. $this->assertEquals(0, count($verifyResult));
  173. }
  174. /**
  175. * Test gc
  176. */
  177. public function testGc()
  178. {
  179. $storageClient = $this->createStorageInstance();
  180. $tableName = $this->generateName();
  181. $sessionHandler = $this->createSessionHandler($storageClient, $tableName);
  182. $sessionHandler->open();
  183. $sessionId = $this->session_id();
  184. $sessionData = serialize( 'PHPAzure' );
  185. $sessionHandler->write($sessionId, $sessionData);
  186. sleep(1); // let time() tick
  187. $result = $sessionHandler->gc(0);
  188. $this->assertTrue($result);
  189. $verifyResult = $storageClient->retrieveEntities($tableName);
  190. $this->assertEquals(0, count($verifyResult));
  191. }
  192. protected function session_id()
  193. {
  194. return md5(self::$uniqId);
  195. }
  196. }