BlobStreamTest.php 11 KB

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