Thiemo Mättig (WMDE) has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/363355 )

Change subject: Use more simple mocks and test values in ShowSearchHitHandler 
test
......................................................................

Use more simple mocks and test values in ShowSearchHitHandler test

Change-Id: I47f3132c4d57ffecc586acac00d458a337feb1e1
---
M repo/tests/phpunit/includes/Hooks/ShowSearchHitHandlerTest.php
1 file changed, 33 insertions(+), 51 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase 
refs/changes/55/363355/1

diff --git a/repo/tests/phpunit/includes/Hooks/ShowSearchHitHandlerTest.php 
b/repo/tests/phpunit/includes/Hooks/ShowSearchHitHandlerTest.php
index 4682852..83ee1d0 100644
--- a/repo/tests/phpunit/includes/Hooks/ShowSearchHitHandlerTest.php
+++ b/repo/tests/phpunit/includes/Hooks/ShowSearchHitHandlerTest.php
@@ -4,7 +4,7 @@
 
 use Language;
 use MediaWikiTestCase;
-use Message;
+use RawMessage;
 use SearchResult;
 use SpecialSearch;
 use Title;
@@ -41,15 +41,7 @@
                        ->disableOriginalConstructor()
                        ->getMock();
                $searchPage->method( 'msg' )
-                       ->with( $this->equalTo( 'colon-separator' ) )
-                       ->will( $this->returnCallback( function ( $key ) {
-                               $msg = $this->getMockBuilder( Message::class )
-                                       ->disableOriginalConstructor()
-                                       ->getMock();
-                               $msg->method( 'escaped' )->willReturn( ': ' );
-
-                               return $msg;
-                       } ) );
+                       ->willReturn( new RawMessage( ': ' ) );
                $searchPage->method( 'getLanguage' )
                        ->willReturn( Language::factory( 'de' ) );
 
@@ -58,41 +50,24 @@
 
        /**
         * @param string $title
+        *
         * @return SearchResult
         */
        private function getSearchResult( $title ) {
                $mockTitle = $this->getMock( Title::class );
-               $mockTitle->method( 'getText' )->willReturn( $title );
+               $mockTitle->method( 'getText' )
+                       ->willReturn( $title );
                // hack: content model equals title/id
-               $mockTitle->method( 'getContentModel' )->willReturn( $title );
+               $mockTitle->method( 'getContentModel' )
+                       ->willReturn( $title );
 
                $searchResult = $this->getMockBuilder( SearchResult::class )
                        ->disableOriginalConstructor()
                        ->getMock();
-               $searchResult->method( 'getTitle' )->willReturn( $mockTitle );
+               $searchResult->method( 'getTitle' )
+                       ->willReturn( $mockTitle );
 
                return $searchResult;
-       }
-
-       /**
-        * @see LanguageFallbackChain::extractPreferredValue
-        * @param array $terms
-        * @return array|null
-        */
-       private function extractPreferredValue( array $terms ) {
-               if ( array_key_exists( 'de', $terms ) ) {
-                       return [
-                               'value' => $terms['de'],
-                               'language' => 'de',
-                       ];
-               } elseif ( array_key_exists( 'en', $terms ) ) {
-                       return [
-                               'value' => $terms['en'],
-                               'language' => 'en',
-                       ];
-               }
-
-               return null;
        }
 
        /**
@@ -103,9 +78,15 @@
                        ->disableOriginalConstructor()
                        ->getMock();
                $languageFallbackChain->method( 'extractPreferredValue' )
-                       ->will( $this->returnCallback( function ( array $terms 
) {
-                               return $this->extractPreferredValue( $terms );
-                       } ) );
+                       ->willReturnCallback( function ( array $terms ) {
+                               foreach ( [ 'de', 'en' ] as $lang ) {
+                                       if ( isset( $terms[$lang] ) ) {
+                                               return [ 'value' => 
$terms[$lang], 'language' => $lang ];
+                                       }
+                               }
+
+                               return null;
+                       } );
 
                return $languageFallbackChain;
        }
@@ -118,10 +99,10 @@
                        ->disableOriginalConstructor()
                        ->getMock();
                $entityContentFactory->method( 'isEntityContentModel' )
-                       ->will( $this->returnCallback( function ( $contentModel 
) {
+                       ->willReturnCallback( function ( $contentModel ) {
                                // hack: content model equals title/id
                                return $contentModel !== self::NON_ENTITY_TITLE;
-                       } ) );
+                       } );
 
                return $entityContentFactory;
        }
@@ -132,34 +113,34 @@
        private function getEntityIdLookup() {
                $entityIdLookup = $this->getMock( EntityIdLookup::class );
                $entityIdLookup->method( 'getEntityIdForTitle' )
-                       ->will( $this->returnCallback( function ( Title $title 
) {
+                       ->willReturnCallback( function ( Title $title ) {
                                return new ItemId( $title->getText() );
-                       } ) );
+                       } );
 
                return $entityIdLookup;
        }
 
        /**
         * @param ItemId $itemId
+        *
         * @return string[]
         */
        private function getDescriptionsArray( ItemId $itemId ) {
-               $terms = [];
                switch ( $itemId->getSerialization() ) {
                        case self::DE_DESCRIPTION_ITEM_ID:
-                               $terms['de'] = '<b>German description</b>';
+                               return [ 'de' => '<b>German description</b>' ];
                        case self::FALLBACK_DESCRIPTION_ITEM_ID:
-                               $terms['en'] = 'fallback description';
+                               return [ 'en' => 'fallback description' ];
                        case self::NO_FALLBACK_DESCRIPTION_ITEM_ID:
-                               $terms['fr'] = 'unused description';
-                       case self::EMPTY_ITEM_ID:
-                               break;
+                               return [ 'fr' => 'unused description' ];
                }
-               return $terms;
+
+               return [];
        }
 
        /**
         * @param ItemId $itemId
+        *
         * @return Item $item
         */
        private function getEntity( ItemId $itemId ) {
@@ -172,7 +153,8 @@
                $item = $this->getMockBuilder( Item::class )
                        ->disableOriginalConstructor()
                        ->getMock();
-               $item->method( 'getDescriptions' )->willReturn( $termList );
+               $item->method( 'getDescriptions' )
+                       ->willReturn( $termList );
 
                return $item;
        }
@@ -183,9 +165,9 @@
        private function getEntityLookup() {
                $entityLookup = $this->getMock( EntityLookup::class );
                $entityLookup->method( 'getEntity' )
-                       ->will( $this->returnCallback( function ( ItemId 
$itemId ) {
+                       ->willReturnCallback( function ( ItemId $itemId ) {
                                return $this->getEntity( $itemId );
-                       } ) );
+                       } );
 
                return $entityLookup;
        }

-- 
To view, visit https://gerrit.wikimedia.org/r/363355
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I47f3132c4d57ffecc586acac00d458a337feb1e1
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Thiemo Mättig (WMDE) <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to