StreamTest.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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-2009 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-2009 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. * @return void
  133. */
  134. public function testReadObject()
  135. {
  136. // Create the bucket
  137. $result = mkdir($this->_bucketName);
  138. $this->assertTrue($result);
  139. // Generate sample data
  140. $data = str_repeat('x', 10000);
  141. // Write to an object
  142. $size = file_put_contents($this->_fileName, $data);
  143. $this->assertEquals(strlen($data), $size);
  144. // Read from an object
  145. $new_data = file_get_contents($this->_fileName);
  146. $this->assertEquals($data, $new_data);
  147. // Read from an oject
  148. $new_data = '';
  149. $f = fopen($this->_fileName, 'r');
  150. while (!feof($f)) {
  151. $new_data .= fread($f, 1024);
  152. }
  153. fclose($f);
  154. $this->assertEquals($data, $new_data);
  155. unset($data);
  156. unset($new_data);
  157. // Remove the object
  158. $result = unlink($this->_fileName);
  159. $this->assertTrue($result);
  160. }
  161. /**
  162. * Test getting the list of available buckets
  163. *
  164. * @return void
  165. */
  166. public function testGetBucketList()
  167. {
  168. $buckets = array($this->_bucket.'zf-test1', $this->_bucket.'zf-test2', $this->_bucket.'zf-test3');
  169. // Create the buckets
  170. foreach ($buckets as $bucket) {
  171. $result = mkdir('s3://'.$bucket);
  172. $this->assertTrue($result);
  173. }
  174. $online_buckets = array();
  175. // Retrieve list of buckets on S3
  176. $e = opendir('s3://');
  177. while (($f = readdir($e)) !== false) {
  178. $online_buckets[] = $f;
  179. }
  180. closedir($e);
  181. // Check that each bucket is in our original list
  182. foreach ($buckets as $bucket) {
  183. $this->assertContains($bucket, $online_buckets);
  184. }
  185. // Remove the buckets
  186. foreach ($buckets as $bucket) {
  187. $result = rmdir('s3://'.$bucket);
  188. $this->assertTrue($result);
  189. }
  190. }
  191. /**
  192. * Test object stat
  193. *
  194. * @return void
  195. */
  196. public function testObjectStat()
  197. {
  198. // Create the bucket
  199. $result = mkdir($this->_bucketName);
  200. $this->assertTrue($result);
  201. $this->assertTrue(is_dir($this->_bucketName));
  202. $data = str_repeat('x', 10000);
  203. $len = strlen($data);
  204. // Write to an object
  205. $size = file_put_contents($this->_fileName, $data);
  206. $this->assertEquals($len, $size);
  207. $this->assertFalse(is_dir($this->_fileName));
  208. // Stat an object
  209. $info = stat($this->_fileName);
  210. $this->assertEquals($len, $info['size']);
  211. unset($data);
  212. // Remove the object
  213. $result = unlink($this->_fileName);
  214. $this->assertTrue($result);
  215. }
  216. }
  217. /**
  218. * @category Zend
  219. * @package Zend_Service_Amazon_S3
  220. * @subpackage UnitTests
  221. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  222. * @license http://framework.zend.com/license/new-bsd New BSD License
  223. * @group Zend_Server
  224. * @group Zend_Server_Amazon
  225. * @group Zend_Server_Amazon_S3
  226. */
  227. class Zend_Service_Amazon_S3_StreamTest_Skip extends PHPUnit_Framework_TestCase
  228. {
  229. public function setUp()
  230. {
  231. $this->markTestSkipped('Zend_Service_Amazon_S3 online tests not enabled with an access key ID and '
  232. . ' secret key ID in TestConfiguration.php');
  233. }
  234. public function testNothing()
  235. {
  236. }
  237. }