Test Failed
Push — 1.0.0-dev ( 6506b5...225896 )
by nguereza
05:07
created

DBSessionHandlerTest::testWhenDataIsExpired()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 11
c 1
b 1
f 0
nc 1
nop 0
dl 0
loc 16
rs 9.9
1
<?php 
2
3
	use PHPUnit\Framework\TestCase;
4
5
	class DBSessionHandlerTest extends TestCase
6
	{	
7
	
8
		private $dbConfig = array(
9
								'driver'    =>  'mysql',
10
								'username'  =>  'root',
11
								'password'  =>  '',
12
								'database'  =>  'db_gesco',
13
								'hostname'  =>  'localhost:3307',
14
								'charset'   => 'utf8',
15
								'collation' => 'utf8_general_ci',
16
								'prefix'    =>  '',
17
								'port'      =>  3307
18
							);
19
		private $db = null;
20
		
21
		private static $config = null;
22
		
23
		public static function setUpBeforeClass()
24
		{
25
			require 'hmvc/models/DBSessionModel.php';
26
			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...
27
			static::$config->init();
28
		}
29
		
30
		
31
		public static function tearDownAfterClass()
32
		{
33
			
34
		}
35
		
36
		protected function setUp()
37
		{
38
			$this->db = new Database($this->dbConfig);
39
			$this->db->setBenchmark(new Benchmark());
40
		}
41
42
		protected function tearDown()
43
		{
44
		}
45
46
		
47
		
48
		public function testUsingSessionConfiguration(){
49
			$secret = 'bXlzZWNyZXQ';
50
			//using value in the configuration
51
			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...
52
			static::$config->set('session_secret', $secret);
53
			$dbsh = new DBSessionHandler();
54
			//assign Database instance manually
55
			$o = &get_instance();
56
			$o->database = $this->db;
57
			
58
			$this->assertTrue($dbsh->open(null, null));
59
			$this->assertTrue($dbsh->close());
60
			$this->assertFalse($dbsh->read('foo'));
0 ignored issues
show
Bug introduced by
It seems like $dbsh->read('foo') can also be of type string; however, parameter $condition of PHPUnit\Framework\Assert::assertFalse() does only seem to accept boolean, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

60
			$this->assertFalse(/** @scrutinizer ignore-type */ $dbsh->read('foo'));
Loading history...
61
			$this->assertTrue($dbsh->write('foo', '444'));
62
			$this->assertNotEmpty($dbsh->read('foo'));
63
			$this->assertEquals($dbsh->read('foo'), '444');
64
			//do update of existing data
65
			$this->assertTrue($dbsh->write('foo', '445'));
66
			$this->assertEquals($dbsh->read('foo'), '445');	
67
			$this->assertTrue($dbsh->destroy('foo'));
68
			$this->assertFalse($dbsh->read('foo'));
69
			$this->assertTrue($dbsh->gc(13));
0 ignored issues
show
Bug introduced by
13 of type integer is incompatible with the type ineteger expected by parameter $maxLifetime of DBSessionHandler::gc(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

69
			$this->assertTrue($dbsh->gc(/** @scrutinizer ignore-type */ 13));
Loading history...
70
			$encoded = $dbsh->encode('foo');
71
			$this->assertNotEmpty($encoded);
72
			$this->assertEquals($dbsh->decode($encoded), 'foo');
73
		}
74
		
75
		public function testWhenDataIsExpired(){
76
			$model = new DBSessionModel($this->db);
77
			//to prevent old data conflict
78
			$model->truncate();
79
			
80
			$secret = 'bXlzZWNyZXQ';
81
			$dbsh = new DBSessionHandler($model);
82
			$dbsh->setSessionSecret($secret);
83
			
84
			$this->assertTrue($dbsh->open(null, null));
85
			$this->assertTrue($dbsh->write('foo', '444'));
86
			$this->assertNotEmpty($dbsh->read('foo'));
87
			$this->assertEquals($dbsh->read('foo'), '444');
88
			//put it in expired
89
			$model->update('foo', array('s_time' => 1234567));
90
			$this->assertFalse($dbsh->read('foo'));
0 ignored issues
show
Bug introduced by
It seems like $dbsh->read('foo') can also be of type string; however, parameter $condition of PHPUnit\Framework\Assert::assertFalse() does only seem to accept boolean, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

90
			$this->assertFalse(/** @scrutinizer ignore-type */ $dbsh->read('foo'));
Loading history...
91
		}
92
		
93
		public function testWhenDataAlreadyExistDoUpdate(){
94
			$model = new DBSessionModel($this->db);
95
			//to prevent old data conflict
96
			$model->truncate();
97
			
98
			$secret = 'bXlzZWNyZXQ';
99
			$dbsh = new DBSessionHandler($model);
100
			$dbsh->setSessionSecret($secret);
101
			
102
			$this->assertTrue($dbsh->open(null, null));
103
			$this->assertTrue($dbsh->write('foo', '444'));
104
			$this->assertNotEmpty($dbsh->read('foo'));
105
			$this->assertEquals($dbsh->read('foo'), '444');
106
			//do update of existing data
107
			$this->assertTrue($dbsh->write('foo', '445'));
108
			$this->assertEquals($dbsh->read('foo'), '445');	
109
		}
110
		
111
		public function testUsingCustomModelInstance(){
112
			
113
			$model = new DBSessionModel($this->db);
114
			//to prevent old data conflict
115
			$model->truncate();
116
			
117
			$secret = 'bXlzZWNyZXQ';
118
			$dbsh = new DBSessionHandler($model);
119
			$dbsh->setSessionSecret($secret);
120
			
121
			$this->assertTrue($dbsh->open(null, null));
122
			$this->assertTrue($dbsh->close());
123
			$this->assertFalse($dbsh->read('foo'));
0 ignored issues
show
Bug introduced by
It seems like $dbsh->read('foo') can also be of type string; however, parameter $condition of PHPUnit\Framework\Assert::assertFalse() does only seem to accept boolean, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

123
			$this->assertFalse(/** @scrutinizer ignore-type */ $dbsh->read('foo'));
Loading history...
124
			$this->assertTrue($dbsh->write('foo', '444'));
125
			$this->assertNotEmpty($dbsh->read('foo'));
126
			$this->assertEquals($dbsh->read('foo'), '444');
127
			//put it in expired
128
			$model->update('foo', array('s_time' => 1234567));
129
			
130
			$this->assertFalse($dbsh->read('foo'));
131
			$this->assertTrue($dbsh->write('foo', '444'));
132
			
133
			//do update of existing data
134
			$this->assertTrue($dbsh->write('foo', '445'));
135
			$this->assertEquals($dbsh->read('foo'), '445');	
136
			$this->assertTrue($dbsh->destroy('foo'));
137
			$this->assertFalse($dbsh->read('foo'));
138
			$this->assertTrue($dbsh->gc(13));
0 ignored issues
show
Bug introduced by
13 of type integer is incompatible with the type ineteger expected by parameter $maxLifetime of DBSessionHandler::gc(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

138
			$this->assertTrue($dbsh->gc(/** @scrutinizer ignore-type */ 13));
Loading history...
139
			$encoded = $dbsh->encode('foo');
140
			$this->assertNotEmpty($encoded);
141
			$this->assertEquals($dbsh->decode($encoded), 'foo');
142
		}
143
			
144
			
145
		public function testUsingCustomLogInstance(){
146
			$model = new DBSessionModel($this->db);
147
			//to prevent old data conflict
148
			$model->truncate();
149
			
150
			$secret = 'bXlzZWNyZXQ';
151
			$dbsh = new DBSessionHandler($model, new Log());
152
			$dbsh->setSessionSecret($secret);
153
			
154
			$this->assertTrue($dbsh->open(null, null));
155
			$this->assertTrue($dbsh->close());
156
			$this->assertFalse($dbsh->read('foo'));
0 ignored issues
show
Bug introduced by
It seems like $dbsh->read('foo') can also be of type string; however, parameter $condition of PHPUnit\Framework\Assert::assertFalse() does only seem to accept boolean, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

156
			$this->assertFalse(/** @scrutinizer ignore-type */ $dbsh->read('foo'));
Loading history...
157
			$this->assertTrue($dbsh->write('foo', '444'));
158
			$this->assertNotEmpty($dbsh->read('foo'));
159
			$this->assertEquals($dbsh->read('foo'), '444');
160
			//put it in expired
161
			$model->update('foo', array('s_time' => 1234567));
162
			
163
			$this->assertFalse($dbsh->read('foo'));
164
			$this->assertTrue($dbsh->write('foo', '444'));
165
			
166
			//do update of existing data
167
			$this->assertTrue($dbsh->write('foo', '445'));
168
			$this->assertEquals($dbsh->read('foo'), '445');	
169
			$this->assertTrue($dbsh->destroy('foo'));
170
			$this->assertFalse($dbsh->read('foo'));
171
			$this->assertTrue($dbsh->gc(13));
0 ignored issues
show
Bug introduced by
13 of type integer is incompatible with the type ineteger expected by parameter $maxLifetime of DBSessionHandler::gc(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

171
			$this->assertTrue($dbsh->gc(/** @scrutinizer ignore-type */ 13));
Loading history...
172
			$encoded = $dbsh->encode('foo');
173
			$this->assertNotEmpty($encoded);
174
			$this->assertEquals($dbsh->decode($encoded), 'foo');
175
		}
176
		
177
		public function testUsingCustomLoaderInstance(){
178
			$model = new DBSessionModel($this->db);
179
			//to prevent old data conflict
180
			$model->truncate();
181
			
182
			$secret = 'bXlzZWNyZXQ';
183
			$dbsh = new DBSessionHandler($model, null, new Loader());
184
			$dbsh->setSessionSecret($secret);
185
			
186
			$this->assertTrue($dbsh->open(null, null));
187
			$this->assertTrue($dbsh->close());
188
			$this->assertFalse($dbsh->read('foo'));
0 ignored issues
show
Bug introduced by
It seems like $dbsh->read('foo') can also be of type string; however, parameter $condition of PHPUnit\Framework\Assert::assertFalse() does only seem to accept boolean, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

188
			$this->assertFalse(/** @scrutinizer ignore-type */ $dbsh->read('foo'));
Loading history...
189
			$this->assertTrue($dbsh->write('foo', '444'));
190
			$this->assertNotEmpty($dbsh->read('foo'));
191
			$this->assertEquals($dbsh->read('foo'), '444');
192
			//put it in expired
193
			$model->update('foo', array('s_time' => 1234567));
194
			
195
			$this->assertFalse($dbsh->read('foo'));
196
			$this->assertTrue($dbsh->write('foo', '444'));
197
			
198
			//do update of existing data
199
			$this->assertTrue($dbsh->write('foo', '445'));
200
			$this->assertEquals($dbsh->read('foo'), '445');	
201
			$this->assertTrue($dbsh->destroy('foo'));
202
			$this->assertFalse($dbsh->read('foo'));
203
			$this->assertTrue($dbsh->gc(13));
0 ignored issues
show
Bug introduced by
13 of type integer is incompatible with the type ineteger expected by parameter $maxLifetime of DBSessionHandler::gc(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

203
			$this->assertTrue($dbsh->gc(/** @scrutinizer ignore-type */ 13));
Loading history...
204
			$encoded = $dbsh->encode('foo');
205
			$this->assertNotEmpty($encoded);
206
			$this->assertEquals($dbsh->decode($encoded), 'foo');
207
		}
208
		
209
		public function testWhenModelInsanceIsNotSet(){
210
			$model = new DBSessionModel($this->db);
211
			//to prevent old data conflict
212
			$model->truncate();
213
			
214
			$secret = 'bXlzZWNyZXQ';
215
			$dbsh = new DBSessionHandler(null, null, new Loader());
216
			$dbsh->setSessionSecret($secret);
217
			
218
			$this->assertTrue($dbsh->open(null, null));
219
			$this->assertTrue($dbsh->close());
220
			$this->assertFalse($dbsh->read('foo'));
0 ignored issues
show
Bug introduced by
It seems like $dbsh->read('foo') can also be of type string; however, parameter $condition of PHPUnit\Framework\Assert::assertFalse() does only seem to accept boolean, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

220
			$this->assertFalse(/** @scrutinizer ignore-type */ $dbsh->read('foo'));
Loading history...
221
			$this->assertTrue($dbsh->write('foo', '444'));
222
			$this->assertNotEmpty($dbsh->read('foo'));
223
			$this->assertEquals($dbsh->read('foo'), '444');
224
			//put it in expired
225
			$model->update('foo', array('s_time' => 1234567));
226
			
227
			$this->assertFalse($dbsh->read('foo'));
228
			$this->assertTrue($dbsh->write('foo', '444'));
229
			
230
			//do update of existing data
231
			$this->assertTrue($dbsh->write('foo', '445'));
232
			$this->assertEquals($dbsh->read('foo'), '445');	
233
			$this->assertTrue($dbsh->destroy('foo'));
234
			$this->assertFalse($dbsh->read('foo'));
235
			$this->assertTrue($dbsh->gc(13));
0 ignored issues
show
Bug introduced by
13 of type integer is incompatible with the type ineteger expected by parameter $maxLifetime of DBSessionHandler::gc(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

235
			$this->assertTrue($dbsh->gc(/** @scrutinizer ignore-type */ 13));
Loading history...
236
			$encoded = $dbsh->encode('foo');
237
			$this->assertNotEmpty($encoded);
238
			$this->assertEquals($dbsh->decode($encoded), 'foo');
239
		}
240
		
241
		public function testWhenModelTableColumnsIsNotSet(){
242
			$model = new DBSessionModel($this->db);
243
			//to prevent old data conflict
244
			$model->truncate();
245
			
246
			//session table is empty
247
			$model->setSessionTableColumns(array());
248
			$dbsh = new DBSessionHandler($model);
249
			$this->assertTrue($dbsh->open(null, null));
250
			
251
		}
252
	}