2
0

StreamTest.php 7.0 KB

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