From 9a06bc75f9abf103b81c39d738ca80fbd21b3114 Mon Sep 17 00:00:00 2001 From: addshore Date: Thu, 24 Oct 2013 23:11:15 +0100 Subject: [PATCH] add basic Status unit test Adds unit tests for some of the more basic parts of the Status class has todos at the bottom of the file for all other methods that need to be tested Change-Id: Ic2f2abafb44ef86af207c7595e440672dfcf4f1e --- tests/phpunit/includes/StatusTest.php | 192 ++++++++++++++++++++++++++ 1 file changed, 192 insertions(+) create mode 100644 tests/phpunit/includes/StatusTest.php diff --git a/tests/phpunit/includes/StatusTest.php b/tests/phpunit/includes/StatusTest.php new file mode 100644 index 0000000000..5f47a59bea --- /dev/null +++ b/tests/phpunit/includes/StatusTest.php @@ -0,0 +1,192 @@ +assertTrue( true ); + } + + /** + * @dataProvider provideValues + * @covers Status::newGood + * @covers Status::getValue + * @covers Status::isGood + * @covers Status::isOK + */ + public function testNewGood( $value = null ){ + $status = Status::newGood( $value ); + $this->assertTrue( $status->isGood() ); + $this->assertTrue( $status->isOK() ); + $this->assertEquals( $value, $status->getValue() ); + } + + public static function provideValues(){ + return array( + array(), + array( 'foo' ), + array( array( 'foo' => 'bar' ) ), + array( new Exception() ), + array( 1234 ), + ); + } + + /** + * @covers Status:newFatal + * @covers Status::isGood + * @covers Status::isOK + * @covers Status:getMessage + */ + 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() ); + } + + /** + * @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() ); + } + + /** + * @dataProvider provideSetResult + * @covers Status::getValue + * @covers Status::isOK + */ + 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' ), + ); + } + + /** + * @dataProvider provideMockMessageDetails + * @covers Status::warning + * @covers Status::getWarningsArray + */ + public function testWarningWithMessage( $mockDetails ) { + $status = new Status(); + $messages = $this->getMockMessages( $mockDetails ); + + foreach( $messages as $message ){ + $status->warning( $message ); + } + $warnings = $status->getWarningsArray(); + + $this->assertEquals( count( $messages ), count( $warnings ) ); + foreach( $messages as $key => $message ) { + $expectedArray = array_merge( array( $message->getKey() ), $message->getParams() ); + $this->assertEquals( $warnings[$key], $expectedArray ); + } + } + + /** + * @dataProvider provideMockMessageDetails + * @covers Status::error + * @covers Status::getErrorsArray + */ + public function testErrorWithMessage( $mockDetails ) { + $status = new Status(); + $messages = $this->getMockMessages( $mockDetails ); + + foreach( $messages as $message ){ + $status->error( $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 ); + } + } + + 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[] + */ + protected function getMockMessages( $messageDetails ){ + $messages = array(); + foreach( $messageDetails as $key => $paramsArray ){ + $messages[] = $this->getMockMessage( $key, $paramsArray ); + } + return $messages; + } + + public static function provideMockMessageDetails(){ + return array( + array( array( 'key1' => array( 'foo' => 'bar' ) ) ), + array( array( 'key1' => array( 'foo' => 'bar' ), 'key2' => array( 'foo2' => 'bar2' ) ) ), + ); + } + + /** + * @covers Status::merge + * @todo test merge with $overwriteValue true + */ + public function testMerge(){ + $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() ) ); + } + + //todo test cleanParams + //todo test getWikiText + //todo test getMessage + //todo test getErrorMessage + //todo test getHTML + //todo test getErrorMessageArray + //todo test getStatusArray + //todo test getErrorsByType + //todo test hasMessage + //todo test replaceMessage + //todo test replaceMessage + +} \ No newline at end of file -- 2.20.1