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