4 * @author Adam Shorland
6 class StatusTest
extends MediaWikiTestCase
{
8 public function testCanConstruct(){
10 $this->assertTrue( true );
14 * @dataProvider provideValues
15 * @covers Status::newGood
16 * @covers Status::getValue
17 * @covers Status::isGood
18 * @covers Status::isOK
20 public function testNewGood( $value = null ){
21 $status = Status
::newGood( $value );
22 $this->assertTrue( $status->isGood() );
23 $this->assertTrue( $status->isOK() );
24 $this->assertEquals( $value, $status->getValue() );
27 public static function provideValues(){
31 array( array( 'foo' => 'bar' ) ),
32 array( new Exception() ),
38 * @covers Status::newFatal
39 * @covers Status::isGood
40 * @covers Status::isOK
41 * @covers Status::getMessage
43 public function testNewFatalWithMessage() {
44 $message = $this->getMockBuilder( 'Message' )
45 ->disableOriginalConstructor()
48 $status = Status
::newFatal( $message );
49 $this->assertFalse( $status->isGood() );
50 $this->assertFalse( $status->isOK() );
51 $this->assertEquals( $message, $status->getMessage() );
55 * @covers Status::newFatal
56 * @covers Status::isGood
57 * @covers Status::isOK
58 * @covers Status::getMessage
60 public function testNewFatalWithString() {
62 $status = Status
::newFatal( $message );
63 $this->assertFalse( $status->isGood() );
64 $this->assertFalse( $status->isOK() );
65 $newMessage = $status->getMessage();
66 $this->assertEquals( $message, $newMessage->getKey() );
70 * @dataProvider provideSetResult
71 * @covers Status::getValue
72 * @covers Status::isOK
74 public function testSetResult( $ok, $value = null ) {
75 $status = new Status();
76 $status->setResult( $ok, $value );
77 $this->assertEquals( $ok, $status->isOK() );
78 $this->assertEquals( $value, $status->getValue() );
81 public static function provideSetResult() {
85 array( true, 'value' ),
86 array( false, 'value' ),
91 * @dataProvider provideMockMessageDetails
92 * @covers Status::warning
93 * @covers Status::getWarningsArray
95 public function testWarningWithMessage( $mockDetails ) {
96 $status = new Status();
97 $messages = $this->getMockMessages( $mockDetails );
99 foreach( $messages as $message ){
100 $status->warning( $message );
102 $warnings = $status->getWarningsArray();
104 $this->assertEquals( count( $messages ), count( $warnings ) );
105 foreach( $messages as $key => $message ) {
106 $expectedArray = array_merge( array( $message->getKey() ), $message->getParams() );
107 $this->assertEquals( $warnings[$key], $expectedArray );
112 * @dataProvider provideMockMessageDetails
113 * @covers Status::error
114 * @covers Status::getErrorsArray
116 public function testErrorWithMessage( $mockDetails ) {
117 $status = new Status();
118 $messages = $this->getMockMessages( $mockDetails );
120 foreach( $messages as $message ){
121 $status->error( $message );
123 $errors = $status->getErrorsArray();
125 $this->assertEquals( count( $messages ), count( $errors ) );
126 foreach( $messages as $key => $message ) {
127 $expectedArray = array_merge( array( $message->getKey() ), $message->getParams() );
128 $this->assertEquals( $errors[$key], $expectedArray );
132 protected function getMockMessage( $key = 'key', $params = array() ) {
133 $message = $this->getMockBuilder( 'Message' )
134 ->disableOriginalConstructor()
136 $message->expects( $this->atLeastOnce() )
138 ->will( $this->returnValue( $key ) );
139 $message->expects( $this->atLeastOnce() )
140 ->method( 'getParams' )
141 ->will( $this->returnValue( $params ) );
146 * @param array $messageDetails eg. array( 'KEY' => array(/PARAMS/) )
149 protected function getMockMessages( $messageDetails ){
151 foreach( $messageDetails as $key => $paramsArray ){
152 $messages[] = $this->getMockMessage( $key, $paramsArray );
157 public static function provideMockMessageDetails(){
159 array( array( 'key1' => array( 'foo' => 'bar' ) ) ),
160 array( array( 'key1' => array( 'foo' => 'bar' ), 'key2' => array( 'foo2' => 'bar2' ) ) ),
165 * @covers Status::merge
166 * @todo test merge with $overwriteValue true
168 public function testMerge(){
169 $status1 = new Status();
170 $status2 = new Status();
171 $message1 = $this->getMockMessage( 'warn1' );
172 $message2 = $this->getMockMessage( 'error2' );
173 $status1->warning( $message1 );
174 $status2->error( $message2 );
176 $status1->merge( $status2 );
177 $this->assertEquals( 2, count( $status1->getWarningsArray() ) +
count( $status1->getErrorsArray() ) );
181 * @covers Status::hasMessage
183 public function testHasMessage() {
184 $status = new Status();
185 $status->fatal( 'bad' );
186 $this->assertTrue( $status->hasMessage( 'bad' ) );
187 $this->assertFalse( $status->hasMessage( 'good' ) );
191 //todo test cleanParams
192 //todo test getWikiText
193 //todo test getMessage
194 //todo test getErrorMessage
196 //todo test getErrorMessageArray
197 //todo test getStatusArray
198 //todo test getErrorsByType
199 //todo test replaceMessage
200 //todo test replaceMessage