summaryrefslogtreecommitdiff
path: root/tests/unit/Web/TCacheHttpSessionTest.php
blob: 15c2a382615f1b1cfdca918f2dee05dffd681764 (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
<?php

Prado::using('System.Web.TCacheHttpSession');
Prado::using('System.Caching.TMemCache');

/**
 * @package System.Web
 */
class TCacheHttpSessionTest extends PHPUnit_Framework_TestCase
{
    protected $app = null;
	protected static $cache = null;
	protected static $session = null;
    
    protected function setUp()
    {
		if(!extension_loaded('memcache'))
		{
			self::markTestSkipped('The memcache extension is not available');
		}
		else 
		{
			$basePath = dirname(__FILE__).'/app';
			$runtimePath = $basePath.'/runtime';
			if(!is_writable($runtimePath))
			{
				self::markTestSkipped("'$runtimePath' is not writable");
			}
			$this->app = new TApplication($basePath);
			self::$cache = new TMemCache();
			self::$cache->setKeyPrefix('MyCache');
			self::$cache->init(null);
			$this->app->setModule('MyCache',self::$cache);
		}
	}
	
	protected function tearDown()
    {
		$this->app = null;
		$this->cache = null;
		$this->session = null;
	}
    
    public function testInit()
    {
        $session = new TCacheHttpSession();
        try
        {
            $session->init(null);
            $this->fail("Expected TConfigurationException is not raised");
        }
        catch(TConfigurationException $e)
        {
        }
        unset($session);
        
        $session = new TCacheHttpSession();
        try
        {
            $session->setCacheModuleID('MaiCache');
            $session->init(null);
            $this->fail("Expected TConfigurationException is not raised");
            $session->open();
        }
        catch(TConfigurationException $e)
        {
        }
        unset($session);
        
        self::$session = new TCacheHttpSession();
        try
        {
            self::$session->setCacheModuleID('MyCache');
            self::$session->init(null);                
        }
        catch(TConfigurationException $e)
        {
            $this->fail('TConfigurationException is not expected');
            self::markTestSkipped('Cannot continue this test');
        }
    }
    
    public function testGetCache()
    {
        $cache = self::$session->getCache();
        $this->assertEquals(true, $cache instanceof TMemCache);
    }
    
    public function testCacheModuleID()
    {
        $id = 'value';
        self::$session->setCacheModuleID($id);
        self::assertEquals($id, self::$session->getCacheModuleID());
    }
    
    public function testKeyPrefix()
    {
        $id = 'value';
        self::$session->setKeyPrefix($id);
        self::assertEquals($id, self::$session->getKeyPrefix());
    }
    
    public function testSetAndGet()
    {
        self::$session['key'] = 'value';
		self::assertEquals('value', self::$session['key']);
	}
	
	public function testAdd()
	{
		self::$session->add('anotherkey', 'value');
		self::assertEquals('value', self::$session['anotherkey']);
	}
	
	public function testRemove()
	{
		self::$session->remove('key');
		self::assertEquals(false, self::$session['key']);
	}
	
	public function testDestroyAndIsStarted()
	{
		$this->testSetAndGet();
		self::$session->destroy();
		self::assertEquals(false, self::$session->getIsStarted());
	}
}
?>