Add basic tests for ULS' ResourceLoader modules

This is a starting point to increase the test coverage in this code
base.

Change-Id: I215713e928e9b55615afd17ccf7a7082ef559e01
This commit is contained in:
Thiemo Kreuz
2018-12-13 22:38:03 +01:00
committed by jenkins-bot
parent f97a4df3bc
commit 24559cd1af
3 changed files with 118 additions and 0 deletions

View File

@@ -0,0 +1,35 @@
<?php
namespace UniversalLanguageSelector\Tests;
use ResourceLoaderULSJsonMessageModule;
/**
* @covers \ResourceLoaderULSJsonMessageModule
*
* @license GPL-2.0-or-later
* @author Thiemo Kreuz
*/
class ResourceLoaderULSJsonMessageModuleTest extends \PHPUnit\Framework\TestCase {
use \PHPUnit4And6Compat;
public function testAllReturnValues() {
$instance = new ResourceLoaderULSJsonMessageModule();
$context = $this->createMock( \ResourceLoaderContext::class );
$context->method( 'getLanguage' )
->willReturn( 'en' );
$this->assertContainsOnly( 'string', $instance->getDependencies(), 'dependencies' );
$this->assertContainsOnly( 'string', $instance->getTargets(), 'targets' );
$summary = $instance->getDefinitionSummary( $context );
$lastElement = end( $summary );
$this->assertArrayHasKey( 'fileHashes', $lastElement );
$this->assertContainsOnly( 'string', $lastElement['fileHashes'] );
$script = $instance->getScript( $context );
$this->assertStringStartsWith( 'mw.uls.loadLocalization("en",{"', $script );
}
}

View File

@@ -0,0 +1,29 @@
<?php
namespace UniversalLanguageSelector\Tests;
use ResourceLoaderULSModule;
/**
* @covers \ResourceLoaderULSModule
*
* @license GPL-2.0-or-later
* @author Thiemo Kreuz
*/
class ResourceLoaderULSModuleTest extends \PHPUnit\Framework\TestCase {
use \PHPUnit4And6Compat;
public function testAllReturnValues() {
$instance = new ResourceLoaderULSModule();
$context = $this->createMock( \ResourceLoaderContext::class );
$context->method( 'getLanguage' )
->willReturn( 'en' );
$script = $instance->getScript( $context );
$this->assertStringStartsWith( 'mw.config.set({"wgULSLanguages":{"', $script );
$this->assertTrue( $instance->enableModuleContentVersion() );
}
}

View File

@@ -0,0 +1,54 @@
<?php
namespace UniversalLanguageSelector\Tests;
use ULSJsonMessageLoader;
/**
* @covers \ULSJsonMessageLoader
*
* @license GPL-2.0-or-later
* @author Thiemo Kreuz
*/
class ULSJsonMessageLoaderTest extends \PHPUnit\Framework\TestCase {
use \PHPUnit4And6Compat;
public function testGetFilenamesWithBadInput() {
$instance = new ULSJsonMessageLoader();
$this->setExpectedException( \MWException::class );
$instance->getFilenames( null );
}
public function testGetMessagesWithBadInput() {
$instance = new ULSJsonMessageLoader();
$this->setExpectedException( \MWException::class );
$instance->getMessages( null );
}
public function testWithInvalidLanguageCode() {
$instance = new ULSJsonMessageLoader();
$languageCode = '0';
$filenames = $instance->getFilenames( $languageCode );
$this->assertSame( [], $filenames );
$messages = $instance->getMessages( $languageCode );
$this->assertSame( [], $messages );
}
public function testWithValidLanguageCode() {
$instance = new ULSJsonMessageLoader();
$languageCode = 'en';
$filenames = $instance->getFilenames( $languageCode );
$this->assertContainsOnly( 'string', $filenames );
$messages = $instance->getMessages( $languageCode );
unset( $messages['@metadata'] );
$this->assertContainsOnly( 'string', array_keys( $messages ) );
$this->assertContainsOnly( 'string', $messages );
}
}