From d85ea2b0305ff62ab42c2c724a707963c3ed3848 Mon Sep 17 00:00:00 2001 From: Timo Tijhof Date: Tue, 24 Sep 2019 23:48:48 +0100 Subject: [PATCH] resourceloader: Convert ImageModule test to stricter unit test Bug: T233343 Change-Id: I1f9d0b6b735c9192bb32baf249eefea76c65929e --- .../ResourceLoaderImageModuleTest.php | 5 ++-- .../ResourceLoaderImageTest.php | 28 +++++++++++++------ 2 files changed, 22 insertions(+), 11 deletions(-) rename tests/phpunit/{ => unit}/includes/resourceloader/ResourceLoaderImageTest.php (84%) diff --git a/tests/phpunit/includes/resourceloader/ResourceLoaderImageModuleTest.php b/tests/phpunit/includes/resourceloader/ResourceLoaderImageModuleTest.php index dad9f1ed4f..6ebbc3a036 100644 --- a/tests/phpunit/includes/resourceloader/ResourceLoaderImageModuleTest.php +++ b/tests/phpunit/includes/resourceloader/ResourceLoaderImageModuleTest.php @@ -292,9 +292,8 @@ TEXT ->disableOriginalConstructor() ->getMock(); $image->method( 'getDataUri' ) - ->will( $this->returnValue( $dataUriReturnValue ) ); - $image->expects( $this->any() ) - ->method( 'getUrl' ) + ->willReturn( $dataUriReturnValue ); + $image->method( 'getUrl' ) ->will( $this->returnValueMap( [ [ $context, 'load.php', null, 'original', 'original.svg' ], [ $context, 'load.php', null, 'rasterized', 'rasterized.png' ], diff --git a/tests/phpunit/includes/resourceloader/ResourceLoaderImageTest.php b/tests/phpunit/unit/includes/resourceloader/ResourceLoaderImageTest.php similarity index 84% rename from tests/phpunit/includes/resourceloader/ResourceLoaderImageTest.php rename to tests/phpunit/unit/includes/resourceloader/ResourceLoaderImageTest.php index c3fc55acc1..5265b3ee17 100644 --- a/tests/phpunit/includes/resourceloader/ResourceLoaderImageTest.php +++ b/tests/phpunit/unit/includes/resourceloader/ResourceLoaderImageTest.php @@ -3,13 +3,17 @@ /** * @group ResourceLoader */ -class ResourceLoaderImageTest extends ResourceLoaderTestCase { +class ResourceLoaderImageTest extends MediaWikiUnitTestCase { - protected $imagesPath; + private $imagesPath; protected function setUp() { parent::setUp(); - $this->imagesPath = __DIR__ . '/../../data/resourceloader'; + $this->imagesPath = __DIR__ . '/../../../data/resourceloader'; + } + + protected function tearDown() { + Language::$dataCache = null; } protected function getTestImage( $name ) { @@ -64,10 +68,18 @@ class ResourceLoaderImageTest extends ResourceLoaderTestCase { ]; $image = $this->getTestImage( $imageName ); - $context = $this->getResourceLoaderContext( [ - 'lang' => $languageCode, - 'dir' => $dirMap[$languageCode], - ] ); + $context = new DerivativeResourceLoaderContext( + $this->createMock( ResourceLoaderContext::class ) + ); + $context->setLanguage( $languageCode ); + $context->setDirection( $dirMap[$languageCode] ); + Language::$dataCache = $this->createMock( LocalisationCache::class ); + Language::$dataCache->method( 'getItem' )->will( $this->returnCallback( function ( $code ) { + return ( [ + 'en-gb' => [ 'en' ], + 'de-formal' => [ 'de' ], + ] )[ $code ] ?? []; + } ) ); $this->assertEquals( $image->getPath( $context ), $this->imagesPath . '/' . $path ); } @@ -93,7 +105,7 @@ class ResourceLoaderImageTest extends ResourceLoaderTestCase { * @covers ResourceLoaderImage::massageSvgPathdata */ public function testGetImageData() { - $context = $this->getResourceLoaderContext(); + $context = $this->createMock( ResourceLoaderContext::class ); $image = $this->getTestImage( 'def' ); $data = file_get_contents( $this->imagesPath . '/def.svg' ); -- 2.20.1