wiki.techinc.nl/tests/phpunit/unit/includes/language/LCStoreStaticArrayTest.php
Umherirrender eb28edd84e language: Add missing documentation to class properties
Add doc-typehints to class properties found by the PropertyDocumentation
sniff to improve the documentation.

Once the sniff is enabled it avoids that new code is missing type
declarations. This is focused on documentation and does not change code.

Change-Id: I73c413727a63d14efb5028b27128784c98724110
2024-09-17 18:25:25 +00:00

120 lines
3 KiB
PHP

<?php
/**
* @covers \LCStoreStaticArray
* @group Language
*/
class LCStoreStaticArrayTest extends MediaWikiUnitTestCase {
/** @var string */
private $dir;
/** @var string */
private $file;
protected function setUp(): void {
parent::setUp();
$this->dir = sys_get_temp_dir() . '/lcstore-array';
$this->file = $this->dir . '/en.l10n.php';
}
protected function tearDown(): void {
@unlink( $this->file );
@rmdir( $this->dir );
parent::tearDown();
}
private function prepareDir() {
@mkdir( $this->dir );
return $this->dir;
}
private function prepareFile( $dir, $lang, array $data ) {
file_put_contents(
$this->file,
'<?php return ' . var_export( $data, true ) . ';'
);
}
public function testEncodeDecode() {
$dir = $this->prepareDir();
$cache = new LCStoreStaticArray( [ 'directory' => $dir ] );
$data = [
'mr-boole' => false,
'the-zero' => 0,
'a-number' => 42,
'some-string' => '0',
'common-data' => [
3 => [ 'three', 'threa', 'phree' ],
6 => [ 'six', 'seaks', 'phrix' ],
],
'some-obj' => new DateTimeZone( '-0630' ),
'unlikely' => [
3 => 'three',
6 => new DateTimeZone( '-0315' ),
],
];
$this->file = $dir . '/en.l10n.php';
$cache->startWrite( 'en' );
foreach ( $data as $key => $value ) {
$cache->set( $key, $value );
}
$cache->finishWrite();
$this->assertEquals(
[
'mr-boole' => false,
'the-zero' => 0,
'a-number' => 42,
'some-string' => '0',
'common-data' => [
'v',
[
3 => [ 'three', 'threa', 'phree' ],
6 => [ 'six', 'seaks', 'phrix' ],
]
],
'some-obj' => [
's',
'O:12:"DateTimeZone":2:{s:13:"timezone_type";i:1;s:8:"timezone";s:6:"-06:30";}'
],
'unlikely' => [
's',
'a:2:{i:3;s:5:"three";i:6;O:12:"DateTimeZone":2:{s:13:"timezone_type";i:1;s:8:"timezone";s:6:"-03:15";}}'
],
],
require $dir . '/en.l10n.php',
'Encoded data'
);
$this->assertSame( false, $cache->get( 'en', 'mr-boole' ), 'decode boolean' );
$this->assertSame( 0, $cache->get( 'en', 'the-zero' ), 'decode number' );
$this->assertSame( '0', $cache->get( 'en', 'some-string' ), 'decode string' );
$this->assertSame( [ 'six', 'seaks', 'phrix' ], $cache->get( 'en', 'common-data' )[6], 'decode array' );
$this->assertInstanceOf(
DateTimeZone::class,
$cache->get( 'en', 'some-obj' ),
'decode object'
);
$this->assertInstanceOf(
DateTimeZone::class,
$cache->get( 'en', 'unlikely' )[6],
'decode nested object'
);
}
public function testDecodeMw132Value() {
$dir = $this->prepareDir();
$this->prepareFile( $dir, 'en', [
'mr-boole' => [ 'v', false ],
'the-zero' => [ 'v', 0 ],
'a-number' => [ 'v', 42 ],
'some-string' => [ 'v', '0' ],
] );
$cache = new LCStoreStaticArray( [ 'directory' => $dir ] );
$this->assertSame( false, $cache->get( 'en', 'mr-boole' ) );
$this->assertSame( 0, $cache->get( 'en', 'the-zero' ) );
$this->assertSame( '0', $cache->get( 'en', 'some-string' ) );
$this->assertSame( 42, $cache->get( 'en', 'a-number' ) );
}
}