Passed
Push — 1.0.0-dev ( 9c9ab7...066288 )
by nguereza
02:38
created

DBSessionHandlerTest::testWhenModelInsanceIsNotSet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 3 Features 0
Metric Value
cc 1
eloc 20
c 3
b 3
f 0
nc 1
nop 0
dl 0
loc 26
rs 9.6
1
<?php 
2
3
	use PHPUnit\Framework\TestCase;
4
5
	class DBSessionHandlerTest extends TestCase
6
	{	
7
	
8
		private $db = null;
9
		
10
		private $model = null;
11
		
12
		private $secret = 'bXlzZWNyZXQ';
13
		
14
		private static $config = null;
15
		
16
		public function __construct(){
17
            $cfg = get_db_config();
18
			$this->db = new Database($cfg);
19
            $qr = new DatabaseQueryRunner($this->db->getPdo());
20
            $qr->setBenchmark(new Benchmark());
21
            $qr->setDriver('sqlite');
22
            $this->db->setQueryRunner($qr);
23
		}
24
		
25
		public static function setUpBeforeClass()
26
		{
27
			require APPS_MODEL_PATH . 'DBSessionModel.php';
28
			self::$config = new Config();
29
			self::$config->init();
30
		}
31
		
32
		
33
		public static function tearDownAfterClass()
34
		{
35
			
36
		}
37
		
38
		protected function setUp()
39
		{
40
			$this->model = new DBSessionModel($this->db);
41
            //to prevent old data conflict
42
			$this->model->truncate();
43
		}
44
45
		protected function tearDown()
46
		{
47
		}
48
49
		
50
		
51
		public function testUsingSessionConfiguration(){
52
			//using value in the configuration
53
			self::$config->set('session_save_path', 'DBSessionModel');
54
			self::$config->set('session_secret', $this->secret);
55
			$dbsh = new DBSessionHandler();
56
			//assign Database instance manually
57
			$o = &get_instance();
58
			$o->database = $this->db;
59
			
60
			$this->assertTrue($dbsh->open(null, null));
61
			$this->assertTrue($dbsh->close());
62
			$this->assertNull($dbsh->read('foo'));
63
			$this->assertTrue($dbsh->write('foo', '444'));
64
			$this->assertNotEmpty($dbsh->read('foo'));
65
			$this->assertEquals($dbsh->read('foo'), '444');
66
			//do update of existing data
67
			$this->assertTrue($dbsh->write('foo', '445'));
68
			$this->assertEquals($dbsh->read('foo'), '445');	
69
			$this->assertTrue($dbsh->destroy('foo'));
70
			$this->assertNull($dbsh->read('foo'));
71
			$this->assertTrue($dbsh->gc(13));
72
			$encoded = $dbsh->encode('foo');
73
			$this->assertNotEmpty($encoded);
74
			$this->assertEquals($dbsh->decode($encoded), 'foo');
75
		}
76
		
77
		public function testWhenDataIsExpired(){
78
			$dbsh = new DBSessionHandler($this->model);
79
			$dbsh->setSessionSecret($this->secret);
80
			
81
			$this->assertTrue($dbsh->open(null, null));
82
			$this->assertTrue($dbsh->write('foo', '444'));
83
			$this->assertNotEmpty($dbsh->read('foo'));
84
			$this->assertEquals($dbsh->read('foo'), '444');
85
			//put it in expired
86
			$this->model->update('foo', array('s_time' => 1234567));
87
			$this->assertNull($dbsh->read('foo'));
88
		}
89
		
90
		public function testWhenDataAlreadyExistDoUpdate(){
91
			$dbsh = new DBSessionHandler($this->model);
92
			$dbsh->setSessionSecret($this->secret);
93
			
94
			$this->assertTrue($dbsh->open(null, null));
95
			$this->assertTrue($dbsh->write('foo', '444'));
96
			$this->assertNotEmpty($dbsh->read('foo'));
97
			$this->assertEquals($dbsh->read('foo'), '444');
98
			//do update of existing data
99
			$this->assertTrue($dbsh->write('foo', '445'));
100
			$this->assertEquals($dbsh->read('foo'), '445');	
101
		}
102
		
103
		public function testUsingCustomModelInstance(){
104
			$dbsh = new DBSessionHandler($this->model);
105
			$dbsh->setSessionSecret($this->secret);
106
			
107
			$this->assertTrue($dbsh->open(null, null));
108
			$this->assertTrue($dbsh->close());
109
			$this->assertNull($dbsh->read('foo'));
110
			$this->assertTrue($dbsh->write('foo', '444'));
111
			$this->assertNotEmpty($dbsh->read('foo'));
112
			$this->assertEquals($dbsh->read('foo'), '444');
113
			//put it in expired
114
			$this->model->update('foo', array('s_time' => 1234567));
115
			
116
			$this->assertNull($dbsh->read('foo'));
117
			$this->assertTrue($dbsh->write('foo', '444'));
118
			
119
			//do update of existing data
120
			$this->assertTrue($dbsh->write('foo', '445'));
121
			$this->assertEquals($dbsh->read('foo'), '445');	
122
			$this->assertTrue($dbsh->destroy('foo'));
123
			$this->assertNull($dbsh->read('foo'));
124
			$this->assertTrue($dbsh->gc(13));
125
			$encoded = $dbsh->encode('foo');
126
			$this->assertNotEmpty($encoded);
127
			$this->assertEquals($dbsh->decode($encoded), 'foo');
128
		}
129
			
130
			
131
		public function testUsingCustomLogInstance(){
132
			$dbsh = new DBSessionHandler($this->model, new Log());
133
			$dbsh->setSessionSecret($this->secret);
134
			
135
			$this->assertTrue($dbsh->open(null, null));
136
			$this->assertTrue($dbsh->close());
137
			$this->assertNull($dbsh->read('foo'));
138
			$this->assertTrue($dbsh->write('foo', '444'));
139
			$this->assertNotEmpty($dbsh->read('foo'));
140
			$this->assertEquals($dbsh->read('foo'), '444');
141
			//put it in expired
142
			$this->model->update('foo', array('s_time' => 1234567));
143
			
144
			$this->assertNull($dbsh->read('foo'));
145
			$this->assertTrue($dbsh->write('foo', '444'));
146
			
147
			//do update of existing data
148
			$this->assertTrue($dbsh->write('foo', '445'));
149
			$this->assertEquals($dbsh->read('foo'), '445');	
150
			$this->assertTrue($dbsh->destroy('foo'));
151
			$this->assertNull($dbsh->read('foo'));
152
			$this->assertTrue($dbsh->gc(13));
153
			$encoded = $dbsh->encode('foo');
154
			$this->assertNotEmpty($encoded);
155
			$this->assertEquals($dbsh->decode($encoded), 'foo');
156
		}
157
		
158
		public function testUsingCustomLoaderInstance(){
159
			$dbsh = new DBSessionHandler($this->model, null, new Loader());
160
			$dbsh->setSessionSecret($this->secret);
161
			
162
			$this->assertTrue($dbsh->open(null, null));
163
			$this->assertTrue($dbsh->close());
164
			$this->assertNull($dbsh->read('foo'));
165
			$this->assertTrue($dbsh->write('foo', '444'));
166
			$this->assertNotEmpty($dbsh->read('foo'));
167
			$this->assertEquals($dbsh->read('foo'), '444');
168
			//put it in expired
169
			$this->model->update('foo', array('s_time' => 1234567));
170
			
171
			$this->assertNull($dbsh->read('foo'));
172
			$this->assertTrue($dbsh->write('foo', '444'));
173
			
174
			//do update of existing data
175
			$this->assertTrue($dbsh->write('foo', '445'));
176
			$this->assertEquals($dbsh->read('foo'), '445');	
177
			$this->assertTrue($dbsh->destroy('foo'));
178
			$this->assertNull($dbsh->read('foo'));
179
			$this->assertTrue($dbsh->gc(13));
180
			$encoded = $dbsh->encode('foo');
181
			$this->assertNotEmpty($encoded);
182
			$this->assertEquals($dbsh->decode($encoded), 'foo');
183
		}
184
		
185
		
186
		public function testWhenModelInsanceIsNotSet(){
187
			$dbsh = new DBSessionHandler(null, null, new Loader());
188
			$dbsh->setSessionSecret($this->secret);
189
			
190
			$this->assertTrue($dbsh->open(null, null));
191
			$this->assertTrue($dbsh->close());
192
			$this->assertNull($dbsh->read('foo'));
193
			$this->assertTrue($dbsh->write('foo', '444'));
194
			$this->assertNotEmpty($dbsh->read('foo'));
195
			$this->assertEquals($dbsh->read('foo'), '444');
196
			//put it in expired
197
			$this->model->update('foo', array('s_time' => 1234567));
198
			
199
			$this->assertNull($dbsh->read('foo'));
200
			$this->assertTrue($dbsh->write('foo', '444'));
201
			
202
			//do update of existing data
203
			$this->assertTrue($dbsh->write('tnh', '445'));
204
			$this->assertTrue($dbsh->write('foo', '445'));
205
			$this->assertEquals($dbsh->read('foo'), '445');	
206
			$this->assertTrue($dbsh->destroy('foo'));
207
			$this->assertNull($dbsh->read('foo'));
208
			$this->assertTrue($dbsh->gc(13));
209
			$encoded = $dbsh->encode('foo');
210
			$this->assertNotEmpty($encoded);
211
			$this->assertEquals($dbsh->decode($encoded), 'foo');
212
		}
213
		
214
		public function testWhenModelTableColumnsIsNotSet(){
215
			//session table is empty
216
			$this->model->setSessionTableColumns(array());
217
			$dbsh = new DBSessionHandler($this->model);
218
			$this->assertTrue($dbsh->open(null, null));
219
		}
220
		
221
		
222
	}