summaryrefslogtreecommitdiff
path: root/tests/unit/Data/SqlMap/SqlMapCacheTest.php
blob: 4f0d74a1db438a62db6727f5222af554852dbf0a (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
<?php

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

/**
 * @package System.Data.SqlMap
 */
class SqlMapCacheTest extends PHPUnit_Framework_TestCase
{
	function testFIFOCache()
	{
		$fifo = new TSqlMapFifoCache();
		$fifo->setCacheSize(2);
		$object1 = new TSqlMapManager;
		$object2 = new TComponent;
		$object3 = new TSqlMapGateway(null);

		$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();
		$lru->setCacheSize(2);
		$object1 = new TSqlMapManager;
		$object2 = new TComponent;
		$object3 = new TSqlMapGateway(null);

		$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));
	}
}