From: Timo Tijhof Date: Sat, 3 Dec 2016 00:46:53 +0000 (-0800) Subject: phpunit: Make getResourceLoaderContext() more extendable X-Git-Tag: 1.31.0-rc.0~4667^2 X-Git-Url: https://git.cyclocoop.org/%27.WWW_URL.%27admin/?a=commitdiff_plain;h=917fa4eded3f1878be9d288599c459f73d48349f;p=lhc%2Fweb%2Fwiklou.git phpunit: Make getResourceLoaderContext() more extendable This makes it easier to add other options in the future, such as setting 'modules' in the context to something else. Change-Id: I53c25fa7ad705cc34e44f95e4f87eb53612d800e --- diff --git a/tests/phpunit/ResourceLoaderTestCase.php b/tests/phpunit/ResourceLoaderTestCase.php index baa481e51c..45cfdbfc61 100644 --- a/tests/phpunit/ResourceLoaderTestCase.php +++ b/tests/phpunit/ResourceLoaderTestCase.php @@ -11,14 +11,23 @@ abstract class ResourceLoaderTestCase extends MediaWikiTestCase { const BLANK_VERSION = '09p30q0'; /** - * @param string $lang - * @param string $dir + * @param array|string $options Language code or options array + * - string 'lang' Language code + * - string 'dir' Language direction (ltr or rtl) * @return ResourceLoaderContext */ - protected function getResourceLoaderContext( $lang = 'en', $dir = 'ltr' ) { + protected function getResourceLoaderContext( $options = [] ) { + if ( is_string( $options ) ) { + // Back-compat for extension tests + $options = [ 'lang' => $options ]; + } + $options += [ + 'lang' => 'en', + 'dir' => 'ltr', + ]; $resourceLoader = new ResourceLoader(); $request = new FauxRequest( [ - 'lang' => $lang, + 'lang' => $options['lang'], 'modules' => 'startup', 'only' => 'scripts', 'skin' => 'vector', @@ -28,7 +37,7 @@ abstract class ResourceLoaderTestCase extends MediaWikiTestCase { ->setConstructorArgs( [ $resourceLoader, $request ] ) ->setMethods( [ 'getDirection' ] ) ->getMock(); - $ctx->method( 'getDirection' )->willReturn( $dir ); + $ctx->method( 'getDirection' )->willReturn( $options['dir'] ); return $ctx; } diff --git a/tests/phpunit/includes/resourceloader/ResourceLoaderFileModuleTest.php b/tests/phpunit/includes/resourceloader/ResourceLoaderFileModuleTest.php index 8b29983e70..4a3b90a294 100644 --- a/tests/phpunit/includes/resourceloader/ResourceLoaderFileModuleTest.php +++ b/tests/phpunit/includes/resourceloader/ResourceLoaderFileModuleTest.php @@ -130,7 +130,7 @@ class ResourceLoaderFileModuleTest extends ResourceLoaderTestCase { $modules = self::getModules(); $rl = new ResourceLoaderFileModule( $modules[$name] ); $rl->setName( $name ); - $ctx = $this->getResourceLoaderContext( 'en', 'ltr' ); + $ctx = $this->getResourceLoaderContext(); $this->assertEquals( $rl->getScript( $ctx ), $expected ); } @@ -210,8 +210,14 @@ class ResourceLoaderFileModuleTest extends ResourceLoaderTestCase { ] ); $expectedModule->setName( 'testing' ); - $contextLtr = $this->getResourceLoaderContext( 'en', 'ltr' ); - $contextRtl = $this->getResourceLoaderContext( 'he', 'rtl' ); + $contextLtr = $this->getResourceLoaderContext( [ + 'lang' => 'en', + 'dir' => 'ltr', + ] ); + $contextRtl = $this->getResourceLoaderContext( [ + 'lang' => 'he', + 'dir' => 'rtl', + ] ); // Since we want to compare the effect of @noflip+@embed against the effect of just @embed, and // the @noflip annotations are always preserved, we need to strip them first. @@ -282,9 +288,9 @@ class ResourceLoaderFileModuleTest extends ResourceLoaderTestCase { 'File has leading BOM' ); - $contextLtr = $this->getResourceLoaderContext( 'en', 'ltr' ); + $context = $this->getResourceLoaderContext(); $this->assertEquals( - $testModule->getStyles( $contextLtr ), + $testModule->getStyles( $context ), [ 'all' => ".efbbbf_bom_char_at_start_of_file {}\n" ], 'Leading BOM removed when concatenating files' ); diff --git a/tests/phpunit/includes/resourceloader/ResourceLoaderImageTest.php b/tests/phpunit/includes/resourceloader/ResourceLoaderImageTest.php index 179a8ed98f..84b56d4f11 100644 --- a/tests/phpunit/includes/resourceloader/ResourceLoaderImageTest.php +++ b/tests/phpunit/includes/resourceloader/ResourceLoaderImageTest.php @@ -61,7 +61,10 @@ class ResourceLoaderImageTest extends ResourceLoaderTestCase { static $contexts = []; $image = $this->getTestImage( $imageName ); - $context = $this->getResourceLoaderContext( $languageCode, $dirMap[$languageCode] ); + $context = $this->getResourceLoaderContext( [ + 'lang' => $languageCode, + 'dir' => $dirMap[$languageCode], + ] ); $this->assertEquals( $image->getPath( $context ), $this->imagesPath . '/' . $path ); } @@ -87,7 +90,7 @@ class ResourceLoaderImageTest extends ResourceLoaderTestCase { * @covers ResourceLoaderImage::massageSvgPathdata */ public function testGetImageData() { - $context = $this->getResourceLoaderContext( 'en', 'ltr' ); + $context = $this->getResourceLoaderContext(); $image = $this->getTestImage( 'remove' ); $data = file_get_contents( $this->imagesPath . '/remove.svg' );