PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/t3lib/cache/backend/t3lib_cache_backend_memcachedbackendTest.php

https://github.com/andreaswolf/typo3-tceforms
PHP | 327 lines | 168 code | 40 blank | 119 comment | 3 complexity | 801046cbd0f4cc8e6f78214a98937517 MD5 | raw file
Possible License(s): Apache-2.0, BSD-2-Clause, LGPL-3.0
  1. <?php
  2. /***************************************************************
  3. * Copyright notice
  4. *
  5. * (c) 2009-2011 Ingo Renner <ingo@typo3.org>
  6. * All rights reserved
  7. *
  8. * This script is part of the TYPO3 project. The TYPO3 project is
  9. * free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * The GNU General Public License can be found at
  15. * http://www.gnu.org/copyleft/gpl.html.
  16. *
  17. * This script is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * This copyright notice MUST APPEAR in all copies of the script!
  23. ***************************************************************/
  24. /**
  25. * Testcase for the cache to memcached backend
  26. *
  27. * This file is a backport from FLOW3
  28. *
  29. * @author Ingo Renner <ingo@typo3.org>
  30. * @package TYPO3
  31. * @subpackage tests
  32. * @version $Id$
  33. */
  34. class t3lib_cache_backend_MemcachedBackendTest extends tx_phpunit_testcase {
  35. /**
  36. * Sets up this testcase
  37. *
  38. * @return void
  39. * @author Christian Jul Jensen <julle@typo3.org>
  40. * @author Ingo Renner <ingo@typo3.org>
  41. */
  42. public function setUp() {
  43. if (!extension_loaded('memcache')) {
  44. $this->markTestSkipped('memcache extension was not available');
  45. }
  46. try {
  47. if (!fsockopen('localhost', 11211)) {
  48. $this->markTestSkipped('memcached not reachable');
  49. }
  50. } catch (Exception $e) {
  51. $this->markTestSkipped('memcached not reachable');
  52. }
  53. }
  54. /**
  55. * @test
  56. * @author Christian Jul Jensen <julle@typo3.org>
  57. * @author Ingo Renner <ingo@typo3.org>
  58. * @expectedException t3lib_cache_Exception
  59. */
  60. public function setThrowsExceptionIfNoFrontEndHasBeenSet() {
  61. $backendOptions = array('servers' => array('localhost:11211'));
  62. $backend = new t3lib_cache_backend_MemcachedBackend($backendOptions);
  63. $data = 'Some data';
  64. $identifier = uniqid('MyIdentifier');
  65. $backend->set($identifier, $data);
  66. }
  67. /**
  68. * @test
  69. * @author Robert Lemke <robert@typo3.org>
  70. * @author Ingo Renner <ingo@typo3.org>
  71. * @expectedException t3lib_cache_Exception
  72. */
  73. public function constructorThrowsExceptionIfNoMemcacheServerIsConfigured() {
  74. $backend = new t3lib_cache_backend_MemcachedBackend();
  75. }
  76. /**
  77. * @test
  78. * @author Christian Jul Jensen <julle@typo3.org>
  79. * @author Ingo Renner <ingo@typo3.org>
  80. * @expectedException t3lib_cache_Exception
  81. */
  82. public function setThrowsExceptionIfConfiguredServersAreUnreachable() {
  83. $backend = $this->setUpBackend(array('servers' => array('julle.did.this:1234')));
  84. $data = 'Somedata';
  85. $identifier = uniqid('MyIdentifier');
  86. $backend->set($identifier, $data);
  87. }
  88. /**
  89. * @test
  90. * @author Christian Jul Jensen <julle@typo3.org>
  91. */
  92. public function itIsPossibleToSetAndCheckExistenceInCache() {
  93. $backend = $this->setUpBackend();
  94. $data = 'Some data';
  95. $identifier = uniqid('MyIdentifier');
  96. $backend->set($identifier, $data);
  97. $inCache = $backend->has($identifier);
  98. $this->assertTrue($inCache, 'Memcache failed to set and check entry');
  99. }
  100. /**
  101. * @test
  102. * @author Christian Jul Jensen <julle@typo3.org>
  103. */
  104. public function itIsPossibleToSetAndGetEntry() {
  105. $backend = $this->setUpBackend();
  106. $data = 'Some data';
  107. $identifier = uniqid('MyIdentifier');
  108. $backend->set($identifier, $data);
  109. $fetchedData = $backend->get($identifier);
  110. $this->assertEquals($data, $fetchedData, 'Memcache failed to set and retrieve data');
  111. }
  112. /**
  113. * @test
  114. * @author Christian Jul Jensen <julle@typo3.org>
  115. */
  116. public function itIsPossibleToRemoveEntryFromCache() {
  117. $backend = $this->setUpBackend();
  118. $data = 'Some data';
  119. $identifier = uniqid('MyIdentifier');
  120. $backend->set($identifier, $data);
  121. $backend->remove($identifier);
  122. $inCache = $backend->has($identifier);
  123. $this->assertFalse($inCache, 'Failed to set and remove data from Memcache');
  124. }
  125. /**
  126. * @test
  127. * @author Christian Jul Jensen <julle@typo3.org>
  128. */
  129. public function itIsPossibleToOverwriteAnEntryInTheCache() {
  130. $backend = $this->setUpBackend();
  131. $data = 'Some data';
  132. $identifier = uniqid('MyIdentifier');
  133. $backend->set($identifier, $data);
  134. $otherData = 'some other data';
  135. $backend->set($identifier, $otherData);
  136. $fetchedData = $backend->get($identifier);
  137. $this->assertEquals($otherData, $fetchedData, 'Memcache failed to overwrite and retrieve data');
  138. }
  139. /**
  140. * @test
  141. * @author Karsten Dambekalns <karsten@typo3.org>
  142. */
  143. public function findIdentifiersByTagFindsCacheEntriesWithSpecifiedTag() {
  144. $backend = $this->setUpBackend();
  145. $data = 'Some data';
  146. $identifier = uniqid('MyIdentifier');
  147. $backend->set($identifier, $data, array('UnitTestTag%tag1', 'UnitTestTag%tag2'));
  148. $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tag1');
  149. $this->assertEquals($identifier, $retrieved[0], 'Could not retrieve expected entry by tag.');
  150. $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tag2');
  151. $this->assertEquals($identifier, $retrieved[0], 'Could not retrieve expected entry by tag.');
  152. }
  153. /**
  154. * @test
  155. * @author Karsten Dambekalns <karsten@typo3.org>
  156. */
  157. public function setRemovesTagsFromPreviousSet() {
  158. $backend = $this->setUpBackend();
  159. $data = 'Some data';
  160. $identifier = uniqid('MyIdentifier');
  161. $backend->set($identifier, $data, array('UnitTestTag%tag1', 'UnitTestTag%tag2'));
  162. $backend->set($identifier, $data, array('UnitTestTag%tag3'));
  163. $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tagX');
  164. $this->assertEquals(array(), $retrieved, 'Found entry which should no longer exist.');
  165. }
  166. /**
  167. * @test
  168. * @author Christian Jul Jensen <julle@typo3.org>
  169. */
  170. public function hasReturnsFalseIfTheEntryDoesntExist() {
  171. $backend = $this->setUpBackend();
  172. $identifier = uniqid('NonExistingIdentifier');
  173. $inCache = $backend->has($identifier);
  174. $this->assertFalse($inCache,'"has" did not return false when checking on non existing identifier');
  175. }
  176. /**
  177. * @test
  178. * @author Christian Jul Jensen <julle@typo3.org>
  179. */
  180. public function removeReturnsFalseIfTheEntryDoesntExist() {
  181. $backend = $this->setUpBackend();
  182. $identifier = uniqid('NonExistingIdentifier');
  183. $inCache = $backend->remove($identifier);
  184. $this->assertFalse($inCache,'"remove" did not return false when checking on non existing identifier');
  185. }
  186. /**
  187. * @test
  188. * @author Robert Lemke <robert@typo3.org>
  189. * @author Karsten Dambekalns <karsten@typo3.org>
  190. */
  191. public function flushByTagRemovesCacheEntriesWithSpecifiedTag() {
  192. $backend = $this->setUpBackend();
  193. $data = 'some data' . microtime();
  194. $backend->set('BackendMemcacheTest1', $data, array('UnitTestTag%test', 'UnitTestTag%boring'));
  195. $backend->set('BackendMemcacheTest2', $data, array('UnitTestTag%test', 'UnitTestTag%special'));
  196. $backend->set('BackendMemcacheTest3', $data, array('UnitTestTag%test'));
  197. $backend->flushByTag('UnitTestTag%special');
  198. $this->assertTrue($backend->has('BackendMemcacheTest1'), 'BackendMemcacheTest1');
  199. $this->assertFalse($backend->has('BackendMemcacheTest2'), 'BackendMemcacheTest2');
  200. $this->assertTrue($backend->has('BackendMemcacheTest3'), 'BackendMemcacheTest3');
  201. }
  202. /**
  203. * @test
  204. * @author Karsten Dambekalns <karsten@typo3.org>
  205. */
  206. public function flushRemovesAllCacheEntries() {
  207. $backend = $this->setUpBackend();
  208. $data = 'some data' . microtime();
  209. $backend->set('BackendMemcacheTest1', $data);
  210. $backend->set('BackendMemcacheTest2', $data);
  211. $backend->set('BackendMemcacheTest3', $data);
  212. $backend->flush();
  213. $this->assertFalse($backend->has('BackendMemcacheTest1'), 'BackendMemcacheTest1');
  214. $this->assertFalse($backend->has('BackendMemcacheTest2'), 'BackendMemcacheTest2');
  215. $this->assertFalse($backend->has('BackendMemcacheTest3'), 'BackendMemcacheTest3');
  216. }
  217. /**
  218. * @test
  219. * @author Karsten Dambekalns <karsten@typo3.org>
  220. * @author Ingo Renner <ingo@typo3.org>
  221. */
  222. public function flushRemovesOnlyOwnEntries() {
  223. $backendOptions = array('servers' => array('localhost:11211'));
  224. $thisCache = $this->getMock(
  225. 't3lib_cache_frontend_AbstractFrontend',
  226. array(),
  227. array(),
  228. '',
  229. FALSE
  230. );
  231. $thisCache->expects($this->any())
  232. ->method('getIdentifier')
  233. ->will($this->returnValue('thisCache'));
  234. $thisBackend = new t3lib_cache_backend_MemcachedBackend($backendOptions);
  235. $thisBackend->setCache($thisCache);
  236. $thatCache = $this->getMock(
  237. 't3lib_cache_frontend_AbstractFrontend',
  238. array(),
  239. array(),
  240. '',
  241. FALSE
  242. );
  243. $thatCache->expects($this->any())
  244. ->method('getIdentifier')
  245. ->will($this->returnValue('thatCache'));
  246. $thatBackend = new t3lib_cache_backend_MemcachedBackend($backendOptions);
  247. $thatBackend->setCache($thatCache);
  248. $thisBackend->set('thisEntry', 'Hello');
  249. $thatBackend->set('thatEntry', 'World!');
  250. $thatBackend->flush();
  251. $this->assertEquals('Hello', $thisBackend->get('thisEntry'));
  252. $this->assertFalse($thatBackend->has('thatEntry'));
  253. }
  254. /**
  255. * Check if we can store ~5 MB of data, this gives some headroom for the
  256. * reflection data.
  257. *
  258. * @test
  259. * @author Karsten Dambekalns <karsten@typo3.org>
  260. */
  261. public function largeDataIsStored() {
  262. $backend = $this->setUpBackend();
  263. $data = str_repeat('abcde', 1024 * 1024);
  264. $backend->set('tooLargeData', $data);
  265. $this->assertTrue($backend->has('tooLargeData'));
  266. $this->assertEquals($backend->get('tooLargeData'), $data);
  267. }
  268. /**
  269. * Sets up the memcached backend used for testing
  270. *
  271. * @param array $backendOptions Options for the memcache backend
  272. * @return t3lib_cache_backend_MemcachedBackend
  273. * @author Christian Jul Jensen <julle@typo3.org>
  274. * @author Karsten Dambekalns <karsten@typo3.org>
  275. * @author Ingo Renner <ingo@typo3.org>
  276. */
  277. protected function setUpBackend(array $backendOptions = array()) {
  278. $cache = $this->getMock('t3lib_cache_frontend_Frontend', array(), array(), '', FALSE);
  279. if (empty($backendOptions)) {
  280. $backendOptions = array('servers' => array('localhost:11211'));
  281. }
  282. $backend = new t3lib_cache_backend_MemcachedBackend($backendOptions);
  283. $backend->setCache($cache);
  284. return $backend;
  285. }
  286. }
  287. ?>