Blame view

framework/tests/cache/CacheTest.php 1.84 KB
0084d336   Administrator   Importers CRUD
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
  <?php 
  
  class CacheTest extends SapphireTest {
  
  	public function testCacheBasics() {
  		$cache = SS_Cache::factory('test');
  		
  		$cache->save('Good', 'cachekey');
  		$this->assertEquals('Good', $cache->load('cachekey'));
  	}
  	
  	public function testCacheCanBeDisabled() {
  		SS_Cache::set_cache_lifetime('test', -1, 10);
  		
  		$cache = SS_Cache::factory('test');
  		
  		$cache->save('Good', 'cachekey');
  		$this->assertFalse($cache->load('cachekey'));
  	}
  	
  	public function testCacheLifetime() {
  		SS_Cache::set_cache_lifetime('test', 0.5, 20);
  		
  		$cache = SS_Cache::factory('test');
  		$this->assertEquals(0.5, $cache->getOption('lifetime'));
  
  		$cache->save('Good', 'cachekey');
  		$this->assertEquals('Good', $cache->load('cachekey'));
  		
  		// As per documentation, sleep may not sleep for the amount of time you tell it to sleep for
  		// This loop can make sure it *does* sleep for that long
  		$endtime = time() + 2;
  		while (time() < $endtime) {
  			// Sleep for another 2 seconds!
  			// This may end up sleeping for 4 seconds, but it's awwwwwwwright.
  			sleep(2);
  		}
  		
  		$this->assertFalse($cache->load('cachekey'));
  	}
  
  	public function testCacheSeperation() {
  		$cache1 = SS_Cache::factory('test1');
  		$cache2 = SS_Cache::factory('test2');
  		
  		$cache1->save('Foo', 'cachekey');
  		$cache2->save('Bar', 'cachekey');
  		$this->assertEquals('Foo', $cache1->load('cachekey'));
  		$this->assertEquals('Bar', $cache2->load('cachekey'));
  		
  		$cache1->remove('cachekey');
  		$this->assertFalse($cache1->load('cachekey'));
  		$this->assertEquals('Bar', $cache2->load('cachekey'));
  	}
  
  	public function testCacheDefault() {
  		SS_Cache::set_cache_lifetime('default', 1200);
  		$default = SS_Cache::get_cache_lifetime('default');
  
  		$this->assertEquals(1200, $default['lifetime']);
  
  		$cache = SS_Cache::factory('somethingnew');
  
  		$this->assertEquals(1200, $cache->getOption('lifetime'));
  	}
  
  }