wiki.techinc.nl/tests/phpunit/unit/includes/user/TempUser/LocalizedNumericSerialMappingTest.php
Doğu Abaris 591e52b67a test: Convert LocalizedNumericSerialMappingTest to a unit test
This commit refactors the LocalizedNumericSerialMappingTest to make
use of mocks for Language and LanguageFactory. Introduced a data
provider for comprehensive testing of getSerialIdForIndex with various
scenarios. Added testConstruct to verify constructor functionality
and LanguageFactory integration. This refactor changes the base test
class from MediaWikiIntegrationTestCase to MediaWikiUnitTestCase,
shifting focus to unit testing.

Change-Id: I00d80d0a1d6d0bb8f7fc5c6e7c6fa4732ef04abb
2024-02-09 10:57:19 -05:00

80 lines
1.9 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace MediaWiki\Tests\User\TempUser;
use Language;
use MediaWiki\Languages\LanguageFactory;
use MediaWiki\User\TempUser\LocalizedNumericSerialMapping;
use MediaWikiUnitTestCase;
/**
* @covers \MediaWiki\User\TempUser\LocalizedNumericSerialMapping
*/
class LocalizedNumericSerialMappingTest extends MediaWikiUnitTestCase {
protected Language $language;
protected array $config;
protected function setUp(): void {
$this->language = $this->createMock( Language::class );
$this->config = [ 'language' => 'ar' ];
}
public function testConstruct() {
$languageFactory = $this->createMock( LanguageFactory::class );
$languageFactory->expects( $this->once() )
->method( 'getLanguage' )
->with( 'ar' )
->willReturn( $this->language );
$map = new LocalizedNumericSerialMapping( $this->config, $languageFactory );
$this->assertInstanceOf( LocalizedNumericSerialMapping::class, $map );
}
/**
* Provide data for testGetSerialIdForIndex
* @return array
*/
public static function provideGetSerialIdForIndex(): array {
return [
[
[ 'language' => 'ar' ],
10,
'١٠',
],
[
[ 'language' => 'ar' ],
100,
'١٠٠',
],
[
[ 'language' => 'ar' ],
1000,
'١٠٠٠',
],
];
}
/**
* @dataProvider provideGetSerialIdForIndex
*
* @param array $config
* @param int $id
* @param string $expected
*/
public function testGetSerialIdForIndex( array $config, int $id, string $expected ) {
$languageFactory = $this->createMock( LanguageFactory::class );
$languageFactory->expects( $this->once() )
->method( 'getLanguage' )
->with( 'ar' )
->willReturn( $this->language );
$map = new LocalizedNumericSerialMapping( $config, $languageFactory );
$this->language->expects( $this->once() )
->method( 'formatNum' )
->with( $id )
->willReturn( $expected );
$this->assertSame( $expected, $map->getSerialIdForIndex( $id ) );
}
}