Completed
Push — 6.0 ( b72c2e...58dbb9 )
by yun
05:58
created

SessionTest::testFileHandler()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 34
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 21
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 34
rs 9.584
1
<?php
2
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
3
namespace think\tests;
4
5
use Mockery as m;
6
use Mockery\MockInterface;
7
use org\bovigo\vfs\vfsStream;
8
use PHPUnit\Framework\TestCase;
9
use think\App;
10
use think\cache\Driver;
11
use think\Config;
12
use think\Container;
13
use think\contract\SessionHandlerInterface;
14
use think\helper\Str;
15
use think\Session;
16
use think\session\driver\Cache;
17
use think\session\driver\File;
18
19
class SessionTest extends TestCase
20
{
21
    /** @var App|MockInterface */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
22
    protected $app;
23
24
    /** @var Session|MockInterface */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
25
    protected $session;
26
27
    /** @var Config|MockInterface */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
28
    protected $config;
29
30
    protected $handler;
31
32
    protected function tearDown(): void
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function tearDown()
Loading history...
33
    {
34
        m::close();
35
    }
36
37
    protected function setUp()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function setUp()
Loading history...
38
    {
39
        $this->app = m::mock(App::class)->makePartial();
40
        Container::setInstance($this->app);
41
42
        $this->app->shouldReceive('make')->with(App::class)->andReturn($this->app);
43
        $this->config = m::mock(Config::class)->makePartial();
44
45
        $this->app->shouldReceive('get')->with('config')->andReturn($this->config);
46
        $handlerClass = "\\think\\session\\driver\\Test" . Str::random(10);
47
        $this->config->shouldReceive("get")->with("session.type", "file")->andReturn($handlerClass);
48
        $this->session = new Session($this->app);
0 ignored issues
show
Bug introduced by
$this->app of type Mockery\Mock is incompatible with the type think\App expected by parameter $app of think\Session::__construct(). ( Ignorable by Annotation )

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

48
        $this->session = new Session(/** @scrutinizer ignore-type */ $this->app);
Loading history...
49
50
        $this->handler = m::mock('overload:' . $handlerClass, SessionHandlerInterface::class);
51
    }
52
53
    public function testLoadData()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function testLoadData()
Loading history...
54
    {
55
        $data = [
56
            "bar" => 'foo',
57
        ];
58
59
        $id = md5(uniqid());
60
61
        $this->handler->shouldReceive("read")->once()->with($id)->andReturn(serialize($data));
62
63
        $this->session->setId($id);
0 ignored issues
show
Bug introduced by
The method setId() does not exist on think\Session. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

63
        $this->session->/** @scrutinizer ignore-call */ 
64
                        setId($id);
Loading history...
64
        $this->session->init();
0 ignored issues
show
Bug introduced by
The method init() does not exist on think\Session. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

64
        $this->session->/** @scrutinizer ignore-call */ 
65
                        init();
Loading history...
65
66
        $this->assertEquals('foo', $this->session->get('bar'));
0 ignored issues
show
Bug introduced by
The method get() does not exist on think\Session. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

66
        $this->assertEquals('foo', $this->session->/** @scrutinizer ignore-call */ get('bar'));
Loading history...
67
        $this->assertTrue($this->session->has('bar'));
0 ignored issues
show
Bug introduced by
The method has() does not exist on think\Session. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

67
        $this->assertTrue($this->session->/** @scrutinizer ignore-call */ has('bar'));
Loading history...
68
        $this->assertFalse($this->session->has('foo'));
69
70
        $this->session->set('foo', 'bar');
0 ignored issues
show
Bug introduced by
The method set() does not exist on think\Session. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

70
        $this->session->/** @scrutinizer ignore-call */ 
71
                        set('foo', 'bar');
Loading history...
71
        $this->assertTrue($this->session->has('foo'));
72
73
        $this->assertEquals('bar', $this->session->pull('foo'));
0 ignored issues
show
Bug introduced by
The method pull() does not exist on think\Session. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

73
        $this->assertEquals('bar', $this->session->/** @scrutinizer ignore-call */ pull('foo'));
Loading history...
74
        $this->assertFalse($this->session->has('foo'));
75
    }
76
77
    public function testSave()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function testSave()
Loading history...
78
    {
79
80
        $id = md5(uniqid());
81
82
        $this->handler->shouldReceive('read')->once()->with($id)->andReturn("");
83
84
        $this->handler->shouldReceive('write')->once()->with($id, serialize([
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
85
            "bar" => 'foo',
86
        ]))->andReturnTrue();
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
87
88
        $this->session->setId($id);
89
        $this->session->init();
90
91
        $this->session->set('bar', 'foo');
92
93
        $this->session->save();
0 ignored issues
show
Bug introduced by
The method save() does not exist on think\Session. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

93
        $this->session->/** @scrutinizer ignore-call */ 
94
                        save();
Loading history...
94
    }
95
96
    public function testFlash()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function testFlash()
Loading history...
97
    {
98
        $this->session->flash('foo', 'bar');
0 ignored issues
show
Bug introduced by
The method flash() does not exist on think\Session. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

98
        $this->session->/** @scrutinizer ignore-call */ 
99
                        flash('foo', 'bar');
Loading history...
99
        $this->session->flash('bar', 0);
100
        $this->session->flash('baz', true);
101
102
        $this->assertTrue($this->session->has('foo'));
103
        $this->assertEquals('bar', $this->session->get('foo'));
104
        $this->assertEquals(0, $this->session->get('bar'));
105
        $this->assertTrue($this->session->get('baz'));
106
107
        $this->session->clearFlashData();
0 ignored issues
show
Bug introduced by
The method clearFlashData() does not exist on think\Session. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

107
        $this->session->/** @scrutinizer ignore-call */ 
108
                        clearFlashData();
Loading history...
108
109
        $this->assertTrue($this->session->has('foo'));
110
        $this->assertEquals('bar', $this->session->get('foo'));
111
        $this->assertEquals(0, $this->session->get('bar'));
112
113
        $this->session->clearFlashData();
114
115
        $this->assertFalse($this->session->has('foo'));
116
        $this->assertNull($this->session->get('foo'));
117
118
        $this->session->flash('foo', 'bar');
119
        $this->assertTrue($this->session->has('foo'));
120
        $this->session->clearFlashData();
121
        $this->session->reflash();
0 ignored issues
show
Bug introduced by
The method reflash() does not exist on think\Session. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

121
        $this->session->/** @scrutinizer ignore-call */ 
122
                        reflash();
Loading history...
122
        $this->session->clearFlashData();
123
124
        $this->assertTrue($this->session->has('foo'));
125
    }
126
127
    public function testClear()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function testClear()
Loading history...
128
    {
129
        $this->session->set('bar', 'foo');
130
        $this->assertEquals('foo', $this->session->get('bar'));
131
        $this->session->clear();
0 ignored issues
show
Bug introduced by
The method clear() does not exist on think\Session. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

131
        $this->session->/** @scrutinizer ignore-call */ 
132
                        clear();
Loading history...
132
        $this->assertFalse($this->session->has('foo'));
133
    }
134
135
    public function testSetName()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function testSetName()
Loading history...
136
    {
137
        $this->session->setName('foo');
0 ignored issues
show
Bug introduced by
The method setName() does not exist on think\Session. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

137
        $this->session->/** @scrutinizer ignore-call */ 
138
                        setName('foo');
Loading history...
138
        $this->assertEquals('foo', $this->session->getName());
0 ignored issues
show
Bug introduced by
The method getName() does not exist on Mockery\MockInterface. Did you maybe mean mockery_getName()? ( Ignorable by Annotation )

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

138
        $this->assertEquals('foo', $this->session->/** @scrutinizer ignore-call */ getName());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method getName() does not exist on think\Session. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

138
        $this->assertEquals('foo', $this->session->/** @scrutinizer ignore-call */ getName());
Loading history...
139
    }
140
141
    public function testDestroy()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function testDestroy()
Loading history...
142
    {
143
        $id = md5(uniqid());
144
145
        $this->handler->shouldReceive('read')->once()->with($id)->andReturn("");
146
        $this->handler->shouldReceive('delete')->once()->with($id)->andReturnTrue();
147
148
        $this->session->setId($id);
149
        $this->session->init();
150
151
        $this->session->set('bar', 'foo');
152
153
        $this->session->destroy();
0 ignored issues
show
Bug introduced by
The method destroy() does not exist on think\Session. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

153
        $this->session->/** @scrutinizer ignore-call */ 
154
                        destroy();
Loading history...
154
155
        $this->assertFalse($this->session->has('bar'));
156
157
        $this->assertNotEquals($id, $this->session->getId());
0 ignored issues
show
Bug introduced by
The method getId() does not exist on think\Session. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

157
        $this->assertNotEquals($id, $this->session->/** @scrutinizer ignore-call */ getId());
Loading history...
158
    }
159
160
    public function testFileHandler()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function testFileHandler()
Loading history...
161
    {
162
        $root = vfsStream::setup();
163
164
        vfsStream::newFile('bar')
165
            ->at($root)
166
            ->lastModified(time());
167
168
        vfsStream::newFile('bar')
169
            ->at(vfsStream::newDirectory("foo")->at($root))
170
            ->lastModified(100);
171
172
        $this->assertTrue($root->hasChild("bar"));
173
        $this->assertTrue($root->hasChild("foo/bar"));
174
175
        $handler = new TestFileHandle($this->app, [
0 ignored issues
show
Bug introduced by
It seems like $this->app can also be of type Mockery\MockInterface; however, parameter $app of think\tests\TestFileHandle::__construct() does only seem to accept think\App, 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

175
        $handler = new TestFileHandle(/** @scrutinizer ignore-type */ $this->app, [
Loading history...
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
176
            'path'           => $root->url(),
177
            'gc_probability' => 1,
178
            'gc_divisor'     => 1,
179
        ]);
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
180
181
        $this->assertTrue($root->hasChild("bar"));
182
        $this->assertFalse($root->hasChild("foo/bar"));
183
184
        $id = md5(uniqid());
185
        $handler->write($id, "bar");
186
187
        $this->assertTrue($root->hasChild("sess_{$id}"));
188
189
        $this->assertEquals("bar", $handler->read($id));
190
191
        $handler->delete($id);
192
193
        $this->assertFalse($root->hasChild("sess_{$id}"));
194
    }
195
196
    public function testCacheHandler()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function testCacheHandler()
Loading history...
197
    {
198
        $id = md5(uniqid());
199
200
        $cache = m::mock(\think\Cache::class);
201
202
        $store = m::mock(Driver::class);
203
204
        $cache->shouldReceive('store')->once()->with('redis')->andReturn($store);
205
206
        $handler = new Cache($cache, ['store' => 'redis']);
0 ignored issues
show
Bug introduced by
$cache of type Mockery\LegacyMockInterface|Mockery\MockInterface is incompatible with the type think\Cache expected by parameter $cache of think\session\driver\Cache::__construct(). ( Ignorable by Annotation )

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

206
        $handler = new Cache(/** @scrutinizer ignore-type */ $cache, ['store' => 'redis']);
Loading history...
207
208
        $store->shouldReceive("set")->with($id, "bar", 1440)->once()->andReturnTrue();
0 ignored issues
show
Bug introduced by
The method andReturnTrue() does not exist on Mockery\ExpectationInterface. Did you maybe mean andReturn()? ( Ignorable by Annotation )

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

208
        $store->shouldReceive("set")->with($id, "bar", 1440)->once()->/** @scrutinizer ignore-call */ andReturnTrue();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
209
        $handler->write($id, "bar");
210
211
        $store->shouldReceive("get")->with($id)->once()->andReturn("bar");
212
        $this->assertEquals("bar", $handler->read($id));
213
214
        $store->shouldReceive("delete")->with($id)->once()->andReturnTrue();
215
        $handler->delete($id);
216
    }
217
}
218
219
class TestFileHandle extends File
220
{
221
    protected function writeFile($path, $content)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function writeFile()
Loading history...
222
    {
223
        return (bool) file_put_contents($path, $content);
224
    }
225
}
226