SessionHandlerTest.php 7.9 KB

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