Thiemo Mättig (WMDE) has uploaded a new change for review. ( https://gerrit.wikimedia.org/r/329615 )
Change subject: Fix assertions and reduce complexity of LexemeViewTest ...................................................................... Fix assertions and reduce complexity of LexemeViewTest Two main things are happening here: * testGetHtml was not asserting anything. * I'm reducing the complexity of provideTestGetHtmlForLexicalCategoryAndLanguage very much. The test does have a single (logical) assertion now: Is the rendered headline exactly the expected one? Plus a lot of inlining and removing unused complexity. For example, the alternative language did not do anything. Change-Id: I1f567a36cd01992bdaa6ebad941b2ac3dccdab8f --- M tests/phpunit/mediawiki/View/LexemeViewTest.php 1 file changed, 49 insertions(+), 116 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/WikibaseLexeme refs/changes/15/329615/1 diff --git a/tests/phpunit/mediawiki/View/LexemeViewTest.php b/tests/phpunit/mediawiki/View/LexemeViewTest.php index 5b3f451..ebdc07b 100644 --- a/tests/phpunit/mediawiki/View/LexemeViewTest.php +++ b/tests/phpunit/mediawiki/View/LexemeViewTest.php @@ -31,16 +31,26 @@ * * @license GPL-2.0+ * @author Amir Sarabadani <ladsgr...@gmail.com> + * @author Thiemo Mättig */ class LexemeViewTest extends PHPUnit_Framework_TestCase { /** + * @param StatementList|null $expectedStatements + * * @return StatementSectionsView */ - private function newStatementSectionsViewMock() { - return $this->getMockBuilder( StatementSectionsView::class ) + private function newStatementSectionsViewMock( StatementList $expectedStatements = null ) { + $statementSectionsView = $this->getMockBuilder( StatementSectionsView::class ) ->disableOriginalConstructor() ->getMock(); + + $statementSectionsView->expects( $expectedStatements ? $this->once() : $this->never() ) + ->method( 'getHtml' ) + ->with( $expectedStatements ) + ->will( $this->returnValue( 'statementSectionsView->getHtml' ) ); + + return $statementSectionsView; } /** @@ -57,7 +67,7 @@ if ( $itemId->getSerialization() === 'Q1' ) { return null; } - return new Term( 'en', '<Label of ' . $itemId->getSerialization() . '>' ); + return new Term( 'en', '<ITEM-' . $itemId->getSerialization() . '>' ); } ) ); @@ -68,9 +78,17 @@ * @return EntityTermsView */ private function newEntityTermsViewMock() { - return $this->getMockBuilder( EntityTermsView::class ) + $entityTermsView = $this->getMockBuilder( EntityTermsView::class ) ->disableOriginalConstructor() ->getMock(); + + $entityTermsView->expects( $this->never() ) + ->method( 'getHtml' ); + + $entityTermsView->expects( $this->never() ) + ->method( 'getTitleHtml' ); + + return $entityTermsView; } /** @@ -84,37 +102,22 @@ return $languageDirectionalityLookup; } - private function newLexemeView( - $contentLanguageCode = 'en', - EntityTermsView $entityTermsView = null, - StatementSectionsView $statementSectionsView = null - ) { - $templateFactory = TemplateFactory::getDefaultInstance(); - - if ( !$entityTermsView ) { - $entityTermsView = $this->newEntityTermsViewMock(); - } - - if ( !$statementSectionsView ) { - $statementSectionsView = $this->newStatementSectionsViewMock(); - } - + private function newLexemeView( StatementList $expectedStatements = null ) { $languageDirectionalityLookup = $this->newLanguageDirectionalityLookupMock(); + $htmlTermRenderer = new FallbackHintHtmlTermRenderer( $languageDirectionalityLookup, - new LanguageNameLookup( $contentLanguageCode ) + new LanguageNameLookup( 'en' ) ); - $labelDescriptionLookup = $this->newLabelDescriptionLookup(); - return new LexemeView( - $templateFactory, - $entityTermsView, - $statementSectionsView, + TemplateFactory::getDefaultInstance(), + $this->newEntityTermsViewMock(), + $this->newStatementSectionsViewMock( $expectedStatements ), $languageDirectionalityLookup, $htmlTermRenderer, - $labelDescriptionLookup, - $contentLanguageCode + $this->newLabelDescriptionLookup(), + 'en' ); } @@ -137,47 +140,14 @@ /** * @dataProvider provideTestGetHtml */ - public function testGetHtml( - Lexeme $entity, - LexemeId $entityId = null, - $contentLanguageCode = 'en', - StatementList $statements = null - ) { - $entityTermsView = $this->newEntityTermsViewMock(); - $entityTermsView - ->method( 'getHtml' ) - ->with( - $contentLanguageCode, - $entity, - $entity, - null, - $entityId - ) - ->will( $this->returnValue( 'entityTermsView->getHtml' ) ); + public function testGetHtml( Lexeme $lexeme ) { + $view = $this->newLexemeView( $lexeme->getStatements() ); - $entityTermsView->expects( $this->never() ) - ->method( 'getEntityTermsForLanguageListView' ); - - $statementSectionsView = $this->newStatementSectionsViewMock(); - $statementSectionsView - ->method( 'getHtml' ) - ->with( - $this->callback( function( StatementList $statementList ) use ( $statements ) { - return $statements ? $statementList === $statements : $statementList->isEmpty(); - } ) - ) - ->will( $this->returnValue( 'statementSectionsView->getHtml' ) ); - - $view = $this->newLexemeView( - $contentLanguageCode, - $entityTermsView, - $statementSectionsView - ); - - $result = $view->getHtml( $entity ); - $this->assertInternalType( 'string', $result ); - $this->assertContains( 'wb-lexeme', $result ); - + $html = $view->getHtml( $lexeme ); + $this->assertInternalType( 'string', $html ); + $this->assertContains( 'id="wb-lexeme-' . ( $lexeme->getId() ?: 'new' ) . '"', $html ); + $this->assertContains( 'class="wikibase-entityview wb-lexeme"', $html ); + $this->assertContains( 'statementSectionsView->getHtml', $html ); } public function provideTestGetHtml() { @@ -188,28 +158,13 @@ return [ [ - new Lexeme() + new Lexeme(), ], [ new Lexeme( $lexemeId ), - $lexemeId ], [ new Lexeme( $lexemeId, null, null, null, $statements ), - $lexemeId, - 'en', - $statements - ], - [ - new Lexeme( $lexemeId ), - $lexemeId, - 'lkt' - ], - [ - new Lexeme( $lexemeId, null, null, null, $statements ), - $lexemeId, - 'lkt', - $statements ], ]; } @@ -232,23 +187,23 @@ return [ [ new Lexeme(), - [ 'wb-lexeme' ] + '' ], [ new Lexeme( $lexemeId, null, $lexicalCategory ), - [ 'wb-lexeme', '<Label of Q3>' ] + '<ITEM-Q3>' ], [ new Lexeme( $lexemeId, null, null, $language ), - [ 'wb-lexeme', 'Lexeme in <Label of Q2>' ] + 'Lexeme in <ITEM-Q2>' ], [ new Lexeme( $lexemeId, null, $lexicalCategory, $language ), - [ 'wb-lexeme', '<Label of Q3> in <Label of Q2>' ] + '<ITEM-Q3> in <ITEM-Q2>' ], [ new Lexeme( $lexemeId, null, $missingLabelItem, $language ), - [ 'wb-lexeme', 'Q1 in <Label of Q2>' ] + 'Q1 in <ITEM-Q2>' ], ]; } @@ -258,36 +213,14 @@ */ public function testGetHtmlForLexicalCategoryAndLanguage( Lexeme $entity, - array $expectedContents + $expectedHeadline ) { - $entityTermsView = $this->newEntityTermsViewMock(); - $entityTermsView - ->method( 'getHtml' ) - ->with( - 'en', - $entity, - $entity, - null, - $entity->getId() - ) - ->will( $this->returnValue( 'entityTermsView->getHtml' ) ); + $view = $this->newLexemeView( $entity->getStatements() ); - $entityTermsView->expects( $this->never() ) - ->method( 'getEntityTermsForLanguageListView' ); - - $statementSectionsView = $this->newStatementSectionsViewMock(); - - $view = $this->newLexemeView( - 'en', - $entityTermsView, - $statementSectionsView - ); - - $result = $view->getHtml( $entity ); - $this->assertInternalType( 'string', $result ); - foreach ( $expectedContents as $expectedContent ) { - $this->assertContains( $expectedContent, $result ); - } + $html = $view->getHtml( $entity ); + $this->assertInternalType( 'string', $html ); + $this->assertContains( '<div class="wikibase-entityview-main">' . $expectedHeadline + . '<div id="toc"></div>statementSectionsView->getHtml</div>', $html ); } } -- To view, visit https://gerrit.wikimedia.org/r/329615 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I1f567a36cd01992bdaa6ebad941b2ac3dccdab8f Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/WikibaseLexeme Gerrit-Branch: master Gerrit-Owner: Thiemo Mättig (WMDE) <thiemo.maet...@wikimedia.de> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits