OnlineTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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 11973 2008-10-15 16:00:56Z matthew $
  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
  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_OnlineTest extends PHPUnit_Framework_TestCase
  42. {
  43. /**
  44. * Reference to Amazon service consumer object
  45. *
  46. * @var Zend_Service_Amazon_S3
  47. */
  48. protected $_amazon;
  49. /**
  50. * Reference to Amazon query API object
  51. *
  52. * @var Zend_Service_Amazon_Query
  53. */
  54. protected $_query;
  55. /**
  56. * Socket based HTTP client adapter
  57. *
  58. * @var Zend_Http_Client_Adapter_Socket
  59. */
  60. protected $_httpClientAdapterSocket;
  61. /**
  62. * Sets up this test case
  63. *
  64. * @return void
  65. */
  66. public function setUp()
  67. {
  68. $this->_amazon = new Zend_Service_Amazon_S3(constant('TESTS_ZEND_SERVICE_AMAZON_ONLINE_ACCESSKEYID'),
  69. constant('TESTS_ZEND_SERVICE_AMAZON_ONLINE_SECRETKEY')
  70. );
  71. $this->_nosuchbucket = "nonexistingbucketnamewhichnobodyshoulduse";
  72. $this->_httpClientAdapterSocket = new Zend_Http_Client_Adapter_Socket();
  73. $this->_bucket = constant('TESTS_ZEND_SERVICE_AMAZON_S3_BUCKET');
  74. $this->_amazon->getHttpClient()
  75. ->setAdapter($this->_httpClientAdapterSocket);
  76. // terms of use compliance: no more than one query per second
  77. sleep(1);
  78. }
  79. /**
  80. * Test creating bucket
  81. *
  82. * @return void
  83. */
  84. public function testCreateBucket()
  85. {
  86. $this->_amazon->createBucket($this->_bucket);
  87. $this->assertTrue($this->_amazon->isBucketAvailable($this->_bucket));
  88. $list = $this->_amazon->getBuckets();
  89. $this->assertContains($this->_bucket, $list);
  90. }
  91. /**
  92. * Test creating object
  93. *
  94. * @return void
  95. */
  96. public function testCreateObject()
  97. {
  98. $this->_amazon->createBucket($this->_bucket);
  99. $this->_amazon->putObject($this->_bucket."/zftest", "testdata");
  100. $this->assertEquals("testdata", $this->_amazon->getObject($this->_bucket."/zftest"));
  101. }
  102. /**
  103. * Test getting info
  104. *
  105. * @return void
  106. */
  107. public function testGetInfo()
  108. {
  109. $this->_amazon->createBucket($this->_bucket);
  110. $data = "testdata";
  111. $this->_amazon->putObject($this->_bucket."/zftest", $data);
  112. $info = $this->_amazon->getInfo($this->_bucket."/zftest");
  113. $this->assertEquals('"'.md5($data).'"', $info["etag"]);
  114. $this->assertEquals(strlen($data), $info["size"]);
  115. $this->_amazon->putObject($this->_bucket."/zftest.jpg", $data, null);
  116. $info = $this->_amazon->getInfo($this->_bucket."/zftest.jpg");
  117. $this->assertEquals( 'image/jpeg', $info["type"]);
  118. }
  119. public function testNoBucket()
  120. {
  121. $this->assertFalse($this->_amazon->putObject($this->_nosuchbucket."/zftest", "testdata"));
  122. $this->assertFalse($this->_amazon->getObject($this->_nosuchbucket."/zftest"));
  123. $this->assertFalse($this->_amazon->getObjectsByBucket($this->_nosuchbucket));
  124. }
  125. public function testNoObject()
  126. {
  127. $this->_amazon->createBucket($this->_bucket);
  128. $this->assertFalse($this->_amazon->getObject($this->_bucket."/zftest-no-such-object/in/there"));
  129. $this->assertFalse($this->_amazon->getInfo($this->_bucket."/zftest-no-such-object/in/there"));
  130. }
  131. public function testOverwriteObject()
  132. {
  133. $this->_amazon->createBucket($this->_bucket);
  134. $data = "testdata";
  135. $this->_amazon->putObject($this->_bucket."/zftest", $data);
  136. $info = $this->_amazon->getInfo($this->_bucket."/zftest");
  137. $this->assertEquals('"'.md5($data).'"', $info["etag"]);
  138. $this->assertEquals(strlen($data), $info["size"]);
  139. $data = "testdata with some other data";
  140. $this->_amazon->putObject($this->_bucket."/zftest", $data);
  141. $info = $this->_amazon->getInfo($this->_bucket."/zftest");
  142. $this->assertEquals('"'.md5($data).'"', $info["etag"]);
  143. $this->assertEquals(strlen($data), $info["size"]);
  144. }
  145. public function testRemoveObject()
  146. {
  147. $this->_amazon->createBucket($this->_bucket);
  148. $data = "testdata";
  149. $this->_amazon->putObject($this->_bucket."/zftest", $data);
  150. $this->_amazon->removeObject($this->_bucket."/zftest", $data);
  151. $this->assertFalse($this->_amazon->getObject($this->_bucket."/zftest"));
  152. $this->assertFalse($this->_amazon->getInfo($this->_bucket."/zftest"));
  153. }
  154. public function testRemoveBucket()
  155. {
  156. $this->_amazon->createBucket($this->_bucket);
  157. $data = "testdata";
  158. $this->_amazon->putObject($this->_bucket."/zftest", $data);
  159. $this->_amazon->cleanBucket($this->_bucket);
  160. $this->_amazon->removeBucket($this->_bucket);
  161. $this->assertFalse($this->_amazon->isBucketAvailable($this->_bucket));
  162. $this->assertFalse($this->_amazon->isObjectAvailable($this->_bucket."/zftest"));
  163. $this->assertFalse($this->_amazon->getObjectsByBucket($this->_bucket));
  164. $list = $this->_amazon->getBuckets();
  165. $this->assertNotContains($this->_bucket, $list);
  166. }
  167. protected function _fileTest($filename, $object, $type, $exp_type)
  168. {
  169. $this->_amazon->putFile($filename, $object, array(Zend_Service_Amazon_S3::S3_CONTENT_TYPE_HEADER => $type));
  170. $data = file_get_contents($filename);
  171. $this->assertTrue($this->_amazon->isObjectAvailable($object));
  172. $info = $this->_amazon->getInfo($object);
  173. $this->assertEquals('"'.md5_file($filename).'"', $info["etag"]);
  174. $this->assertEquals(filesize($filename), $info["size"]);
  175. $this->assertEquals($exp_type, $info["type"]);
  176. $fdata = $this->_amazon->getObject($object);
  177. $this->assertEquals($data, $fdata);
  178. }
  179. public function testPutFile()
  180. {
  181. $filedir = dirname(__FILE__)."/_files/";
  182. $this->_amazon->createBucket($this->_bucket);
  183. $this->_fileTest($filedir."testdata", $this->_bucket."/zftestfile", null, 'binary/octet-stream');
  184. $this->_fileTest($filedir."testdata", $this->_bucket."/zftestfile2", 'text/plain', 'text/plain');
  185. $this->_fileTest($filedir."testdata.html", $this->_bucket."/zftestfile3", null, 'text/html');
  186. $this->_fileTest($filedir."testdata.html", $this->_bucket."/zftestfile3.html", 'text/plain', 'text/plain');
  187. }
  188. public function testPutNoFile()
  189. {
  190. $filedir = dirname(__FILE__)."/_files/";
  191. try {
  192. $this->_amazon->putFile($filedir."nosuchfile", $this->_bucket."/zftestfile");
  193. $this->fail("Expected exception not thrown");
  194. } catch(Zend_Service_Amazon_S3_Exception $e) {
  195. $this->assertContains("Cannot read", $e->getMessage());
  196. $this->assertContains("nosuchfile", $e->getMessage());
  197. }
  198. $this->assertFalse($this->_amazon->isObjectAvailable($this->_bucket."/zftestfile"));
  199. }
  200. public function testObjectEncoding()
  201. {
  202. $this->_amazon->createBucket($this->_bucket);
  203. $this->_amazon->putObject($this->_bucket."/this is a 100% test", "testdata");
  204. $this->assertEquals("testdata", $this->_amazon->getObject($this->_bucket."/this is a 100% test"));
  205. $this->_amazon->putObject($this->_bucket."/это тоже тест!", "testdata123");
  206. $this->assertEquals("testdata123", $this->_amazon->getObject($this->_bucket."/это тоже тест!"));
  207. }
  208. public function testBadNames()
  209. {
  210. try {
  211. $this->_amazon->createBucket("This is a Very Bad Name");
  212. $this->fail("Expected exception not thrown");
  213. } catch(Zend_Service_Amazon_S3_Exception $e) {
  214. $this->assertContains("contains invalid characters", $e->getMessage());
  215. }
  216. try {
  217. $this->_amazon->isBucketAvailable("This is a Very Bad Name");
  218. $this->fail("Expected exception not thrown");
  219. } catch(Zend_Uri_Exception $e) {
  220. $this->assertContains("Invalid URI", $e->getMessage());
  221. }
  222. try {
  223. $this->_amazon->putObject("This is a Very Bad Name/And It Gets Worse", "testdata");
  224. $this->fail("Expected exception not thrown");
  225. } catch(Zend_Service_Amazon_S3_Exception $e) {
  226. $this->assertContains("contains invalid characters", $e->getMessage());
  227. }
  228. try {
  229. $this->_amazon->getObject("This is a Very Bad Name/And It Gets Worse");
  230. $this->fail("Expected exception not thrown");
  231. } catch(Zend_Service_Amazon_S3_Exception $e) {
  232. $this->assertContains("contains invalid characters", $e->getMessage());
  233. }
  234. try {
  235. $this->_amazon->getInfo("This is a Very Bad Name/And It Gets Worse");
  236. $this->fail("Expected exception not thrown");
  237. } catch(Zend_Service_Amazon_S3_Exception $e) {
  238. $this->assertContains("contains invalid characters", $e->getMessage());
  239. }
  240. }
  241. public function testAcl()
  242. {
  243. $this->_amazon->createBucket($this->_bucket);
  244. $filedir = dirname(__FILE__)."/_files/";
  245. $this->_amazon->putFile($filedir."testdata.html", $this->_bucket."/zftestfile.html");
  246. $this->_amazon->putFile($filedir."testdata.html", $this->_bucket."/zftestfile2.html",
  247. array(Zend_Service_Amazon_S3::S3_ACL_HEADER => Zend_Service_Amazon_S3::S3_ACL_PUBLIC_READ));
  248. $url = Zend_Service_Amazon_S3::S3_ENDPOINT."/".$this->_bucket."/zftestfile.html";
  249. $data = @file_get_contents($url);
  250. $this->assertFalse($data);
  251. $url = Zend_Service_Amazon_S3::S3_ENDPOINT."/".$this->_bucket."/zftestfile2.html";
  252. $data = @file_get_contents($url);
  253. $this->assertEquals(file_get_contents($filedir."testdata.html"), $data);
  254. }
  255. public function tearDown()
  256. {
  257. $this->_amazon->cleanBucket($this->_bucket);
  258. $this->_amazon->removeBucket($this->_bucket);
  259. }
  260. }
  261. class Zend_Service_Amazon_S3_OnlineTest_Skip extends PHPUnit_Framework_TestCase
  262. {
  263. public function setUp()
  264. {
  265. $this->markTestSkipped('Zend_Service_Amazon_S3 online tests not enabled with an access key ID in '
  266. . 'TestConfiguration.php');
  267. }
  268. public function testNothing()
  269. {
  270. }
  271. }