StreamTest.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. $this->_amazon->cleanBucket($this->_bucket);
  75. $this->_amazon->removeBucket($this->_bucket);
  76. }
  77. /**
  78. * Test creating and removing buckets
  79. *
  80. * @return void
  81. */
  82. public function testBuckets()
  83. {
  84. // Create the bucket
  85. $result = mkdir($this->_bucketName);
  86. $this->assertTrue($result);
  87. // Remove the bucket
  88. $result = rmdir($this->_bucketName);
  89. $this->assertTrue($result);
  90. }
  91. /**
  92. * Test writing to an object
  93. *
  94. * @return void
  95. */
  96. public function testWriteObject()
  97. {
  98. // Create the bucket
  99. $result = mkdir($this->_bucketName);
  100. $this->assertTrue($result);
  101. // Generate sample data
  102. $data = str_repeat('x', 10000);
  103. // Write to an object
  104. $size = file_put_contents($this->_fileName, $data);
  105. $this->assertEquals(strlen($data), $size);
  106. // Write to an object
  107. $f = fopen($this->_fileName, 'w');
  108. for ($i = 0; $i < 100; $i++) {
  109. fwrite($f, 'x');
  110. }
  111. fclose($f);
  112. unset($data);
  113. // Check object size
  114. $size = filesize($this->_fileName);
  115. $this->assertEquals(100, $size);
  116. // Remove the object
  117. $result = unlink($this->_fileName);
  118. $this->assertTrue($result);
  119. }
  120. /**
  121. * Test reading from an object
  122. *
  123. * @return void
  124. */
  125. public function testReadObject()
  126. {
  127. // Create the bucket
  128. $result = mkdir($this->_bucketName);
  129. $this->assertTrue($result);
  130. // Generate sample data
  131. $data = str_repeat('x', 10000);
  132. // Write to an object
  133. $size = file_put_contents($this->_fileName, $data);
  134. $this->assertEquals(strlen($data), $size);
  135. // Read from an object
  136. $new_data = file_get_contents($this->_fileName);
  137. $this->assertEquals($data, $new_data);
  138. // Read from an oject
  139. $new_data = '';
  140. $f = fopen($this->_fileName, 'r');
  141. while (!feof($f)) {
  142. $new_data .= fread($f, 1024);
  143. }
  144. fclose($f);
  145. $this->assertEquals($data, $new_data);
  146. unset($data);
  147. unset($new_data);
  148. // Remove the object
  149. $result = unlink($this->_fileName);
  150. $this->assertTrue($result);
  151. }
  152. /**
  153. * Test getting the list of available buckets
  154. *
  155. * @return void
  156. */
  157. public function testGetBucketList()
  158. {
  159. $buckets = array('zf-test1', 'zf-test2', 'zf-test3');
  160. // Create the buckets
  161. foreach ($buckets as $bucket) {
  162. $result = mkdir('s3://'.$bucket);
  163. $this->assertTrue($result);
  164. }
  165. $online_buckets = array();
  166. // Retrieve list of buckets on S3
  167. $e = opendir('s3://');
  168. while (($f = readdir($e)) !== false) {
  169. $online_buckets[] = $f;
  170. }
  171. closedir($e);
  172. // Check that each bucket is in our original list
  173. foreach ($online_buckets as $bucket) {
  174. $this->assertContains($bucket, $buckets);
  175. }
  176. // Remove the buckets
  177. foreach ($buckets as $bucket) {
  178. $result = rmdir('s3://'.$bucket);
  179. $this->assertTrue($result);
  180. }
  181. }
  182. /**
  183. * Test object stat
  184. *
  185. * @return void
  186. */
  187. public function testObjectStat()
  188. {
  189. // Create the bucket
  190. $result = mkdir($this->_bucketName);
  191. $this->assertTrue($result);
  192. $data = str_repeat('x', 10000);
  193. $len = strlen($data);
  194. // Write to an object
  195. $size = file_put_contents($this->_fileName, $data);
  196. $this->assertEquals($len, $size);
  197. // Stat an object
  198. $info = stat($this->_fileName);
  199. $this->assertEquals($len, $info['size']);
  200. unset($data);
  201. // Remove the object
  202. $result = unlink($this->_fileName);
  203. $this->assertTrue($result);
  204. }
  205. }
  206. class Zend_Service_Amazon_S3_StreamTest_Skip extends PHPUnit_Framework_TestCase
  207. {
  208. public function setUp()
  209. {
  210. $this->markTestSkipped('Zend_Service_Amazon_S3 online tests not enabled with an access key ID and '
  211. . ' secret key ID in TestConfiguration.php');
  212. }
  213. public function testNothing()
  214. {
  215. }
  216. }