Passed
Push — 1.0.0-dev ( 7e13dc...c7a39c )
by nguereza
06:11
created

DBSessionHandlerTest::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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