summaryrefslogtreecommitdiff
path: root/tests/simple_unit/SqlMap/CacheTest.php
blob: ff87cd3a71748e4ea75dc672a5cf8a0a53d416e7 (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php

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

/**
 * @package System.DataAccess.SQLMap
 */
class CacheTest extends BaseCase
{
	function __construct()
	{
		parent::__construct();

		$this->initSqlMap();

		//force autoload
		new Account;
	}

	function resetDatabase()
	{
		$this->initScript('account-init.sql');
	}

	/**
	 * Test for JIRA 29
	 */
	function testJIRA28()
	{
		$account = $this->sqlmap->queryForObject("GetNoAccountWithCache",-99);
		$this->assertNull($account);
	}

	/**
	 * Test Cache query
	 */
	function testQueryWithCache()
	{
		$this->resetDatabase();

		$list1 = $this->sqlmap->queryForList("GetCachedAccountsViaResultMap");

		$list2 = $this->sqlmap->queryForList("GetCachedAccountsViaResultMap");

		$this->assertTrue($list1 === $list2);

		$account = $list1[1];
		$account->setEmailAddress("somebody@cache.com");

		//this will cause the cache to flush
		$this->sqlmap->update("UpdateAccountViaInlineParameters", $account);

		$list3 = $this->sqlmap->queryForList("GetCachedAccountsViaResultMap");

		$this->assertTrue($list1 !== $list3);

		$this->resetDatabase();
	}


	/**
	 * Test flush Cache
	 */
	function testFlushDataCache()
	{
		$list1 = $this->sqlmap->queryForList("GetCachedAccountsViaResultMap");
		$list2 = $this->sqlmap->queryForList("GetCachedAccountsViaResultMap");

		$this->assertTrue($list1 === $list2);
		$this->sqlmap->flushCaches();

		$list3 = $this->sqlmap->queryForList("GetCachedAccountsViaResultMap");

		$this->assertTrue($list1 !== $list3);
	}

	/**
	 *
	 */
	function testFlushDataCacheOnExecute()
	{
		$list1 = $this->sqlmap->queryForList("GetCachedAccountsViaResultMap");

		$list2 = $this->sqlmap->queryForList("GetCachedAccountsViaResultMap");

		$this->assertTrue($list1 === $list2);
		$this->sqlmap->update("UpdateAccountViaInlineParameters", $list1[0]);

		$list3 = $this->sqlmap->queryForList("GetCachedAccountsViaResultMap");

		$this->assertTrue($list1 !== $list3);
	}

	/**
	 */
	protected function 	getCacheModel()
	{
		$cache = new TSqlMapCacheModel();
	//	$cache->setFlushInterval(5*60);
		$cache->setImplementation('LRU');
		$cache->initialize();
		return $cache;
	}

	/**
	 * Test CacheHit
	 */
	function testCacheHit()
	{
		$cache = $this->getCacheModel();
		$key = new TSqlMapCacheKey('testkey');
		$cache->set($key, 'a');

		$returnedObject = $cache->get($key);

		$this->assertIdentical('a', $returnedObject);

		$this->assertIdentical(1, $cache->getHitRatio());
	}



	/**
	 * Test CacheMiss
	 */
	function testCacheMiss()
	{
		$cache = $this->getCacheModel();
		$key = new TSqlMapCacheKey('testKey');
		$value = 'testValue';
		$cache->set($key, $value);

		$wrongKey = new TSqlMapCacheKey('wrongKey');

		$returnedObject = $cache->get($wrongKey);
		$this->assertNotEqual($value, $returnedObject);
		$this->assertNull($returnedObject) ;
		$this->assertIdentical(0, $cache->getHitRatio());
	}

	/**
	 * Test CacheHitMiss
	 */
	function testCacheHitMiss()
	{
		$cache = $this->getCacheModel();
		$key = new TSqlMapCacheKey('testKey');

		$value = "testValue";
		$cache->set($key, $value);

		$returnedObject = $cache->get($key);
		$this->assertIdentical($value, $returnedObject);

		$wrongKey = new TSqlMapCacheKey('wrongKey');

		$returnedObject = $cache->get($wrongKey);
		$this->assertNotEqual($value, $returnedObject);
		$this->assertNull($returnedObject) ;
		$this->assertIdentical(0.5, $cache->getHitRatio());
	}
}