Skip to content

Commit e2f50bc

Browse files
committed
better test with clean before each test
1 parent de47888 commit e2f50bc

7 files changed

Lines changed: 165 additions & 30 deletions

File tree

‎src/Database.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function setCurrentDatabase($database)
4242
/**
4343
* @param int $userId
4444
*/
45-
public function setUserId(int $userId): void
45+
public function setUserId(?int $userId): void
4646
{
4747
$this->userId = $userId;
4848
}

‎tests/DatabaseTest.php‎

Lines changed: 85 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Rancoud\Session\Test;
66

7+
use Exception;
78
use PHPUnit\Framework\TestCase;
89
use Rancoud\Session\Database;
910

@@ -38,7 +39,18 @@ public static function setUpBeforeClass()
3839
try {
3940
static::$db->exec($sql);
4041
static::$db->truncateTable('sessions');
41-
} catch (\Exception $e) {
42+
} catch (Exception $e) {
43+
var_dump(static::$db->getErrors());
44+
45+
return;
46+
}
47+
}
48+
49+
protected function setUp()
50+
{
51+
try {
52+
static::$db->truncateTable('sessions');
53+
} catch (Exception $e) {
4254
var_dump(static::$db->getErrors());
4355

4456
return;
@@ -74,7 +86,7 @@ public function testWrite()
7486
$data = 'azerty';
7587
try {
7688
$success = $database->write($sessionId, $data);
77-
} catch (\Exception $e) {
89+
} catch (Exception $e) {
7890
var_dump(static::$db->getErrors());
7991

8092
return;
@@ -98,7 +110,7 @@ public function testRead()
98110
try {
99111
$database->write($sessionId, $data);
100112
$dataOutput = $database->read($sessionId);
101-
} catch (\Exception $e) {
113+
} catch (Exception $e) {
102114
var_dump(static::$db->getErrors());
103115

104116
return;
@@ -110,7 +122,7 @@ public function testRead()
110122
$sessionId = '';
111123
try {
112124
$dataOutput = $database->read($sessionId);
113-
} catch (\Exception $e) {
125+
} catch (Exception $e) {
114126
var_dump(static::$db->getErrors());
115127

116128
return;
@@ -127,22 +139,31 @@ public function testDestroy()
127139
$sessionId = 'todelete';
128140
try {
129141
$success = $database->destroy($sessionId);
130-
} catch (\Exception $e) {
142+
} catch (Exception $e) {
131143
var_dump(static::$db->getErrors());
132144

133145
return;
134146
}
135147
static::assertTrue($success);
136148

137149
$sessionId = 'sessionId';
150+
$data = 'azerty';
151+
try {
152+
$database->write($sessionId, $data);
153+
} catch (Exception $e) {
154+
var_dump(static::$db->getErrors());
155+
156+
return;
157+
}
158+
138159
$sql = 'SELECT COUNT(id) FROM sessions WHERE id = :id';
139160
$params = ['id' => $sessionId];
140161
$isRowExist = (static::$db->count($sql, $params) === 1);
141162

142163
static::assertTrue($isRowExist);
143164
try {
144165
$success = $database->destroy($sessionId);
145-
} catch (\Exception $e) {
166+
} catch (Exception $e) {
146167
var_dump(static::$db->getErrors());
147168

148169
return;
@@ -164,7 +185,7 @@ public function testGc()
164185

165186
try {
166187
$success = $database->write($sessionId, $data);
167-
} catch (\Exception $e) {
188+
} catch (Exception $e) {
168189
var_dump(static::$db->getErrors());
169190

170191
return;
@@ -177,7 +198,7 @@ public function testGc()
177198
$lifetime = -1000;
178199
try {
179200
$success = $database->gc($lifetime);
180-
} catch (\Exception $e) {
201+
} catch (Exception $e) {
181202
var_dump(static::$db->getErrors());
182203

183204
return;
@@ -187,4 +208,60 @@ public function testGc()
187208
$isRowNotExist = (static::$db->count($sql, $params) === 0);
188209
static::assertTrue($isRowNotExist);
189210
}
211+
212+
public function testSetUserId()
213+
{
214+
$database = new Database();
215+
$database->setCurrentDatabase(static::$db);
216+
217+
$sessionId = 'sessionId';
218+
$data = 'azerty';
219+
$userId = 5;
220+
$database->setUserId($userId);
221+
222+
try {
223+
$success = $database->write($sessionId, $data);
224+
} catch (Exception $e) {
225+
var_dump(static::$db->getErrors());
226+
227+
return;
228+
}
229+
static::assertTrue($success);
230+
231+
$sql = 'SELECT id_user FROM sessions WHERE id = :id';
232+
$params = ['id' => $sessionId];
233+
try {
234+
$userIdInDatabase = static::$db->selectVar($sql, $params);
235+
} catch (Exception $e) {
236+
var_dump(static::$db->getErrors());
237+
238+
return;
239+
}
240+
static::assertNotNull($userIdInDatabase);
241+
static::assertEquals($userId, $userIdInDatabase);
242+
243+
$userId = null;
244+
$database->setUserId($userId);
245+
246+
try {
247+
$success = $database->write($sessionId, $data);
248+
} catch (Exception $e) {
249+
var_dump(static::$db->getErrors());
250+
251+
return;
252+
}
253+
static::assertTrue($success);
254+
255+
$sql = 'SELECT id_user FROM sessions WHERE id = :id';
256+
$params = ['id' => $sessionId];
257+
try {
258+
$userIdInDatabase = static::$db->selectVar($sql, $params);
259+
} catch (Exception $e) {
260+
var_dump(static::$db->getErrors());
261+
262+
return;
263+
}
264+
static::assertNull($userIdInDatabase);
265+
static::assertEquals($userId, $userIdInDatabase);
266+
}
190267
}

‎tests/DefaultEncryptionTest.php‎

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,28 @@
55
namespace Rancoud\Session\Test;
66

77
use PHPUnit\Framework\TestCase;
8-
use Rancoud\Session\DefaultEncryption;
98
use Rancoud\Session\Session;
109

1110
/**
1211
* Class DefaultEncryptionTest.
1312
*/
1413
class DefaultEncryptionTest extends TestCase
1514
{
15+
protected function setUp()
16+
{
17+
$path = ini_get('session.save_path');
18+
if (empty($path)) {
19+
$path = DIRECTORY_SEPARATOR . 'tmp';
20+
}
21+
22+
$pattern = $path . DIRECTORY_SEPARATOR . 'sess_*';
23+
foreach (glob($pattern) as $file) {
24+
if (file_exists($file)) {
25+
unlink($file);
26+
}
27+
}
28+
}
29+
1630
private function foundSessionFile()
1731
{
1832
$path = ini_get('session.save_path');
@@ -34,7 +48,7 @@ private function foundSessionFile()
3448
/**
3549
* @runInSeparateProcess
3650
*/
37-
public function testWrite()
51+
public function testReadAndWrite()
3852
{
3953
Session::useDefaultEncryptionDriver('randomKey');
4054

@@ -46,23 +60,7 @@ public function testWrite()
4660

4761
$encryptionTrait = $this->getObjectForTrait('Rancoud\Session\Encryption');
4862
$encryptionTrait->setKey('randomKey');
49-
$endData = $encryptionTrait->decrypt($data);
50-
static::assertEquals('a|s:1:"b";', $endData);
63+
$dataDecrypted = $encryptionTrait->decrypt($data);
64+
static::assertEquals('a|s:1:"b";', $dataDecrypted);
5165
}
52-
53-
/*
54-
public function testRead()
55-
{
56-
$defaultEncryption = new DefaultEncryption();
57-
$defaultEncryption->setKey('randomKey');
58-
$sessionId = 'test';
59-
$data = $defaultEncryption->read($sessionId);
60-
static::assertTrue(!empty($data));
61-
static::assertTrue(is_string($data));
62-
63-
$sessionId = '';
64-
$data = $defaultEncryption->read($sessionId);
65-
static::assertTrue(empty($data));
66-
static::assertTrue(is_string($data));
67-
}*/
6866
}

‎tests/FileEncryptionTest.php‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,21 @@
1212
*/
1313
class FileEncryptionTest extends TestCase
1414
{
15+
protected function setUp()
16+
{
17+
$path = ini_get('session.save_path');
18+
if (empty($path)) {
19+
$path = DIRECTORY_SEPARATOR . 'tmp';
20+
}
21+
22+
$pattern = $path . DIRECTORY_SEPARATOR . 'sess_*';
23+
foreach (glob($pattern) as $file) {
24+
if (file_exists($file)) {
25+
unlink($file);
26+
}
27+
}
28+
}
29+
1530
private function getPath()
1631
{
1732
$path = ini_get('session.save_path');
@@ -71,7 +86,7 @@ public function testWrite()
7186
$encryptionTrait = $this->getObjectForTrait('Rancoud\Session\Encryption');
7287
$encryptionTrait->setKey('randomKey');
7388
$dataInFileDecrypted = $encryptionTrait->decrypt($dataInFile);
74-
static::assertEquals('azerty', $dataInFileDecrypted);
89+
static::assertEquals($data, $dataInFileDecrypted);
7590
}
7691

7792
public function testRead()

‎tests/FileTest.php‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,21 @@
1212
*/
1313
class FileTest extends TestCase
1414
{
15+
protected function setUp()
16+
{
17+
$path = ini_get('session.save_path');
18+
if (empty($path)) {
19+
$path = DIRECTORY_SEPARATOR . 'tmp';
20+
}
21+
22+
$pattern = $path . DIRECTORY_SEPARATOR . 'sess_*';
23+
foreach (glob($pattern) as $file) {
24+
if (file_exists($file)) {
25+
unlink($file);
26+
}
27+
}
28+
}
29+
1530
/**
1631
* @return string
1732
*/

‎tests/FileWithNewPrefixTest.php‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,21 @@
1212
*/
1313
class FileWithNewPrefixTest extends TestCase
1414
{
15+
protected function setUp()
16+
{
17+
$path = ini_get('session.save_path');
18+
if (empty($path)) {
19+
$path = DIRECTORY_SEPARATOR . 'tmp';
20+
}
21+
22+
$pattern = $path . DIRECTORY_SEPARATOR . 'myprefix_*';
23+
foreach (glob($pattern) as $file) {
24+
if (file_exists($file)) {
25+
unlink($file);
26+
}
27+
}
28+
}
29+
1530
/**
1631
* @return string
1732
*/

‎tests/SessionTest.php‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,21 @@
1414
*/
1515
class SessionTest extends TestCase
1616
{
17+
protected function setUp()
18+
{
19+
$path = ini_get('session.save_path');
20+
if (empty($path)) {
21+
$path = DIRECTORY_SEPARATOR . 'tmp';
22+
}
23+
24+
$pattern = $path . DIRECTORY_SEPARATOR . 'sess_*';
25+
foreach (glob($pattern) as $file) {
26+
if (file_exists($file)) {
27+
unlink($file);
28+
}
29+
}
30+
}
31+
1732
/**
1833
* @runInSeparateProcess
1934
*/

0 commit comments

Comments
 (0)