BlobStreamTest.php 11 KB

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