2
0

OfflineTest.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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\Rackspace
  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. */
  21. require_once 'Zend/Service/Rackspace/Files.php';
  22. require_once 'Zend/Http/Client/Adapter/Test.php';
  23. /**
  24. * Test helper
  25. */
  26. /**
  27. * @category Zend
  28. * @package Zend_Service_Rackspace_Files
  29. * @subpackage UnitTests
  30. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. * @group Zend_Service_Rackspace_Files
  33. */
  34. class Zend_Service_Rackspace_Files_OfflineTest
  35. extends PHPUnit_Framework_TestCase
  36. {
  37. /**
  38. * Reference to RackspaceFiles
  39. *
  40. * @var Zend_Service_Rackspace_Files
  41. */
  42. protected $rackspace;
  43. /**
  44. * HTTP client adapter for testing
  45. *
  46. * @var Zend_Http_Client_Adapter_Test
  47. */
  48. protected $httpClientAdapterTest;
  49. /**
  50. * Metadata for container/object test
  51. *
  52. * @var array
  53. */
  54. protected $metadata;
  55. /**
  56. * Another metadata for container/object test
  57. *
  58. * @var array
  59. */
  60. protected $metadata2;
  61. /**
  62. * Reference to Container
  63. *
  64. * @var Zend_Service_Rackspace_Files_Container
  65. */
  66. protected $container;
  67. /**
  68. * Set up the test case
  69. *
  70. * @return void
  71. */
  72. public function setUp()
  73. {
  74. $this->rackspace = new Zend_Service_Rackspace_Files('foo', 'bar');
  75. $this->container = new Zend_Service_Rackspace_Files_Container(
  76. $this->rackspace,
  77. array(
  78. 'name' => TESTS_ZEND_SERVICE_RACKSPACE_CONTAINER_NAME
  79. )
  80. );
  81. $this->httpClientAdapterTest = new Zend_Http_Client_Adapter_Test();
  82. $this->rackspace->getHttpClient()->setAdapter(
  83. $this->httpClientAdapterTest
  84. );
  85. // authentication (from a file)
  86. $this->httpClientAdapterTest->setResponse(
  87. self::loadResponse('../../_files/testAuthenticate')
  88. );
  89. $this->assertTrue(
  90. $this->rackspace->authenticate(), 'Authentication failed'
  91. );
  92. $this->metadata = array(
  93. 'foo' => 'bar',
  94. 'foo2' => 'bar2'
  95. );
  96. $this->metadata2 = array(
  97. 'hello' => 'world'
  98. );
  99. // load the HTTP response (from a file)
  100. $this->httpClientAdapterTest->setResponse(
  101. $this->loadResponse($this->getName())
  102. );
  103. }
  104. /**
  105. * Utility method for returning a string HTTP response, which is loaded from a file
  106. *
  107. * @param string $name
  108. *
  109. * @return string
  110. */
  111. protected function loadResponse($name)
  112. {
  113. return file_get_contents(__DIR__ . '/_files/' . $name . '.response');
  114. }
  115. public function testCreateContainer()
  116. {
  117. $container =
  118. $this->rackspace->createContainer('zf-unit-test', $this->metadata);
  119. $this->assertTrue($container !== false);
  120. $this->assertEquals($container->getName(), 'zf-unit-test');
  121. }
  122. public function testGetCountContainers()
  123. {
  124. $num = $this->rackspace->getCountContainers();
  125. $this->assertTrue($num > 0);
  126. }
  127. public function testGetContainer()
  128. {
  129. $container = $this->rackspace->getContainer('zf-unit-test');
  130. $this->assertTrue($container !== false);
  131. $this->assertEquals($container->getName(), 'zf-unit-test');
  132. }
  133. public function testGetContainers()
  134. {
  135. $containers = $this->rackspace->getContainers();
  136. $this->assertTrue($containers !== false);
  137. $found = false;
  138. foreach ($containers as $container) {
  139. if ($container->getName() == 'zf-unit-test') {
  140. $found = true;
  141. break;
  142. }
  143. }
  144. $this->assertTrue($found);
  145. }
  146. public function testGetMetadataContainer()
  147. {
  148. $data = $this->rackspace->getMetadataContainer('zf-unit-test');
  149. $this->assertTrue($data !== false);
  150. $this->assertEquals($data['name'], 'zf-unit-test');
  151. $this->assertEquals($data['metadata'], $this->metadata);
  152. }
  153. public function testGetInfoAccount()
  154. {
  155. $data = $this->rackspace->getInfoAccount();
  156. $this->assertTrue($data !== false);
  157. $this->assertTrue($data['tot_containers'] > 0);
  158. }
  159. public function testStoreObject()
  160. {
  161. $content = 'This is a test!';
  162. $result = $this->rackspace->storeObject(
  163. 'zf-unit-test',
  164. 'zf-object-test',
  165. $content,
  166. $this->metadata
  167. );
  168. $this->assertTrue($result);
  169. }
  170. public function testGetObject()
  171. {
  172. $object = $this->rackspace->getObject(
  173. 'zf-unit-test',
  174. 'zf-object-test'
  175. );
  176. $this->assertTrue($object !== false);
  177. $this->assertEquals($object->getName(), 'zf-object-test');
  178. $this->assertEquals($object->getSize(), 15);
  179. $this->assertEquals($object->getMetadata(), $this->metadata);
  180. }
  181. public function testCopyObject()
  182. {
  183. $result = $this->rackspace->copyObject(
  184. 'zf-unit-test',
  185. 'zf-object-test',
  186. 'zf-unit-test',
  187. 'zf-object-test' . '-copy'
  188. );
  189. $this->assertTrue($result);
  190. $this->assertNotContains('application/x-www-form-urlencoded', $this->rackspace->getHttpClient()->getLastRequest());
  191. }
  192. public function testGetObjects()
  193. {
  194. $objects = $this->rackspace->getObjects('zf-unit-test');
  195. $this->assertTrue($objects !== false);
  196. $this->assertEquals($objects[0]->getName(), 'zf-object-test');
  197. $this->assertEquals($objects[1]->getName(), 'zf-object-test' . '-copy');
  198. }
  199. public function testGetSizeContainers()
  200. {
  201. $size = $this->rackspace->getSizeContainers();
  202. $this->assertTrue($size !== false);
  203. $this->assertTrue(is_numeric($size));
  204. }
  205. public function testGetCountObjects()
  206. {
  207. $count = $this->rackspace->getCountObjects();
  208. $this->assertTrue($count !== false);
  209. $this->assertTrue(is_numeric($count));
  210. }
  211. public function testSetMetadataObject()
  212. {
  213. $result = $this->rackspace->setMetadataObject(
  214. 'zf-unit-test',
  215. 'zf-object-test',
  216. $this->metadata2
  217. );
  218. $this->assertTrue($result);
  219. }
  220. public function testGetMetadataObject()
  221. {
  222. $data = $this->rackspace->getMetadataObject(
  223. 'zf-unit-test',
  224. 'zf-object-test'
  225. );
  226. $this->assertTrue($data !== false);
  227. $this->assertEquals($data['metadata'], $this->metadata2);
  228. }
  229. public function testEnableCdnContainer()
  230. {
  231. $data = $this->rackspace->enableCdnContainer('zf-unit-test');
  232. $this->assertTrue($data !== false);
  233. $this->assertTrue(is_array($data));
  234. $this->assertTrue(!empty($data['cdn_uri']));
  235. $this->assertTrue(!empty($data['cdn_uri_ssl']));
  236. }
  237. public function testGetCdnContainers()
  238. {
  239. $containers = $this->rackspace->getCdnContainers();
  240. $this->assertTrue($containers !== false);
  241. $found = false;
  242. foreach ($containers as $container) {
  243. if ($container->getName() == 'zf-unit-test') {
  244. $found = true;
  245. break;
  246. }
  247. }
  248. $this->assertTrue($found);
  249. }
  250. public function testUpdateCdnContainer()
  251. {
  252. $data =
  253. $this->rackspace->updateCdnContainer('zf-unit-test', null, false);
  254. $this->assertTrue($data !== false);
  255. }
  256. public function testDeleteObject()
  257. {
  258. $this->assertTrue(
  259. $this->rackspace->deleteObject(
  260. 'zf-unit-test',
  261. 'zf-object-test'
  262. )
  263. );
  264. }
  265. public function testDeleteObject2()
  266. {
  267. $this->assertTrue(
  268. $this->rackspace->deleteObject(
  269. 'zf-unit-test',
  270. 'zf-object-test' . '-copy'
  271. )
  272. );
  273. }
  274. public function testDeleteContainer()
  275. {
  276. $this->assertTrue($this->rackspace->deleteContainer('zf-unit-test'));
  277. }
  278. /**
  279. * @group ZF-12542
  280. */
  281. public function testGetInfoCdnContainer()
  282. {
  283. $info = $this->rackspace->getInfoCdnContainer(
  284. TESTS_ZEND_SERVICE_RACKSPACE_CONTAINER_NAME
  285. );
  286. $this->assertTrue($info !== false);
  287. $this->assertTrue(is_array($info));
  288. $this->assertTrue(!empty($info['ttl']));
  289. $this->assertTrue(!empty($info['cdn_uri']));
  290. $this->assertTrue(!empty($info['cdn_uri_ssl']));
  291. $this->assertTrue($info['cdn_enabled']);
  292. $this->assertTrue($info['log_retention']);
  293. }
  294. /**
  295. * @group ZF-12542
  296. */
  297. public function testGetCdnTtl()
  298. {
  299. $ttl = $this->container->getCdnTtl();
  300. $this->assertTrue($ttl !== false);
  301. }
  302. /**
  303. * @group ZF-12542
  304. */
  305. public function testGetCdnUri()
  306. {
  307. $uri = $this->container->getCdnUri();
  308. $this->assertTrue($uri !== false);
  309. }
  310. /**
  311. * @group ZF-12542
  312. */
  313. public function testGetCdnUriSsl()
  314. {
  315. $uri = $this->container->getCdnUriSsl();
  316. $this->assertTrue($uri !== false);
  317. }
  318. }