Skip to content

Commit 6814616

Browse files
committed
fix(tests): Adapt Middleware tests to API change
Removed a few tests rendered obsolete by the refactoring. Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
1 parent 447ee17 commit 6814616

File tree

10 files changed

+53
-70
lines changed

10 files changed

+53
-70
lines changed

lib/private/AppFramework/Utility/ControllerMethodReflector.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ public function __construct(
3333
* @param string $method the method which we want to inspect
3434
*/
3535
public function reflect($object, string $method) {
36+
$this->annotations = [];
37+
$this->types = [];
38+
$this->parameters = [];
39+
$this->ranges = [];
3640
$this->reflectionMethod = new \ReflectionMethod($object, $method);
3741
$this->startLine = $this->reflectionMethod->getStartLine();
3842
$this->file = $this->reflectionMethod->getFileName();

tests/lib/AppFramework/Http/DispatcherTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ protected function setUp(): void {
123123

124124
$this->request = $this->createMock(Request::class);
125125

126-
$this->reflector = new ControllerMethodReflector();
126+
$this->reflector = new ControllerMethodReflector(\OCP\Server::get(LoggerInterface::class));
127127

128128
$this->dispatcher = new Dispatcher(
129129
$this->http,

tests/lib/AppFramework/Middleware/Security/BruteForceMiddlewareTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class BruteForceMiddlewareTest extends TestCase {
3030
protected function setUp(): void {
3131
parent::setUp();
3232

33-
$this->reflector = new ControllerMethodReflector();
33+
$this->reflector = new ControllerMethodReflector(\OCP\Server::get(LoggerInterface::class));
3434
$this->throttler = $this->createMock(IThrottler::class);
3535
$this->request = $this->createMock(IRequest::class);
3636
$this->logger = $this->createMock(LoggerInterface::class);

tests/lib/AppFramework/Middleware/Security/CORSMiddlewareTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class CORSMiddlewareTest extends \Test\TestCase {
3333

3434
protected function setUp(): void {
3535
parent::setUp();
36-
$this->reflector = new ControllerMethodReflector();
36+
$this->reflector = new ControllerMethodReflector(\OCP\Server::get(LoggerInterface::class));
3737
$this->session = $this->createMock(Session::class);
3838
$this->throttler = $this->createMock(IThrottler::class);
3939
$this->logger = $this->createMock(LoggerInterface::class);
@@ -79,6 +79,7 @@ public function testNoAnnotationNoCORSHEADER(): void {
7979
$this->createMock(IRequestId::class),
8080
$this->createMock(IConfig::class)
8181
);
82+
$this->reflector->reflect($this->controller, __FUNCTION__);
8283
$middleware = new CORSMiddleware($request, new MiddlewareUtils($this->reflector, $this->logger), $this->session, $this->throttler);
8384

8485
$response = $middleware->afterController($this->controller, __FUNCTION__, new Response());
@@ -300,6 +301,7 @@ public function testAfterExceptionWithSecurityExceptionNoStatus(): void {
300301
$this->createMock(IRequestId::class),
301302
$this->createMock(IConfig::class)
302303
);
304+
$this->reflector->reflect($this->controller, __FUNCTION__);
303305
$middleware = new CORSMiddleware($request, new MiddlewareUtils($this->reflector, $this->logger), $this->session, $this->throttler);
304306
$response = $middleware->afterException($this->controller, __FUNCTION__, new SecurityException('A security exception'));
305307

@@ -316,6 +318,7 @@ public function testAfterExceptionWithSecurityExceptionWithStatus(): void {
316318
$this->createMock(IRequestId::class),
317319
$this->createMock(IConfig::class)
318320
);
321+
$this->reflector->reflect($this->controller, __FUNCTION__);
319322
$middleware = new CORSMiddleware($request, new MiddlewareUtils($this->reflector, $this->logger), $this->session, $this->throttler);
320323
$response = $middleware->afterException($this->controller, __FUNCTION__, new SecurityException('A security exception', 501));
321324

@@ -335,6 +338,7 @@ public function testAfterExceptionWithRegularException(): void {
335338
$this->createMock(IRequestId::class),
336339
$this->createMock(IConfig::class)
337340
);
341+
$this->reflector->reflect($this->controller, __FUNCTION__);
338342
$middleware = new CORSMiddleware($request, new MiddlewareUtils($this->reflector, $this->logger), $this->session, $this->throttler);
339343
$middleware->afterException($this->controller, __FUNCTION__, new \Exception('A regular exception'));
340344
}

tests/lib/AppFramework/Middleware/Security/PasswordConfirmationMiddlewareTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class PasswordConfirmationMiddlewareTest extends TestCase {
4545
private Manager $userManager;
4646

4747
protected function setUp(): void {
48-
$this->reflector = new ControllerMethodReflector();
48+
$this->reflector = new ControllerMethodReflector(\OCP\Server::get(LoggerInterface::class));
4949
$this->session = $this->createMock(ISession::class);
5050
$this->userSession = $this->createMock(IUserSession::class);
5151
$this->user = $this->createMock(IUser::class);

tests/lib/AppFramework/Middleware/Security/RateLimitingMiddlewareTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ protected function setUp(): void {
4545

4646
$this->request = $this->createMock(IRequest::class);
4747
$this->userSession = $this->createMock(IUserSession::class);
48-
$this->reflector = new ControllerMethodReflector();
48+
$this->reflector = new ControllerMethodReflector(\OCP\Server::get(LoggerInterface::class));
4949
$this->limiter = $this->createMock(Limiter::class);
5050
$this->session = $this->createMock(ISession::class);
5151
$this->appConfig = $this->createMock(IAppConfig::class);

tests/lib/AppFramework/Middleware/Security/SameSiteCookieMiddlewareTest.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,9 @@ public function testBeforeControllerIndexHasAnnotation(): void {
6161
$this->request->method('getScriptName')
6262
->willReturn('/index.php');
6363

64-
$this->reflector->method('hasAnnotation')
65-
->with('NoSameSiteCookieRequired')
64+
$this->reflector->expects(self::once())
65+
->method('hasAnnotationOrAttribute')
66+
->with('NoSameSiteCookieRequired', NoSameSiteCookieRequired::class)
6667
->willReturn(true);
6768

6869
$this->middleware->beforeController(new HasAnnotationController('foo', $this->request), 'foo');
@@ -73,8 +74,9 @@ public function testBeforeControllerIndexNoAnnotationPassingCheck(): void {
7374
$this->request->method('getScriptName')
7475
->willReturn('/index.php');
7576

76-
$this->reflector->method('hasAnnotation')
77-
->with('NoSameSiteCookieRequired')
77+
$this->reflector->expects(self::once())
78+
->method('hasAnnotationOrAttribute')
79+
->with('NoSameSiteCookieRequired', NoSameSiteCookieRequired::class)
7880
->willReturn(false);
7981

8082
$this->request->method('passesLaxCookieCheck')
@@ -90,8 +92,9 @@ public function testBeforeControllerIndexNoAnnotationFailingCheck(): void {
9092
$this->request->method('getScriptName')
9193
->willReturn('/index.php');
9294

93-
$this->reflector->method('hasAnnotation')
94-
->with('NoSameSiteCookieRequired')
95+
$this->reflector->expects(self::once())
96+
->method('hasAnnotationOrAttribute')
97+
->with('NoSameSiteCookieRequired', NoSameSiteCookieRequired::class)
9598
->willReturn(false);
9699

97100
$this->request->method('passesLaxCookieCheck')

tests/lib/AppFramework/Middleware/Security/SecurityMiddlewareTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ protected function setUp(): void {
7272
'test',
7373
$this->request
7474
);
75-
$this->reader = new ControllerMethodReflector();
75+
$this->reader = new ControllerMethodReflector(\OCP\Server::get(LoggerInterface::class));
7676
$this->logger = $this->createMock(LoggerInterface::class);
7777
$this->navigationManager = $this->createMock(INavigationManager::class);
7878
$this->urlGenerator = $this->createMock(IURLGenerator::class);
@@ -433,6 +433,7 @@ public function testCsrfOcsController(string $controllerClass, bool $hasOcsApiHe
433433
->willReturn(true);
434434

435435
$controller = new $controllerClass('test', $this->request);
436+
$this->reader->reflect($controller, 'foo');
436437

437438
try {
438439
$this->middleware->beforeController($controller, 'foo');

tests/lib/AppFramework/Middleware/SessionMiddlewareTest.php

Lines changed: 13 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use OC\AppFramework\Middleware\SessionMiddleware;
1212
use OC\AppFramework\Utility\ControllerMethodReflector;
1313
use OCP\AppFramework\Controller;
14+
use OCP\AppFramework\Http\Attribute\UseSession;
1415
use OCP\AppFramework\Http\Response;
1516
use OCP\IRequest;
1617
use OCP\ISession;
@@ -19,8 +20,8 @@
1920
use Test\TestCase;
2021

2122
class SessionMiddlewareTest extends TestCase {
22-
private ControllerMethodReflector|MockObject $reflector;
23-
private ISession|MockObject $session;
23+
private ControllerMethodReflector&MockObject $reflector;
24+
private ISession&MockObject $session;
2425
private Controller $controller;
2526
private SessionMiddleware $middleware;
2627

@@ -39,70 +40,39 @@ protected function setUp(): void {
3940
public function testSessionNotClosedOnBeforeController(): void {
4041
$this->configureSessionMock(0, 1);
4142
$this->reflector->expects(self::once())
42-
->method('hasAnnotation')
43-
->with('UseSession')
43+
->method('hasAnnotationOrAttribute')
44+
->with('UseSession', UseSession::class)
4445
->willReturn(true);
4546

4647
$this->middleware->beforeController($this->controller, 'withAnnotation');
4748
}
4849

49-
public function testSessionNotClosedOnBeforeControllerWithAttribute(): void {
50-
$this->configureSessionMock(0, 1);
51-
$this->reflector->expects(self::once())
52-
->method('hasAnnotation')
53-
->with('UseSession')
54-
->willReturn(false);
55-
56-
$this->middleware->beforeController($this->controller, 'withAttribute');
57-
}
58-
5950
public function testSessionClosedOnAfterController(): void {
6051
$this->configureSessionMock(1);
6152
$this->reflector->expects(self::once())
62-
->method('hasAnnotation')
63-
->with('UseSession')
53+
->method('hasAnnotationOrAttribute')
54+
->with('UseSession', UseSession::class)
6455
->willReturn(true);
6556

6657
$this->middleware->afterController($this->controller, 'withAnnotation', new Response());
6758
}
6859

69-
public function testSessionClosedOnAfterControllerWithAttribute(): void {
70-
$this->configureSessionMock(1);
71-
$this->reflector->expects(self::once())
72-
->method('hasAnnotation')
73-
->with('UseSession')
74-
->willReturn(true);
75-
76-
$this->middleware->afterController($this->controller, 'withAttribute', new Response());
77-
}
78-
7960
public function testSessionReopenedAndClosedOnBeforeController(): void {
8061
$this->configureSessionMock(1, 1);
8162
$this->reflector->expects(self::exactly(2))
82-
->method('hasAnnotation')
83-
->with('UseSession')
63+
->method('hasAnnotationOrAttribute')
64+
->with('UseSession', UseSession::class)
8465
->willReturn(true);
8566

8667
$this->middleware->beforeController($this->controller, 'withAnnotation');
8768
$this->middleware->afterController($this->controller, 'withAnnotation', new Response());
8869
}
8970

90-
public function testSessionReopenedAndClosedOnBeforeControllerWithAttribute(): void {
91-
$this->configureSessionMock(1, 1);
92-
$this->reflector->expects(self::exactly(2))
93-
->method('hasAnnotation')
94-
->with('UseSession')
95-
->willReturn(false);
96-
97-
$this->middleware->beforeController($this->controller, 'withAttribute');
98-
$this->middleware->afterController($this->controller, 'withAttribute', new Response());
99-
}
100-
10171
public function testSessionClosedOnBeforeController(): void {
10272
$this->configureSessionMock(0);
10373
$this->reflector->expects(self::once())
104-
->method('hasAnnotation')
105-
->with('UseSession')
74+
->method('hasAnnotationOrAttribute')
75+
->with('UseSession', UseSession::class)
10676
->willReturn(false);
10777

10878
$this->middleware->beforeController($this->controller, 'without');
@@ -111,8 +81,8 @@ public function testSessionClosedOnBeforeController(): void {
11181
public function testSessionNotClosedOnAfterController(): void {
11282
$this->configureSessionMock(0);
11383
$this->reflector->expects(self::once())
114-
->method('hasAnnotation')
115-
->with('UseSession')
84+
->method('hasAnnotationOrAttribute')
85+
->with('UseSession', UseSession::class)
11686
->willReturn(false);
11787

11888
$this->middleware->afterController($this->controller, 'without', new Response());

tests/lib/AppFramework/Utility/ControllerMethodReflectorTest.php

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
namespace Test\AppFramework\Utility;
1010

1111
use OC\AppFramework\Utility\ControllerMethodReflector;
12+
use Psr\Log\LoggerInterface;
1213

1314
class BaseController {
1415
/**
@@ -69,7 +70,7 @@ class ControllerMethodReflectorTest extends \Test\TestCase {
6970
* @Annotation
7071
*/
7172
public function testReadAnnotation(): void {
72-
$reader = new ControllerMethodReflector();
73+
$reader = new ControllerMethodReflector(\OCP\Server::get(LoggerInterface::class));
7374
$reader->reflect(
7475
'\Test\AppFramework\Utility\ControllerMethodReflectorTest',
7576
'testReadAnnotation'
@@ -82,7 +83,7 @@ public function testReadAnnotation(): void {
8283
* @Annotation(parameter=value)
8384
*/
8485
public function testGetAnnotationParameterSingle(): void {
85-
$reader = new ControllerMethodReflector();
86+
$reader = new ControllerMethodReflector(\OCP\Server::get(LoggerInterface::class));
8687
$reader->reflect(
8788
self::class,
8889
__FUNCTION__
@@ -95,7 +96,7 @@ public function testGetAnnotationParameterSingle(): void {
9596
* @Annotation(parameter1=value1, parameter2=value2,parameter3=value3)
9697
*/
9798
public function testGetAnnotationParameterMultiple(): void {
98-
$reader = new ControllerMethodReflector();
99+
$reader = new ControllerMethodReflector(\OCP\Server::get(LoggerInterface::class));
99100
$reader->reflect(
100101
self::class,
101102
__FUNCTION__
@@ -111,7 +112,7 @@ public function testGetAnnotationParameterMultiple(): void {
111112
* @param test
112113
*/
113114
public function testReadAnnotationNoLowercase(): void {
114-
$reader = new ControllerMethodReflector();
115+
$reader = new ControllerMethodReflector(\OCP\Server::get(LoggerInterface::class));
115116
$reader->reflect(
116117
'\Test\AppFramework\Utility\ControllerMethodReflectorTest',
117118
'testReadAnnotationNoLowercase'
@@ -127,7 +128,7 @@ public function testReadAnnotationNoLowercase(): void {
127128
* @param int $test
128129
*/
129130
public function testReadTypeIntAnnotations(): void {
130-
$reader = new ControllerMethodReflector();
131+
$reader = new ControllerMethodReflector(\OCP\Server::get(LoggerInterface::class));
131132
$reader->reflect(
132133
'\Test\AppFramework\Utility\ControllerMethodReflectorTest',
133134
'testReadTypeIntAnnotations'
@@ -145,7 +146,7 @@ public function arguments3($a, float $b, int $c, $d) {
145146
}
146147

147148
public function testReadTypeIntAnnotationsScalarTypes(): void {
148-
$reader = new ControllerMethodReflector();
149+
$reader = new ControllerMethodReflector(\OCP\Server::get(LoggerInterface::class));
149150
$reader->reflect(
150151
'\Test\AppFramework\Utility\ControllerMethodReflectorTest',
151152
'arguments3'
@@ -163,7 +164,7 @@ public function testReadTypeIntAnnotationsScalarTypes(): void {
163164
* @param double $test something special
164165
*/
165166
public function testReadTypeDoubleAnnotations(): void {
166-
$reader = new ControllerMethodReflector();
167+
$reader = new ControllerMethodReflector(\OCP\Server::get(LoggerInterface::class));
167168
$reader->reflect(
168169
'\Test\AppFramework\Utility\ControllerMethodReflectorTest',
169170
'testReadTypeDoubleAnnotations'
@@ -177,7 +178,7 @@ public function testReadTypeDoubleAnnotations(): void {
177178
* @param string $foo
178179
*/
179180
public function testReadTypeWhitespaceAnnotations(): void {
180-
$reader = new ControllerMethodReflector();
181+
$reader = new ControllerMethodReflector(\OCP\Server::get(LoggerInterface::class));
181182
$reader->reflect(
182183
'\Test\AppFramework\Utility\ControllerMethodReflectorTest',
183184
'testReadTypeWhitespaceAnnotations'
@@ -190,7 +191,7 @@ public function testReadTypeWhitespaceAnnotations(): void {
190191
public function arguments($arg, $arg2 = 'hi') {
191192
}
192193
public function testReflectParameters(): void {
193-
$reader = new ControllerMethodReflector();
194+
$reader = new ControllerMethodReflector(\OCP\Server::get(LoggerInterface::class));
194195
$reader->reflect(
195196
'\Test\AppFramework\Utility\ControllerMethodReflectorTest',
196197
'arguments'
@@ -203,7 +204,7 @@ public function testReflectParameters(): void {
203204
public function arguments2($arg) {
204205
}
205206
public function testReflectParameters2(): void {
206-
$reader = new ControllerMethodReflector();
207+
$reader = new ControllerMethodReflector(\OCP\Server::get(LoggerInterface::class));
207208
$reader->reflect(
208209
'\Test\AppFramework\Utility\ControllerMethodReflectorTest',
209210
'arguments2'
@@ -214,15 +215,15 @@ public function testReflectParameters2(): void {
214215

215216

216217
public function testInheritance(): void {
217-
$reader = new ControllerMethodReflector();
218+
$reader = new ControllerMethodReflector(\OCP\Server::get(LoggerInterface::class));
218219
$reader->reflect('Test\AppFramework\Utility\EndController', 'test');
219220

220221
$this->assertTrue($reader->hasAnnotation('Annotation'));
221222
}
222223

223224

224225
public function testInheritanceOverride(): void {
225-
$reader = new ControllerMethodReflector();
226+
$reader = new ControllerMethodReflector(\OCP\Server::get(LoggerInterface::class));
226227
$reader->reflect('Test\AppFramework\Utility\EndController', 'test2');
227228

228229
$this->assertTrue($reader->hasAnnotation('NoAnnotation'));
@@ -231,14 +232,14 @@ public function testInheritanceOverride(): void {
231232

232233

233234
public function testInheritanceOverrideNoDocblock(): void {
234-
$reader = new ControllerMethodReflector();
235+
$reader = new ControllerMethodReflector(\OCP\Server::get(LoggerInterface::class));
235236
$reader->reflect('Test\AppFramework\Utility\EndController', 'test3');
236237

237238
$this->assertFalse($reader->hasAnnotation('Annotation'));
238239
}
239240

240241
public function testRangeDetectionPsalm(): void {
241-
$reader = new ControllerMethodReflector();
242+
$reader = new ControllerMethodReflector(\OCP\Server::get(LoggerInterface::class));
242243
$reader->reflect('Test\AppFramework\Utility\EndController', 'test4');
243244

244245
$rangeInfo1 = $reader->getRange('rangedOne');
@@ -259,7 +260,7 @@ public function testRangeDetectionPsalm(): void {
259260
}
260261

261262
public function testRangeDetectionNative(): void {
262-
$reader = new ControllerMethodReflector();
263+
$reader = new ControllerMethodReflector(\OCP\Server::get(LoggerInterface::class));
263264
$reader->reflect('Test\AppFramework\Utility\EndController', 'test5');
264265

265266
$rangeInfo1 = $reader->getRange('rangedOne');

0 commit comments

Comments
 (0)