TestCase.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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_Cloud_AdapterTestCase
  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. /**
  22. * @see Zend_Cloud_StorageService_Adapter
  23. */
  24. require_once 'Zend/Cloud/StorageService/Adapter.php';
  25. /**
  26. * @see Zend_Config
  27. */
  28. require_once 'Zend/Config.php';
  29. /**
  30. * @see Zend_Cloud_StorageService_Factory
  31. */
  32. require_once 'Zend/Cloud/StorageService/Factory.php';
  33. /**
  34. * This class forces the adapter tests to implement tests for all methods on
  35. * Zend_Cloud_StorageService
  36. *
  37. * @category Zend
  38. * @package Zend_Cloud
  39. * @subpackage UnitTests
  40. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  41. * @license http://framework.zend.com/license/new-bsd New BSD License
  42. */
  43. abstract class Zend_Cloud_StorageService_TestCase extends PHPUnit_Framework_TestCase
  44. {
  45. /**
  46. * Reference to storage adapter to test
  47. *
  48. * @var Zend_Cloud_StorageService
  49. */
  50. protected $_commonStorage;
  51. protected $_dummyNamePrefix = 'TestItem';
  52. protected $_dummyDataPrefix = 'TestData';
  53. protected $_clientType = 'stdClass';
  54. /**
  55. * Config object
  56. *
  57. * @var Zend_Config
  58. */
  59. protected $_config;
  60. /**
  61. * Period to wait for propagation in seconds
  62. * Should be set by adapter
  63. *
  64. * @var int
  65. */
  66. protected $_waitPeriod = 1;
  67. public function setUp()
  68. {
  69. $this->_config = $this->_getConfig();
  70. $this->_commonStorage = Zend_Cloud_StorageService_Factory::getAdapter($this->_config);
  71. }
  72. public function testGetClient()
  73. {
  74. $this->assertTrue(is_a($this->_commonStorage->getClient(), $this->_clientType));
  75. }
  76. public function testNoParams()
  77. {
  78. $config = array(Zend_Cloud_StorageService_Factory::STORAGE_ADAPTER_KEY => $this->_config->get(Zend_Cloud_StorageService_Factory::STORAGE_ADAPTER_KEY));
  79. $this->setExpectedException('Zend_Cloud_StorageService_Exception');
  80. $s = Zend_Cloud_StorageService_Factory::getAdapter($config);
  81. }
  82. /**
  83. * Test fetch item
  84. *
  85. * @return void
  86. */
  87. public function testFetchItemString()
  88. {
  89. $dummyNameText = null;
  90. $dummyNameStream = null;
  91. try {
  92. $originalData = $this->_dummyDataPrefix . 'FetchItem';
  93. $dummyNameText = $this->_dummyNamePrefix . 'ForFetchText';
  94. $this->_clobberItem($originalData, $dummyNameText);
  95. $this->_wait();
  96. $returnedData = $this->_commonStorage->fetchItem($dummyNameText);
  97. $this->assertEquals($originalData, $returnedData);
  98. $this->_commonStorage->deleteItem($dummyNameText);
  99. $this->_wait();
  100. $this->assertFalse($this->_commonStorage->fetchItem($dummyNameText));
  101. } catch (Exception $e) {
  102. try {
  103. $this->_commonStorage->deleteItem($dummyNameText);
  104. } catch (Zend_Cloud_Exception $ignoreMe) {
  105. }
  106. throw $e;
  107. }
  108. }
  109. /**
  110. * Test fetch item
  111. *
  112. * @return void
  113. */
  114. public function testFetchItemStream()
  115. {
  116. // TODO: Add support for streaming fetch
  117. return $this->markTestIncomplete('Cloud API doesn\'t support streamed fetches yet');
  118. $dummyNameText = null;
  119. $dummyNameStream = null;
  120. try {
  121. $originalFilename = realpath(dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files/data/dummy_data.txt');
  122. $dummyNameStream = $this->_dummyNamePrefix . 'ForFetchStream';
  123. $stream = fopen($originalFilename, 'r');
  124. $this->_clobberItem($stream, $dummyNameStream);
  125. $this->_wait();
  126. $returnedData = $this->_commonStorage->fetchItem($dummyNameStream);
  127. $this->assertEquals(file_get_contents($originalFilename), $returnedData);
  128. $this->_commonStorage->deleteItem($dummyNameStream);
  129. } catch (Exception $e) {
  130. try {
  131. $this->_commonStorage->deleteItem($dummyNameStream);
  132. } catch (Zend_Cloud_Exception $ignoreMe) {
  133. }
  134. throw $e;
  135. }
  136. }
  137. /**
  138. * Test store item
  139. *
  140. * @return void
  141. */
  142. public function testStoreItemText()
  143. {
  144. $dummyNameText = null;
  145. try {
  146. // Test string data
  147. $originalData = $this->_dummyDataPrefix . 'StoreItem';
  148. $dummyNameText = $this->_dummyNamePrefix . 'ForStoreText';
  149. $this->_clobberItem($originalData, $dummyNameText);
  150. $this->_wait();
  151. $returnedData = $this->_commonStorage->fetchItem($dummyNameText);
  152. $this->assertEquals($originalData, $returnedData);
  153. $this->_commonStorage->deleteItem($dummyNameText);
  154. } catch (Exception $e) {
  155. try {
  156. $this->_commonStorage->deleteItem($dummyNameText);
  157. } catch (Zend_Cloud_Exception $ignoreMe) {
  158. }
  159. throw $e;
  160. }
  161. }
  162. /**
  163. * Test store item
  164. *
  165. * @return void
  166. */
  167. public function testStoreItemStream()
  168. {
  169. $dummyNameStream = $this->_dummyNamePrefix . 'ForStoreStream';
  170. try {
  171. // Test stream data
  172. $originalFilename = realpath(dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files/data/dummy_data.txt');
  173. $stream = fopen($originalFilename, 'r');
  174. $this->_commonStorage->storeItem($dummyNameStream, $stream);
  175. $this->_wait();
  176. $returnedData = $this->_commonStorage->fetchItem($dummyNameStream);
  177. $this->assertEquals(file_get_contents($originalFilename), $returnedData);
  178. $this->_commonStorage->deleteItem($dummyNameStream);
  179. } catch (Exception $e) {
  180. try {
  181. $this->_commonStorage->deleteItem($dummyNameStream);
  182. } catch (Zend_Cloud_Exception $ignoreMe) {
  183. }
  184. throw $e;
  185. }
  186. }
  187. /**
  188. * Test delete item
  189. *
  190. * @return void
  191. */
  192. public function testDeleteItem()
  193. {
  194. $dummyName = $this->_dummyNamePrefix . 'ForDelete';
  195. try {
  196. // Test string data
  197. $originalData = $this->_dummyDataPrefix . 'DeleteItem';
  198. $this->_clobberItem($originalData, $dummyName);
  199. $this->_wait();
  200. $returnedData = $this->_commonStorage->fetchItem($dummyName);
  201. $this->assertEquals($originalData, $returnedData);
  202. $this->_wait();
  203. $this->_commonStorage->deleteItem($dummyName);
  204. $this->_wait();
  205. $this->assertFalse($this->_commonStorage->fetchItem($dummyName));
  206. } catch (Exception $e) {
  207. try {
  208. $this->_commonStorage->deleteItem($dummyName);
  209. } catch (Zend_Cloud_Exception $ignorme) {
  210. }
  211. throw $e;
  212. }
  213. }
  214. /**
  215. * Test copy item
  216. *
  217. * @return void
  218. */
  219. public function testCopyItem()
  220. {
  221. $this->markTestSkipped('This test should be re-enabled when the semantics of "copy" change');
  222. try {
  223. // Test string data
  224. $originalData = $this->_dummyDataPrefix . 'CopyItem';
  225. $dummyName1 = $this->_dummyNamePrefix . 'ForCopy1';
  226. $dummyName2 = $this->_dummyNamePrefix . 'ForCopy2';
  227. $this->_clobberItem($originalData, $dummyName1);
  228. $this->_wait();
  229. $returnedData = $this->_commonStorage->fetchItem($dummyName1);
  230. $this->assertEquals($originalData, $returnedData);
  231. $this->_wait();
  232. $this->_commonStorage->copyItem($dummyName1, $dummyName2);
  233. $copiedData = $this->_commonStorage->fetchItem($dummyName2);
  234. $this->assertEquals($originalData, $copiedData);
  235. $this->_commonStorage->deleteItem($dummyName1);
  236. $this->_commonStorage->fetchItem($dummyName1);
  237. $this->_commonStorage->deleteItem($dummyName2);
  238. $this->_commonStorage->fetchItem($dummyName2);
  239. } catch (Exception $e) {
  240. try {
  241. $this->_commonStorage->deleteItem($dummyName1);
  242. $this->_commonStorage->deleteItem($dummyName2);
  243. } catch (Zend_Cloud_Exception $ignoreme) {
  244. }
  245. throw $e;
  246. }
  247. }
  248. /**
  249. * Test move item
  250. *
  251. * @return void
  252. */
  253. public function testMoveItem()
  254. {
  255. $this->markTestSkipped('This test should be re-enabled when the semantics of "move" change');
  256. try {
  257. // Test string data
  258. $originalData = $this->_dummyDataPrefix . 'MoveItem';
  259. $dummyName1 = $this->_dummyNamePrefix . 'ForMove1';
  260. $dummyName2 = $this->_dummyNamePrefix . 'ForMove2';
  261. $this->_clobberItem($originalData, $dummyName1);
  262. $this->_wait();
  263. $this->_commonStorage->moveItem($dummyName1, $dummyName2);
  264. $this->_wait();
  265. $movedData = $this->_commonStorage->fetchItem($dummyName2);
  266. $this->assertEquals($originalData, $movedData);
  267. $this->assertFalse($this->_commonStorage->fetchItem($dummyName1));
  268. $this->_commonStorage->deleteItem($dummyName2);
  269. $this->assertFalse($this->_commonStorage->fetchItem($dummyName2));
  270. } catch (Exception $e) {
  271. try {
  272. $this->_commonStorage->deleteItem($dummyName1);
  273. $this->_commonStorage->deleteItem($dummyName2);
  274. } catch (Zend_Cloud_Exception $ignoreme) {
  275. }
  276. throw $e;
  277. }
  278. }
  279. /**
  280. * Test fetch metadata
  281. *
  282. * @return void
  283. */
  284. public function testFetchMetadata()
  285. {
  286. try {
  287. // Test string data
  288. $data = $this->_dummyDataPrefix . 'FetchMetadata';
  289. $dummyName = $this->_dummyNamePrefix . 'ForMetadata';
  290. $this->_clobberItem($data, $dummyName);
  291. $this->_wait();
  292. $this->_commonStorage->storeMetadata($dummyName, array('zend' => 'zend'));
  293. $this->_wait();
  294. // Hopefully we can assert more about the metadata in the future :/
  295. $this->assertTrue(is_array($this->_commonStorage->fetchMetadata($dummyName)));
  296. $this->_commonStorage->deleteItem($dummyName);
  297. } catch (Exception $e) {
  298. try {
  299. $this->_commonStorage->deleteItem($dummyName);
  300. } catch (Zend_Cloud_Exception $ignoreme) {
  301. }
  302. throw $e;
  303. }
  304. }
  305. /**
  306. * Test list items
  307. *
  308. * @return void
  309. */
  310. public function testListItems()
  311. {
  312. $dummyName1 = null;
  313. $dummyName2 = null;
  314. try {
  315. $dummyName1 = $this->_dummyNamePrefix . 'ForListItem1';
  316. $dummyData1 = $this->_dummyDataPrefix . 'Item1';
  317. $this->_clobberItem($dummyData1, $dummyName1);
  318. $dummyName2 = $this->_dummyNamePrefix . 'ForListItem2';
  319. $dummyData2 = $this->_dummyDataPrefix . 'Item2';
  320. $this->_clobberItem($dummyData2, $dummyName2);
  321. $this->_wait();
  322. $objects = $this->_commonStorage->listItems('');
  323. $this->assertEquals(2, sizeof($objects));
  324. // PHPUnit does an identical comparison for assertContains(), so we just
  325. // use assertTrue and in_array()
  326. $this->assertTrue(in_array($dummyName1, $objects));
  327. $this->assertTrue(in_array($dummyName2, $objects));
  328. $this->_commonStorage->deleteItem($dummyName1);
  329. $this->_commonStorage->deleteItem($dummyName2);
  330. } catch (Exception $e) {
  331. try {
  332. $this->_commonStorage->deleteItem($dummyName1);
  333. $this->_commonStorage->deleteItem($dummyName2);
  334. } catch (Zend_Cloud_Exception $ignoreme) {
  335. }
  336. throw $e;
  337. }
  338. }
  339. protected function _wait()
  340. {
  341. sleep($this->_waitPeriod);
  342. }
  343. /**
  344. * Put given item at given path
  345. *
  346. * Removes old item if it was stored there.
  347. *
  348. * @param string $data Data item to place there
  349. * @param string $path Path to write
  350. */
  351. protected function _clobberItem($data, $path)
  352. {
  353. if($this->_commonStorage->fetchItem($path)) {
  354. $this->_commonStorage->deleteItem($path);
  355. }
  356. $this->_wait();
  357. $this->_commonStorage->storeItem($path, $data);
  358. }
  359. /**
  360. * Get adapter configuration for concrete test
  361. * @returns Zend_Config
  362. */
  363. abstract protected function _getConfig();
  364. }