summaryrefslogtreecommitdiff
path: root/tests/unit/SQLMap/SqlMapCacheTest.php
blob: f17461154c3f2b64d0184dee36a18a6e1b80c4d5 (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
<?php

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

/**
 * @package System.DataAccess.SQLMap
 */
class SqlMapCacheTest extends PHPUnit2_Framework_TestCase
{
	function testFIFOCache()
	{
		$fifo = new TSqlMapFifoCache(2);
		$object1 = new TSqlMapper;
		$object2 = new TComponent;
		$object3 = new TMapper;
		
		$key1 = 'key1';
		$key2 = 'key2';
		$key3 = 'key3';

		$fifo->set($key1, $object1);
		$fifo->set($key2, $object2);
		
		$this->assertTrue($object1 === $fifo->get($key1));
		$this->assertTrue($object2 === $fifo->get($key2));

		//object 1 should be removed
		$fifo->set($key3, $object3);

		$this->assertNull($fifo->get($key1));
		$this->assertTrue($object2 === $fifo->get($key2));
		$this->assertTrue($object3 === $fifo->get($key3));

		//object 2 should be removed
		$fifo->set($key1, $object1);

		$this->assertNull($fifo->get($key2));
		$this->assertTrue($object3 === $fifo->get($key3));
		$this->assertTrue($object1 === $fifo->get($key1));
	}

	function testLruCache()
	{
		$lru = new TSqlMapLruCache(2);

		$object1 = new TSqlMapper;
		$object2 = new TComponent;
		$object3 = new TMapper;
		
		$key1 = 'key1';
		$key2 = 'key2';
		$key3 = 'key3';

		$lru->set($key1, $object1);
		$lru->set($key2, $object2);

		$this->assertTrue($object2 === $lru->get($key2));
		$this->assertTrue($object1 === $lru->get($key1));
		
		//object 2 should be removed, i.e. least recently used
		$lru->set($key3, $object3);
		
		$this->assertNull($lru->get($key2));
		$this->assertTrue($object1 === $lru->get($key1));
		$this->assertTrue($object3 === $lru->get($key3));

		//object 1 will be removed
		$lru->set($key2, $object2);

		$this->assertNull($lru->get($key1));
		$this->assertTrue($object2 === $lru->get($key2));
		$this->assertTrue($object3 === $lru->get($key3));
	}
}


?>