OfflineTest.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 extends PHPUnit_Framework_TestCase
  35. {
  36. /**
  37. * Reference to RackspaceFiles
  38. *
  39. * @var Zend_Service_Rackspace_Files
  40. */
  41. protected $rackspace;
  42. /**
  43. * HTTP client adapter for testing
  44. *
  45. * @var Zend_Http_Client_Adapter_Test
  46. */
  47. protected $httpClientAdapterTest;
  48. /**
  49. * Metadata for container/object test
  50. *
  51. * @var array
  52. */
  53. protected $metadata;
  54. /**
  55. * Another metadata for container/object test
  56. *
  57. * @var array
  58. */
  59. protected $metadata2;
  60. /**
  61. * Set up the test case
  62. *
  63. * @return void
  64. */
  65. public function setUp()
  66. {
  67. $this->rackspace = new Zend_Service_Rackspace_Files('foo','bar');
  68. $this->httpClientAdapterTest = new Zend_Http_Client_Adapter_Test();
  69. $this->rackspace->getHttpClient()->setAdapter($this->httpClientAdapterTest);
  70. // authentication (from a file)
  71. $this->httpClientAdapterTest->setResponse(self::loadResponse('../../_files/testAuthenticate'));
  72. $this->assertTrue($this->rackspace->authenticate(),'Authentication failed');
  73. $this->metadata = array (
  74. 'foo' => 'bar',
  75. 'foo2' => 'bar2'
  76. );
  77. $this->metadata2 = array (
  78. 'hello' => 'world'
  79. );
  80. // load the HTTP response (from a file)
  81. $this->httpClientAdapterTest->setResponse($this->loadResponse($this->getName()));
  82. }
  83. /**
  84. * Utility method for returning a string HTTP response, which is loaded from a file
  85. *
  86. * @param string $name
  87. * @return string
  88. */
  89. protected function loadResponse($name)
  90. {
  91. return file_get_contents(__DIR__ . '/_files/' . $name . '.response');
  92. }
  93. public function testCreateContainer()
  94. {
  95. $container= $this->rackspace->createContainer('zf-unit-test',$this->metadata);
  96. $this->assertTrue($container!==false);
  97. $this->assertEquals($container->getName(),'zf-unit-test');
  98. }
  99. public function testGetCountContainers()
  100. {
  101. $num= $this->rackspace->getCountContainers();
  102. $this->assertTrue($num>0);
  103. }
  104. public function testGetContainer()
  105. {
  106. $container= $this->rackspace->getContainer('zf-unit-test');
  107. $this->assertTrue($container!==false);
  108. $this->assertEquals($container->getName(),'zf-unit-test');
  109. }
  110. public function testGetContainers()
  111. {
  112. $containers= $this->rackspace->getContainers();
  113. $this->assertTrue($containers!==false);
  114. $found=false;
  115. foreach ($containers as $container) {
  116. if ($container->getName()=='zf-unit-test') {
  117. $found=true;
  118. break;
  119. }
  120. }
  121. $this->assertTrue($found);
  122. }
  123. public function testGetMetadataContainer()
  124. {
  125. $data= $this->rackspace->getMetadataContainer('zf-unit-test');
  126. $this->assertTrue($data!==false);
  127. $this->assertEquals($data['name'],'zf-unit-test');
  128. $this->assertEquals($data['metadata'],$this->metadata);
  129. }
  130. public function testGetInfoAccount()
  131. {
  132. $data= $this->rackspace->getInfoAccount();
  133. $this->assertTrue($data!==false);
  134. $this->assertTrue($data['tot_containers']>0);
  135. }
  136. public function testStoreObject()
  137. {
  138. $content= 'This is a test!';
  139. $result= $this->rackspace->storeObject('zf-unit-test',
  140. 'zf-object-test',
  141. $content,
  142. $this->metadata);
  143. $this->assertTrue($result);
  144. }
  145. public function testGetObject()
  146. {
  147. $object= $this->rackspace->getObject('zf-unit-test',
  148. 'zf-object-test');
  149. $this->assertTrue($object!==false);
  150. $this->assertEquals($object->getName(),'zf-object-test');
  151. $this->assertEquals($object->getSize(),15);
  152. $this->assertEquals($object->getMetadata(),$this->metadata);
  153. }
  154. public function testCopyObject()
  155. {
  156. $result= $this->rackspace->copyObject('zf-unit-test',
  157. 'zf-object-test',
  158. 'zf-unit-test',
  159. 'zf-object-test'.'-copy');
  160. $this->assertTrue($result);
  161. }
  162. public function testGetObjects()
  163. {
  164. $objects= $this->rackspace->getObjects('zf-unit-test');
  165. $this->assertTrue($objects!==false);
  166. $this->assertEquals($objects[0]->getName(),'zf-object-test');
  167. $this->assertEquals($objects[1]->getName(),'zf-object-test'.'-copy');
  168. }
  169. public function testGetSizeContainers()
  170. {
  171. $size= $this->rackspace->getSizeContainers();
  172. $this->assertTrue($size!==false);
  173. $this->assertTrue(is_numeric($size));
  174. }
  175. public function testGetCountObjects()
  176. {
  177. $count= $this->rackspace->getCountObjects();
  178. $this->assertTrue($count!==false);
  179. $this->assertTrue(is_numeric($count));
  180. }
  181. public function testSetMetadataObject()
  182. {
  183. $result= $this->rackspace->setMetadataObject('zf-unit-test',
  184. 'zf-object-test',
  185. $this->metadata2);
  186. $this->assertTrue($result);
  187. }
  188. public function testGetMetadataObject()
  189. {
  190. $data= $this->rackspace->getMetadataObject('zf-unit-test',
  191. 'zf-object-test');
  192. $this->assertTrue($data!==false);
  193. $this->assertEquals($data['metadata'],$this->metadata2);
  194. }
  195. public function testEnableCdnContainer()
  196. {
  197. $data= $this->rackspace->enableCdnContainer('zf-unit-test');
  198. $this->assertTrue($data!==false);
  199. $this->assertTrue(is_array($data));
  200. $this->assertTrue(!empty($data['cdn_uri']));
  201. $this->assertTrue(!empty($data['cdn_uri_ssl']));
  202. }
  203. public function testGetCdnContainers()
  204. {
  205. $containers= $this->rackspace->getCdnContainers();
  206. $this->assertTrue($containers!==false);
  207. $found= false;
  208. foreach ($containers as $container) {
  209. if ($container->getName()=='zf-unit-test') {
  210. $found= true;
  211. break;
  212. }
  213. }
  214. $this->assertTrue($found);
  215. }
  216. public function testUpdateCdnContainer()
  217. {
  218. $data= $this->rackspace->updateCdnContainer('zf-unit-test',null,false);
  219. $this->assertTrue($data!==false);
  220. }
  221. public function testDeleteObject()
  222. {
  223. $this->assertTrue($this->rackspace->deleteObject('zf-unit-test',
  224. 'zf-object-test'));
  225. }
  226. public function testDeleteObject2()
  227. {
  228. $this->assertTrue($this->rackspace->deleteObject('zf-unit-test',
  229. 'zf-object-test'.'-copy'));
  230. }
  231. public function testDeleteContainer()
  232. {
  233. $this->assertTrue($this->rackspace->deleteContainer('zf-unit-test'));
  234. }
  235. }