TestCase.php 13 KB

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