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
|
<?php
namespace MediaWiki\Tests\Rest;
use MediaWiki\Config\ServiceOptions;
use MediaWiki\MainConfigNames;
use MediaWiki\Rest\BasicAccess\StaticBasicAuthorizer;
use MediaWiki\Rest\Module\Module;
use MediaWiki\Rest\Reporter\PHPErrorReporter;
use MediaWiki\Rest\RequestData;
use MediaWiki\Rest\ResponseFactory;
use MediaWiki\Rest\Router;
use MediaWiki\Rest\Validator\Validator;
use MediaWiki\Tests\Rest\Handler\SessionHelperTestTrait;
use MediaWiki\Tests\Unit\Permissions\MockAuthorityTrait;
use Psr\Container\ContainerInterface;
use Wikimedia\ObjectCache\EmptyBagOStuff;
use Wikimedia\ObjectFactory\ObjectFactory;
/**
* A trait providing utility function for testing the REST framework.
* This trait is intended to be used on subclasses of MediaWikiUnitTestCase
* or MediaWikiIntegrationTestCase.
*
* @stable to use
*/
trait RestTestTrait {
use SessionHelperTestTrait;
use MockAuthorityTrait;
/**
* @param array $params Constructor parameters, as an associative array.
* In addition to the actual parameters, the following pseudo-parameters
* are supported:
* - 'config': an associative array of configuration variables, used
* to construct the 'options' parameter.
* - 'request': A request object, used to construct the 'validator' parameter.
* @return Router
*/
private function newRouter( array $params = [] ) {
$responseFactory = new ResponseFactory( [] );
$responseFactory->setShowExceptionDetails( true );
$objectFactory = new ObjectFactory(
$this->getMockForAbstractClass( ContainerInterface::class )
);
$authority = $params['authority'] ?? $this->mockAnonUltimateAuthority();
$config = ( $params['config'] ?? [] ) + [
MainConfigNames::CanonicalServer => 'https://wiki.example.com',
MainConfigNames::InternalServer => 'http://api.local:8080',
MainConfigNames::RestPath => '/rest',
MainConfigNames::ScriptPath => '/w',
MainConfigNames::RightsUrl => 'https://rights.url',
MainConfigNames::RightsText => 'your rights',
MainConfigNames::EmergencyContact => 'admin@test.test',
MainConfigNames::Sitename => 'Test Site',
];
$request = $params['request'] ?? new RequestData();
return new Router(
$params['routeFiles'] ?? [ MW_INSTALL_PATH . '/tests/phpunit/unit/includes/Rest/testRoutes.json' ],
$params['extraRoutes'] ?? [],
$params['options'] ?? new ServiceOptions( Router::CONSTRUCTOR_OPTIONS, $config ),
$params['cacheBag'] ?? new EmptyBagOStuff(),
$params['responseFactory'] ?? $responseFactory,
$params['basicAuth'] ?? new StaticBasicAuthorizer(),
$params['authority'] ?? $authority,
$params['objectFactory'] ?? $objectFactory,
$params['validator'] ?? new Validator( $objectFactory, $request, $authority ),
$params['errorReporter'] ?? new PHPErrorReporter(),
$params['hookContainer'] ?? $this->createHookContainer(),
$params['session'] ?? $this->getSession( true )
);
}
/**
* @since 1.43
* @param array $params Constructor parameters for Module and Router, as an associative array.
* @return Module
*/
private function newModule( array $params ) {
$objectFactory = new ObjectFactory(
$this->getMockForAbstractClass( ContainerInterface::class )
);
$authority = $params['authority'] ?? $this->mockAnonUltimateAuthority();
$request = $params['request'] ?? new RequestData();
$module = $this->getMockBuilder( Module::class )
->setConstructorArgs( [
$params['router'] ?? $this->newRouter( $params ),
$params['pathPrefix'] ?? 'mock',
$params['responseFactory'] ?? new ResponseFactory( [] ),
$params['basicAuth'] ?? new StaticBasicAuthorizer(),
$params['objectFactory'] ?? $objectFactory,
$params['restValidator'] ?? new Validator( $objectFactory, $request, $authority ),
$params['errorReporter'] ?? new PHPErrorReporter()
] )
->onlyMethods( [] )
->getMockForAbstractClass();
return $module;
}
}
|