Debian Codegraph @

tests/phpunit/integration/includes/Rest/Handler/RedirectHandlerTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tests\Rest\Handler;
4
5 use MediaWiki\Rest\Handler\RedirectHandler;
6 use MediaWiki\Rest\RequestData;
7 use MediaWiki\Rest\RouteDefinitionException;
8 use MediaWikiIntegrationTestCase;
9
10 /**
11 * @covers \MediaWiki\Rest\Handler\RedirectHandler
12 */
13 class RedirectHandlerTest extends MediaWikiIntegrationTestCase {
14 use HandlerTestTrait;
15
16 public function redirectConfigProvider() {
17 return [
18 [
19 [ 'path' => '/v1/other/path/{param}', 'code' => 301 ],
20 301,
21 '/rest/v1/other/path/value'
22 ],
23 [
24 [ 'path' => '/v1/other/path/{param}' ],
25 308,
26 '/rest/v1/other/path/value'
27 ],
28
29 // Add more test cases
30 ];
31 }
32
33 public function provideFailure() {
34 return [
35 [
36 [ 'path' => '', 'code' => 308 ],
37 RouteDefinitionException::class
38 ],
39 [
40 [ 'code' => 308 ],
41 RouteDefinitionException::class
42 ],
43 // Add more test cases
44 ];
45 }
46
47 /**
48 * @dataProvider redirectConfigProvider
49 */
50 public function testExecute( $redirectConfig, $expectedCode, $expectedLocation ) {
51 $request = new RequestData( [ 'pathParams' => [ 'param' => 'value' ] ] );
52 $handler = new RedirectHandler();
53
54 // Execute the handler with configuration
55 $response = $this->executeHandler( $handler, $request, [ 'redirect' => $redirectConfig ] );
56
57 // Assertions for the response
58 $this->assertEquals( $expectedCode, $response->getStatusCode() ); // Check status code
59 $this->assertEquals( $expectedLocation, $response->getHeaderLine( 'Location' ) ); // Check Location header
60 }
61
62 /**
63 * @dataProvider provideFailure
64 */
65 public function testFailure( $redirectConfig, $expectedException ) {
66 $request = new RequestData( [ 'pathParams' => [ 'param' => 'value' ] ] );
67 $handler = new RedirectHandler();
68
69 $this->expectException( $expectedException );
70
71 // Execute the handler with configuration
72 $this->executeHandler( $handler, $request, [ 'redirect' => $redirectConfig ] );
73 }
74 }