PageRenderTime 53ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/t3lib/cache/backend/t3lib_cache_backend_apcbackendTest.php

https://github.com/andreaswolf/typo3-tceforms
PHP | 275 lines | 135 code | 35 blank | 105 comment | 3 complexity | 3f1d62c5e9547c62a2d4753e93e7eee5 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 APC cache 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_ApcBackendTest extends tx_phpunit_testcase {
  35. /**
  36. * Sets up this testcase
  37. *
  38. * @return void
  39. * @author Karsten Dambekalns <karsten@typo3.org>
  40. * @author Ingo Renner <ingo@typo3.org>
  41. */
  42. public function setUp() {
  43. if (!extension_loaded('apc')) {
  44. $this->markTestSkipped('APC extension was not available');
  45. }
  46. if (ini_get('apc.slam_defense') == 1) {
  47. $this->markTestSkipped('This testcase can only be executed with apc.slam_defense = Off');
  48. }
  49. }
  50. /**
  51. * @test
  52. * @author Christian Jul Jensen <julle@typo3.org>
  53. * @author Karsten Dambekalns <karsten@typo3.org>
  54. * @expectedException t3lib_cache_Exception
  55. */
  56. public function setThrowsExceptionIfNoFrontEndHasBeenSet() {
  57. $backend = new t3lib_cache_backend_ApcBackend();
  58. $data = 'Some data';
  59. $identifier = uniqid('MyIdentifier');
  60. $backend->set($identifier, $data);
  61. }
  62. /**
  63. * @test
  64. * @author Christian Jul Jensen <julle@typo3.org>
  65. */
  66. public function itIsPossibleToSetAndCheckExistenceInCache() {
  67. $backend = $this->setUpBackend();
  68. $data = 'Some data';
  69. $identifier = uniqid('MyIdentifier');
  70. $backend->set($identifier, $data);
  71. $inCache = $backend->has($identifier);
  72. $this->assertTrue($inCache, 'APC backend failed to set and check entry');
  73. }
  74. /**
  75. * @test
  76. * @author Christian Jul Jensen <julle@typo3.org>
  77. */
  78. public function itIsPossibleToSetAndGetEntry() {
  79. $backend = $this->setUpBackend();
  80. $data = 'Some data';
  81. $identifier = uniqid('MyIdentifier');
  82. $backend->set($identifier, $data);
  83. $fetchedData = $backend->get($identifier);
  84. $this->assertEquals($data, $fetchedData, 'APC backend failed to set and retrieve data');
  85. }
  86. /**
  87. * @test
  88. * @author Christian Jul Jensen <julle@typo3.org>
  89. */
  90. public function itIsPossibleToRemoveEntryFromCache() {
  91. $backend = $this->setUpBackend();
  92. $data = 'Some data';
  93. $identifier = uniqid('MyIdentifier');
  94. $backend->set($identifier, $data);
  95. $backend->remove($identifier);
  96. $inCache = $backend->has($identifier);
  97. $this->assertFalse($inCache, 'Failed to set and remove data from APC backend');
  98. }
  99. /**
  100. * @test
  101. * @author Christian Jul Jensen <julle@typo3.org>
  102. */
  103. public function itIsPossibleToOverwriteAnEntryInTheCache() {
  104. $backend = $this->setUpBackend();
  105. $data = 'Some data';
  106. $identifier = uniqid('MyIdentifier');
  107. $backend->set($identifier, $data);
  108. $otherData = 'some other data';
  109. $backend->set($identifier, $otherData);
  110. $fetchedData = $backend->get($identifier);
  111. $this->assertEquals($otherData, $fetchedData, 'APC backend failed to overwrite and retrieve data');
  112. }
  113. /**
  114. * @test
  115. * @author Karsten Dambekalns <karsten@typo3.org>
  116. */
  117. public function findIdentifiersByTagFindsSetEntries() {
  118. $backend = $this->setUpBackend();
  119. $data = 'Some data';
  120. $identifier = uniqid('MyIdentifier');
  121. $backend->set($identifier, $data, array('UnitTestTag%tag1', 'UnitTestTag%tag2'));
  122. $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tag1');
  123. $this->assertEquals($identifier, $retrieved[0], 'Could not retrieve expected entry by tag.');
  124. $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tag2');
  125. $this->assertEquals($identifier, $retrieved[0], 'Could not retrieve expected entry by tag.');
  126. }
  127. /**
  128. * @test
  129. * @author Karsten Dambekalns <karsten@typo3.org>
  130. */
  131. public function setRemovesTagsFromPreviousSet() {
  132. $backend = $this->setUpBackend();
  133. $data = 'Some data';
  134. $identifier = uniqid('MyIdentifier');
  135. $backend->set($identifier, $data, array('UnitTestTag%tag1', 'UnitTestTag%tagX'));
  136. $backend->set($identifier, $data, array('UnitTestTag%tag3'));
  137. $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tagX');
  138. $this->assertEquals(array(), $retrieved, 'Found entry which should no longer exist.');
  139. }
  140. /**
  141. * @test
  142. * @author Christian Jul Jensen <julle@typo3.org>
  143. */
  144. public function hasReturnsFalseIfTheEntryDoesntExist() {
  145. $backend = $this->setUpBackend();
  146. $identifier = uniqid('NonExistingIdentifier');
  147. $inCache = $backend->has($identifier);
  148. $this->assertFalse($inCache,'"has" did not return false when checking on non existing identifier');
  149. }
  150. /**
  151. * @test
  152. * @author Christian Jul Jensen <julle@typo3.org>
  153. */
  154. public function removeReturnsFalseIfTheEntryDoesntExist() {
  155. $backend = $this->setUpBackend();
  156. $identifier = uniqid('NonExistingIdentifier');
  157. $inCache = $backend->remove($identifier);
  158. $this->assertFalse($inCache,'"remove" did not return false when checking on non existing identifier');
  159. }
  160. /**
  161. * @test
  162. * @author Robert Lemke <robert@typo3.org>
  163. * @author Karsten Dambekalns <karsten@typo3.org>
  164. */
  165. public function flushByTagRemovesCacheEntriesWithSpecifiedTag() {
  166. $backend = $this->setUpBackend();
  167. $data = 'some data' . microtime();
  168. $backend->set('BackendAPCTest1', $data, array('UnitTestTag%test', 'UnitTestTag%boring'));
  169. $backend->set('BackendAPCTest2', $data, array('UnitTestTag%test', 'UnitTestTag%special'));
  170. $backend->set('BackendAPCTest3', $data, array('UnitTestTag%test'));
  171. $backend->flushByTag('UnitTestTag%special');
  172. $this->assertTrue($backend->has('BackendAPCTest1'), 'BackendAPCTest1');
  173. $this->assertFalse($backend->has('BackendAPCTest2'), 'BackendAPCTest2');
  174. $this->assertTrue($backend->has('BackendAPCTest3'), 'BackendAPCTest3');
  175. }
  176. /**
  177. * @test
  178. * @author Karsten Dambekalns <karsten@typo3.org>
  179. */
  180. public function flushRemovesAllCacheEntries() {
  181. $backend = $this->setUpBackend();
  182. $data = 'some data' . microtime();
  183. $backend->set('BackendAPCTest1', $data);
  184. $backend->set('BackendAPCTest2', $data);
  185. $backend->set('BackendAPCTest3', $data);
  186. $backend->flush();
  187. $this->assertFalse($backend->has('BackendAPCTest1'), 'BackendAPCTest1');
  188. $this->assertFalse($backend->has('BackendAPCTest2'), 'BackendAPCTest2');
  189. $this->assertFalse($backend->has('BackendAPCTest3'), 'BackendAPCTest3');
  190. }
  191. /**
  192. * @test
  193. * @author Karsten Dambekalns <karsten@typo3.org>
  194. * @author Ingo Renner <ingo@typo3.org>
  195. */
  196. public function flushRemovesOnlyOwnEntries() {
  197. $thisCache = $this->getMock('t3lib_cache_frontend_Frontend', array(), array(), '', FALSE);
  198. $thisCache->expects($this->any())->method('getIdentifier')->will($this->returnValue('thisCache'));
  199. $thisBackend = new t3lib_cache_backend_ApcBackend();
  200. $thisBackend->setCache($thisCache);
  201. $thatCache = $this->getMock('t3lib_cache_frontend_Frontend', array(), array(), '', FALSE);
  202. $thatCache->expects($this->any())->method('getIdentifier')->will($this->returnValue('thatCache'));
  203. $thatBackend = new t3lib_cache_backend_ApcBackend();
  204. $thatBackend->setCache($thatCache);
  205. $thisBackend->set('thisEntry', 'Hello');
  206. $thatBackend->set('thatEntry', 'World!');
  207. $thatBackend->flush();
  208. $this->assertEquals('Hello', $thisBackend->get('thisEntry'));
  209. $this->assertFalse($thatBackend->has('thatEntry'));
  210. }
  211. /**
  212. * Check if we can store ~5 MB of data
  213. *
  214. * @test
  215. * @author Karsten Dambekalns <karsten@typo3.org>
  216. */
  217. public function largeDataIsStored() {
  218. $backend = $this->setUpBackend();
  219. $data = str_repeat('abcde', 1024 * 1024);
  220. $identifier = uniqid('tooLargeData');
  221. $backend->set($identifier, $data);
  222. $this->assertTrue($backend->has($identifier));
  223. $this->assertEquals($backend->get($identifier), $data);
  224. }
  225. /**
  226. * Sets up the APC backend used for testing
  227. *
  228. * @param array $backendOptions Options for the APC backend
  229. * @return t3lib_cache_backend_ApcBackend
  230. * @author Karsten Dambekalns <karsten@typo3.org>
  231. * @author Ingo Renner <ingo@typo3.org>
  232. */
  233. protected function setUpBackend() {
  234. $cache = $this->getMock('t3lib_cache_frontend_Frontend', array(), array(), '', FALSE);
  235. $backend = new t3lib_cache_backend_ApcBackend();
  236. $backend->setCache($cache);
  237. return $backend;
  238. }
  239. }
  240. ?>