/Tests/Unit/Cache/Backend/ApcBackendTest.php
PHP | 249 lines | 145 code | 36 blank | 68 comment | 7 complexity | 399d9e2ba8a51d322d35cd415b1df523 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-3.0
- <?php
- namespace TYPO3\FLOW3\Tests\Unit\Cache\Backend;
- /* *
- * This script belongs to the FLOW3 framework. *
- * *
- * It is free software; you can redistribute it and/or modify it under *
- * the terms of the GNU Lesser General Public License, either version 3 *
- * of the License, or (at your option) any later version. *
- * *
- * The TYPO3 project - inspiring people to share! *
- * */
- use TYPO3\FLOW3\Core\ApplicationContext;
- /**
- * Testcase for the APC cache backend
- *
- */
- class ApcBackendTest extends \TYPO3\FLOW3\Tests\UnitTestCase {
- /**
- * @var \TYPO3\FLOW3\Utility\Environment
- */
- protected $mockEnvironment;
- /**
- * Sets up this testcase
- *
- * @return void
- */
- public function setUp() {
- $this->markTestSkipped('Disabling ALL apc tests for now as they are so unreliable');
- if (!extension_loaded('apc') || ini_get('apc.enabled') == 0 || ini_get('apc.enable_cli') == 0) {
- $this->markTestSkipped('APC extension was not available, or it was disabled for CLI.');
- }
- if (ini_get('apc.slam_defense') == 1) {
- $this->markTestSkipped('This testcase can only be executed with apc.slam_defense = Off');
- }
- $this->mockEnvironment = $this->getMock('TYPO3\FLOW3\Utility\Environment', array(), array(), '', FALSE);
- }
- /**
- * @test
- * @expectedException \TYPO3\FLOW3\Cache\Exception
- */
- public function setThrowsExceptionIfNoFrontEndHasBeenSet() {
- $backend = new \TYPO3\FLOW3\Cache\Backend\ApcBackend(new ApplicationContext('Testing'));
- $backend->injectEnvironment($this->mockEnvironment);
- $data = 'Some data';
- $identifier = 'MyIdentifier' . md5(uniqid(mt_rand(), TRUE));
- $backend->set($identifier, $data);
- }
- /**
- * @test
- */
- public function itIsPossibleToSetAndCheckExistenceInCache() {
- $backend = $this->setUpBackend();
- $data = 'Some data';
- $identifier = 'MyIdentifier' . md5(uniqid(mt_rand(), TRUE));
- $backend->set($identifier, $data);
- $inCache = $backend->has($identifier);
- $this->assertTrue($inCache, 'APC backend failed to set and check entry');
- }
- /**
- * @ test
- */
- public function itIsPossibleToSetAndGetEntry() {
- $backend = $this->setUpBackend();
- $data = 'Some data';
- $identifier = 'MyIdentifier' . md5(uniqid(mt_rand(), TRUE));
- $backend->set($identifier, $data);
- $fetchedData = $backend->get($identifier);
- $this->assertEquals($data, $fetchedData, 'APC backend failed to set and retrieve data');
- }
- /**
- * @test
- */
- public function itIsPossibleToRemoveEntryFromCache() {
- $backend = $this->setUpBackend();
- $data = 'Some data';
- $identifier = 'MyIdentifier' . md5(uniqid(mt_rand(), TRUE));
- $backend->set($identifier, $data);
- $backend->remove($identifier);
- $inCache = $backend->has($identifier);
- $this->assertFalse($inCache, 'Failed to set and remove data from APC backend');
- }
- /**
- * @test
- */
- public function itIsPossibleToOverwriteAnEntryInTheCache() {
- $backend = $this->setUpBackend();
- $data = 'Some data';
- $identifier = 'MyIdentifier' . md5(uniqid(mt_rand(), TRUE));
- $backend->set($identifier, $data);
- $otherData = 'some other data';
- $backend->set($identifier, $otherData);
- $fetchedData = $backend->get($identifier);
- $this->assertEquals($otherData, $fetchedData, 'APC backend failed to overwrite and retrieve data');
- }
- /**
- * @test
- */
- public function findIdentifiersByTagFindsSetEntries() {
- $backend = $this->setUpBackend();
- $data = 'Some data';
- $identifier = 'MyIdentifier' . md5(uniqid(mt_rand(), TRUE));
- $backend->set($identifier, $data, array('UnitTestTag%tag1', 'UnitTestTag%tag2'));
- $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tag1');
- $this->assertEquals($identifier, $retrieved[0], 'Could not retrieve expected entry by tag.');
- $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tag2');
- $this->assertEquals($identifier, $retrieved[0], 'Could not retrieve expected entry by tag.');
- }
- /**
- * @test
- */
- public function setRemovesTagsFromPreviousSet() {
- $backend = $this->setUpBackend();
- $data = 'Some data';
- $identifier = 'MyIdentifier' . md5(uniqid(mt_rand(), TRUE));
- $backend->set($identifier, $data, array('UnitTestTag%tag1', 'UnitTestTag%tagX'));
- $backend->set($identifier, $data, array('UnitTestTag%tag3'));
- $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tagX');
- $this->assertEquals(array(), $retrieved, 'Found entry which should no longer exist.');
- }
- /**
- * @test
- */
- public function hasReturnsFalseIfTheEntryDoesntExist() {
- $backend = $this->setUpBackend();
- $identifier = 'NonExistingIdentifier' . md5(uniqid(mt_rand(), TRUE));
- $inCache = $backend->has($identifier);
- $this->assertFalse($inCache,'"has" did not return false when checking on non existing identifier');
- }
- /**
- * @test
- */
- public function removeReturnsFalseIfTheEntryDoesntExist() {
- $backend = $this->setUpBackend();
- $identifier = 'NonExistingIdentifier' . md5(uniqid(mt_rand(), TRUE));
- $inCache = $backend->remove($identifier);
- $this->assertFalse($inCache,'"remove" did not return false when checking on non existing identifier');
- }
- /**
- * @test
- */
- public function flushByTagRemovesCacheEntriesWithSpecifiedTag() {
- $backend = $this->setUpBackend();
- $data = 'some data' . microtime();
- $backend->set('BackendAPCTest1', $data, array('UnitTestTag%test', 'UnitTestTag%boring'));
- $backend->set('BackendAPCTest2', $data, array('UnitTestTag%test', 'UnitTestTag%special'));
- $backend->set('BackendAPCTest3', $data, array('UnitTestTag%test'));
- $backend->flushByTag('UnitTestTag%special');
- $this->assertTrue($backend->has('BackendAPCTest1'), 'BackendAPCTest1');
- $this->assertFalse($backend->has('BackendAPCTest2'), 'BackendAPCTest2');
- $this->assertTrue($backend->has('BackendAPCTest3'), 'BackendAPCTest3');
- }
- /**
- * @test
- */
- public function flushRemovesAllCacheEntries() {
- $this->markTestSkipped('Somehow, this test does not work...');
- $backend = $this->setUpBackend();
- $data = 'some data' . microtime();
- $backend->set('BackendAPCTest1', $data);
- $backend->set('BackendAPCTest2', $data);
- $backend->set('BackendAPCTest3', $data);
- $backend->flush();
- $this->assertFalse($backend->has('BackendAPCTest1'), 'BackendAPCTest1');
- $this->assertFalse($backend->has('BackendAPCTest2'), 'BackendAPCTest2');
- $this->assertFalse($backend->has('BackendAPCTest3'), 'BackendAPCTest3');
- }
- /**
- * @test
- */
- public function flushRemovesOnlyOwnEntries() {
- $thisCache = $this->getMock('TYPO3\FLOW3\Cache\Frontend\FrontendInterface', array(), array(), '', FALSE);
- $thisCache->expects($this->any())->method('getIdentifier')->will($this->returnValue('thisCache'));
- $thisBackend = new \TYPO3\FLOW3\Cache\Backend\ApcBackend('Testing');
- $thisBackend->injectEnvironment($this->mockEnvironment);
- $thisBackend->setCache($thisCache);
- $thatCache = $this->getMock('TYPO3\FLOW3\Cache\Frontend\FrontendInterface', array(), array(), '', FALSE);
- $thatCache->expects($this->any())->method('getIdentifier')->will($this->returnValue('thatCache'));
- $thatBackend = new \TYPO3\FLOW3\Cache\Backend\ApcBackend('Testing');
- $thatBackend->injectEnvironment($this->mockEnvironment);
- $thatBackend->setCache($thatCache);
- $thisBackend->set('thisEntry', 'Hello');
- $thatBackend->set('thatEntry', 'World!');
- $thatBackend->flush();
- $this->assertEquals('Hello', $thisBackend->get('thisEntry'));
- $this->assertFalse($thatBackend->has('thatEntry'));
- }
- /**
- * Check if we can store ~5 MB of data, this gives some headroom.
- *
- * @test
- */
- public function largeDataIsStored() {
- $backend = $this->setUpBackend();
- $data = str_repeat('abcde', 1024 * 1024);
- $identifier = 'tooLargeData' . md5(uniqid(mt_rand(), TRUE));
- $backend->set($identifier, $data);
- $this->assertTrue($backend->has($identifier));
- $this->assertEquals($backend->get($identifier), $data);
- }
- /**
- * Sets up the APC backend used for testing
- *
- * @return \TYPO3\FLOW3\Cache\Backend\ApcBackend
- */
- protected function setUpBackend() {
- $cache = $this->getMock('TYPO3\FLOW3\Cache\Frontend\FrontendInterface', array(), array(), '', FALSE);
- $backend = new \TYPO3\FLOW3\Cache\Backend\ApcBackend(new ApplicationContext('Testing'));
- $backend->injectEnvironment($this->mockEnvironment);
- $backend->setCache($cache);
- return $backend;
- }
- }
- ?>