wiki.techinc.nl/tests/phpunit/includes/Rest/EntryPointTest.php
Thiemo Kreuz b95a07380a Remove meaningless ->expects( $this->any() ) from all tests
It is not entirely meaningless. It might be an indicator that
the number of calls to a method is intentionally unlimited.
This is similar to e.g. an @inheritDoc PHPDoc comment that
marks a method as being "intentionally undocumented".

However, what's the meaning of being "intentionally
unconstrained"? Let's just not have any constraint then.

I feel all these ->expects( $this->any() ) bloat the test
code so much that it's never worth it.

Change-Id: I9925e7706bd03e1666f6eb0b284cb42b0dd3be23
2021-04-23 11:58:58 +02:00

128 lines
3.2 KiB
PHP

<?php
namespace MediaWiki\Tests\Rest;
use EmptyBagOStuff;
use GuzzleHttp\Psr7\Stream;
use GuzzleHttp\Psr7\Uri;
use MediaWiki\Permissions\UltimateAuthority;
use MediaWiki\Rest\BasicAccess\StaticBasicAuthorizer;
use MediaWiki\Rest\CorsUtils;
use MediaWiki\Rest\EntryPoint;
use MediaWiki\Rest\Handler;
use MediaWiki\Rest\RequestData;
use MediaWiki\Rest\RequestInterface;
use MediaWiki\Rest\ResponseFactory;
use MediaWiki\Rest\Router;
use MediaWiki\Rest\Validator\Validator;
use MediaWiki\User\UserIdentityValue;
use Psr\Container\ContainerInterface;
use RequestContext;
use WebResponse;
use Wikimedia\ObjectFactory;
/**
* @covers \MediaWiki\Rest\EntryPoint
* @covers \MediaWiki\Rest\Router
*/
class EntryPointTest extends \MediaWikiIntegrationTestCase {
private function createRouter( RequestInterface $request ) {
global $IP;
$objectFactory = new ObjectFactory(
$this->getMockForAbstractClass( ContainerInterface::class )
);
$user = new UserIdentityValue( 0, __CLASS__ );
$authority = new UltimateAuthority( $user );
return new Router(
[ "$IP/tests/phpunit/unit/includes/Rest/testRoutes.json" ],
[],
'http://wiki.example.com',
'/rest',
new EmptyBagOStuff(),
new ResponseFactory( [] ),
new StaticBasicAuthorizer(),
$authority,
$objectFactory,
new Validator( $objectFactory, $request, $authority ),
$this->createHookContainer()
);
}
private function createWebResponse() {
return $this->getMockBuilder( WebResponse::class )
->onlyMethods( [ 'header' ] )
->getMock();
}
private function createCorsUtils() {
$cors = $this->createMock( CorsUtils::class );
$cors->method( 'modifyResponse' )
->will( $this->returnArgument( 1 ) );
return $cors;
}
public static function mockHandlerHeader() {
return new class extends Handler {
public function execute() {
$response = $this->getResponseFactory()->create();
$response->setHeader( 'Foo', 'Bar' );
return $response;
}
};
}
public function testHeader() {
$webResponse = $this->createWebResponse();
$webResponse->method( 'header' )
->withConsecutive(
[ 'HTTP/1.1 200 OK', true, null ],
[ 'Foo: Bar', true, null ]
);
$request = new RequestData( [ 'uri' => new Uri( '/rest/mock/EntryPoint/header' ) ] );
$entryPoint = new EntryPoint(
RequestContext::getMain(),
$request,
$webResponse,
$this->createRouter( $request ),
$this->createCorsUtils()
);
$entryPoint->execute();
$this->assertTrue( true );
}
public static function mockHandlerBodyRewind() {
return new class extends Handler {
public function execute() {
$response = $this->getResponseFactory()->create();
$stream = new Stream( fopen( 'php://memory', 'w+' ) );
$stream->write( 'hello' );
$response->setBody( $stream );
return $response;
}
};
}
/**
* Make sure EntryPoint rewinds a seekable body stream before reading.
*/
public function testBodyRewind() {
$request = new RequestData( [ 'uri' => new Uri( '/rest/mock/EntryPoint/bodyRewind' ) ] );
$entryPoint = new EntryPoint(
RequestContext::getMain(),
$request,
$this->createWebResponse(),
$this->createRouter( $request ),
$this->createCorsUtils()
);
ob_start();
$entryPoint->execute();
$this->assertSame( 'hello', ob_get_clean() );
}
}