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
|
<?php
declare(strict_types=1);
/**
* ownCloud - App Framework
*
* This file is licensed under the Affero General Public License version 3 or
* later. See the COPYING file.
*
* @author Thomas Müller <deepdiver@owncloud.com>
* @copyright Thomas Müller 2014
*/
namespace Test\AppFramework\Middleware;
use OC\AppFramework\Middleware\SessionMiddleware;
use OC\AppFramework\Utility\ControllerMethodReflector;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\Attribute\UseSession;
use OCP\AppFramework\Http\Response;
use OCP\IRequest;
use OCP\ISession;
use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase;
class SessionMiddlewareTest extends TestCase {
private ControllerMethodReflector|MockObject $reflector;
private ISession|MockObject $session;
private Controller $controller;
private SessionMiddleware $middleware;
protected function setUp(): void {
parent::setUp();
$this->reflector = $this->createMock(ControllerMethodReflector::class);
$this->session = $this->createMock(ISession::class);
$this->controller = new class('app', $this->createMock(IRequest::class)) extends Controller {
/**
* @UseSession
*/
public function withAnnotation() {
}
#[UseSession]
public function withAttribute() {
}
public function without() {
}
};
$this->middleware = new SessionMiddleware(
$this->reflector,
$this->session,
);
}
public function testSessionNotClosedOnBeforeController(): void {
$this->configureSessionMock(0, 1);
$this->reflector->expects(self::once())
->method('hasAnnotation')
->with('UseSession')
->willReturn(true);
$this->middleware->beforeController($this->controller, 'withAnnotation');
}
public function testSessionNotClosedOnBeforeControllerWithAttribute(): void {
$this->configureSessionMock(0, 1);
$this->reflector->expects(self::once())
->method('hasAnnotation')
->with('UseSession')
->willReturn(false);
$this->middleware->beforeController($this->controller, 'withAttribute');
}
public function testSessionClosedOnAfterController(): void {
$this->configureSessionMock(1);
$this->reflector->expects(self::once())
->method('hasAnnotation')
->with('UseSession')
->willReturn(true);
$this->middleware->afterController($this->controller, 'withAnnotation', new Response());
}
public function testSessionClosedOnAfterControllerWithAttribute(): void {
$this->configureSessionMock(1);
$this->reflector->expects(self::once())
->method('hasAnnotation')
->with('UseSession')
->willReturn(true);
$this->middleware->afterController($this->controller, 'withAttribute', new Response());
}
public function testSessionReopenedAndClosedOnBeforeController(): void {
$this->configureSessionMock(1, 1);
$this->reflector->expects(self::exactly(2))
->method('hasAnnotation')
->with('UseSession')
->willReturn(true);
$this->middleware->beforeController($this->controller, 'withAnnotation');
$this->middleware->afterController($this->controller, 'withAnnotation', new Response());
}
public function testSessionReopenedAndClosedOnBeforeControllerWithAttribute(): void {
$this->configureSessionMock(1, 1);
$this->reflector->expects(self::exactly(2))
->method('hasAnnotation')
->with('UseSession')
->willReturn(false);
$this->middleware->beforeController($this->controller, 'withAttribute');
$this->middleware->afterController($this->controller, 'withAttribute', new Response());
}
public function testSessionClosedOnBeforeController(): void {
$this->configureSessionMock(0);
$this->reflector->expects(self::once())
->method('hasAnnotation')
->with('UseSession')
->willReturn(false);
$this->middleware->beforeController($this->controller, 'without');
}
public function testSessionNotClosedOnAfterController(): void {
$this->configureSessionMock(0);
$this->reflector->expects(self::once())
->method('hasAnnotation')
->with('UseSession')
->willReturn(false);
$this->middleware->afterController($this->controller, 'without', new Response());
}
private function configureSessionMock(int $expectedCloseCount, int $expectedReopenCount = 0): void {
$this->session->expects($this->exactly($expectedCloseCount))
->method('close');
$this->session->expects($this->exactly($expectedReopenCount))
->method('reopen');
}
}
|