2013-10-24 22:11:15 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @author Adam Shorland
|
|
|
|
|
*/
|
2013-11-25 20:19:23 +00:00
|
|
|
class StatusTest extends MediaWikiLangTestCase {
|
2013-10-24 22:11:15 +00:00
|
|
|
|
2013-11-19 18:03:54 +00:00
|
|
|
public function testCanConstruct() {
|
2013-10-24 22:11:15 +00:00
|
|
|
new Status();
|
|
|
|
|
$this->assertTrue( true );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider provideValues
|
|
|
|
|
* @covers Status::newGood
|
|
|
|
|
*/
|
2013-11-19 18:03:54 +00:00
|
|
|
public function testNewGood( $value = null ) {
|
2013-10-24 22:11:15 +00:00
|
|
|
$status = Status::newGood( $value );
|
|
|
|
|
$this->assertTrue( $status->isGood() );
|
|
|
|
|
$this->assertTrue( $status->isOK() );
|
|
|
|
|
$this->assertEquals( $value, $status->getValue() );
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-19 18:03:54 +00:00
|
|
|
public static function provideValues() {
|
2013-10-24 22:11:15 +00:00
|
|
|
return array(
|
|
|
|
|
array(),
|
|
|
|
|
array( 'foo' ),
|
|
|
|
|
array( array( 'foo' => 'bar' ) ),
|
|
|
|
|
array( new Exception() ),
|
|
|
|
|
array( 1234 ),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2013-10-25 01:12:47 +00:00
|
|
|
* @covers Status::newFatal
|
2013-10-24 22:11:15 +00:00
|
|
|
*/
|
|
|
|
|
public function testNewFatalWithMessage() {
|
|
|
|
|
$message = $this->getMockBuilder( 'Message' )
|
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
|
|
$status = Status::newFatal( $message );
|
|
|
|
|
$this->assertFalse( $status->isGood() );
|
|
|
|
|
$this->assertFalse( $status->isOK() );
|
|
|
|
|
$this->assertEquals( $message, $status->getMessage() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2013-10-25 01:12:47 +00:00
|
|
|
* @covers Status::newFatal
|
2013-10-24 22:11:15 +00:00
|
|
|
*/
|
|
|
|
|
public function testNewFatalWithString() {
|
|
|
|
|
$message = 'foo';
|
|
|
|
|
$status = Status::newFatal( $message );
|
|
|
|
|
$this->assertFalse( $status->isGood() );
|
|
|
|
|
$this->assertFalse( $status->isOK() );
|
2013-11-15 15:43:42 +00:00
|
|
|
$this->assertEquals( $message, $status->getMessage()->getKey() );
|
2013-10-24 22:11:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider provideSetResult
|
2013-11-15 15:43:42 +00:00
|
|
|
* @covers Status::setResult
|
2013-10-24 22:11:15 +00:00
|
|
|
*/
|
|
|
|
|
public function testSetResult( $ok, $value = null ) {
|
|
|
|
|
$status = new Status();
|
|
|
|
|
$status->setResult( $ok, $value );
|
|
|
|
|
$this->assertEquals( $ok, $status->isOK() );
|
|
|
|
|
$this->assertEquals( $value, $status->getValue() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function provideSetResult() {
|
|
|
|
|
return array(
|
|
|
|
|
array( true ),
|
|
|
|
|
array( false ),
|
|
|
|
|
array( true, 'value' ),
|
|
|
|
|
array( false, 'value' ),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-15 15:43:42 +00:00
|
|
|
/**
|
|
|
|
|
* @dataProvider provideIsOk
|
|
|
|
|
* @covers Status::isOk
|
|
|
|
|
*/
|
|
|
|
|
public function testIsOk( $ok ) {
|
|
|
|
|
$status = new Status();
|
|
|
|
|
$status->ok = $ok;
|
|
|
|
|
$this->assertEquals( $ok, $status->isOK() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function provideIsOk() {
|
|
|
|
|
return array(
|
|
|
|
|
array( true ),
|
|
|
|
|
array( false ),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers Status::getValue
|
|
|
|
|
*/
|
|
|
|
|
public function testGetValue() {
|
|
|
|
|
$status = new Status();
|
|
|
|
|
$status->value = 'foobar';
|
|
|
|
|
$this->assertEquals( 'foobar', $status->getValue() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider provideIsGood
|
|
|
|
|
* @covers Status::isGood
|
|
|
|
|
*/
|
|
|
|
|
public function testIsGood( $ok, $errors, $expected ) {
|
|
|
|
|
$status = new Status();
|
|
|
|
|
$status->ok = $ok;
|
|
|
|
|
$status->errors = $errors;
|
|
|
|
|
$this->assertEquals( $expected, $status->isGood() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function provideIsGood() {
|
|
|
|
|
return array(
|
|
|
|
|
array( true, array(), true ),
|
|
|
|
|
array( true, array( 'foo' ), false ),
|
|
|
|
|
array( false, array(), false ),
|
|
|
|
|
array( false, array( 'foo' ), false ),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-24 22:11:15 +00:00
|
|
|
/**
|
|
|
|
|
* @dataProvider provideMockMessageDetails
|
|
|
|
|
* @covers Status::warning
|
|
|
|
|
* @covers Status::getWarningsArray
|
2013-11-19 18:12:16 +00:00
|
|
|
* @covers Status::getStatusArray
|
2013-10-24 22:11:15 +00:00
|
|
|
*/
|
|
|
|
|
public function testWarningWithMessage( $mockDetails ) {
|
|
|
|
|
$status = new Status();
|
|
|
|
|
$messages = $this->getMockMessages( $mockDetails );
|
|
|
|
|
|
2013-11-19 18:03:54 +00:00
|
|
|
foreach ( $messages as $message ) {
|
2013-10-24 22:11:15 +00:00
|
|
|
$status->warning( $message );
|
|
|
|
|
}
|
|
|
|
|
$warnings = $status->getWarningsArray();
|
|
|
|
|
|
|
|
|
|
$this->assertEquals( count( $messages ), count( $warnings ) );
|
2013-11-19 18:03:54 +00:00
|
|
|
foreach ( $messages as $key => $message ) {
|
2013-10-24 22:11:15 +00:00
|
|
|
$expectedArray = array_merge( array( $message->getKey() ), $message->getParams() );
|
|
|
|
|
$this->assertEquals( $warnings[$key], $expectedArray );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider provideMockMessageDetails
|
|
|
|
|
* @covers Status::error
|
|
|
|
|
* @covers Status::getErrorsArray
|
2013-11-19 18:12:16 +00:00
|
|
|
* @covers Status::getStatusArray
|
2013-10-24 22:11:15 +00:00
|
|
|
*/
|
|
|
|
|
public function testErrorWithMessage( $mockDetails ) {
|
|
|
|
|
$status = new Status();
|
|
|
|
|
$messages = $this->getMockMessages( $mockDetails );
|
|
|
|
|
|
2013-11-19 18:03:54 +00:00
|
|
|
foreach ( $messages as $message ) {
|
2013-10-24 22:11:15 +00:00
|
|
|
$status->error( $message );
|
|
|
|
|
}
|
|
|
|
|
$errors = $status->getErrorsArray();
|
|
|
|
|
|
|
|
|
|
$this->assertEquals( count( $messages ), count( $errors ) );
|
2013-11-19 18:03:54 +00:00
|
|
|
foreach ( $messages as $key => $message ) {
|
2013-10-24 22:11:15 +00:00
|
|
|
$expectedArray = array_merge( array( $message->getKey() ), $message->getParams() );
|
|
|
|
|
$this->assertEquals( $errors[$key], $expectedArray );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-25 15:11:57 +00:00
|
|
|
/**
|
|
|
|
|
* @dataProvider provideMockMessageDetails
|
|
|
|
|
* @covers Status::fatal
|
|
|
|
|
* @covers Status::getErrorsArray
|
|
|
|
|
* @covers Status::getStatusArray
|
|
|
|
|
*/
|
|
|
|
|
public function testFatalWithMessage( $mockDetails ) {
|
|
|
|
|
$status = new Status();
|
|
|
|
|
$messages = $this->getMockMessages( $mockDetails );
|
|
|
|
|
|
|
|
|
|
foreach ( $messages as $message ) {
|
|
|
|
|
$status->fatal( $message );
|
|
|
|
|
}
|
|
|
|
|
$errors = $status->getErrorsArray();
|
|
|
|
|
|
|
|
|
|
$this->assertEquals( count( $messages ), count( $errors ) );
|
|
|
|
|
foreach ( $messages as $key => $message ) {
|
|
|
|
|
$expectedArray = array_merge( array( $message->getKey() ), $message->getParams() );
|
|
|
|
|
$this->assertEquals( $errors[$key], $expectedArray );
|
|
|
|
|
}
|
|
|
|
|
$this->assertFalse( $status->isOK() );
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-24 22:11:15 +00:00
|
|
|
protected function getMockMessage( $key = 'key', $params = array() ) {
|
|
|
|
|
$message = $this->getMockBuilder( 'Message' )
|
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
|
->getMock();
|
|
|
|
|
$message->expects( $this->atLeastOnce() )
|
|
|
|
|
->method( 'getKey' )
|
|
|
|
|
->will( $this->returnValue( $key ) );
|
|
|
|
|
$message->expects( $this->atLeastOnce() )
|
|
|
|
|
->method( 'getParams' )
|
|
|
|
|
->will( $this->returnValue( $params ) );
|
|
|
|
|
return $message;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param array $messageDetails eg. array( 'KEY' => array(/PARAMS/) )
|
|
|
|
|
* @return Message[]
|
|
|
|
|
*/
|
2013-11-19 18:03:54 +00:00
|
|
|
protected function getMockMessages( $messageDetails ) {
|
2013-10-24 22:11:15 +00:00
|
|
|
$messages = array();
|
2013-11-19 18:03:54 +00:00
|
|
|
foreach ( $messageDetails as $key => $paramsArray ) {
|
2013-10-24 22:11:15 +00:00
|
|
|
$messages[] = $this->getMockMessage( $key, $paramsArray );
|
|
|
|
|
}
|
|
|
|
|
return $messages;
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-19 18:03:54 +00:00
|
|
|
public static function provideMockMessageDetails() {
|
2013-10-24 22:11:15 +00:00
|
|
|
return array(
|
|
|
|
|
array( array( 'key1' => array( 'foo' => 'bar' ) ) ),
|
|
|
|
|
array( array( 'key1' => array( 'foo' => 'bar' ), 'key2' => array( 'foo2' => 'bar2' ) ) ),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers Status::merge
|
|
|
|
|
*/
|
2013-11-19 18:03:54 +00:00
|
|
|
public function testMerge() {
|
2013-10-24 22:11:15 +00:00
|
|
|
$status1 = new Status();
|
|
|
|
|
$status2 = new Status();
|
|
|
|
|
$message1 = $this->getMockMessage( 'warn1' );
|
|
|
|
|
$message2 = $this->getMockMessage( 'error2' );
|
|
|
|
|
$status1->warning( $message1 );
|
|
|
|
|
$status2->error( $message2 );
|
|
|
|
|
|
|
|
|
|
$status1->merge( $status2 );
|
|
|
|
|
$this->assertEquals( 2, count( $status1->getWarningsArray() ) + count( $status1->getErrorsArray() ) );
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-25 15:08:49 +00:00
|
|
|
/**
|
|
|
|
|
* @covers Status::merge
|
|
|
|
|
*/
|
|
|
|
|
public function testMergeWithOverwriteValue() {
|
|
|
|
|
$status1 = new Status();
|
|
|
|
|
$status2 = new Status();
|
|
|
|
|
$message1 = $this->getMockMessage( 'warn1' );
|
|
|
|
|
$message2 = $this->getMockMessage( 'error2' );
|
|
|
|
|
$status1->warning( $message1 );
|
|
|
|
|
$status2->error( $message2 );
|
|
|
|
|
$status2->value = 'FooValue';
|
|
|
|
|
|
|
|
|
|
$status1->merge( $status2, true );
|
|
|
|
|
$this->assertEquals( 2, count( $status1->getWarningsArray() ) + count( $status1->getErrorsArray() ) );
|
|
|
|
|
$this->assertEquals( 'FooValue', $status1->getValue() );
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-26 00:52:46 +00:00
|
|
|
/**
|
|
|
|
|
* @covers Status::hasMessage
|
|
|
|
|
*/
|
|
|
|
|
public function testHasMessage() {
|
|
|
|
|
$status = new Status();
|
|
|
|
|
$status->fatal( 'bad' );
|
|
|
|
|
$this->assertTrue( $status->hasMessage( 'bad' ) );
|
|
|
|
|
$this->assertFalse( $status->hasMessage( 'good' ) );
|
2013-11-15 15:43:42 +00:00
|
|
|
}
|
2013-10-26 00:52:46 +00:00
|
|
|
|
2013-11-15 15:43:42 +00:00
|
|
|
/**
|
|
|
|
|
* @dataProvider provideCleanParams
|
|
|
|
|
* @covers Status::cleanParams
|
|
|
|
|
*/
|
|
|
|
|
public function testCleanParams( $cleanCallback, $params, $expected ) {
|
|
|
|
|
$method = new ReflectionMethod( 'Status', 'cleanParams' );
|
2013-11-21 17:19:44 +00:00
|
|
|
$method->setAccessible( true );
|
2013-11-15 15:43:42 +00:00
|
|
|
$status = new Status();
|
|
|
|
|
$status->cleanCallback = $cleanCallback;
|
|
|
|
|
|
|
|
|
|
$this->assertEquals( $expected, $method->invoke( $status, $params ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function provideCleanParams() {
|
2014-01-25 15:14:49 +00:00
|
|
|
$cleanCallback = function( $value ) {
|
|
|
|
|
return '-' . $value . '-';
|
|
|
|
|
};
|
|
|
|
|
|
2013-11-15 15:43:42 +00:00
|
|
|
return array(
|
|
|
|
|
array( false, array( 'foo' => 'bar' ), array( 'foo' => 'bar' ) ),
|
2014-01-25 15:14:49 +00:00
|
|
|
array( $cleanCallback, array( 'foo' => 'bar' ), array( 'foo' => '-bar-' ) ),
|
2013-11-15 15:43:42 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2013-11-15 22:42:59 +00:00
|
|
|
* @dataProvider provideGetWikiTextAndHtml
|
2013-11-15 15:43:42 +00:00
|
|
|
* @covers Status::getWikiText
|
|
|
|
|
* @todo test long and short context messages generated through this method
|
|
|
|
|
* this can not really be done now due to use of wfMessage()->plain()
|
|
|
|
|
* It is possible to mock such methods but only if namespaces are used
|
|
|
|
|
*/
|
2013-12-06 17:53:18 +00:00
|
|
|
public function testGetWikiText( Status $status, $wikitext, $html ) {
|
|
|
|
|
$this->assertEquals( $wikitext, $status->getWikiText() );
|
2013-11-15 15:43:42 +00:00
|
|
|
}
|
|
|
|
|
|
2013-11-15 22:42:59 +00:00
|
|
|
/**
|
|
|
|
|
* @dataProvider provideGetWikiTextAndHtml
|
|
|
|
|
* @covers Status::getHtml
|
|
|
|
|
* @todo test long and short context messages generated through this method
|
|
|
|
|
* this can not really be done now due to use of $this->getWikiText using wfMessage()->plain()
|
|
|
|
|
* It is possible to mock such methods but only if namespaces are used
|
|
|
|
|
*/
|
2013-12-15 19:56:09 +00:00
|
|
|
public function testGetHtml( Status $status, $wikitext, $html ) {
|
2013-12-06 17:53:18 +00:00
|
|
|
$this->assertEquals( $html, $status->getHTML() );
|
2013-11-15 22:42:59 +00:00
|
|
|
}
|
|
|
|
|
|
2013-11-15 15:43:42 +00:00
|
|
|
/**
|
|
|
|
|
* @return array of arrays with values;
|
|
|
|
|
* 0 => status object
|
|
|
|
|
* 1 => expected string (with no context)
|
|
|
|
|
*/
|
2013-11-15 22:42:59 +00:00
|
|
|
public static function provideGetWikiTextAndHtml() {
|
2013-11-15 15:43:42 +00:00
|
|
|
$testCases = array();
|
|
|
|
|
|
|
|
|
|
$testCases[ 'GoodStatus' ] = array(
|
|
|
|
|
new Status(),
|
|
|
|
|
"Internal error: Status::getWikiText called for a good result, this is incorrect\n",
|
2013-12-06 17:53:18 +00:00
|
|
|
"<p>Internal error: Status::getWikiText called for a good result, this is incorrect\n</p>",
|
2013-11-15 15:43:42 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$status = new Status();
|
|
|
|
|
$status->ok = false;
|
|
|
|
|
$testCases[ 'GoodButNoError' ] = array(
|
|
|
|
|
$status,
|
|
|
|
|
"Internal error: Status::getWikiText: Invalid result object: no error text but not OK\n",
|
2013-12-06 17:53:18 +00:00
|
|
|
"<p>Internal error: Status::getWikiText: Invalid result object: no error text but not OK\n</p>",
|
2013-11-15 15:43:42 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$status = new Status();
|
|
|
|
|
$status->warning( 'fooBar!' );
|
|
|
|
|
$testCases[ '1StringWarning' ] = array(
|
|
|
|
|
$status,
|
|
|
|
|
"<fooBar!>",
|
2013-12-06 17:53:18 +00:00
|
|
|
"<p><fooBar!>\n</p>",
|
2013-11-15 15:43:42 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$status = new Status();
|
|
|
|
|
$status->warning( 'fooBar!' );
|
|
|
|
|
$status->warning( 'fooBar2!' );
|
|
|
|
|
$testCases[ '2StringWarnings' ] = array(
|
|
|
|
|
$status,
|
|
|
|
|
"* <fooBar!>\n* <fooBar2!>\n",
|
2013-12-06 17:53:18 +00:00
|
|
|
"<ul>\n<li> <fooBar!>\n</li>\n<li> <fooBar2!>\n</li>\n</ul>\n",
|
2013-11-15 15:43:42 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$status = new Status();
|
|
|
|
|
$status->warning( new Message( 'fooBar!', array( 'foo', 'bar' ) ) );
|
|
|
|
|
$testCases[ '1MessageWarning' ] = array(
|
|
|
|
|
$status,
|
|
|
|
|
"<fooBar!>",
|
2013-12-06 17:53:18 +00:00
|
|
|
"<p><fooBar!>\n</p>",
|
2013-11-15 15:43:42 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$status = new Status();
|
|
|
|
|
$status->warning( new Message( 'fooBar!', array( 'foo', 'bar' ) ) );
|
|
|
|
|
$status->warning( new Message( 'fooBar2!' ) );
|
|
|
|
|
$testCases[ '2MessageWarnings' ] = array(
|
|
|
|
|
$status,
|
|
|
|
|
"* <fooBar!>\n* <fooBar2!>\n",
|
2013-12-06 17:53:18 +00:00
|
|
|
"<ul>\n<li> <fooBar!>\n</li>\n<li> <fooBar2!>\n</li>\n</ul>\n",
|
2013-11-15 15:43:42 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return $testCases;
|
2013-10-26 00:52:46 +00:00
|
|
|
}
|
|
|
|
|
|
2013-11-15 22:42:59 +00:00
|
|
|
/**
|
|
|
|
|
* @dataProvider provideGetMessage
|
|
|
|
|
* @covers Status::getMessage
|
2014-03-06 20:19:15 +00:00
|
|
|
* @todo test long and short context messages generated through this method
|
2013-11-15 22:42:59 +00:00
|
|
|
*/
|
2014-03-06 20:19:15 +00:00
|
|
|
public function testGetMessage( Status $status, $expectedParams = array(), $expectedKey ) {
|
|
|
|
|
$message = $status->getMessage();
|
2013-11-15 22:42:59 +00:00
|
|
|
$this->assertInstanceOf( 'Message', $message );
|
2014-03-06 20:16:27 +00:00
|
|
|
$this->assertEquals( $expectedParams, $message->getParams(), 'Message::getParams' );
|
|
|
|
|
$this->assertEquals( $expectedKey, $message->getKey(), 'Message::getKey' );
|
2013-11-15 22:42:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return array of arrays with values;
|
|
|
|
|
* 0 => status object
|
2014-03-06 20:16:27 +00:00
|
|
|
* 1 => expected Message parameters (with no context)
|
|
|
|
|
* 2 => expected Message key
|
2013-11-15 22:42:59 +00:00
|
|
|
*/
|
|
|
|
|
public static function provideGetMessage() {
|
|
|
|
|
$testCases = array();
|
|
|
|
|
|
|
|
|
|
$testCases[ 'GoodStatus' ] = array(
|
|
|
|
|
new Status(),
|
|
|
|
|
array( "Status::getMessage called for a good result, this is incorrect\n" ),
|
|
|
|
|
'internalerror_info'
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$status = new Status();
|
|
|
|
|
$status->ok = false;
|
|
|
|
|
$testCases[ 'GoodButNoError' ] = array(
|
|
|
|
|
$status,
|
|
|
|
|
array( "Status::getMessage: Invalid result object: no error text but not OK\n" ),
|
|
|
|
|
'internalerror_info'
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$status = new Status();
|
|
|
|
|
$status->warning( 'fooBar!' );
|
|
|
|
|
$testCases[ '1StringWarning' ] = array(
|
|
|
|
|
$status,
|
|
|
|
|
array(),
|
2014-03-06 20:16:27 +00:00
|
|
|
'fooBar!'
|
2013-11-15 22:42:59 +00:00
|
|
|
);
|
|
|
|
|
|
2014-03-06 20:16:27 +00:00
|
|
|
// FIXME: Assertion tries to compare a StubUserLang with a Language object, because
|
|
|
|
|
// "data providers are executed before both the call to the setUpBeforeClass static method
|
|
|
|
|
// and the first call to the setUp method. Because of that you can't access any variables
|
|
|
|
|
// you create there from within a data provider."
|
|
|
|
|
// http://phpunit.de/manual/3.7/en/writing-tests-for-phpunit.html
|
2013-11-15 22:42:59 +00:00
|
|
|
// $status = new Status();
|
|
|
|
|
// $status->warning( 'fooBar!' );
|
|
|
|
|
// $status->warning( 'fooBar2!' );
|
|
|
|
|
// $testCases[ '2StringWarnings' ] = array(
|
|
|
|
|
// $status,
|
2014-03-06 20:16:27 +00:00
|
|
|
// array( new Message( 'fooBar!' ), new Message( 'fooBar2!' ) ),
|
|
|
|
|
// "* \$1\n* \$2"
|
2013-11-15 22:42:59 +00:00
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
$status = new Status();
|
|
|
|
|
$status->warning( new Message( 'fooBar!', array( 'foo', 'bar' ) ) );
|
|
|
|
|
$testCases[ '1MessageWarning' ] = array(
|
|
|
|
|
$status,
|
|
|
|
|
array( 'foo', 'bar' ),
|
2014-03-06 20:16:27 +00:00
|
|
|
'fooBar!'
|
2013-11-15 22:42:59 +00:00
|
|
|
);
|
|
|
|
|
|
2014-03-06 20:16:27 +00:00
|
|
|
$status = new Status();
|
|
|
|
|
$status->warning( new Message( 'fooBar!', array( 'foo', 'bar' ) ) );
|
|
|
|
|
$status->warning( new Message( 'fooBar2!' ) );
|
|
|
|
|
$testCases[ '2MessageWarnings' ] = array(
|
|
|
|
|
$status,
|
|
|
|
|
array( new Message( 'fooBar!', array( 'foo', 'bar' ) ), new Message( 'fooBar2!' ) ),
|
|
|
|
|
"* \$1\n* \$2"
|
|
|
|
|
);
|
2013-11-15 22:42:59 +00:00
|
|
|
|
|
|
|
|
return $testCases;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers Status::replaceMessage
|
|
|
|
|
*/
|
|
|
|
|
public function testReplaceMessage() {
|
|
|
|
|
$status = new Status();
|
|
|
|
|
$message = new Message( 'key1', array( 'foo1', 'bar1' ) );
|
|
|
|
|
$status->error( $message );
|
|
|
|
|
$newMessage = new Message( 'key2', array( 'foo2', 'bar2' ) );
|
|
|
|
|
|
|
|
|
|
$status->replaceMessage( $message, $newMessage );
|
|
|
|
|
|
|
|
|
|
$this->assertEquals( $newMessage, $status->errors[0]['message'] );
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-19 18:12:16 +00:00
|
|
|
/**
|
|
|
|
|
* @covers Status::getErrorMessage
|
|
|
|
|
*/
|
|
|
|
|
public function testGetErrorMessage() {
|
|
|
|
|
$method = new ReflectionMethod( 'Status', 'getErrorMessage' );
|
2013-12-01 19:58:51 +00:00
|
|
|
$method->setAccessible( true );
|
2013-11-19 18:12:16 +00:00
|
|
|
$status = new Status();
|
|
|
|
|
$key = 'foo';
|
|
|
|
|
$params = array( 'bar' );
|
|
|
|
|
|
|
|
|
|
/** @var Message $message */
|
|
|
|
|
$message = $method->invoke( $status, array_merge( array( $key ), $params ) );
|
|
|
|
|
$this->assertInstanceOf( 'Message', $message );
|
|
|
|
|
$this->assertEquals( $key, $message->getKey() );
|
|
|
|
|
$this->assertEquals( $params, $message->getParams() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers Status::getErrorMessageArray
|
|
|
|
|
*/
|
|
|
|
|
public function testGetErrorMessageArray() {
|
|
|
|
|
$method = new ReflectionMethod( 'Status', 'getErrorMessageArray' );
|
2013-12-01 19:58:51 +00:00
|
|
|
$method->setAccessible( true );
|
2013-11-19 18:12:16 +00:00
|
|
|
$status = new Status();
|
|
|
|
|
$key = 'foo';
|
|
|
|
|
$params = array( 'bar' );
|
|
|
|
|
|
|
|
|
|
/** @var Message[] $messageArray */
|
|
|
|
|
$messageArray = $method->invoke(
|
|
|
|
|
$status,
|
|
|
|
|
array(
|
|
|
|
|
array_merge( array( $key ), $params ),
|
|
|
|
|
array_merge( array( $key ), $params )
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$this->assertInternalType( 'array', $messageArray );
|
|
|
|
|
$this->assertCount( 2, $messageArray );
|
2013-12-01 19:58:51 +00:00
|
|
|
foreach ( $messageArray as $message ) {
|
2013-11-19 18:12:16 +00:00
|
|
|
$this->assertInstanceOf( 'Message', $message );
|
|
|
|
|
$this->assertEquals( $key, $message->getKey() );
|
|
|
|
|
$this->assertEquals( $params, $message->getParams() );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers Status::getErrorsByType
|
|
|
|
|
*/
|
|
|
|
|
public function testGetErrorsByType() {
|
|
|
|
|
$status = new Status();
|
|
|
|
|
$warning = new Message( 'warning111' );
|
|
|
|
|
$error = new Message( 'error111' );
|
|
|
|
|
$status->warning( $warning );
|
|
|
|
|
$status->error( $error );
|
|
|
|
|
|
|
|
|
|
$warnings = $status->getErrorsByType( 'warning' );
|
|
|
|
|
$errors = $status->getErrorsByType( 'error' );
|
|
|
|
|
|
|
|
|
|
$this->assertCount( 1, $warnings );
|
|
|
|
|
$this->assertCount( 1, $errors );
|
|
|
|
|
$this->assertEquals( $warning, $warnings[0]['message'] );
|
|
|
|
|
$this->assertEquals( $error, $errors[0]['message'] );
|
|
|
|
|
}
|
2013-10-24 22:11:15 +00:00
|
|
|
|
2014-02-22 10:41:17 +00:00
|
|
|
/**
|
|
|
|
|
* @covers Status::__wakeup
|
|
|
|
|
*/
|
|
|
|
|
public function testWakeUpSanitizesCallback() {
|
|
|
|
|
$status = new Status();
|
|
|
|
|
$status->cleanCallback = function( $value ) {
|
|
|
|
|
return '-' . $value . '-';
|
|
|
|
|
};
|
|
|
|
|
$status->__wakeup();
|
|
|
|
|
$this->assertEquals( false, $status->cleanCallback );
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-22 11:00:18 +00:00
|
|
|
/**
|
|
|
|
|
* @dataProvider provideNonObjectMessages
|
|
|
|
|
* @covers Status::getStatusArray
|
|
|
|
|
*/
|
|
|
|
|
public function testGetStatusArrayWithNonObjectMessages( $nonObjMsg ) {
|
|
|
|
|
$status = new Status();
|
2014-03-20 18:59:20 +00:00
|
|
|
if ( !array_key_exists( 1, $nonObjMsg ) ) {
|
2014-02-22 11:00:18 +00:00
|
|
|
$status->warning( $nonObjMsg[0] );
|
|
|
|
|
} else {
|
|
|
|
|
$status->warning( $nonObjMsg[0], $nonObjMsg[1] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$array = $status->getWarningsArray(); // We use getWarningsArray to access getStatusArray
|
|
|
|
|
|
|
|
|
|
$this->assertEquals( 1, count( $array ) );
|
|
|
|
|
$this->assertEquals( $nonObjMsg, $array[0] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function provideNonObjectMessages() {
|
|
|
|
|
return array(
|
|
|
|
|
array( array( 'ImaString', array( 'param1' => 'value1' ) ) ),
|
|
|
|
|
array( array( 'ImaString' ) ),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-25 01:12:47 +00:00
|
|
|
}
|