From a467d23e763daf46bf0148168148c9c7a80566f6 Mon Sep 17 00:00:00 2001 From: addshore Date: Fri, 15 Nov 2013 16:43:42 +0100 Subject: [PATCH] Cleanup and expand Status tests Change-Id: Ib06df7196c903505fbc0a3945513c5681acaaff3 --- tests/phpunit/includes/StatusTest.php | 149 +++++++++++++++++++++++--- 1 file changed, 133 insertions(+), 16 deletions(-) diff --git a/tests/phpunit/includes/StatusTest.php b/tests/phpunit/includes/StatusTest.php index 0a7cc0438f..4f64e4f555 100644 --- a/tests/phpunit/includes/StatusTest.php +++ b/tests/phpunit/includes/StatusTest.php @@ -13,9 +13,6 @@ class StatusTest extends MediaWikiTestCase { /** * @dataProvider provideValues * @covers Status::newGood - * @covers Status::getValue - * @covers Status::isGood - * @covers Status::isOK */ public function testNewGood( $value = null ){ $status = Status::newGood( $value ); @@ -36,9 +33,6 @@ class StatusTest extends MediaWikiTestCase { /** * @covers Status::newFatal - * @covers Status::isGood - * @covers Status::isOK - * @covers Status::getMessage */ public function testNewFatalWithMessage() { $message = $this->getMockBuilder( 'Message' ) @@ -53,23 +47,18 @@ class StatusTest extends MediaWikiTestCase { /** * @covers Status::newFatal - * @covers Status::isGood - * @covers Status::isOK - * @covers Status::getMessage */ public function testNewFatalWithString() { $message = 'foo'; $status = Status::newFatal( $message ); $this->assertFalse( $status->isGood() ); $this->assertFalse( $status->isOK() ); - $newMessage = $status->getMessage(); - $this->assertEquals( $message, $newMessage->getKey() ); + $this->assertEquals( $message, $status->getMessage()->getKey() ); } /** * @dataProvider provideSetResult - * @covers Status::getValue - * @covers Status::isOK + * @covers Status::setResult */ public function testSetResult( $ok, $value = null ) { $status = new Status(); @@ -87,6 +76,52 @@ class StatusTest extends MediaWikiTestCase { ); } + /** + * @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 ), + ); + } + /** * @dataProvider provideMockMessageDetails * @covers Status::warning @@ -185,11 +220,94 @@ class StatusTest extends MediaWikiTestCase { $status->fatal( 'bad' ); $this->assertTrue( $status->hasMessage( 'bad' ) ); $this->assertFalse( $status->hasMessage( 'good' ) ); + } + /** + * @dataProvider provideCleanParams + * @covers Status::cleanParams + */ + public function testCleanParams( $cleanCallback, $params, $expected ) { + $method = new ReflectionMethod( 'Status', 'cleanParams' ); + $method->setAccessible(TRUE); + $status = new Status(); + $status->cleanCallback = $cleanCallback; + + $this->assertEquals( $expected, $method->invoke( $status, $params ) ); + } + + /** + * @todo test cleanParams with a callback + */ + public static function provideCleanParams() { + return array( + array( false, array( 'foo' => 'bar' ), array( 'foo' => 'bar' ) ), + ); + } + + /** + * @dataProvider provideGetWikiText + * @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 + */ + public function testGetWikiText( Status $status, $expected ) { + $this->assertEquals( $expected, $status->getWikiText() ); + } + + /** + * @return array of arrays with values; + * 0 => status object + * 1 => expected string (with no context) + */ + public static function provideGetWikiText() { + $testCases = array(); + + $testCases[ 'GoodStatus' ] = array( + new Status(), + "Internal error: Status::getWikiText called for a good result, this is incorrect\n", + ); + + $status = new Status(); + $status->ok = false; + $testCases[ 'GoodButNoError' ] = array( + $status, + "Internal error: Status::getWikiText: Invalid result object: no error text but not OK\n", + ); + + $status = new Status(); + $status->warning( 'fooBar!' ); + $testCases[ '1StringWarning' ] = array( + $status, + "", + ); + + $status = new Status(); + $status->warning( 'fooBar!' ); + $status->warning( 'fooBar2!' ); + $testCases[ '2StringWarnings' ] = array( + $status, + "* \n* \n", + ); + + $status = new Status(); + $status->warning( new Message( 'fooBar!', array( 'foo', 'bar' ) ) ); + $testCases[ '1MessageWarning' ] = array( + $status, + "", + ); + + $status = new Status(); + $status->warning( new Message( 'fooBar!', array( 'foo', 'bar' ) ) ); + $status->warning( new Message( 'fooBar2!' ) ); + $testCases[ '2MessageWarnings' ] = array( + $status, + "* \n* \n", + ); + + return $testCases; } - //todo test cleanParams - //todo test getWikiText //todo test getMessage //todo test getErrorMessage //todo test getHTML @@ -197,6 +315,5 @@ class StatusTest extends MediaWikiTestCase { //todo test getStatusArray //todo test getErrorsByType //todo test replaceMessage - //todo test replaceMessage } -- 2.20.1