BlobStreamTest.php 9.3 KB

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