forked from jasny/session-middleware
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSessionMiddlewareTest.php
More file actions
167 lines (127 loc) · 5.78 KB
/
Copy pathSessionMiddlewareTest.php
File metadata and controls
167 lines (127 loc) · 5.78 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
namespace Jasny\Session\Tests;
use Jasny\PHPUnit\CallbackMockTrait;
use Jasny\PHPUnit\ExpectWarningTrait;
use Jasny\Session\SessionInterface;
use Jasny\Session\SessionMiddleware;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
/**
* @covers \Jasny\Session\SessionMiddleware
*/
class SessionMiddlewareTest extends TestCase
{
use CallbackMockTrait;
use ExpectWarningTrait;
/** @var SessionInterface&MockObject */
protected $session;
protected SessionMiddleware $middleware;
public function setUp(): void
{
$this->session = $this->createMock(SessionInterface::class);
$this->middleware = new SessionMiddleware($this->session);
}
public function testPsr15WithoutSessionAttribute()
{
$requestWithSession = $this->createMock(ServerRequestInterface::class);
$request = $this->createMock(ServerRequestInterface::class);
$request->expects($this->once())->method('getAttribute')
->with('session')
->willReturn(null);
$request->expects($this->once())->method('withAttribute')
->with('session', $this->identicalTo($this->session))
->willReturn($requestWithSession);
$this->session->expects($this->once())->method('start');
$this->session->expects($this->once())->method('stop');
$response = $this->createMock(ResponseInterface::class);
$handler = $this->createMock(RequestHandlerInterface::class);
$handler->expects($this->once())->method('handle')
->with($this->identicalTo($requestWithSession))
->willReturn($response);
$ret = $this->middleware->process($request, $handler);
$this->assertSame($response, $ret);
}
public function testPsr15WithSessionAttribute()
{
$request = $this->createMock(ServerRequestInterface::class);
$request->expects($this->once())->method('getAttribute')
->with('session')
->willReturn($this->session);
$request->expects($this->never())->method('withAttribute');
$this->session->expects($this->once())->method('start');
$this->session->expects($this->once())->method('stop');
$response = $this->createMock(ResponseInterface::class);
$handler = $this->createMock(RequestHandlerInterface::class);
$handler->expects($this->once())->method('handle')
->with($this->identicalTo($request))
->willReturn($response);
$ret = $this->middleware->process($request, $handler);
$this->assertSame($response, $ret);
}
public function testWithInvalidSessionAttribute()
{
$request = $this->createMock(ServerRequestInterface::class);
$request->expects($this->once())->method('getAttribute')
->with('session')
->willReturn([]);
$request->expects($this->never())->method('withAttribute');
$this->session->expects($this->never())->method('start');
$this->session->expects($this->never())->method('stop');
$response = $this->createMock(ResponseInterface::class);
$handler = $this->createMock(RequestHandlerInterface::class);
$handler->expects($this->once())->method('handle')
->with($this->identicalTo($request))
->willReturn($response);
$this->expectWarningMessage("'session' attribute of server request isn't a session object");
$ret = $this->middleware->process($request, $handler);
$this->assertSame($response, $ret);
}
public function testDoublePassWithoutSessionAttribute()
{
$requestWithSession = $this->createMock(ServerRequestInterface::class);
$response = $this->createMock(ResponseInterface::class);
$request = $this->createMock(ServerRequestInterface::class);
$request->expects($this->once())->method('getAttribute')
->with('session')
->willReturn(null);
$request->expects($this->once())->method('withAttribute')
->with('session', $this->identicalTo($this->session))
->willReturn($requestWithSession);
$this->session->expects($this->once())->method('start');
$this->session->expects($this->once())->method('stop');
$baseResponse = $this->createMock(ResponseInterface::class);
$next = $this->createCallbackMock(
$this->once(),
[$this->identicalTo($requestWithSession), $this->identicalTo($baseResponse)],
$response,
);
$doublePass = $this->middleware->asDoublePass();
$this->assertIsCallable($doublePass);
$ret = $doublePass($request, $baseResponse, $next);
$this->assertSame($response, $ret);
}
public function testDoublePassWithSessionAttribute()
{
$request = $this->createMock(ServerRequestInterface::class);
$request->expects($this->once())->method('getAttribute')
->with('session')
->willReturn($this->session);
$request->expects($this->never())->method('withAttribute');
$response = $this->createMock(ResponseInterface::class);
$this->session->expects($this->once())->method('start');
$this->session->expects($this->once())->method('stop');
$baseResponse = $this->createMock(ResponseInterface::class);
$next = $this->createCallbackMock(
$this->once(),
[$this->identicalTo($request), $this->identicalTo($baseResponse)],
$response,
);
$doublePass = $this->middleware->asDoublePass();
$this->assertIsCallable($doublePass);
$ret = $doublePass($request, $baseResponse, $next);
$this->assertSame($response, $ret);
}
}