Debian Codegraph @

tests/phpunit/unit/includes/libs/StatusValueTest.php

raw symbols SCIP
1 <?php
2
3 namespace Wikimedia\Tests\Unit;
4
5 use MediaWiki\Message\Message;
6 use MediaWikiUnitTestCase;
7 use StatusValue;
8
9 /**
10 * @covers \StatusValue
11 */
12 class StatusValueTest extends MediaWikiUnitTestCase {
13
14 public function provideToString() {
15 yield [
16 true, null, null,
17 '<OK, no errors detected, no value set>',
18 'Empty, good state'
19 ];
20 yield [
21 false, null, null,
22 '<Error, no errors detected, no value set>',
23 'Empty, error state'
24 ];
25
26 yield [
27 true, 'TestValue', null,
28 '<OK, no errors detected, string value set>',
29 'Simple string, good state'
30 ];
31 yield [
32 false, 'TestValue', null,
33 '<Error, no errors detected, string value set>',
34 'Simple string, error state'
35 ];
36
37 yield [
38 true, 42, null,
39 '<OK, no errors detected, int value set>',
40 'Simple int, good state'
41 ];
42 yield [
43 false, 42, null,
44 '<Error, no errors detected, int value set>',
45 'Simple int, error state'
46 ];
47
48 yield [
49 true, [ 'TestValue' => false ], null,
50 '<OK, no errors detected, array value set>',
51 'Simple array, good state'
52 ];
53 yield [
54 false, [ 'TestValue' => false ], null,
55 '<Error, no errors detected, array value set>',
56 'Simple array, error state'
57 ];
58
59 $basicErrorReport = "\n"
60 . "+----------+---------------------------+--------------------------------------+\n"
61 . "| error | This is the error | |\n"
62 . "+----------+---------------------------+--------------------------------------+\n";
63
64 yield [
65 true, null, [ 'This is the error' ],
66 '<OK, collected 1 message(s) on the way, no value set>' . $basicErrorReport,
67 'Empty, string error, good state'
68 ];
69 yield [
70 false, null, [ 'This is the error' ],
71 '<Error, collected 1 message(s) on the way, no value set>' . $basicErrorReport,
72 'Empty, string error, error state'
73 ];
74
75 yield [
76 false, 'TestValue', [ 'This is the error' ],
77 '<Error, collected 1 message(s) on the way, string value set>' . $basicErrorReport,
78 'Simple string, string error, error state'
79 ];
80
81 yield [
82 false, 42, [ 'This is the error' ],
83 '<Error, collected 1 message(s) on the way, int value set>' . $basicErrorReport,
84 'Simple int, string error, error state'
85 ];
86
87 yield [
88 false, [ 'TestValue' => false ], [ 'This is the error' ],
89 '<Error, collected 1 message(s) on the way, array value set>' . $basicErrorReport,
90 'Simple array, string error, error state'
91 ];
92
93 yield [
94 false, null, [ [ 'message' => 'This is the error', 'params' => false ] ],
95 '<Error, collected 1 message(s) on the way, no value set>' . $basicErrorReport,
96 'Error with false value shows as no parameter'
97 ];
98
99 $specialErrorReport = "\n"
100 . "+----------+---------------------------+--------------------------------------+\n"
101 . "| error | This is the error | 1 |\n"
102 . "+----------+---------------------------+--------------------------------------+\n";
103 yield [
104 false, null, [ [ 'message' => 'This is the error', 'params' => true ] ],
105 '<Error, collected 1 message(s) on the way, no value set>' . $specialErrorReport,
106 'Error with true value shows as 1 int'
107 ];
108
109 $specialErrorReport = "\n"
110 . "+----------+---------------------------+--------------------------------------+\n"
111 . "| error | This is the error | 0 |\n"
112 . "+----------+---------------------------+--------------------------------------+\n";
113 yield [
114 false, null, [ [ 'message' => 'This is the error', 'params' => 0 ] ],
115 '<Error, collected 1 message(s) on the way, no value set>' . $specialErrorReport,
116 'Error with 0 int value'
117 ];
118
119 $specialErrorReport = "\n"
120 . "+----------+---------------------------+--------------------------------------+\n"
121 . "| error | This is the error | 42 |\n"
122 . "+----------+---------------------------+--------------------------------------+\n";
123 yield [
124 false, null, [ [ 'message' => 'This is the error', 'params' => 42 ] ],
125 '<Error, collected 1 message(s) on the way, no value set>' . $specialErrorReport,
126 'Error with 42 int value'
127 ];
128
129 $specialErrorReport = "\n"
130 . "+----------+---------------------------+--------------------------------------+\n"
131 . "| error | This is the error | TestValue |\n"
132 . "+----------+---------------------------+--------------------------------------+\n";
133 yield [
134 false, null, [ [ 'message' => 'This is the error', 'params' => 'TestValue' ] ],
135 '<Error, collected 1 message(s) on the way, no value set>' . $specialErrorReport,
136 'Error with a string parameter'
137 ];
138
139 $specialErrorReport = "\n"
140 . "+----------+---------------------------+--------------------------------------+\n"
141 . "| error | This is the error | [ TestValue, 42, 1, [ foo, baz ] ] |\n"
142 . "+----------+---------------------------+--------------------------------------+\n";
143 yield [
144 false, null, [
145 [ 'message' => 'This is the error', 'params' => [ 'TestValue', 42, true, [ 'foo', 'bar' => 'baz' ] ] ]
146 ],
147 '<Error, collected 1 message(s) on the way, no value set>' . $specialErrorReport,
148 'Error with an array of parameters'
149 ];
150
151 $multiErrorReport = "\n"
152 . "+----------+---------------------------+--------------------------------------+\n"
153 . "| error | Basic string parsing | Naïve string parsing |\n"
154 . "| error | Wrapped string | This is a longer input parameter and |\n"
155 . "| | | thus will wrap |\n"
156 . "| error | Multi-byte string | 캐나다∂는 북미에 있는 나라로 면적이 매우 넓습니다. |\n"
157 . "| error | Multi-byte wrapped string | 캐나다는 태평양에서 대서양까지, 북쪽으로는 북극과 접해 있는 북미 |\n"
158 . "| | | 의 큰 나라입니다. |\n"
159 . "+----------+---------------------------+--------------------------------------+\n";
160 yield [
161 false, null, [
162 [ 'message' => 'Basic string parsing', 'params' => 'Naïve string parsing' ],
163 [ 'message' => 'Wrapped string', 'params' => 'This is a longer input parameter and thus will wrap' ],
164 [ 'message' => 'Multi-byte string', 'params' => '캐나다∂는 북미에 있는 나라로 면적이 매우 넓습니다.' ],
165 [ 'message' => 'Multi-byte wrapped string', 'params' => '캐나다는 태평양에서 대서양까지, 북쪽으로는 북극과 접해 있는 북미의 큰 나라입니다.' ]
166 ],
167 '<Error, collected 4 message(s) on the way, no value set>' . $multiErrorReport,
168 'Three errors with different kinds of string parameters including long strings that are split when simple'
169 ];
170 }
171
172 /**
173 * @dataProvider provideToString
174 */
175 public function testToString( bool $sucess, $message, $errors, string $expected, string $testExplanation ) {
176 $status = StatusValue::newGood();
177
178 $status->setResult( $sucess, $message );
179
180 if ( isset( $errors ) ) {
181 foreach ( $errors as $key => $error ) {
182 if ( is_string( $error ) ) {
183 $status->error( $error );
184 } else {
185 $status->error( $error['message'], $error['params'] );
186
187 }
188 }
189 }
190
191 $this->assertEquals( $expected, $status->__toString(), $testExplanation );
192 }
193
194 public function testGetErrorsByType() {
195 $status = new StatusValue();
196 $warning = new Message( 'warning111' );
197 $error = new Message( 'error111' );
198 $status->warning( $warning );
199 $status->error( $error );
200
201 $this->assertCount( 2, $status->getErrors() );
202 $this->assertCount( 1, $status->getErrorsByType( 'warning' ) );
203 $this->assertCount( 1, $status->getErrorsByType( 'error' ) );
204 $this->assertEquals( $warning, $status->getErrorsByType( 'warning' )[0]['message'] );
205 $this->assertEquals( $error, $status->getErrorsByType( 'error' )[0]['message'] );
206
207 $this->assertCount( 2, $status->getMessages() );
208 $this->assertCount( 1, $status->getMessages( 'warning' ) );
209 $this->assertCount( 1, $status->getMessages( 'error' ) );
210 $this->assertEquals( 'warning111', $status->getMessages( 'warning' )[0]->getKey() );
211 $this->assertEquals( 'error111', $status->getMessages( 'error' )[0]->getKey() );
212 }
213 }