/framework/Kolab_Storage/test/Horde/Kolab/Storage/Unit/CacheTest.php

https://github.com/imr/horde · PHP · 302 lines · 235 code · 27 blank · 40 comment · 0 complexity · 23f084a21baa98dadd8135f150d4dd64 MD5 · raw file

  1. <?php
  2. /**
  3. * Test the Kolab cache.
  4. *
  5. * PHP version 5
  6. *
  7. * @category Kolab
  8. * @package Kolab_Storage
  9. * @author Gunnar Wrobel <wrobel@pardus.de>
  10. * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
  11. * @link http://pear.horde.org/index.php?package=Kolab_Storage
  12. */
  13. /**
  14. * Test the Kolab cache.
  15. *
  16. * Copyright 2008-2014 Horde LLC (http://www.horde.org/)
  17. *
  18. * See the enclosed file COPYING for license information (LGPL). If you
  19. * did not receive this file, see http://www.horde.org/licenses/lgpl21.
  20. *
  21. * @category Kolab
  22. * @package Kolab_Storage
  23. * @author Gunnar Wrobel <wrobel@pardus.de>
  24. * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
  25. * @link http://pear.horde.org/index.php?package=Kolab_Storage
  26. */
  27. class Horde_Kolab_Storage_Unit_CacheTest
  28. extends Horde_Kolab_Storage_TestCase
  29. {
  30. public function setUp()
  31. {
  32. parent::setUp();
  33. $this->horde_cache = new Horde_Cache(
  34. new Horde_Cache_Storage_Mock()
  35. );
  36. $this->cache = new Horde_Kolab_Storage_Cache($this->horde_cache);
  37. }
  38. public function testGetDataCache()
  39. {
  40. $this->assertInstanceOf(
  41. 'Horde_Kolab_Storage_Cache_Data',
  42. $this->cache->getDataCache($this->_getDataParameters())
  43. );
  44. }
  45. public function testCachedDataCache()
  46. {
  47. $this->assertSame(
  48. $this->cache->getDataCache($this->_getDataParameters()),
  49. $this->cache->getDataCache($this->_getDataParameters())
  50. );
  51. }
  52. public function testNewHostNewData()
  53. {
  54. $params = $this->_getDataParameters();
  55. $params['host'] = 'b';
  56. $this->assertNotSame(
  57. $this->cache->getDataCache($params),
  58. $this->cache->getDataCache($this->_getDataParameters())
  59. );
  60. }
  61. /**
  62. * @expectedException Horde_Kolab_Storage_Exception
  63. */
  64. public function testDataMissingHost()
  65. {
  66. $params = $this->_getDataParameters();
  67. unset($params['host']);
  68. $this->cache->getDataCache($params);
  69. }
  70. public function testNewPortNewData()
  71. {
  72. $params = $this->_getDataParameters();
  73. $params['port'] = 2;
  74. $this->assertNotSame(
  75. $this->cache->getDataCache($params),
  76. $this->cache->getDataCache($this->_getDataParameters())
  77. );
  78. }
  79. /**
  80. * @expectedException Horde_Kolab_Storage_Exception
  81. */
  82. public function testDataMissingPort()
  83. {
  84. $params = $this->_getDataParameters();
  85. unset($params['port']);
  86. $this->cache->getDataCache($params);
  87. }
  88. public function testNewFolderNewData()
  89. {
  90. $params = $this->_getDataParameters();
  91. $params['folder'] = 'J';
  92. $this->assertNotSame(
  93. $this->cache->getDataCache($params),
  94. $this->cache->getDataCache($this->_getDataParameters())
  95. );
  96. }
  97. /**
  98. * @expectedException Horde_Kolab_Storage_Exception
  99. */
  100. public function testDataMissingFolder()
  101. {
  102. $params = $this->_getDataParameters();
  103. unset($params['folder']);
  104. $this->cache->getDataCache($params);
  105. }
  106. public function testNewTypeNewData()
  107. {
  108. $params = $this->_getDataParameters();
  109. $params['type'] = 'f';
  110. $this->assertNotSame(
  111. $this->cache->getDataCache($params),
  112. $this->cache->getDataCache($this->_getDataParameters())
  113. );
  114. }
  115. /**
  116. * @expectedException Horde_Kolab_Storage_Exception
  117. */
  118. public function testDataMissingType()
  119. {
  120. $params = $this->_getDataParameters();
  121. unset($params['type']);
  122. $this->cache->getDataCache($params);
  123. }
  124. public function testNewOwnerNewData()
  125. {
  126. $params = $this->_getDataParameters();
  127. $params['owner'] = 'f';
  128. $this->assertNotSame(
  129. $this->cache->getDataCache($params),
  130. $this->cache->getDataCache($this->_getDataParameters())
  131. );
  132. }
  133. public function testKeyCollision()
  134. {
  135. $params2 = $this->_getDataParameters();
  136. $params2['folder'] = 'I';
  137. $params2['type'] = 'e/';
  138. $params = $this->_getDataParameters();
  139. $params['folder'] = 'I/e';
  140. $params['type'] = '';
  141. $this->assertNotSame(
  142. $this->cache->getDataCache($params),
  143. $this->cache->getDataCache($params2)
  144. );
  145. }
  146. /**
  147. * @expectedException Horde_Kolab_Storage_Exception
  148. */
  149. public function testDataMissingOwner()
  150. {
  151. $params = $this->_getDataParameters();
  152. unset($params['owner']);
  153. $this->cache->getDataCache($params);
  154. }
  155. public function testLoadData()
  156. {
  157. $this->assertFalse(
  158. $this->cache->loadData('test')
  159. );
  160. }
  161. public function testStoreData()
  162. {
  163. $this->cache->storeData('test', true);
  164. $this->assertTrue(
  165. $this->cache->loadData('test')
  166. );
  167. }
  168. public function testLoadAttachment()
  169. {
  170. $this->assertFalse(
  171. $this->cache->loadAttachment('test', '1', '1')
  172. );
  173. }
  174. public function testStoreAttachment()
  175. {
  176. $this->cache->storeAttachment('test', '1', '1', $this->_getResource());
  177. $this->assertEquals(
  178. 'test',
  179. stream_get_contents(
  180. $this->cache->loadAttachment('test', '1', '1')
  181. )
  182. );
  183. }
  184. public function testStoreSameAttachment()
  185. {
  186. $resource = $this->_getResource();
  187. $resource2 = $this->_getResource();
  188. $this->cache->storeAttachment('test', '1', '1', $resource);
  189. $this->cache->storeAttachment('test', '1', '1', $resource2);
  190. rewind($resource);
  191. rewind($resource2);
  192. $this->assertSame(
  193. stream_get_contents($resource2),
  194. stream_get_contents($this->cache->loadAttachment('test', '1', '1'))
  195. );
  196. $this->assertSame(
  197. stream_get_contents($resource),
  198. stream_get_contents($this->cache->loadAttachment('test', '1', '1'))
  199. );
  200. }
  201. public function testStoreDifferentUidAttachment()
  202. {
  203. $resource = $this->_getResource();
  204. $resource2 = $this->_getResource();
  205. $this->cache->storeAttachment('test', '1', '1', $resource);
  206. $this->cache->storeAttachment('test', '2', '1', $resource2);
  207. rewind($resource);
  208. rewind($resource2);
  209. $this->assertSame(
  210. stream_get_contents($resource),
  211. stream_get_contents($this->cache->loadAttachment('test', '1', '1'))
  212. );
  213. $this->assertSame(
  214. stream_get_contents($resource2),
  215. stream_get_contents($this->cache->loadAttachment('test', '2', '1'))
  216. );
  217. }
  218. public function testStoreDifferentAttachmentId()
  219. {
  220. $resource = $this->_getResource();
  221. $resource2 = $this->_getResource();
  222. $this->cache->storeAttachment('test', '1', '1', $resource);
  223. $this->cache->storeAttachment('test', '1', '2', $resource2);
  224. rewind($resource);
  225. rewind($resource2);
  226. $this->assertSame(
  227. stream_get_contents($resource),
  228. stream_get_contents($this->cache->loadAttachment('test', '1', '1'))
  229. );
  230. $this->assertSame(
  231. stream_get_contents($resource2),
  232. stream_get_contents($this->cache->loadAttachment('test', '1', '2'))
  233. );
  234. }
  235. public function testLoadList()
  236. {
  237. $this->assertFalse(
  238. $this->cache->loadList('test')
  239. );
  240. }
  241. public function testStoreList()
  242. {
  243. $this->cache->storeList('test', true);
  244. $this->assertTrue(
  245. $this->cache->loadList('test')
  246. );
  247. }
  248. public function testCachingListData()
  249. {
  250. $this->cache->storeList('user@example.com:143', array('folders' => array('a', 'b')));
  251. $this->assertEquals(array('folders' => array('a', 'b')), $this->cache->loadList('user@example.com:143'));
  252. }
  253. private function _getIdParameters()
  254. {
  255. return array('host' => 'a', 'port' => 1, 'user' => 'x');
  256. }
  257. private function _getDataParameters()
  258. {
  259. return array(
  260. 'host' => 'a',
  261. 'port' => 1,
  262. 'prefix' => 'P',
  263. 'folder' => 'I',
  264. 'type' => 'e',
  265. 'owner' => 'x',
  266. );
  267. }
  268. private function _getResource()
  269. {
  270. $resource = fopen('php://temp', 'r+');
  271. fwrite($resource, 'test');
  272. rewind($resource);
  273. return $resource;
  274. }
  275. }