PageRenderTime 31ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/ZendFramework/tests/Zend/Service/Amazon/S3/StreamTest.php

https://bitbucket.org/Dal-Papa/is-340-publish-base
PHP | 277 lines | 132 code | 40 blank | 105 comment | 5 complexity | 8656c7360f8b73b010fc7df24a4e38e0 MD5 | raw file
  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-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: StreamTest.php 24593 2012-01-05 20:35:02Z matthew $
  21. */
  22. /**
  23. * @see Zend_Service_Amazon
  24. */
  25. require_once 'Zend/Service/Amazon/S3.php';
  26. /**
  27. * @see Zend_Http_Client_Adapter_Socket
  28. */
  29. require_once 'Zend/Http/Client/Adapter/Socket.php';
  30. /**
  31. * @category Zend
  32. * @package Zend_Service_Amazon_S3
  33. * @subpackage UnitTests
  34. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. * @group Zend_Service
  37. * @group Zend_Service_Amazon
  38. * @group Zend_Service_Amazon_S3
  39. */
  40. class Zend_Service_Amazon_S3_StreamTest extends PHPUnit_Framework_TestCase
  41. {
  42. /**
  43. * Sets up this test case
  44. *
  45. * @return void
  46. */
  47. public function setUp()
  48. {
  49. $this->_amazon = new Zend_Service_Amazon_S3(constant('TESTS_ZEND_SERVICE_AMAZON_ONLINE_ACCESSKEYID'),
  50. constant('TESTS_ZEND_SERVICE_AMAZON_ONLINE_SECRETKEY')
  51. );
  52. $this->_nosuchbucket = "nonexistingbucketnamewhichnobodyshoulduse";
  53. $this->_httpClientAdapterSocket = new Zend_Http_Client_Adapter_Socket();
  54. $this->_bucket = constant('TESTS_ZEND_SERVICE_AMAZON_S3_BUCKET');
  55. $this->_bucketName = "s3://".$this->_bucket;
  56. $this->_fileName = $this->_bucketName."/sample_file.txt";
  57. $this->_amazon->getHttpClient()
  58. ->setAdapter($this->_httpClientAdapterSocket);
  59. $this->_amazon->registerStreamWrapper();
  60. $this->_amazon->cleanBucket($this->_bucket);
  61. $this->_amazon->removeBucket($this->_bucket);
  62. // terms of use compliance: no more than one query per second
  63. sleep(1);
  64. }
  65. /**
  66. * Tear down each test
  67. *
  68. * @return void
  69. */
  70. public function tearDown()
  71. {
  72. $this->_amazon->unregisterStreamWrapper();
  73. $buckets = $this->_amazon->getBuckets();
  74. foreach($buckets as $bucket) {
  75. if(substr($bucket, 0, strlen($this->_bucket)) != $this->_bucket) {
  76. continue;
  77. }
  78. $this->_amazon->cleanBucket($bucket);
  79. $this->_amazon->removeBucket($bucket);
  80. }
  81. }
  82. /**
  83. * Test creating and removing buckets
  84. *
  85. * @return void
  86. */
  87. public function testBuckets()
  88. {
  89. // Create the bucket
  90. $result = mkdir($this->_bucketName);
  91. $this->assertTrue($result);
  92. // Remove the bucket
  93. $result = rmdir($this->_bucketName);
  94. $this->assertTrue($result);
  95. }
  96. /**
  97. * Test writing to an object
  98. *
  99. * @return void
  100. */
  101. public function testWriteObject()
  102. {
  103. // Create the bucket
  104. $result = mkdir($this->_bucketName);
  105. $this->assertTrue($result);
  106. // Generate sample data
  107. $data = str_repeat('x', 10000);
  108. // Write to an object
  109. $size = file_put_contents($this->_fileName, $data);
  110. $this->assertEquals(strlen($data), $size);
  111. // Write to an object
  112. $f = fopen($this->_fileName, 'w');
  113. for ($i = 0; $i < 100; $i++) {
  114. fwrite($f, 'x');
  115. }
  116. fclose($f);
  117. unset($data);
  118. // Check object size
  119. $size = filesize($this->_fileName);
  120. $this->assertEquals(100, $size);
  121. // Remove the object
  122. $result = unlink($this->_fileName);
  123. $this->assertTrue($result);
  124. }
  125. /**
  126. * Test reading from an object
  127. *
  128. * @group ZF-10035
  129. * @return void
  130. */
  131. public function testReadObject()
  132. {
  133. // Create the bucket
  134. $result = mkdir($this->_bucketName);
  135. $this->assertTrue($result);
  136. // Generate sample data
  137. $data = str_repeat('x', 10000);
  138. // Write to an object
  139. $size = file_put_contents($this->_fileName, $data);
  140. $this->assertEquals(strlen($data), $size);
  141. // Read from an object
  142. $new_data = file_get_contents($this->_fileName);
  143. $this->assertEquals($data, $new_data);
  144. // Read from an oject
  145. $new_data = '';
  146. $f = fopen($this->_fileName, 'r');
  147. fseek($f, 1000);
  148. while (!feof($f)) {
  149. $chunk = fread($f, 1000);
  150. $new_data .= $chunk;
  151. $this->assertEquals(strlen($chunk), 1000);
  152. }
  153. fclose($f);
  154. $this->assertEquals(substr($data, 1000), $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-2012 Zend Technologies USA Inc. (http://www.zend.com)
  222. * @license http://framework.zend.com/license/new-bsd New BSD License
  223. * @group Zend_Service
  224. * @group Zend_Service_Amazon
  225. * @group Zend_Service_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. }