summaryrefslogtreecommitdiff
path: root/tests/UnitTests/framework/Data/CacheTestCase.php
blob: 68a7b0671eaa1233a5b555034c940015ce5dd1d3 (plain)
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?php

require_once(dirname(__FILE__).'/../common.php');

class CacheTestCase extends UnitTestCase
{
	private $_cache;

	public function getCache()
	{
		return $this->_cache;
	}

	public function setCache($cache)
	{
		$this->_cache=$cache;
	}

	public function basicOperations()
	{
		$object=new TComponent;
		$number=12345;
		$string='12345\'"';
		$array=array('123'=>123,'abc'=>'def');

		// test set (first time)
		$this->assertFalse($this->_cache->get('object'));
		$this->assertTrue($this->_cache->set('object',$object));
		$this->assertTrue($this->_cache->get('object') instanceof TComponent);
		$this->assertFalse($this->_cache->get('number'));
		$this->assertTrue($this->_cache->set('number',$number));
		$this->assertTrue($this->_cache->get('number')===$number);
		$this->assertFalse($this->_cache->get('string'));
		$this->assertTrue($this->_cache->set('string',$string));
		$this->assertTrue($this->_cache->get('string')===$string);
		$this->assertFalse($this->_cache->get('array'));
		$this->assertTrue($this->_cache->set('array',$array));
		$this->assertTrue($this->_cache->get('array')===$array);

		// test set (second time)
		$this->assertTrue($this->_cache->set('object',$array));
		$this->assertTrue($this->_cache->get('object')===$array);

		// test delete
		$this->assertTrue($this->_cache->delete('object'));
		$this->assertFalse($this->_cache->get('object'));
		$this->assertTrue($this->_cache->delete('number'));
		$this->assertFalse($this->_cache->get('number'));
		$this->assertTrue($this->_cache->delete('string'));
		$this->assertFalse($this->_cache->get('string'));
		$this->assertTrue($this->_cache->delete('array'));
		$this->assertFalse($this->_cache->get('array'));

		// test add (first time)
		$this->assertFalse($this->_cache->get('object'));
		$this->assertTrue($this->_cache->add('object',$object));
		$this->assertTrue($this->_cache->get('object') instanceof TComponent);
		$this->assertFalse($this->_cache->get('number'));
		$this->assertTrue($this->_cache->add('number',$number));
		$this->assertTrue($this->_cache->get('number')===$number);
		$this->assertFalse($this->_cache->get('string'));
		$this->assertTrue($this->_cache->add('string',$string));
		$this->assertTrue($this->_cache->get('string')===$string);
		$this->assertFalse($this->_cache->get('array'));
		$this->assertTrue($this->_cache->add('array',$array));
		$this->assertTrue($this->_cache->get('array')===$array);

		// test add (second time)
		$this->assertFalse($this->_cache->add('object',$array));
		$this->assertTrue($this->_cache->get('object') instanceof TComponent);

		// test replace
		$this->assertTrue($this->_cache->replace('object',$array));
		$this->assertTrue($this->_cache->get('object')===$array);
		$this->assertFalse($this->_cache->replace('object2',$array));
		$this->assertFalse($this->_cache->get('object2'));

		// test flush
		$this->assertTrue($this->_cache->set('number',$number));
		$this->assertTrue($this->_cache->get('number')===$number);
		$this->assertTrue($this->_cache->flush());
		$this->assertFalse($this->_cache->get('number'));

		// test expiring
		// set a value with 5sec valid time
		$this->_cache->set('expiring',123,3);
		$this->assertTrue($this->_cache->get('expiring')===123);
		$this->_cache->set('nonexpiring',456);
		$this->assertTrue($this->_cache->get('nonexpiring')===456);

		// wait 6sec to see if the value still exists
		sleep(4);
		$this->assertFalse($this->_cache->get('expiring'));
		$this->assertTrue($this->_cache->get('nonexpiring')===456);
	}
}

?>