PageRenderTime 37ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/Tests/Unit/Cache/Backend/MemcachedBackendTest.php

https://github.com/christianjul/FLOW3-Composer
PHP | 284 lines | 165 code | 41 blank | 78 comment | 4 complexity | 9960a224bf898a4a401f0ffec1dc8c64 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-3.0
  1. <?php
  2. namespace TYPO3\FLOW3\Tests\Unit\Cache\Backend;
  3. /* *
  4. * This script belongs to the FLOW3 framework. *
  5. * *
  6. * It is free software; you can redistribute it and/or modify it under *
  7. * the terms of the GNU Lesser General Public License, either version 3 *
  8. * of the License, or (at your option) any later version. *
  9. * *
  10. * The TYPO3 project - inspiring people to share! *
  11. * */
  12. use TYPO3\FLOW3\Core\ApplicationContext;
  13. /**
  14. * Testcase for the cache to memcached backend
  15. *
  16. */
  17. class MemcachedBackendTest extends \TYPO3\FLOW3\Tests\UnitTestCase {
  18. /**
  19. * @var \TYPO3\FLOW3\Utility\Environment
  20. */
  21. protected $mockEnvironment;
  22. /**
  23. * Sets up this testcase
  24. *
  25. * @return void
  26. */
  27. public function setUp() {
  28. if (!extension_loaded('memcache')) {
  29. $this->markTestSkipped('memcache extension was not available');
  30. }
  31. try {
  32. if (!@fsockopen('localhost', 11211)) {
  33. $this->markTestSkipped('memcached not reachable');
  34. }
  35. } catch (\Exception $e) {
  36. $this->markTestSkipped('memcached not reachable');
  37. }
  38. $this->mockEnvironment = $this->getMock('TYPO3\FLOW3\Utility\Environment', array(), array(), '', FALSE);
  39. }
  40. /**
  41. * @test
  42. * @expectedException \TYPO3\FLOW3\Cache\Exception
  43. */
  44. public function setThrowsExceptionIfNoFrontEndHasBeenSet() {
  45. $backendOptions = array('servers' => array('localhost:11211'));
  46. $backend = new \TYPO3\FLOW3\Cache\Backend\MemcachedBackend(new ApplicationContext('Testing'), $backendOptions);
  47. $backend->injectEnvironment($this->mockEnvironment);
  48. $backend->initializeObject();
  49. $data = 'Some data';
  50. $identifier = 'MyIdentifier' . md5(uniqid(mt_rand(), TRUE));
  51. $backend->set($identifier, $data);
  52. }
  53. /**
  54. * @test
  55. * @expectedException \TYPO3\FLOW3\Cache\Exception
  56. */
  57. public function initializeObjectThrowsExceptionIfNoMemcacheServerIsConfigured() {
  58. $backend = new \TYPO3\FLOW3\Cache\Backend\MemcachedBackend(new ApplicationContext('Testing'));
  59. $backend->initializeObject();
  60. }
  61. /**
  62. * @test
  63. * @expectedException \TYPO3\FLOW3\Cache\Exception
  64. */
  65. public function setThrowsExceptionIfConfiguredServersAreUnreachable() {
  66. $backend = $this->setUpBackend(array('servers' => array('localhost:11212')));
  67. $data = 'Somedata';
  68. $identifier = 'MyIdentifier' . md5(uniqid(mt_rand(), TRUE));
  69. $backend->set($identifier, $data);
  70. }
  71. /**
  72. * @test
  73. */
  74. public function itIsPossibleToSetAndCheckExistenceInCache() {
  75. $backend = $this->setUpBackend();
  76. $data = 'Some data';
  77. $identifier = 'MyIdentifier' . md5(uniqid(mt_rand(), TRUE));
  78. $backend->set($identifier, $data);
  79. $inCache = $backend->has($identifier);
  80. $this->assertTrue($inCache, 'Memcache failed to set and check entry');
  81. }
  82. /**
  83. * @test
  84. */
  85. public function itIsPossibleToSetAndGetEntry() {
  86. $backend = $this->setUpBackend();
  87. $data = 'Some data';
  88. $identifier = 'MyIdentifier' . md5(uniqid(mt_rand(), TRUE));
  89. $backend->set($identifier, $data);
  90. $fetchedData = $backend->get($identifier);
  91. $this->assertEquals($data, $fetchedData, 'Memcache failed to set and retrieve data');
  92. }
  93. /**
  94. * @test
  95. */
  96. public function itIsPossibleToRemoveEntryFromCache() {
  97. $backend = $this->setUpBackend();
  98. $data = 'Some data';
  99. $identifier = 'MyIdentifier' . md5(uniqid(mt_rand(), TRUE));
  100. $backend->set($identifier, $data);
  101. $backend->remove($identifier);
  102. $inCache = $backend->has($identifier);
  103. $this->assertFalse($inCache, 'Failed to set and remove data from Memcache');
  104. }
  105. /**
  106. * @test
  107. */
  108. public function itIsPossibleToOverwriteAnEntryInTheCache() {
  109. $backend = $this->setUpBackend();
  110. $data = 'Some data';
  111. $identifier = 'MyIdentifier' . md5(uniqid(mt_rand(), TRUE));
  112. $backend->set($identifier, $data);
  113. $otherData = 'some other data';
  114. $backend->set($identifier, $otherData);
  115. $fetchedData = $backend->get($identifier);
  116. $this->assertEquals($otherData, $fetchedData, 'Memcache failed to overwrite and retrieve data');
  117. }
  118. /**
  119. * @test
  120. */
  121. public function findIdentifiersByTagFindsCacheEntriesWithSpecifiedTag() {
  122. $backend = $this->setUpBackend();
  123. $data = 'Some data';
  124. $identifier = 'MyIdentifier' . md5(uniqid(mt_rand(), TRUE));
  125. $backend->set($identifier, $data, array('UnitTestTag%tag1', 'UnitTestTag%tag2'));
  126. $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tag1');
  127. $this->assertEquals($identifier, $retrieved[0], 'Could not retrieve expected entry by tag.');
  128. $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tag2');
  129. $this->assertEquals($identifier, $retrieved[0], 'Could not retrieve expected entry by tag.');
  130. }
  131. /**
  132. * @test
  133. */
  134. public function setRemovesTagsFromPreviousSet() {
  135. $backend = $this->setUpBackend();
  136. $data = 'Some data';
  137. $identifier = 'MyIdentifier' . md5(uniqid(mt_rand(), TRUE));
  138. $backend->set($identifier, $data, array('UnitTestTag%tag1', 'UnitTestTag%tagX'));
  139. $backend->set($identifier, $data, array('UnitTestTag%tag3'));
  140. $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tagX');
  141. $this->assertEquals(array(), $retrieved, 'Found entry which should no longer exist.');
  142. }
  143. /**
  144. * @test
  145. */
  146. public function hasReturnsFalseIfTheEntryDoesntExist() {
  147. $backend = $this->setUpBackend();
  148. $identifier = 'NonExistingIdentifier' . md5(uniqid(mt_rand(), TRUE));
  149. $inCache = $backend->has($identifier);
  150. $this->assertFalse($inCache,'"has" did not return false when checking on non existing identifier');
  151. }
  152. /**
  153. * @test
  154. */
  155. public function removeReturnsFalseIfTheEntryDoesntExist() {
  156. $backend = $this->setUpBackend();
  157. $identifier = 'NonExistingIdentifier' . md5(uniqid(mt_rand(), TRUE));
  158. $inCache = $backend->remove($identifier);
  159. $this->assertFalse($inCache,'"remove" did not return false when checking on non existing identifier');
  160. }
  161. /**
  162. * @test
  163. */
  164. public function flushByTagRemovesCacheEntriesWithSpecifiedTag() {
  165. $backend = $this->setUpBackend();
  166. $data = 'some data' . microtime();
  167. $backend->set('BackendMemcacheTest1', $data, array('UnitTestTag%test', 'UnitTestTag%boring'));
  168. $backend->set('BackendMemcacheTest2', $data, array('UnitTestTag%test', 'UnitTestTag%special'));
  169. $backend->set('BackendMemcacheTest3', $data, array('UnitTestTag%test'));
  170. $backend->flushByTag('UnitTestTag%special');
  171. $this->assertTrue($backend->has('BackendMemcacheTest1'), 'BackendMemcacheTest1');
  172. $this->assertFalse($backend->has('BackendMemcacheTest2'), 'BackendMemcacheTest2');
  173. $this->assertTrue($backend->has('BackendMemcacheTest3'), 'BackendMemcacheTest3');
  174. }
  175. /**
  176. * @test
  177. */
  178. public function flushRemovesAllCacheEntries() {
  179. $backend = $this->setUpBackend();
  180. $data = 'some data' . microtime();
  181. $backend->set('BackendMemcacheTest1', $data);
  182. $backend->set('BackendMemcacheTest2', $data);
  183. $backend->set('BackendMemcacheTest3', $data);
  184. $backend->flush();
  185. $this->assertFalse($backend->has('BackendMemcacheTest1'), 'BackendMemcacheTest1');
  186. $this->assertFalse($backend->has('BackendMemcacheTest2'), 'BackendMemcacheTest2');
  187. $this->assertFalse($backend->has('BackendMemcacheTest3'), 'BackendMemcacheTest3');
  188. }
  189. /**
  190. * @test
  191. */
  192. public function flushRemovesOnlyOwnEntries() {
  193. $backendOptions = array('servers' => array('localhost:11211'));
  194. $thisCache = $this->getMock('TYPO3\FLOW3\Cache\Frontend\AbstractFrontend', array(), array(), '', FALSE);
  195. $thisCache->expects($this->any())->method('getIdentifier')->will($this->returnValue('thisCache'));
  196. $thisBackend = new \TYPO3\FLOW3\Cache\Backend\MemcachedBackend(new ApplicationContext('Testing'), $backendOptions);
  197. $thisBackend->injectEnvironment($this->mockEnvironment);
  198. $thisBackend->setCache($thisCache);
  199. $thisBackend->initializeObject();
  200. $thatCache = $this->getMock('TYPO3\FLOW3\Cache\Frontend\AbstractFrontend', array(), array(), '', FALSE);
  201. $thatCache->expects($this->any())->method('getIdentifier')->will($this->returnValue('thatCache'));
  202. $thatBackend = new \TYPO3\FLOW3\Cache\Backend\MemcachedBackend(new ApplicationContext('Testing'), $backendOptions);
  203. $thatBackend->injectEnvironment($this->mockEnvironment);
  204. $thatBackend->setCache($thatCache);
  205. $thatBackend->initializeObject();
  206. $thisBackend->set('thisEntry', 'Hello');
  207. $thatBackend->set('thatEntry', 'World!');
  208. $thatBackend->flush();
  209. $this->assertEquals('Hello', $thisBackend->get('thisEntry'));
  210. $this->assertFalse($thatBackend->has('thatEntry'));
  211. }
  212. /**
  213. * Check if we can store ~5 MB of data, this gives some headroom for the
  214. * reflection data.
  215. *
  216. * @test
  217. */
  218. public function largeDataIsStored() {
  219. $backend = $this->setUpBackend();
  220. $data = str_repeat('abcde', 1024 * 1024);
  221. $backend->set('tooLargeData', $data);
  222. $this->assertTrue($backend->has('tooLargeData'));
  223. $this->assertEquals($backend->get('tooLargeData'), $data);
  224. }
  225. /**
  226. * Sets up the memcached backend used for testing
  227. *
  228. * @param array $backendOptions Options for the memcache backend
  229. * @return \TYPO3\FLOW3\Cache\Backend\MemcachedBackend
  230. */
  231. protected function setUpBackend(array $backendOptions = array()) {
  232. $cache = $this->getMock('TYPO3\FLOW3\Cache\Frontend\FrontendInterface', array(), array(), '', FALSE);
  233. if ($backendOptions == array()) {
  234. $backendOptions = array('servers' => array('localhost:11211'));
  235. }
  236. $backend = new \TYPO3\FLOW3\Cache\Backend\MemcachedBackend(new ApplicationContext('Testing'), $backendOptions);
  237. $backend->injectEnvironment($this->mockEnvironment);
  238. $backend->setCache($cache);
  239. $backend->initializeObject();
  240. return $backend;
  241. }
  242. }
  243. ?>