4 * @author Adam Shorland
6 class StatusTest
extends MediaWikiTestCase
{
8 public function testCanConstruct() {
10 $this->assertTrue( true );
14 * @dataProvider provideValues
15 * @covers Status::newGood
17 public function testNewGood( $value = null ) {
18 $status = Status
::newGood( $value );
19 $this->assertTrue( $status->isGood() );
20 $this->assertTrue( $status->isOK() );
21 $this->assertEquals( $value, $status->getValue() );
24 public static function provideValues() {
28 array( array( 'foo' => 'bar' ) ),
29 array( new Exception() ),
35 * @covers Status::newFatal
37 public function testNewFatalWithMessage() {
38 $message = $this->getMockBuilder( 'Message' )
39 ->disableOriginalConstructor()
42 $status = Status
::newFatal( $message );
43 $this->assertFalse( $status->isGood() );
44 $this->assertFalse( $status->isOK() );
45 $this->assertEquals( $message, $status->getMessage() );
49 * @covers Status::newFatal
51 public function testNewFatalWithString() {
53 $status = Status
::newFatal( $message );
54 $this->assertFalse( $status->isGood() );
55 $this->assertFalse( $status->isOK() );
56 $this->assertEquals( $message, $status->getMessage()->getKey() );
60 * @dataProvider provideSetResult
61 * @covers Status::setResult
63 public function testSetResult( $ok, $value = null ) {
64 $status = new Status();
65 $status->setResult( $ok, $value );
66 $this->assertEquals( $ok, $status->isOK() );
67 $this->assertEquals( $value, $status->getValue() );
70 public static function provideSetResult() {
74 array( true, 'value' ),
75 array( false, 'value' ),
80 * @dataProvider provideIsOk
81 * @covers Status::isOk
83 public function testIsOk( $ok ) {
84 $status = new Status();
86 $this->assertEquals( $ok, $status->isOK() );
89 public static function provideIsOk() {
97 * @covers Status::getValue
99 public function testGetValue() {
100 $status = new Status();
101 $status->value
= 'foobar';
102 $this->assertEquals( 'foobar', $status->getValue() );
106 * @dataProvider provideIsGood
107 * @covers Status::isGood
109 public function testIsGood( $ok, $errors, $expected ) {
110 $status = new Status();
112 $status->errors
= $errors;
113 $this->assertEquals( $expected, $status->isGood() );
116 public static function provideIsGood() {
118 array( true, array(), true ),
119 array( true, array( 'foo' ), false ),
120 array( false, array(), false ),
121 array( false, array( 'foo' ), false ),
126 * @dataProvider provideMockMessageDetails
127 * @covers Status::warning
128 * @covers Status::getWarningsArray
130 public function testWarningWithMessage( $mockDetails ) {
131 $status = new Status();
132 $messages = $this->getMockMessages( $mockDetails );
134 foreach ( $messages as $message ) {
135 $status->warning( $message );
137 $warnings = $status->getWarningsArray();
139 $this->assertEquals( count( $messages ), count( $warnings ) );
140 foreach ( $messages as $key => $message ) {
141 $expectedArray = array_merge( array( $message->getKey() ), $message->getParams() );
142 $this->assertEquals( $warnings[$key], $expectedArray );
147 * @dataProvider provideMockMessageDetails
148 * @covers Status::error
149 * @covers Status::getErrorsArray
151 public function testErrorWithMessage( $mockDetails ) {
152 $status = new Status();
153 $messages = $this->getMockMessages( $mockDetails );
155 foreach ( $messages as $message ) {
156 $status->error( $message );
158 $errors = $status->getErrorsArray();
160 $this->assertEquals( count( $messages ), count( $errors ) );
161 foreach ( $messages as $key => $message ) {
162 $expectedArray = array_merge( array( $message->getKey() ), $message->getParams() );
163 $this->assertEquals( $errors[$key], $expectedArray );
167 protected function getMockMessage( $key = 'key', $params = array() ) {
168 $message = $this->getMockBuilder( 'Message' )
169 ->disableOriginalConstructor()
171 $message->expects( $this->atLeastOnce() )
173 ->will( $this->returnValue( $key ) );
174 $message->expects( $this->atLeastOnce() )
175 ->method( 'getParams' )
176 ->will( $this->returnValue( $params ) );
181 * @param array $messageDetails eg. array( 'KEY' => array(/PARAMS/) )
184 protected function getMockMessages( $messageDetails ) {
186 foreach ( $messageDetails as $key => $paramsArray ) {
187 $messages[] = $this->getMockMessage( $key, $paramsArray );
192 public static function provideMockMessageDetails() {
194 array( array( 'key1' => array( 'foo' => 'bar' ) ) ),
195 array( array( 'key1' => array( 'foo' => 'bar' ), 'key2' => array( 'foo2' => 'bar2' ) ) ),
200 * @covers Status::merge
201 * @todo test merge with $overwriteValue true
203 public function testMerge() {
204 $status1 = new Status();
205 $status2 = new Status();
206 $message1 = $this->getMockMessage( 'warn1' );
207 $message2 = $this->getMockMessage( 'error2' );
208 $status1->warning( $message1 );
209 $status2->error( $message2 );
211 $status1->merge( $status2 );
212 $this->assertEquals( 2, count( $status1->getWarningsArray() ) +
count( $status1->getErrorsArray() ) );
216 * @covers Status::hasMessage
218 public function testHasMessage() {
219 $status = new Status();
220 $status->fatal( 'bad' );
221 $this->assertTrue( $status->hasMessage( 'bad' ) );
222 $this->assertFalse( $status->hasMessage( 'good' ) );
226 * @dataProvider provideCleanParams
227 * @covers Status::cleanParams
229 public function testCleanParams( $cleanCallback, $params, $expected ) {
230 $method = new ReflectionMethod( 'Status', 'cleanParams' );
231 $method->setAccessible( true );
232 $status = new Status();
233 $status->cleanCallback
= $cleanCallback;
235 $this->assertEquals( $expected, $method->invoke( $status, $params ) );
239 * @todo test cleanParams with a callback
241 public static function provideCleanParams() {
243 array( false, array( 'foo' => 'bar' ), array( 'foo' => 'bar' ) ),
248 * @dataProvider provideGetWikiText
249 * @covers Status::getWikiText
250 * @todo test long and short context messages generated through this method
251 * this can not really be done now due to use of wfMessage()->plain()
252 * It is possible to mock such methods but only if namespaces are used
254 public function testGetWikiText( Status
$status, $expected ) {
255 $this->assertEquals( $expected, $status->getWikiText() );
259 * @return array of arrays with values;
261 * 1 => expected string (with no context)
263 public static function provideGetWikiText() {
264 $testCases = array();
266 $testCases[ 'GoodStatus' ] = array(
268 "Internal error: Status::getWikiText called for a good result, this is incorrect\n",
271 $status = new Status();
273 $testCases[ 'GoodButNoError' ] = array(
275 "Internal error: Status::getWikiText: Invalid result object: no error text but not OK\n",
278 $status = new Status();
279 $status->warning( 'fooBar!' );
280 $testCases[ '1StringWarning' ] = array(
285 $status = new Status();
286 $status->warning( 'fooBar!' );
287 $status->warning( 'fooBar2!' );
288 $testCases[ '2StringWarnings' ] = array(
290 "* <fooBar!>\n* <fooBar2!>\n",
293 $status = new Status();
294 $status->warning( new Message( 'fooBar!', array( 'foo', 'bar' ) ) );
295 $testCases[ '1MessageWarning' ] = array(
300 $status = new Status();
301 $status->warning( new Message( 'fooBar!', array( 'foo', 'bar' ) ) );
302 $status->warning( new Message( 'fooBar2!' ) );
303 $testCases[ '2MessageWarnings' ] = array(
305 "* <fooBar!>\n* <fooBar2!>\n",
311 //todo test getMessage
312 //todo test getErrorMessage
314 //todo test getErrorMessageArray
315 //todo test getStatusArray
316 //todo test getErrorsByType
317 //todo test replaceMessage