If a REST handler has a title parameter, e.g.
'page' => [ static::PARAM_SOURCE => 'post', ParamValidator::PARAM_TYPE => 'title', ParamValidator::PARAM_REQUIRED => true, ],
and you try to test it with HandlerTestTrait, e.g.
$handler = // Instantiate the handler $reqData = new RequestData( /* Pass data */ ); $this->executeHandlerAndGetBodyData( $handler, $request );
it will fail with
Wikimedia\Services\ServiceContainer::get('TitleFactory') was not expected to be called.
The reason is that HandlerTestTrait::validateHandler has:
$serviceContainer = $this->createNoOpMock( ServiceContainer::class ); $objectFactory = new ObjectFactory( $serviceContainer ); $validator = new Validator( $objectFactory, $handler->getRequest(), $handler->getAuthority() );
And createNoOpMock creates a mock which never expects any method to be called. Note that even if you replace it with createMock it still fails because ServiceContainer::get( 'TitleFactory' ) returns null (since the method has no return type information) and that is passed to TitleDef::__construct. So it needs something like
$serviceContainer = $this->createMock( ServiceContainer::class ); $serviceContainer->method( 'get' )->with( 'TitleFactory' )->willReturn( $this->createMock( TitleFactory::class ) );
but then the mocked TitleFactory's newFromText will return null because the actual method can also return null. So you also need
$serviceContainer = $this->createMock( ServiceContainer::class ); $tf = $this->createMock( TitleFactory::class ); $tf->method( 'newFromText' )->willReturn( $this->createMock( Title::class ) ); $serviceContainer->method( 'get' )->with( 'TitleFactory' )->willReturn( $tf );
which means it's always going to assume that the Title is valid, and you cannot test the case where it isn't. So perhaps it'd be better if HandlerTestTrait allowed callers to inject a validator, and fall back to a mock when it's not provided.