BlobStreamTest.php 8.8 KB

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