From 5f8a94a0b39acc20b1cd0c1a4552729a67dd4651 Mon Sep 17 00:00:00 2001 From: Timo Tijhof Date: Wed, 30 May 2018 18:21:31 +0100 Subject: [PATCH] resourceloader: Improve coverage of ResourceLoader::respond() - Cover case of simple module load. The bulk of this use case is already covered by a lower-level test for makeModuleResponse(). The added case here exists to cover the wrapper method, ResourceLoader::respond(). - Cover logic for catching and logging internal errors. Change-Id: I4315bb00137ff80ee2b790c6b4d4b5fbd93f6bc1 --- .../resourceloader/ResourceLoaderTest.php | 62 ++++++++++++++++++- 1 file changed, 61 insertions(+), 1 deletion(-) diff --git a/tests/phpunit/includes/resourceloader/ResourceLoaderTest.php b/tests/phpunit/includes/resourceloader/ResourceLoaderTest.php index e0b8c5e03c..c15eb2ce44 100644 --- a/tests/phpunit/includes/resourceloader/ResourceLoaderTest.php +++ b/tests/phpunit/includes/resourceloader/ResourceLoaderTest.php @@ -962,7 +962,7 @@ mw.example(); /** * @covers ResourceLoader::respond */ - public function testRespond() { + public function testRespondEmpty() { $rl = $this->getMockBuilder( EmptyResourceLoader::class ) ->setMethods( [ 'tryRespondNotModified', @@ -978,6 +978,66 @@ mw.example(); $rl->respond( $context ); } + /** + * @covers ResourceLoader::respond + */ + public function testRespondSimple() { + $module = new ResourceLoaderTestModule( [ 'script' => 'foo();' ] ); + $rl = $this->getMockBuilder( EmptyResourceLoader::class ) + ->setMethods( [ + 'measureResponseTime', + 'tryRespondNotModified', + 'sendResponseHeaders', + 'makeModuleResponse', + ] ) + ->getMock(); + $rl->register( 'test', $module ); + $context = $this->getResourceLoaderContext( + [ 'modules' => 'test', 'only' => null ], + $rl + ); + + $rl->expects( $this->once() )->method( 'makeModuleResponse' ) + ->with( $context, [ 'test' => $module ] ) + ->willReturn( 'implement_foo;' ); + $this->expectOutputRegex( '/^implement_foo;/' ); + + $rl->respond( $context ); + } + + /** + * @covers ResourceLoader::respond + */ + public function testRespondInternalFailures() { + $module = new ResourceLoaderTestModule( [ 'script' => 'foo();' ] ); + $rl = $this->getMockBuilder( EmptyResourceLoader::class ) + ->setMethods( [ + 'measureResponseTime', + 'preloadModuleInfo', + 'getCombinedVersion', + 'tryRespondNotModified', + 'makeModuleResponse', + 'sendResponseHeaders', + ] ) + ->getMock(); + $rl->register( 'test', $module ); + $context = $this->getResourceLoaderContext( [ 'modules' => 'test' ], $rl ); + // Disable logging from outputErrorAndLog + $this->setLogger( 'exception', new Psr\Log\NullLogger() ); + + $rl->expects( $this->once() )->method( 'preloadModuleInfo' ) + ->willThrowException( new Exception( 'Preload error' ) ); + $rl->expects( $this->once() )->method( 'getCombinedVersion' ) + ->willThrowException( new Exception( 'Version error' ) ); + $rl->expects( $this->once() )->method( 'makeModuleResponse' ) + ->with( $context, [ 'test' => $module ] ) + ->willReturn( 'foo;' ); + // Internal errors should be caught and logged without affecting module output + $this->expectOutputRegex( '/^\/\*.+Preload error.+Version error.+\*\/.*foo;/ms' ); + + $rl->respond( $context ); + } + /** * @covers ResourceLoader::measureResponseTime */ -- 2.20.1