StreamTest.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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_Amazon_S3
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * Test helper
  24. */
  25. require_once dirname(__FILE__) . '/../../../../TestHelper.php';
  26. /**
  27. * @see Zend_Service_Amazon
  28. */
  29. require_once 'Zend/Service/Amazon/S3.php';
  30. /**
  31. * @see Zend_Http_Client_Adapter_Socket
  32. */
  33. require_once 'Zend/Http/Client/Adapter/Socket.php';
  34. /**
  35. * @category Zend
  36. * @package Zend_Service_Amazon_S3
  37. * @subpackage UnitTests
  38. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  39. * @license http://framework.zend.com/license/new-bsd New BSD License
  40. * @group Zend_Service
  41. * @group Zend_Service_Amazon
  42. * @group Zend_Service_Amazon_S3
  43. */
  44. class Zend_Service_Amazon_S3_StreamTest extends PHPUnit_Framework_TestCase
  45. {
  46. /**
  47. * Sets up this test case
  48. *
  49. * @return void
  50. */
  51. public function setUp()
  52. {
  53. $this->_amazon = new Zend_Service_Amazon_S3(constant('TESTS_ZEND_SERVICE_AMAZON_ONLINE_ACCESSKEYID'),
  54. constant('TESTS_ZEND_SERVICE_AMAZON_ONLINE_SECRETKEY')
  55. );
  56. $this->_nosuchbucket = "nonexistingbucketnamewhichnobodyshoulduse";
  57. $this->_httpClientAdapterSocket = new Zend_Http_Client_Adapter_Socket();
  58. $this->_bucket = constant('TESTS_ZEND_SERVICE_AMAZON_S3_BUCKET');
  59. $this->_bucketName = "s3://".$this->_bucket;
  60. $this->_fileName = $this->_bucketName."/sample_file.txt";
  61. $this->_amazon->getHttpClient()
  62. ->setAdapter($this->_httpClientAdapterSocket);
  63. $this->_amazon->registerStreamWrapper();
  64. $this->_amazon->cleanBucket($this->_bucket);
  65. $this->_amazon->removeBucket($this->_bucket);
  66. // terms of use compliance: no more than one query per second
  67. sleep(1);
  68. }
  69. /**
  70. * Tear down each test
  71. *
  72. * @return void
  73. */
  74. public function tearDown()
  75. {
  76. $this->_amazon->unregisterStreamWrapper();
  77. $buckets = $this->_amazon->getBuckets();
  78. foreach($buckets as $bucket) {
  79. if(substr($bucket, 0, strlen($this->_bucket)) != $this->_bucket) {
  80. continue;
  81. }
  82. $this->_amazon->cleanBucket($bucket);
  83. $this->_amazon->removeBucket($bucket);
  84. }
  85. }
  86. /**
  87. * Test creating and removing buckets
  88. *
  89. * @return void
  90. */
  91. public function testBuckets()
  92. {
  93. // Create the bucket
  94. $result = mkdir($this->_bucketName);
  95. $this->assertTrue($result);
  96. // Remove the bucket
  97. $result = rmdir($this->_bucketName);
  98. $this->assertTrue($result);
  99. }
  100. /**
  101. * Test writing to an object
  102. *
  103. * @return void
  104. */
  105. public function testWriteObject()
  106. {
  107. // Create the bucket
  108. $result = mkdir($this->_bucketName);
  109. $this->assertTrue($result);
  110. // Generate sample data
  111. $data = str_repeat('x', 10000);
  112. // Write to an object
  113. $size = file_put_contents($this->_fileName, $data);
  114. $this->assertEquals(strlen($data), $size);
  115. // Write to an object
  116. $f = fopen($this->_fileName, 'w');
  117. for ($i = 0; $i < 100; $i++) {
  118. fwrite($f, 'x');
  119. }
  120. fclose($f);
  121. unset($data);
  122. // Check object size
  123. $size = filesize($this->_fileName);
  124. $this->assertEquals(100, $size);
  125. // Remove the object
  126. $result = unlink($this->_fileName);
  127. $this->assertTrue($result);
  128. }
  129. /**
  130. * Test reading from an object
  131. *
  132. * @group ZF-10035
  133. * @return void
  134. */
  135. public function testReadObject()
  136. {
  137. // Create the bucket
  138. $result = mkdir($this->_bucketName);
  139. $this->assertTrue($result);
  140. // Generate sample data
  141. $data = str_repeat('x', 10000);
  142. // Write to an object
  143. $size = file_put_contents($this->_fileName, $data);
  144. $this->assertEquals(strlen($data), $size);
  145. // Read from an object
  146. $new_data = file_get_contents($this->_fileName);
  147. $this->assertEquals($data, $new_data);
  148. // Read from an oject
  149. $new_data = '';
  150. $f = fopen($this->_fileName, 'r');
  151. fseek($f, 1000);
  152. while (!feof($f)) {
  153. $chunk = fread($f, 1000);
  154. $new_data .= $chunk;
  155. $this->assertEquals(strlen($chunk), 1000);
  156. }
  157. fclose($f);
  158. $this->assertEquals(substr($data, 1000), $new_data);
  159. unset($data);
  160. unset($new_data);
  161. // Remove the object
  162. $result = unlink($this->_fileName);
  163. $this->assertTrue($result);
  164. }
  165. /**
  166. * Test getting the list of available buckets
  167. *
  168. * @return void
  169. */
  170. public function testGetBucketList()
  171. {
  172. $buckets = array($this->_bucket.'zf-test1', $this->_bucket.'zf-test2', $this->_bucket.'zf-test3');
  173. // Create the buckets
  174. foreach ($buckets as $bucket) {
  175. $result = mkdir('s3://'.$bucket);
  176. $this->assertTrue($result);
  177. }
  178. $online_buckets = array();
  179. // Retrieve list of buckets on S3
  180. $e = opendir('s3://');
  181. while (($f = readdir($e)) !== false) {
  182. $online_buckets[] = $f;
  183. }
  184. closedir($e);
  185. // Check that each bucket is in our original list
  186. foreach ($buckets as $bucket) {
  187. $this->assertContains($bucket, $online_buckets);
  188. }
  189. // Remove the buckets
  190. foreach ($buckets as $bucket) {
  191. $result = rmdir('s3://'.$bucket);
  192. $this->assertTrue($result);
  193. }
  194. }
  195. /**
  196. * Test object stat
  197. *
  198. * @return void
  199. */
  200. public function testObjectStat()
  201. {
  202. // Create the bucket
  203. $result = mkdir($this->_bucketName);
  204. $this->assertTrue($result);
  205. $this->assertTrue(is_dir($this->_bucketName));
  206. $data = str_repeat('x', 10000);
  207. $len = strlen($data);
  208. // Write to an object
  209. $size = file_put_contents($this->_fileName, $data);
  210. $this->assertEquals($len, $size);
  211. $this->assertFalse(is_dir($this->_fileName));
  212. // Stat an object
  213. $info = stat($this->_fileName);
  214. $this->assertEquals($len, $info['size']);
  215. unset($data);
  216. // Remove the object
  217. $result = unlink($this->_fileName);
  218. $this->assertTrue($result);
  219. }
  220. }
  221. /**
  222. * @category Zend
  223. * @package Zend_Service_Amazon_S3
  224. * @subpackage UnitTests
  225. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  226. * @license http://framework.zend.com/license/new-bsd New BSD License
  227. * @group Zend_Service
  228. * @group Zend_Service_Amazon
  229. * @group Zend_Service_Amazon_S3
  230. */
  231. class Zend_Service_Amazon_S3_StreamTest_Skip extends PHPUnit_Framework_TestCase
  232. {
  233. public function setUp()
  234. {
  235. $this->markTestSkipped('Zend_Service_Amazon_S3 online tests not enabled with an access key ID and '
  236. . ' secret key ID in TestConfiguration.php');
  237. }
  238. public function testNothing()
  239. {
  240. }
  241. }