/tests/Zend/Cloud/StorageService/TestCase.php

https://github.com/shevron/zf2 · PHP · 389 lines · 64 code · 22 blank · 303 comment · 1 complexity · d5b615cf873966e02df4f42c80d10989 MD5 · raw file

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