2
0

SessionHandlerTest.php 8.0 KB

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