PageRenderTime 73ms CodeModel.GetById 20ms RepoModel.GetById 2ms app.codeStats 0ms

/Tests/Unit/Cache/Backend/ApcBackendTest.php

https://github.com/christianjul/FLOW3-Composer
PHP | 249 lines | 145 code | 36 blank | 68 comment | 7 complexity | 399d9e2ba8a51d322d35cd415b1df523 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 APC cache backend
  15. *
  16. */
  17. class ApcBackendTest 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. $this->markTestSkipped('Disabling ALL apc tests for now as they are so unreliable');
  29. if (!extension_loaded('apc') || ini_get('apc.enabled') == 0 || ini_get('apc.enable_cli') == 0) {
  30. $this->markTestSkipped('APC extension was not available, or it was disabled for CLI.');
  31. }
  32. if (ini_get('apc.slam_defense') == 1) {
  33. $this->markTestSkipped('This testcase can only be executed with apc.slam_defense = Off');
  34. }
  35. $this->mockEnvironment = $this->getMock('TYPO3\FLOW3\Utility\Environment', array(), array(), '', FALSE);
  36. }
  37. /**
  38. * @test
  39. * @expectedException \TYPO3\FLOW3\Cache\Exception
  40. */
  41. public function setThrowsExceptionIfNoFrontEndHasBeenSet() {
  42. $backend = new \TYPO3\FLOW3\Cache\Backend\ApcBackend(new ApplicationContext('Testing'));
  43. $backend->injectEnvironment($this->mockEnvironment);
  44. $data = 'Some data';
  45. $identifier = 'MyIdentifier' . md5(uniqid(mt_rand(), TRUE));
  46. $backend->set($identifier, $data);
  47. }
  48. /**
  49. * @test
  50. */
  51. public function itIsPossibleToSetAndCheckExistenceInCache() {
  52. $backend = $this->setUpBackend();
  53. $data = 'Some data';
  54. $identifier = 'MyIdentifier' . md5(uniqid(mt_rand(), TRUE));
  55. $backend->set($identifier, $data);
  56. $inCache = $backend->has($identifier);
  57. $this->assertTrue($inCache, 'APC backend failed to set and check entry');
  58. }
  59. /**
  60. * @ test
  61. */
  62. public function itIsPossibleToSetAndGetEntry() {
  63. $backend = $this->setUpBackend();
  64. $data = 'Some data';
  65. $identifier = 'MyIdentifier' . md5(uniqid(mt_rand(), TRUE));
  66. $backend->set($identifier, $data);
  67. $fetchedData = $backend->get($identifier);
  68. $this->assertEquals($data, $fetchedData, 'APC backend failed to set and retrieve data');
  69. }
  70. /**
  71. * @test
  72. */
  73. public function itIsPossibleToRemoveEntryFromCache() {
  74. $backend = $this->setUpBackend();
  75. $data = 'Some data';
  76. $identifier = 'MyIdentifier' . md5(uniqid(mt_rand(), TRUE));
  77. $backend->set($identifier, $data);
  78. $backend->remove($identifier);
  79. $inCache = $backend->has($identifier);
  80. $this->assertFalse($inCache, 'Failed to set and remove data from APC backend');
  81. }
  82. /**
  83. * @test
  84. */
  85. public function itIsPossibleToOverwriteAnEntryInTheCache() {
  86. $backend = $this->setUpBackend();
  87. $data = 'Some data';
  88. $identifier = 'MyIdentifier' . md5(uniqid(mt_rand(), TRUE));
  89. $backend->set($identifier, $data);
  90. $otherData = 'some other data';
  91. $backend->set($identifier, $otherData);
  92. $fetchedData = $backend->get($identifier);
  93. $this->assertEquals($otherData, $fetchedData, 'APC backend failed to overwrite and retrieve data');
  94. }
  95. /**
  96. * @test
  97. */
  98. public function findIdentifiersByTagFindsSetEntries() {
  99. $backend = $this->setUpBackend();
  100. $data = 'Some data';
  101. $identifier = 'MyIdentifier' . md5(uniqid(mt_rand(), TRUE));
  102. $backend->set($identifier, $data, array('UnitTestTag%tag1', 'UnitTestTag%tag2'));
  103. $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tag1');
  104. $this->assertEquals($identifier, $retrieved[0], 'Could not retrieve expected entry by tag.');
  105. $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tag2');
  106. $this->assertEquals($identifier, $retrieved[0], 'Could not retrieve expected entry by tag.');
  107. }
  108. /**
  109. * @test
  110. */
  111. public function setRemovesTagsFromPreviousSet() {
  112. $backend = $this->setUpBackend();
  113. $data = 'Some data';
  114. $identifier = 'MyIdentifier' . md5(uniqid(mt_rand(), TRUE));
  115. $backend->set($identifier, $data, array('UnitTestTag%tag1', 'UnitTestTag%tagX'));
  116. $backend->set($identifier, $data, array('UnitTestTag%tag3'));
  117. $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tagX');
  118. $this->assertEquals(array(), $retrieved, 'Found entry which should no longer exist.');
  119. }
  120. /**
  121. * @test
  122. */
  123. public function hasReturnsFalseIfTheEntryDoesntExist() {
  124. $backend = $this->setUpBackend();
  125. $identifier = 'NonExistingIdentifier' . md5(uniqid(mt_rand(), TRUE));
  126. $inCache = $backend->has($identifier);
  127. $this->assertFalse($inCache,'"has" did not return false when checking on non existing identifier');
  128. }
  129. /**
  130. * @test
  131. */
  132. public function removeReturnsFalseIfTheEntryDoesntExist() {
  133. $backend = $this->setUpBackend();
  134. $identifier = 'NonExistingIdentifier' . md5(uniqid(mt_rand(), TRUE));
  135. $inCache = $backend->remove($identifier);
  136. $this->assertFalse($inCache,'"remove" did not return false when checking on non existing identifier');
  137. }
  138. /**
  139. * @test
  140. */
  141. public function flushByTagRemovesCacheEntriesWithSpecifiedTag() {
  142. $backend = $this->setUpBackend();
  143. $data = 'some data' . microtime();
  144. $backend->set('BackendAPCTest1', $data, array('UnitTestTag%test', 'UnitTestTag%boring'));
  145. $backend->set('BackendAPCTest2', $data, array('UnitTestTag%test', 'UnitTestTag%special'));
  146. $backend->set('BackendAPCTest3', $data, array('UnitTestTag%test'));
  147. $backend->flushByTag('UnitTestTag%special');
  148. $this->assertTrue($backend->has('BackendAPCTest1'), 'BackendAPCTest1');
  149. $this->assertFalse($backend->has('BackendAPCTest2'), 'BackendAPCTest2');
  150. $this->assertTrue($backend->has('BackendAPCTest3'), 'BackendAPCTest3');
  151. }
  152. /**
  153. * @test
  154. */
  155. public function flushRemovesAllCacheEntries() {
  156. $this->markTestSkipped('Somehow, this test does not work...');
  157. $backend = $this->setUpBackend();
  158. $data = 'some data' . microtime();
  159. $backend->set('BackendAPCTest1', $data);
  160. $backend->set('BackendAPCTest2', $data);
  161. $backend->set('BackendAPCTest3', $data);
  162. $backend->flush();
  163. $this->assertFalse($backend->has('BackendAPCTest1'), 'BackendAPCTest1');
  164. $this->assertFalse($backend->has('BackendAPCTest2'), 'BackendAPCTest2');
  165. $this->assertFalse($backend->has('BackendAPCTest3'), 'BackendAPCTest3');
  166. }
  167. /**
  168. * @test
  169. */
  170. public function flushRemovesOnlyOwnEntries() {
  171. $thisCache = $this->getMock('TYPO3\FLOW3\Cache\Frontend\FrontendInterface', array(), array(), '', FALSE);
  172. $thisCache->expects($this->any())->method('getIdentifier')->will($this->returnValue('thisCache'));
  173. $thisBackend = new \TYPO3\FLOW3\Cache\Backend\ApcBackend('Testing');
  174. $thisBackend->injectEnvironment($this->mockEnvironment);
  175. $thisBackend->setCache($thisCache);
  176. $thatCache = $this->getMock('TYPO3\FLOW3\Cache\Frontend\FrontendInterface', array(), array(), '', FALSE);
  177. $thatCache->expects($this->any())->method('getIdentifier')->will($this->returnValue('thatCache'));
  178. $thatBackend = new \TYPO3\FLOW3\Cache\Backend\ApcBackend('Testing');
  179. $thatBackend->injectEnvironment($this->mockEnvironment);
  180. $thatBackend->setCache($thatCache);
  181. $thisBackend->set('thisEntry', 'Hello');
  182. $thatBackend->set('thatEntry', 'World!');
  183. $thatBackend->flush();
  184. $this->assertEquals('Hello', $thisBackend->get('thisEntry'));
  185. $this->assertFalse($thatBackend->has('thatEntry'));
  186. }
  187. /**
  188. * Check if we can store ~5 MB of data, this gives some headroom.
  189. *
  190. * @test
  191. */
  192. public function largeDataIsStored() {
  193. $backend = $this->setUpBackend();
  194. $data = str_repeat('abcde', 1024 * 1024);
  195. $identifier = 'tooLargeData' . md5(uniqid(mt_rand(), TRUE));
  196. $backend->set($identifier, $data);
  197. $this->assertTrue($backend->has($identifier));
  198. $this->assertEquals($backend->get($identifier), $data);
  199. }
  200. /**
  201. * Sets up the APC backend used for testing
  202. *
  203. * @return \TYPO3\FLOW3\Cache\Backend\ApcBackend
  204. */
  205. protected function setUpBackend() {
  206. $cache = $this->getMock('TYPO3\FLOW3\Cache\Frontend\FrontendInterface', array(), array(), '', FALSE);
  207. $backend = new \TYPO3\FLOW3\Cache\Backend\ApcBackend(new ApplicationContext('Testing'));
  208. $backend->injectEnvironment($this->mockEnvironment);
  209. $backend->setCache($cache);
  210. return $backend;
  211. }
  212. }
  213. ?>