Debian Codegraph @

tests/phpunit/unit/includes/language/LCStoreStaticArrayTest.php

raw symbols SCIP
1 <?php
2
3 /**
4 * @covers \LCStoreStaticArray
5 * @group Language
6 */
7 class LCStoreStaticArrayTest extends MediaWikiUnitTestCase {
8 /** @var string */
9 private $dir;
10 /** @var string */
11 private $file;
12
13 protected function setUp(): void {
14 parent::setUp();
15 $this->dir = sys_get_temp_dir() . '/lcstore-array';
16 $this->file = $this->dir . '/en.l10n.php';
17 }
18
19 protected function tearDown(): void {
20 @unlink( $this->file );
21 @rmdir( $this->dir );
22 parent::tearDown();
23 }
24
25 private function prepareDir() {
26 @mkdir( $this->dir );
27 return $this->dir;
28 }
29
30 private function prepareFile( $dir, $lang, array $data ) {
31 file_put_contents(
32 $this->file,
33 '<?php return ' . var_export( $data, true ) . ';'
34 );
35 }
36
37 public function testEncodeDecode() {
38 $dir = $this->prepareDir();
39 $cache = new LCStoreStaticArray( [ 'directory' => $dir ] );
40
41 $data = [
42 'mr-boole' => false,
43 'the-zero' => 0,
44 'a-number' => 42,
45 'some-string' => '0',
46 'common-data' => [
47 3 => [ 'three', 'threa', 'phree' ],
48 6 => [ 'six', 'seaks', 'phrix' ],
49 ],
50 'some-obj' => new DateTimeZone( '-0630' ),
51 'unlikely' => [
52 3 => 'three',
53 6 => new DateTimeZone( '-0315' ),
54 ],
55 ];
56 $this->file = $dir . '/en.l10n.php';
57 $cache->startWrite( 'en' );
58 foreach ( $data as $key => $value ) {
59 $cache->set( $key, $value );
60 }
61 $cache->finishWrite();
62
63 $this->assertEquals(
64 [
65 'mr-boole' => false,
66 'the-zero' => 0,
67 'a-number' => 42,
68 'some-string' => '0',
69 'common-data' => [
70 'v',
71 [
72 3 => [ 'three', 'threa', 'phree' ],
73 6 => [ 'six', 'seaks', 'phrix' ],
74 ]
75 ],
76 'some-obj' => [
77 's',
78 'O:12:"DateTimeZone":2:{s:13:"timezone_type";i:1;s:8:"timezone";s:6:"-06:30";}'
79 ],
80 'unlikely' => [
81 's',
82 '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";}}'
83 ],
84 ],
85 require $dir . '/en.l10n.php',
86 'Encoded data'
87 );
88
89 $this->assertSame( false, $cache->get( 'en', 'mr-boole' ), 'decode boolean' );
90 $this->assertSame( 0, $cache->get( 'en', 'the-zero' ), 'decode number' );
91 $this->assertSame( '0', $cache->get( 'en', 'some-string' ), 'decode string' );
92 $this->assertSame( [ 'six', 'seaks', 'phrix' ], $cache->get( 'en', 'common-data' )[6], 'decode array' );
93 $this->assertInstanceOf(
94 DateTimeZone::class,
95 $cache->get( 'en', 'some-obj' ),
96 'decode object'
97 );
98 $this->assertInstanceOf(
99 DateTimeZone::class,
100 $cache->get( 'en', 'unlikely' )[6],
101 'decode nested object'
102 );
103 }
104
105 public function testDecodeMw132Value() {
106 $dir = $this->prepareDir();
107 $this->prepareFile( $dir, 'en', [
108 'mr-boole' => [ 'v', false ],
109 'the-zero' => [ 'v', 0 ],
110 'a-number' => [ 'v', 42 ],
111 'some-string' => [ 'v', '0' ],
112 ] );
113 $cache = new LCStoreStaticArray( [ 'directory' => $dir ] );
114
115 $this->assertSame( false, $cache->get( 'en', 'mr-boole' ) );
116 $this->assertSame( 0, $cache->get( 'en', 'the-zero' ) );
117 $this->assertSame( '0', $cache->get( 'en', 'some-string' ) );
118 $this->assertSame( 42, $cache->get( 'en', 'a-number' ) );
119 }
120 }