Test for Status::hasMessage
[lhc/web/wiklou.git] / tests / phpunit / includes / StatusTest.php
1 <?php
2
3 /**
4 * @author Adam Shorland
5 */
6 class StatusTest extends MediaWikiTestCase {
7
8 public function testCanConstruct(){
9 new Status();
10 $this->assertTrue( true );
11 }
12
13 /**
14 * @dataProvider provideValues
15 * @covers Status::newGood
16 * @covers Status::getValue
17 * @covers Status::isGood
18 * @covers Status::isOK
19 */
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() );
25 }
26
27 public static function provideValues(){
28 return array(
29 array(),
30 array( 'foo' ),
31 array( array( 'foo' => 'bar' ) ),
32 array( new Exception() ),
33 array( 1234 ),
34 );
35 }
36
37 /**
38 * @covers Status::newFatal
39 * @covers Status::isGood
40 * @covers Status::isOK
41 * @covers Status::getMessage
42 */
43 public function testNewFatalWithMessage() {
44 $message = $this->getMockBuilder( 'Message' )
45 ->disableOriginalConstructor()
46 ->getMock();
47
48 $status = Status::newFatal( $message );
49 $this->assertFalse( $status->isGood() );
50 $this->assertFalse( $status->isOK() );
51 $this->assertEquals( $message, $status->getMessage() );
52 }
53
54 /**
55 * @covers Status::newFatal
56 * @covers Status::isGood
57 * @covers Status::isOK
58 * @covers Status::getMessage
59 */
60 public function testNewFatalWithString() {
61 $message = 'foo';
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() );
67 }
68
69 /**
70 * @dataProvider provideSetResult
71 * @covers Status::getValue
72 * @covers Status::isOK
73 */
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() );
79 }
80
81 public static function provideSetResult() {
82 return array(
83 array( true ),
84 array( false ),
85 array( true, 'value' ),
86 array( false, 'value' ),
87 );
88 }
89
90 /**
91 * @dataProvider provideMockMessageDetails
92 * @covers Status::warning
93 * @covers Status::getWarningsArray
94 */
95 public function testWarningWithMessage( $mockDetails ) {
96 $status = new Status();
97 $messages = $this->getMockMessages( $mockDetails );
98
99 foreach( $messages as $message ){
100 $status->warning( $message );
101 }
102 $warnings = $status->getWarningsArray();
103
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 );
108 }
109 }
110
111 /**
112 * @dataProvider provideMockMessageDetails
113 * @covers Status::error
114 * @covers Status::getErrorsArray
115 */
116 public function testErrorWithMessage( $mockDetails ) {
117 $status = new Status();
118 $messages = $this->getMockMessages( $mockDetails );
119
120 foreach( $messages as $message ){
121 $status->error( $message );
122 }
123 $errors = $status->getErrorsArray();
124
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 );
129 }
130 }
131
132 protected function getMockMessage( $key = 'key', $params = array() ) {
133 $message = $this->getMockBuilder( 'Message' )
134 ->disableOriginalConstructor()
135 ->getMock();
136 $message->expects( $this->atLeastOnce() )
137 ->method( 'getKey' )
138 ->will( $this->returnValue( $key ) );
139 $message->expects( $this->atLeastOnce() )
140 ->method( 'getParams' )
141 ->will( $this->returnValue( $params ) );
142 return $message;
143 }
144
145 /**
146 * @param array $messageDetails eg. array( 'KEY' => array(/PARAMS/) )
147 * @return Message[]
148 */
149 protected function getMockMessages( $messageDetails ){
150 $messages = array();
151 foreach( $messageDetails as $key => $paramsArray ){
152 $messages[] = $this->getMockMessage( $key, $paramsArray );
153 }
154 return $messages;
155 }
156
157 public static function provideMockMessageDetails(){
158 return array(
159 array( array( 'key1' => array( 'foo' => 'bar' ) ) ),
160 array( array( 'key1' => array( 'foo' => 'bar' ), 'key2' => array( 'foo2' => 'bar2' ) ) ),
161 );
162 }
163
164 /**
165 * @covers Status::merge
166 * @todo test merge with $overwriteValue true
167 */
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 );
175
176 $status1->merge( $status2 );
177 $this->assertEquals( 2, count( $status1->getWarningsArray() ) + count( $status1->getErrorsArray() ) );
178 }
179
180 /**
181 * @covers Status::hasMessage
182 */
183 public function testHasMessage() {
184 $status = new Status();
185 $status->fatal( 'bad' );
186 $this->assertTrue( $status->hasMessage( 'bad' ) );
187 $this->assertFalse( $status->hasMessage( 'good' ) );
188
189 }
190
191 //todo test cleanParams
192 //todo test getWikiText
193 //todo test getMessage
194 //todo test getErrorMessage
195 //todo test getHTML
196 //todo test getErrorMessageArray
197 //todo test getStatusArray
198 //todo test getErrorsByType
199 //todo test replaceMessage
200 //todo test replaceMessage
201
202 }