OfflineTest.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. }
  191. public function testGetObjects()
  192. {
  193. $objects = $this->rackspace->getObjects('zf-unit-test');
  194. $this->assertTrue($objects !== false);
  195. $this->assertEquals($objects[0]->getName(), 'zf-object-test');
  196. $this->assertEquals($objects[1]->getName(), 'zf-object-test' . '-copy');
  197. }
  198. public function testGetObjectsPseudoDirs()
  199. {
  200. $objects = $this->rackspace->getObjects('zf-unit-test', array('delimiter' => '/', 'prefix' => 'dir/'));
  201. $this->assertTrue($objects !== false);
  202. $this->assertEquals($objects[0]->getName(), 'dir/subdir1/');
  203. $this->assertEquals($objects[1]->getName(), 'dir/subdir2/');
  204. }
  205. public function testGetSizeContainers()
  206. {
  207. $size = $this->rackspace->getSizeContainers();
  208. $this->assertTrue($size !== false);
  209. $this->assertTrue(is_numeric($size));
  210. }
  211. public function testGetCountObjects()
  212. {
  213. $count = $this->rackspace->getCountObjects();
  214. $this->assertTrue($count !== false);
  215. $this->assertTrue(is_numeric($count));
  216. }
  217. public function testSetMetadataObject()
  218. {
  219. $result = $this->rackspace->setMetadataObject(
  220. 'zf-unit-test',
  221. 'zf-object-test',
  222. $this->metadata2
  223. );
  224. $this->assertTrue($result);
  225. }
  226. public function testGetMetadataObject()
  227. {
  228. $data = $this->rackspace->getMetadataObject(
  229. 'zf-unit-test',
  230. 'zf-object-test'
  231. );
  232. $this->assertTrue($data !== false);
  233. $this->assertEquals($data['metadata'], $this->metadata2);
  234. }
  235. public function testEnableCdnContainer()
  236. {
  237. $data = $this->rackspace->enableCdnContainer('zf-unit-test');
  238. $this->assertTrue($data !== false);
  239. $this->assertTrue(is_array($data));
  240. $this->assertTrue(!empty($data['cdn_uri']));
  241. $this->assertTrue(!empty($data['cdn_uri_ssl']));
  242. }
  243. public function testGetCdnContainers()
  244. {
  245. $containers = $this->rackspace->getCdnContainers();
  246. $this->assertTrue($containers !== false);
  247. $found = false;
  248. foreach ($containers as $container) {
  249. if ($container->getName() == 'zf-unit-test') {
  250. $found = true;
  251. break;
  252. }
  253. }
  254. $this->assertTrue($found);
  255. }
  256. public function testUpdateCdnContainer()
  257. {
  258. $data =
  259. $this->rackspace->updateCdnContainer('zf-unit-test', null, false);
  260. $this->assertTrue($data !== false);
  261. }
  262. public function testDeleteObject()
  263. {
  264. $this->assertTrue(
  265. $this->rackspace->deleteObject(
  266. 'zf-unit-test',
  267. 'zf-object-test'
  268. )
  269. );
  270. }
  271. public function testDeleteObject2()
  272. {
  273. $this->assertTrue(
  274. $this->rackspace->deleteObject(
  275. 'zf-unit-test',
  276. 'zf-object-test' . '-copy'
  277. )
  278. );
  279. }
  280. public function testDeleteContainer()
  281. {
  282. $this->assertTrue($this->rackspace->deleteContainer('zf-unit-test'));
  283. }
  284. /**
  285. * @group ZF-12542
  286. */
  287. public function testGetInfoCdnContainer()
  288. {
  289. $info = $this->rackspace->getInfoCdnContainer(
  290. TESTS_ZEND_SERVICE_RACKSPACE_CONTAINER_NAME
  291. );
  292. $this->assertTrue($info !== false);
  293. $this->assertTrue(is_array($info));
  294. $this->assertTrue(!empty($info['ttl']));
  295. $this->assertTrue(!empty($info['cdn_uri']));
  296. $this->assertTrue(!empty($info['cdn_uri_ssl']));
  297. $this->assertTrue($info['cdn_enabled']);
  298. $this->assertTrue($info['log_retention']);
  299. }
  300. /**
  301. * @group ZF-12542
  302. */
  303. public function testGetCdnTtl()
  304. {
  305. $ttl = $this->container->getCdnTtl();
  306. $this->assertTrue($ttl !== false);
  307. }
  308. /**
  309. * @group ZF-12542
  310. */
  311. public function testGetCdnUri()
  312. {
  313. $uri = $this->container->getCdnUri();
  314. $this->assertTrue($uri !== false);
  315. }
  316. /**
  317. * @group ZF-12542
  318. */
  319. public function testGetCdnUriSsl()
  320. {
  321. $uri = $this->container->getCdnUriSsl();
  322. $this->assertTrue($uri !== false);
  323. }
  324. }