Thiemo Mättig (WMDE) has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/256927

Change subject: Inject StatementGrouper into StatementSectionsView instead
......................................................................

Inject StatementGrouper into StatementSectionsView instead

Suggested in
https://gerrit.wikimedia.org/r/#/c/256709/1/view/src/ItemView.php
I also think this is a good idea It reduces the total number of
injections.

Bug: T119596
Change-Id: Ic458864c9d7fd382c4e538613f591fb60539ac8b
---
M view/src/EntityViewFactory.php
M view/src/ItemView.php
M view/src/PropertyView.php
M view/src/StatementSectionsView.php
M view/tests/phpunit/StatementSectionsViewTest.php
5 files changed, 53 insertions(+), 52 deletions(-)


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

diff --git a/view/src/EntityViewFactory.php b/view/src/EntityViewFactory.php
index a78ed1a..92d912e 100644
--- a/view/src/EntityViewFactory.php
+++ b/view/src/EntityViewFactory.php
@@ -165,9 +165,8 @@
         ) {
                $entityTermsView = $this->newEntityTermsView( $languageCode, 
$editSectionGenerator );
 
-               $statementGrouper = 
$this->statementGrouperFactory->getStatementGrouper( $entityType );
-
                $statementSectionsView = $this->newStatementSectionsView(
+                       $entityType,
                        $languageCode,
                        $fallbackChain,
                        $labelDescriptionLookup,
@@ -193,7 +192,6 @@
                                return new ItemView(
                                        $this->templateFactory,
                                        $entityTermsView,
-                                       $statementGrouper,
                                        $statementSectionsView,
                                        $language,
                                        $siteLinksView,
@@ -203,7 +201,6 @@
                                return new PropertyView(
                                        $this->templateFactory,
                                        $entityTermsView,
-                                       $statementGrouper,
                                        $statementSectionsView,
                                        $this->dataTypeFactory,
                                        $language
@@ -214,6 +211,7 @@
        }
 
        /**
+        * @param string $entityType
         * @param string $languageCode
         * @param LanguageFallbackChain $fallbackChain
         * @param LabelDescriptionLookup $labelDescriptionLookup
@@ -222,32 +220,37 @@
         * @return StatementSectionsView
         */
        private function newStatementSectionsView(
+               $entityType,
                $languageCode,
                LanguageFallbackChain $fallbackChain,
                LabelDescriptionLookup $labelDescriptionLookup,
                EditSectionGenerator $editSectionGenerator
        ) {
-               $propertyIdFormatter = 
$this->htmlIdFormatterFactory->getEntityIdFormatter( $labelDescriptionLookup );
-
+               $snakFormatter = 
$this->htmlSnakFormatterFactory->getSnakFormatter(
+                       $languageCode,
+                       $fallbackChain,
+                       $labelDescriptionLookup
+               );
+               $propertyIdFormatter = 
$this->htmlIdFormatterFactory->getEntityIdFormatter(
+                       $labelDescriptionLookup
+               );
                $snakHtmlGenerator = new SnakHtmlGenerator(
                        $this->templateFactory,
-                       $this->htmlSnakFormatterFactory->getSnakFormatter( 
$languageCode, $fallbackChain, $labelDescriptionLookup ),
+                       $snakFormatter,
                        $propertyIdFormatter
                );
-
-               $claimHtmlGenerator = new ClaimHtmlGenerator(
-                       $this->templateFactory,
-                       $snakHtmlGenerator
-               );
-
                $statementGroupListView = new StatementGroupListView(
                        $this->templateFactory,
                        $propertyIdFormatter,
                        $editSectionGenerator,
-                       $claimHtmlGenerator
+                       new ClaimHtmlGenerator( $this->templateFactory, 
$snakHtmlGenerator )
                );
 
-               return new StatementSectionsView( $this->templateFactory, 
$statementGroupListView );
+               return new StatementSectionsView(
+                       $this->templateFactory,
+                       $this->statementGrouperFactory->getStatementGrouper( 
$entityType ),
+                       $statementGroupListView
+               );
        }
 
        /**
diff --git a/view/src/ItemView.php b/view/src/ItemView.php
index 2c9c3e8..4af2034 100644
--- a/view/src/ItemView.php
+++ b/view/src/ItemView.php
@@ -5,8 +5,6 @@
 use InvalidArgumentException;
 use Language;
 use Wikibase\DataModel\Entity\Item;
-use Wikibase\DataModel\Services\Statement\Grouper\NullStatementGrouper;
-use Wikibase\DataModel\Services\Statement\Grouper\StatementGrouper;
 use Wikibase\EntityRevision;
 use Wikibase\View\Template\TemplateFactory;
 
@@ -21,11 +19,6 @@
  * @author Daniel Werner
  */
 class ItemView extends EntityView {
-
-       /**
-        * @var StatementGrouper
-        */
-       private $statementGrouper;
 
        /**
         * @var StatementSectionsView
@@ -47,7 +40,6 @@
         *
         * @param TemplateFactory $templateFactory
         * @param EntityTermsView $entityTermsView
-        * @param StatementGrouper $statementGrouper
         * @param StatementSectionsView $statementSectionsView
         * @param Language $language
         * @param SiteLinksView $siteLinksView
@@ -56,7 +48,6 @@
        public function __construct(
                TemplateFactory $templateFactory,
                EntityTermsView $entityTermsView,
-               StatementGrouper $statementGrouper,
                StatementSectionsView $statementSectionsView,
                Language $language,
                SiteLinksView $siteLinksView,
@@ -64,7 +55,6 @@
        ) {
                parent::__construct( $templateFactory, $entityTermsView, 
$language );
 
-               $this->statementGrouper = $statementGrouper;
                $this->statementSectionsView = $statementSectionsView;
                $this->siteLinksView = $siteLinksView;
                $this->siteLinkGroups = $siteLinkGroups;
@@ -81,8 +71,7 @@
                }
 
                $html = parent::getMainHtml( $entityRevision );
-               $statementLists = $this->statementGrouper->groupStatements( 
$item->getStatements() );
-               $html .= $this->statementSectionsView->getHtml( $statementLists 
);
+               $html .= $this->statementSectionsView->getHtml( 
$item->getStatements() );
 
                return $html;
        }
diff --git a/view/src/PropertyView.php b/view/src/PropertyView.php
index 34b8878..c2276d9 100644
--- a/view/src/PropertyView.php
+++ b/view/src/PropertyView.php
@@ -7,8 +7,6 @@
 use InvalidArgumentException;
 use Language;
 use Wikibase\DataModel\Entity\Property;
-use Wikibase\DataModel\Services\Statement\Grouper\NullStatementGrouper;
-use Wikibase\DataModel\Services\Statement\Grouper\StatementGrouper;
 use Wikibase\EntityRevision;
 use Wikibase\View\Template\TemplateFactory;
 
@@ -25,11 +23,6 @@
 class PropertyView extends EntityView {
 
        /**
-        * @var StatementGrouper
-        */
-       private $statementGrouper;
-
-       /**
         * @var StatementSectionsView
         */
        private $statementSectionsView;
@@ -42,7 +35,6 @@
        /**
         * @param TemplateFactory $templateFactory
         * @param EntityTermsView $entityTermsView
-        * @param StatementGrouper $statementGrouper
         * @param StatementSectionsView $statementSectionsView
         * @param DataTypeFactory $dataTypeFactory
         * @param Language $language
@@ -50,14 +42,12 @@
        public function __construct(
                TemplateFactory $templateFactory,
                EntityTermsView $entityTermsView,
-               StatementGrouper $statementGrouper,
                StatementSectionsView $statementSectionsView,
                DataTypeFactory $dataTypeFactory,
                Language $language
        ) {
                parent::__construct( $templateFactory, $entityTermsView, 
$language );
 
-               $this->statementGrouper = $statementGrouper;
                $this->statementSectionsView = $statementSectionsView;
                $this->dataTypeFactory = $dataTypeFactory;
        }
@@ -75,8 +65,7 @@
                $html = parent::getMainHtml( $entityRevision );
                $html .= $this->getHtmlForDataType( $this->getDataType( 
$property ) );
 
-               $statementLists = $this->statementGrouper->groupStatements( 
$property->getStatements() );
-               $html .= $this->statementSectionsView->getHtml( $statementLists 
);
+               $html .= $this->statementSectionsView->getHtml( 
$property->getStatements() );
 
                $footer = wfMessage( 'wikibase-property-footer' );
 
diff --git a/view/src/StatementSectionsView.php 
b/view/src/StatementSectionsView.php
index 59765ad..2cea236 100644
--- a/view/src/StatementSectionsView.php
+++ b/view/src/StatementSectionsView.php
@@ -3,6 +3,7 @@
 namespace Wikibase\View;
 
 use InvalidArgumentException;
+use Wikibase\DataModel\Services\Statement\Grouper\StatementGrouper;
 use Wikibase\DataModel\Statement\StatementList;
 use Wikibase\View\Template\TemplateFactory;
 
@@ -20,25 +21,33 @@
        private $templateFactory;
 
        /**
+        * @var StatementGrouper
+        */
+       private $statementGrouper;
+
+       /**
         * @var StatementGroupListView
         */
        private $statementListView;
 
        public function __construct(
                TemplateFactory $templateFactory,
+               StatementGrouper $statementGrouper,
                StatementGroupListView $statementListView
        ) {
                $this->templateFactory = $templateFactory;
+               $this->statementGrouper = $statementGrouper;
                $this->statementListView = $statementListView;
        }
 
        /**
-        * @param StatementList[] $statementLists
+        * @param StatementList $statementList
         *
         * @throws InvalidArgumentException
         * @return string HTML
         */
-       public function getHtml( array $statementLists ) {
+       public function getHtml( StatementList $statementList ) {
+               $statementLists = $this->statementGrouper->groupStatements( 
$statementList );
                $html = '';
 
                foreach ( $statementLists as $key => $statements ) {
diff --git a/view/tests/phpunit/StatementSectionsViewTest.php 
b/view/tests/phpunit/StatementSectionsViewTest.php
index f46ac24..26674a0 100644
--- a/view/tests/phpunit/StatementSectionsViewTest.php
+++ b/view/tests/phpunit/StatementSectionsViewTest.php
@@ -4,6 +4,7 @@
 
 use Language;
 use MediaWikiTestCase;
+use Wikibase\DataModel\Services\Statement\Grouper\NullStatementGrouper;
 use Wikibase\DataModel\Snak\PropertyNoValueSnak;
 use Wikibase\DataModel\Statement\StatementList;
 use Wikibase\View\StatementSectionsView;
@@ -33,10 +34,17 @@
                ) );
        }
 
-       private function newInstance() {
+       private function newInstance( array $statementLists = array() ) {
                $templateFactory = new TemplateFactory( new TemplateRegistry( 
array(
                        'wb-section-heading' => '<HEADING id="$2" 
class="$3">$1</HEADING>',
                ) ) );
+
+               $statementGrouper = $this->getMock(
+                       
'Wikibase\DataModel\Services\Statement\Grouper\StatementGrouper'
+               );
+               $statementGrouper->expects( $this->any() )
+                       ->method( 'groupStatements' )
+                       ->will( $this->returnValue( $statementLists ) );
 
                $statementListView = $this->getMockBuilder( 
'Wikibase\View\StatementGroupListView' )
                        ->disableOriginalConstructor()
@@ -45,20 +53,23 @@
                        ->method( 'getHtml' )
                        ->will( $this->returnValue( '<LIST>' ) );
 
-               return new StatementSectionsView( $templateFactory, 
$statementListView );
+               return new StatementSectionsView(
+                       $templateFactory,
+                       $statementGrouper,
+                       $statementListView
+               );
        }
 
        /**
-        * @dataProvider statementListsProvider
+        * @dataProvider statementListProvider
         */
        public function testGetHtml( array $statementLists, $expected ) {
-               $view = $this->newInstance();
-               $html = $view->getHtml( $statementLists );
+               $view = $this->newInstance( $statementLists );
+               $html = $view->getHtml( new StatementList() );
                $this->assertSame( $expected, $html );
        }
 
-       public function statementListsProvider() {
-               $empty = new StatementList();
+       public function statementListProvider() {
                $statements = new StatementList();
                $statements->addNewStatement( new PropertyNoValueSnak( 1 ) );
 
@@ -89,15 +100,15 @@
        }
 
        /**
-        * @dataProvider invalidConstructorArgumentProvider
+        * @dataProvider invalidArrayProvider
         */
        public function testGivenInvalidArray_getHtmlFails( $array ) {
-               $view = $this->newInstance();
+               $view = $this->newInstance( $array );
                $this->setExpectedException( 'InvalidArgumentException' );
-               $view->getHtml( $array );
+               $view->getHtml( new StatementList() );
        }
 
-       public function invalidConstructorArgumentProvider() {
+       public function invalidArrayProvider() {
                return array(
                        array( array( 'statements' => array() ) ),
                        array( array( array() ) ),

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ic458864c9d7fd382c4e538613f591fb60539ac8b
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