Repository navigation
Expand file tree
/
Copy pathFileHandlerTest.php
More file actions
106 lines (95 loc) · 2.99 KB
/
Copy pathFileHandlerTest.php
File metadata and controls
106 lines (95 loc) · 2.99 KB
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
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact [email protected]
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace HyperfTest\Session;
use Hyperf\Engine\Channel;
use Hyperf\Session\Handler\FileHandler;
use Hyperf\Stringable\Str;
use Hyperf\Support\Filesystem\Filesystem;
use Mockery;
use PHPUnit\Framework\Attributes\CoversNothing;
use PHPUnit\Framework\TestCase;
/**
* @internal
* @coversNothing
*/
#[CoversNothing]
class FileHandlerTest extends TestCase
{
protected function tearDown(): void
{
Mockery::close();
}
public function testReadAndWrite()
{
$handler = new FileHandler(new Filesystem(), $path = '/tmp', 10);
// Useless methods of FileHandler.
$this->assertTrue($handler->open('', ''));
$this->assertTrue($handler->close('', ''));
$id = Str::random(40);
$data = [
'int' => 1,
'true' => true,
'false' => false,
'float' => 1.23,
'string' => 'foo',
0 => 1,
'array' => [
'int' => 1,
'true' => true,
'false' => false,
'float' => 1.23,
'string' => 'foo',
0 => 1,
],
];
$this->assertTrue($handler->write($id, serialize($data)));
$this->assertSame($data, unserialize($handler->read($id)));
$this->assertFileExists('/tmp/' . $id);
$handler->destroy($id);
$this->assertFileDoesNotExist('/tmp/' . $id);
}
public function testReadNotExistsSessionId()
{
$handler = new FileHandler(new Filesystem(), $path = '/tmp', 10);
$this->assertSame('', $handler->read('not-exist'));
}
public function testGc()
{
$handler = new FileHandler(new Filesystem(), $path = __DIR__ . '/runtime/session', 1);
$id = Str::random(40);
$handler->write($id, 'foo');
sleep(1);
$handler->gc(1);
$this->assertFileDoesNotExist($path . '/' . $id);
}
public function testReadAfterRewritten()
{
$fs = Mockery::mock(Filesystem::class . '[lastModified]');
$chan = new Channel(1);
$fs->shouldReceive('lastModified')->withAnyArgs()->once()->andReturnUsing(function (string $path) {
return 0;
});
$fs->shouldReceive('lastModified')->withAnyArgs()->once()->andReturnUsing(function (string $path) use ($chan) {
$t = filemtime($path);
$chan->push($t);
return $t;
});
$handler = new FileHandler($fs, __DIR__ . '/runtime/session', 1);
$id = Str::random(40);
$handler->write($id, 'foo');
$this->assertEmpty($handler->read($id));
$time = time();
sleep(1);
$handler->write($id, 'foo');
$this->assertSame('foo', $handler->read($id));
$this->assertTrue($chan->pop(5) > $time);
}
}