jenkins-bot has submitted this change and it was merged. Change subject: Add QueryResultTest, improved DISerializerTest ......................................................................
Add QueryResultTest, improved DISerializerTest Change-Id: I567968f6b3f93743fbf83db53d810fef1231ba0f --- M tests/phpunit/MockObjectRepository.php M tests/phpunit/includes/dataitems/DISerializerTest.php A tests/phpunit/includes/storage/QueryResultTest.php 3 files changed, 222 insertions(+), 110 deletions(-) Approvals: Mwjames: Looks good to me, approved jenkins-bot: Verified diff --git a/tests/phpunit/MockObjectRepository.php b/tests/phpunit/MockObjectRepository.php index bf7abd7..c768114 100644 --- a/tests/phpunit/MockObjectRepository.php +++ b/tests/phpunit/MockObjectRepository.php @@ -457,6 +457,14 @@ ->will( $this->returnValue( $this->builder->setValue( 'getCount' ) ) ); $queryResult->expects( $this->any() ) + ->method( 'getResults' ) + ->will( $this->returnValue( $this->builder->setValue( 'getResults' ) ) ); + + $queryResult->expects( $this->any() ) + ->method( 'getStore' ) + ->will( $this->returnValue( $this->builder->setValue( 'getStore' ) ) ); + + $queryResult->expects( $this->any() ) ->method( 'serializeToArray' ) ->will( $this->returnValue( $this->builder->setValue( 'serializeToArray' ) ) ); @@ -1075,6 +1083,14 @@ ->will( $this->returnValue( $this->builder->setValue( 'getMode', SMWPrintRequest::PRINT_THIS ) ) ); $printRequest->expects( $this->any() ) + ->method( 'getTypeID' ) + ->will( $this->returnValue( $this->builder->setValue( 'getTypeID' ) ) ); + + $printRequest->expects( $this->any() ) + ->method( 'getOutputFormat' ) + ->will( $this->returnValue( $this->builder->setValue( 'getOutputFormat' ) ) ); + + $printRequest->expects( $this->any() ) ->method( 'getParameter' ) ->will( $this->returnValue( $this->builder->setValue( 'getParameter', 'center' ) ) ); @@ -1103,6 +1119,10 @@ ->will( $this->returnValue( $this->builder->setValue( 'getPrintRequest' ) ) ); $resultArray->expects( $this->any() ) + ->method( 'getContent' ) + ->will( $this->returnValue( $this->builder->setValue( 'getContent' ) ) ); + + $resultArray->expects( $this->any() ) ->method( 'getNextDataValue' ) ->will( $this->onConsecutiveCalls( $this->builder->setValue( 'getNextDataValue' ), false ) ); diff --git a/tests/phpunit/includes/dataitems/DISerializerTest.php b/tests/phpunit/includes/dataitems/DISerializerTest.php index 409f3c5..fa82856 100644 --- a/tests/phpunit/includes/dataitems/DISerializerTest.php +++ b/tests/phpunit/includes/dataitems/DISerializerTest.php @@ -5,6 +5,7 @@ use SMW\DISerializer; use SMWQueryProcessor; use SMWQueryResult; +use SMWDataItem as DataItem; /** * @covers \SMW\DISerializer @@ -27,136 +28,99 @@ } /** - * DataProvider + * @dataProvider numberDataProvider * + * @since 1.9 + */ + public function testQueryResultSerializerOnMock( $setup, $expected ) { + + $results = DISerializer::getSerializedQueryResult( $setup['queryResult'] ); + + $this->assertInternalType( 'array' , $results ); + $this->assertEquals( $expected['printrequests'], $results['printrequests'] ); + + } + + /** * @return array */ - public function getDataProvider() { - return array( - array( + public function numberDataProvider() { - // #1 Standard query - array( 'query' => array( - '[[Modification date::+]]', - '?Modification date', - 'limit=10' - ) - ), - array( - array( - 'label'=> '', - 'typeid' => '_wpg', - 'mode' => 2, - 'format' => false - ), - array( - 'label'=> 'Modification date', - 'typeid' => '_dat', - 'mode' => 1, - 'format' => '' - ) - ) - ), + $provider = array(); - // #2 Query containing a printrequest formatting - array( - array( 'query' => array( - '[[Modification date::+]]', - '?Modification date#ISO', - 'limit=10' - ) - ), - array( - array( - 'label'=> '', - 'typeid' => '_wpg', - 'mode' => 2, - 'format' => false - ), - array( - 'label'=> 'Modification date', - 'typeid' => '_dat', - 'mode' => 1, - 'format' => 'ISO' - ) - ) - ), - ); - } - - /** - * Helper function that returns a SMWQueryResult object - * - * @return SMWQueryResult - */ - private function getQueryResult( $queryString ) { - - $this->runOnlyOnSQLStore(); - - list( $query, $formattedParams ) = SMWQueryProcessor::getQueryAndParamsFromFunctionParams( - $queryString, - SMW_OUTPUT_WIKI, - SMWQueryProcessor::INLINE_QUERY, - false + $setup = array( + array( 'printRequest' => 'Foo-1', 'typeId' => '_num', 'number' => 10, 'dataValue' => 'Quuey' ), + array( 'printRequest' => 'Foo-2', 'typeId' => '_num', 'number' => 20, 'dataValue' => 'Vey' ), ); - return smwfGetStore()->getQueryResult( $query ); + $provider[] = array( + array( + 'queryResult' => $this->buildMockQueryResult( $setup ) + ), + array( + 'printrequests' => array( + array( 'label' => 'Foo-1', 'typeid' => '_num', 'mode' => 2, 'format' => false ), + array( 'label' => 'Foo-2', 'typeid' => '_num', 'mode' => 2, 'format' => false ) + ), + ) + ); + + return $provider; } /** - * @test DISerializer::getSerializedQueryResult - * @dataProvider getDataProvider - * - * @since 1.9 - * - * @param array $test - * @param array $expected + * @return QueryResult */ - public function testDISerializerQueryResult( array $test, array $expected ) { + private function buildMockQueryResult( $setup ) { - $queryResult = $this->getQueryResult( $test['query'] ); - $this->assertInstanceOf( '\SMWQueryResult', $queryResult ); + $printRequests = array(); + $resultArray = array(); + $getResults = array(); - $results = DISerializer::getSerializedQueryResult( $queryResult ); - $this->assertInternalType( 'array' , $results ); + foreach ( $setup as $value ) { - // Compare array structure - $this->assertEquals( $expected[0], $results['printrequests'][0] ); - $this->assertEquals( $expected[1], $results['printrequests'][1] ); - } + $printRequest = $this->newMockBuilder()->newObject( 'PrintRequest', array( + 'getText' => $value['printRequest'], + 'getLabel' => $value['printRequest'], + 'getTypeID' => $value['typeId'], + 'getOutputFormat' => false + ) ); - /** - * @test SMWQueryResult::toArray - * @dataProvider getDataProvider - * - * @since 1.9 - * - * @param array $test - * @param array $expected - */ - public function testQueryResulttoArray( array $test, array $expected ) { + $printRequests[] = $printRequest; + $getResults[] = \SMW\DIWikipage::newFromTitle( $this->newTitle( NS_MAIN, $value['printRequest'] ) ); - $queryResult = $this->getQueryResult( $test['query'] ); - $this->assertInstanceOf( '\SMWQueryResult', $queryResult ); + $dataItem = $this->newMockBuilder()->newObject( 'DataItem', array( + 'getDIType' => DataItem::TYPE_NUMBER, + 'getNumber' => $value['number'] + ) ); - $results = $queryResult->toArray(); + $dataValue = $this->newMockBuilder()->newObject( 'DataValue', array( + 'DataValueType' => 'SMWNumberValue', + 'getTypeID' => '_num', + 'getShortWikiText' => $value['dataValue'], + 'getDataItem' => $dataItem + ) ); - // Compare array structure - $this->assertEquals( $expected[0], $results['printrequests'][0] ); - $this->assertEquals( $expected[1], $results['printrequests'][1] ); - } + $resultArray[] = $this->newMockBuilder()->newObject( 'ResultArray', array( + 'getText' => $value['printRequest'], + 'getPrintRequest' => $printRequest, + 'getNextDataValue' => $dataValue, + 'getNextDataItem' => $dataItem, + 'getContent' => $dataItem + ) ); - /** - * @test DISerializer::getSerialization - * - * @since 1.9 - */ - public function testNumberSerialization() { + } - // Number serialization - $dataItem = new \SMWDINumber( 1001 ); - $results = DISerializer::getSerialization( $dataItem ); - $this->assertEquals( $results, 1001 ); + $queryResult = $this->newMockBuilder()->newObject( 'QueryResult', array( + 'getPrintRequests' => $printRequests, + 'getNext' => $resultArray, + 'getResults' => $getResults, + 'getStore' => $this->newMockBuilder()->newObject( 'Store' ), + 'getLink' => new \SMWInfolink( true, 'Lala' , 'Lula' ), + 'hasFurtherResults' => true + ) ); + + return $queryResult; } } diff --git a/tests/phpunit/includes/storage/QueryResultTest.php b/tests/phpunit/includes/storage/QueryResultTest.php new file mode 100644 index 0000000..ed12319 --- /dev/null +++ b/tests/phpunit/includes/storage/QueryResultTest.php @@ -0,0 +1,128 @@ +<?php + +namespace SMW\Test; + +use SMWQueryProcessor; + +/** + * @covers \SMWQueryResult + * + * @group SMW + * @group SMWExtension + * + * @license GNU GPL v2+ + * @author mwjames + */ +class QueryResultTest extends SemanticMediaWikiTestCase { + + /** + * Returns the name of the class to be tested + * + * @return string + */ + public function getClass() { + return '\SMWQueryResult'; + } + + /** + * Helper method that returns a QueryResult object + * + * @since 1.9 + */ + private function newQueryResultOnSQLStore( $queryString ) { + + $this->runOnlyOnSQLStore(); + + list( $query, $formattedParams ) = SMWQueryProcessor::getQueryAndParamsFromFunctionParams( + $queryString, + SMW_OUTPUT_WIKI, + SMWQueryProcessor::INLINE_QUERY, + false + ); + + return \SMW\StoreFactory::getStore()->getQueryResult( $query ); + } + + /** + * @dataProvider queryDataProvider + * + * @since 1.9 + */ + public function testConstructor( array $test ) { + $this->assertInstanceOf( $this->getClass(), $this->newQueryResultOnSQLStore( $test['query'] ) ); + } + + /** + * @dataProvider queryDataProvider + * + * @since 1.9 + */ + public function testToArray( array $test, array $expected ) { + + $instance = $this->newQueryResultOnSQLStore( $test['query'] ); + $results = $instance->toArray(); + + // Compare array structure + $this->assertEquals( $expected[0], $results['printrequests'][0] ); + $this->assertEquals( $expected[1], $results['printrequests'][1] ); + } + + /** + * @return array + */ + public function queryDataProvider() { + + $provider = array(); + + // #1 Standard query + $provider[] =array( + array( 'query' => array( + '[[Modification date::+]]', + '?Modification date', + 'limit=10' + ) + ), + array( + array( + 'label'=> '', + 'typeid' => '_wpg', + 'mode' => 2, + 'format' => false + ), + array( + 'label'=> 'Modification date', + 'typeid' => '_dat', + 'mode' => 1, + 'format' => '' + ) + ) + ); + + // #2 Query containing a printrequest formatting + $provider[] =array( + array( 'query' => array( + '[[Modification date::+]]', + '?Modification date#ISO', + 'limit=10' + ) + ), + array( + array( + 'label'=> '', + 'typeid' => '_wpg', + 'mode' => 2, + 'format' => false + ), + array( + 'label'=> 'Modification date', + 'typeid' => '_dat', + 'mode' => 1, + 'format' => 'ISO' + ) + ) + ); + + return $provider; + } + +} -- To view, visit https://gerrit.wikimedia.org/r/89626 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: I567968f6b3f93743fbf83db53d810fef1231ba0f Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/SemanticMediaWiki Gerrit-Branch: master Gerrit-Owner: Mwjames <jamesin.hongkon...@gmail.com> Gerrit-Reviewer: Mwjames <jamesin.hongkon...@gmail.com> Gerrit-Reviewer: jenkins-bot _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits