BlobStreamTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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: BlobStreamTest.php 24354 2009-07-24 08:48:54Z unknown $
  19. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  20. * @license http://framework.zend.com/license/new-bsd New BSD License
  21. */
  22. require_once dirname(dirname(dirname(dirname(__FILE__)))) . '/TestHelper.php';
  23. if (!defined('PHPUnit_MAIN_METHOD')) {
  24. define('PHPUnit_MAIN_METHOD', 'Zend_Service_WindowsAzure_BlobStreamTest::main');
  25. }
  26. require_once 'Zend/Service/WindowsAzure/Storage/Blob.php';
  27. /**
  28. * @category Zend
  29. * @package Zend_Service_WindowsAzure
  30. * @subpackage UnitTests
  31. * @group Zend_Service
  32. * @group Zend_Service_WindowsAzure
  33. * @version $Id: BlobStreamTest.php 24354 2009-07-24 08:48:54Z unknown $
  34. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. */
  37. class Zend_Service_WindowsAzure_BlobStreamTest extends PHPUnit_Framework_TestCase
  38. {
  39. static $path;
  40. public function __construct()
  41. {
  42. self::$path = dirname(__FILE__).'/_files/';
  43. }
  44. public static function main()
  45. {
  46. if (TESTS_ZEND_SERVICE_WINDOWSAZURE_BLOB_RUNTESTS) {
  47. $suite = new PHPUnit_Framework_TestSuite("Zend_Service_WindowsAzure_BlobStreamTest");
  48. $result = PHPUnit_TextUI_TestRunner::run($suite);
  49. }
  50. }
  51. /**
  52. * Test setup
  53. */
  54. protected function setUp()
  55. {
  56. }
  57. /**
  58. * Test teardown
  59. */
  60. protected function tearDown()
  61. {
  62. $storageClient = $this->createStorageInstance();
  63. for ($i = 1; $i <= self::$uniqId; $i++)
  64. {
  65. try { $storageClient->deleteContainer(TESTS_ZEND_SERVICE_WINDOWSAZURE_BLOBSTREAM_CONTAINER_PREFIX . $i); } catch (Exception $e) { }
  66. try { $storageClient->unregisterStreamWrapper('azure'); } catch (Exception $e) { }
  67. }
  68. }
  69. protected function createStorageInstance()
  70. {
  71. $storageClient = null;
  72. if (TESTS_ZEND_SERVICE_WINDOWSAZURE_BLOB_RUNONPROD) {
  73. $storageClient = new Zend_Service_WindowsAzure_Storage_Blob(TESTS_ZEND_SERVICE_WINDOWSAZURE_BLOB_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));
  74. } else {
  75. $storageClient = new Zend_Service_WindowsAzure_Storage_Blob(TESTS_ZEND_SERVICE_WINDOWSAZURE_BLOB_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));
  76. }
  77. if (TESTS_ZEND_SERVICE_WINDOWSAZURE_STORAGE_USEPROXY) {
  78. $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);
  79. }
  80. return $storageClient;
  81. }
  82. protected static $uniqId = 0;
  83. protected function generateName()
  84. {
  85. self::$uniqId++;
  86. return TESTS_ZEND_SERVICE_WINDOWSAZURE_BLOBSTREAM_CONTAINER_PREFIX . self::$uniqId;
  87. }
  88. /**
  89. * Test read file
  90. */
  91. public function testReadFile()
  92. {
  93. if (TESTS_ZEND_SERVICE_WINDOWSAZURE_BLOB_RUNTESTS) {
  94. $containerName = $this->generateName();
  95. $fileName = 'azure://' . $containerName . '/test.txt';
  96. $storageClient = $this->createStorageInstance();
  97. $storageClient->registerStreamWrapper();
  98. $fh = fopen($fileName, 'w');
  99. fwrite($fh, "Hello world!");
  100. fclose($fh);
  101. $result = file_get_contents($fileName);
  102. $storageClient->unregisterStreamWrapper();
  103. $this->assertEquals('Hello world!', $result);
  104. }
  105. }
  106. /**
  107. * Test write file
  108. */
  109. public function testWriteFile()
  110. {
  111. if (TESTS_ZEND_SERVICE_WINDOWSAZURE_BLOB_RUNTESTS) {
  112. $containerName = $this->generateName();
  113. $fileName = 'azure://' . $containerName . '/test.txt';
  114. $storageClient = $this->createStorageInstance();
  115. $storageClient->registerStreamWrapper();
  116. $fh = fopen($fileName, 'w');
  117. fwrite($fh, "Hello world!");
  118. fclose($fh);
  119. $storageClient->unregisterStreamWrapper();
  120. $instance = $storageClient->getBlobInstance($containerName, 'test.txt');
  121. $this->assertEquals('test.txt', $instance->Name);
  122. }
  123. }
  124. /**
  125. * Test unlink file
  126. */
  127. public function testUnlinkFile()
  128. {
  129. if (TESTS_ZEND_SERVICE_WINDOWSAZURE_BLOB_RUNTESTS) {
  130. $containerName = $this->generateName();
  131. $fileName = 'azure://' . $containerName . '/test.txt';
  132. $storageClient = $this->createStorageInstance();
  133. $storageClient->registerStreamWrapper();
  134. $fh = fopen($fileName, 'w');
  135. fwrite($fh, "Hello world!");
  136. fclose($fh);
  137. unlink($fileName);
  138. $storageClient->unregisterStreamWrapper();
  139. $result = $storageClient->listBlobs($containerName);
  140. $this->assertEquals(0, count($result));
  141. }
  142. }
  143. /**
  144. * Test copy file
  145. */
  146. public function testCopyFile()
  147. {
  148. if (TESTS_ZEND_SERVICE_WINDOWSAZURE_BLOB_RUNTESTS) {
  149. $containerName = $this->generateName();
  150. $sourceFileName = 'azure://' . $containerName . '/test.txt';
  151. $destinationFileName = 'azure://' . $containerName . '/test2.txt';
  152. $storageClient = $this->createStorageInstance();
  153. $storageClient->registerStreamWrapper();
  154. $fh = fopen($sourceFileName, 'w');
  155. fwrite($fh, "Hello world!");
  156. fclose($fh);
  157. copy($sourceFileName, $destinationFileName);
  158. $storageClient->unregisterStreamWrapper();
  159. $instance = $storageClient->getBlobInstance($containerName, 'test2.txt');
  160. $this->assertEquals('test2.txt', $instance->Name);
  161. }
  162. }
  163. /**
  164. * Test rename file
  165. */
  166. public function testRenameFile()
  167. {
  168. if (TESTS_ZEND_SERVICE_WINDOWSAZURE_BLOB_RUNTESTS) {
  169. $containerName = $this->generateName();
  170. $sourceFileName = 'azure://' . $containerName . '/test.txt';
  171. $destinationFileName = 'azure://' . $containerName . '/test2.txt';
  172. $storageClient = $this->createStorageInstance();
  173. $storageClient->registerStreamWrapper();
  174. $fh = fopen($sourceFileName, 'w');
  175. fwrite($fh, "Hello world!");
  176. fclose($fh);
  177. rename($sourceFileName, $destinationFileName);
  178. $storageClient->unregisterStreamWrapper();
  179. $instance = $storageClient->getBlobInstance($containerName, 'test2.txt');
  180. $this->assertEquals('test2.txt', $instance->Name);
  181. }
  182. }
  183. /**
  184. * Test mkdir
  185. */
  186. public function testMkdir()
  187. {
  188. if (TESTS_ZEND_SERVICE_WINDOWSAZURE_BLOB_RUNTESTS) {
  189. $containerName = $this->generateName();
  190. $storageClient = $this->createStorageInstance();
  191. $storageClient->registerStreamWrapper();
  192. mkdir('azure://' . $containerName);
  193. $storageClient->unregisterStreamWrapper();
  194. $result = $storageClient->listContainers();
  195. $this->assertEquals(1, count($result));
  196. $this->assertEquals($containerName, $result[0]->Name);
  197. }
  198. }
  199. /**
  200. * Test rmdir
  201. */
  202. public function testRmdir()
  203. {
  204. if (TESTS_ZEND_SERVICE_WINDOWSAZURE_BLOB_RUNTESTS) {
  205. $containerName = $this->generateName();
  206. $storageClient = $this->createStorageInstance();
  207. $storageClient->registerStreamWrapper();
  208. mkdir('azure://' . $containerName);
  209. rmdir('azure://' . $containerName);
  210. $storageClient->unregisterStreamWrapper();
  211. $result = $storageClient->listContainers();
  212. $this->assertEquals(0, count($result));
  213. }
  214. }
  215. /**
  216. * Test opendir
  217. */
  218. public function testOpendir()
  219. {
  220. if (TESTS_ZEND_SERVICE_WINDOWSAZURE_BLOB_RUNTESTS) {
  221. $containerName = $this->generateName();
  222. $storageClient = $this->createStorageInstance();
  223. $storageClient->createContainer($containerName);
  224. $storageClient->putBlob($containerName, 'images/WindowsAzure1.gif', self::$path . 'WindowsAzure.gif');
  225. $storageClient->putBlob($containerName, 'images/WindowsAzure2.gif', self::$path . 'WindowsAzure.gif');
  226. $storageClient->putBlob($containerName, 'images/WindowsAzure3.gif', self::$path . 'WindowsAzure.gif');
  227. $storageClient->putBlob($containerName, 'images/WindowsAzure4.gif', self::$path . 'WindowsAzure.gif');
  228. $storageClient->putBlob($containerName, 'images/WindowsAzure5.gif', self::$path . 'WindowsAzure.gif');
  229. $result1 = $storageClient->listBlobs($containerName);
  230. $storageClient->registerStreamWrapper();
  231. $result2 = array();
  232. if ($handle = opendir('azure://' . $containerName)) {
  233. while (false !== ($file = readdir($handle))) {
  234. $result2[] = $file;
  235. }
  236. closedir($handle);
  237. }
  238. $storageClient->unregisterStreamWrapper();
  239. $result = $storageClient->listContainers();
  240. $this->assertEquals(count($result1), count($result2));
  241. }
  242. }
  243. }
  244. // Call Zend_Service_WindowsAzure_BlobStreamTest::main() if this source file is executed directly.
  245. if (PHPUnit_MAIN_METHOD == "Zend_Service_WindowsAzure_BlobStreamTest::main") {
  246. Zend_Service_WindowsAzure_BlobStreamTest::main();
  247. }