jenkins-bot has submitted this change and it was merged.
Change subject: Improvements to EntityContentFactory and its tests
......................................................................
Improvements to EntityContentFactory and its tests
Change-Id: I563d902b3d6a5c9b84b4a43652dc8d0f90c7d57b
---
M DataModel/DataModel/Snak/PropertyValueSnak.php
M lib/tests/phpunit/store/WikiPageEntityLookupTest.php
M repo/includes/WikibaseRepo.php
M repo/includes/content/EntityContentFactory.php
M repo/tests/phpunit/includes/content/EntityContentFactoryTest.php
5 files changed, 106 insertions(+), 18 deletions(-)
Approvals:
Daniel Kinzler: Looks good to me, approved
jenkins-bot: Verified
diff --git a/DataModel/DataModel/Snak/PropertyValueSnak.php
b/DataModel/DataModel/Snak/PropertyValueSnak.php
index 2a9e3db..fcc4d7b 100644
--- a/DataModel/DataModel/Snak/PropertyValueSnak.php
+++ b/DataModel/DataModel/Snak/PropertyValueSnak.php
@@ -124,6 +124,7 @@
* The DataValue
*
* @since 0.3
+ * @deprecated since 0.4
*
* @param EntityId $propertyId
* @param mixed $rawDataValue
diff --git a/lib/tests/phpunit/store/WikiPageEntityLookupTest.php
b/lib/tests/phpunit/store/WikiPageEntityLookupTest.php
index cacbe41..1b9636e 100644
--- a/lib/tests/phpunit/store/WikiPageEntityLookupTest.php
+++ b/lib/tests/phpunit/store/WikiPageEntityLookupTest.php
@@ -111,6 +111,7 @@
throw new \MWException( "Can't generate test entities
in a client database." );
}
+ // FIXME: this is using repo functionality
$content =
\Wikibase\EntityContentFactory::singleton()->newFromEntity( $entity );
$status = $content->save( "storeTestEntity" );
diff --git a/repo/includes/WikibaseRepo.php b/repo/includes/WikibaseRepo.php
index d63ec1b..62e40f2 100644
--- a/repo/includes/WikibaseRepo.php
+++ b/repo/includes/WikibaseRepo.php
@@ -5,6 +5,7 @@
use DataTypes\DataTypeFactory;
use ValueFormatters\FormatterOptions;
use ValueParsers\ParserOptions;
+use Wikibase\EntityContentFactory;
use Wikibase\Lib\EntityIdFormatter;
use Wikibase\Lib\EntityIdLabelFormatter;
use Wikibase\Lib\EntityIdParser;
@@ -84,6 +85,20 @@
/**
* @since 0.4
*
+ * @return EntityContentFactory
+ */
+ public function getEntityContentFactory() {
+ $entityNamespaces = $this->settings->getSetting(
'entityNamespaces' );
+
+ return new EntityContentFactory(
+ $this->getIdFormatter(),
+ is_array( $entityNamespaces ) ? array_keys(
$entityNamespaces ) : array()
+ );
+ }
+
+ /**
+ * @since 0.4
+ *
* @return EntityIdFormatter
*/
public function getIdFormatter() {
diff --git a/repo/includes/content/EntityContentFactory.php
b/repo/includes/content/EntityContentFactory.php
index 6e47e9b..10f71c5 100644
--- a/repo/includes/content/EntityContentFactory.php
+++ b/repo/includes/content/EntityContentFactory.php
@@ -1,7 +1,13 @@
<?php
namespace Wikibase;
-use MWException, Title, WikiPage, Revision;
+
+use MWException;
+use Title;
+use WikiPage;
+use Revision;
+use Wikibase\Lib\EntityIdFormatter;
+use Wikibase\Repo\WikibaseRepo;
/**
* Factory for EntityContent objects.
@@ -33,6 +39,7 @@
/**
* @since 0.2
+ * @deprecated since 0.4
*
* @return EntityContentFactory
*/
@@ -40,10 +47,24 @@
static $instance = false;
if ( $instance === false ) {
- $instance = new static();
+ $instance =
WikibaseRepo::newInstance()->getEntityContentFactory();
}
return $instance;
+ }
+
+ // TODO: inject this map and allow extensions to somehow extend it
+ protected static $typeMap = array(
+ Item::ENTITY_TYPE => CONTENT_MODEL_WIKIBASE_ITEM,
+ Property::ENTITY_TYPE => CONTENT_MODEL_WIKIBASE_PROPERTY,
+ );
+
+ protected $idFormatter;
+ protected $contentModelIds;
+
+ public function __construct( EntityIdFormatter $idFormatter, array
$contentModelIds ) {
+ $this->idFormatter = $idFormatter;
+ $this->contentModelIds = $contentModelIds;
}
/**
@@ -62,15 +83,13 @@
/**
* Returns a list of content model IDs that are used to represent
Wikibase entities.
- * Configured via $wgWBRepoSettings['entityNamespaces'].
*
* @since 0.2
*
* @return array An array of string content model IDs.
*/
public function getEntityContentModels() {
- $namespaces = Settings::get( 'entityNamespaces' );
- return is_array( $namespaces ) ? array_keys( $namespaces ) :
array();
+ return $this->contentModelIds;
}
/**
@@ -139,7 +158,7 @@
*/
public function getTitleForId( EntityId $id ) {
return Title::newFromText(
- $id->getPrefixedId(),
+ $this->idFormatter->format( $id ),
NamespaceUtils::getEntityNamespace(
self::$typeMap[$id->getEntityType()] )
);
}
@@ -177,12 +196,6 @@
return $revision->getContent();
}
-
- // TODO: move to sane place
- protected static $typeMap = array(
- Item::ENTITY_TYPE => CONTENT_MODEL_WIKIBASE_ITEM,
- Property::ENTITY_TYPE => CONTENT_MODEL_WIKIBASE_PROPERTY,
- );
/**
* Constructs a new EntityContent from an Entity.
diff --git a/repo/tests/phpunit/includes/content/EntityContentFactoryTest.php
b/repo/tests/phpunit/includes/content/EntityContentFactoryTest.php
index 14f0787..7935334 100644
--- a/repo/tests/phpunit/includes/content/EntityContentFactoryTest.php
+++ b/repo/tests/phpunit/includes/content/EntityContentFactoryTest.php
@@ -1,10 +1,12 @@
<?php
namespace Wikibase\Test;
+
use Wikibase\EntityContentFactory;
+use Wikibase\EntityId;
/**
- * Tests for the Wikibase\EntityContentFactory class.
+ * @covers Wikibase\EntityContentFactory
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -30,19 +32,48 @@
* @group Wikibase
* @group WikibaseEntity
* @group WikibaseContent
- * @group Database
*
* @licence GNU GPL v2+
* @author Jeroen De Dauw < [email protected] >
*/
-class EntityContentFactoryTest extends \MediaWikiTestCase {
+class EntityContentFactoryTest extends \PHPUnit_Framework_TestCase {
- public function testGetEntityContentModels() {
- $this->assertInternalType( 'array',
EntityContentFactory::singleton()->getEntityContentModels() );
+ /**
+ * @dataProvider contentModelsProvider
+ */
+ public function testGetEntityContentModels( array $contentModelIds ) {
+ $factory = new EntityContentFactory(
+ $this->newMockIdFormatter(),
+ $contentModelIds
+ );
+
+ $this->assertEquals( $contentModelIds,
$factory->getEntityContentModels() );
+ }
+
+ protected function newMockIdFormatter() {
+ $idFormatter = $this->getMockBuilder(
'Wikibase\Lib\EntityIdFormatter' )
+ ->disableOriginalConstructor()->getMock();
+
+ $idFormatter->expects( $this->any() )
+ ->method( 'format' )
+ ->will( $this->returnValue( 'Nyan' ) );
+
+ return $idFormatter;
+ }
+
+ public function contentModelsProvider() {
+ $argLists = array();
+
+ $argLists[] = array( array() );
+ $argLists[] = array( array( 0 ) );
+ $argLists[] = array( array( 42, 1337, 9001 ) );
+ $argLists[] = array( array( 0, 1, 2, 3, 4, 5, 6, 7 ) );
+
+ return $argLists;
}
public function testIsEntityContentModel() {
- $factory = EntityContentFactory::singleton();
+ $factory = $this->newFactory();
foreach ( $factory->getEntityContentModels() as $type ) {
$this->assertTrue( $factory->isEntityContentModel(
$type ) );
@@ -51,4 +82,31 @@
$this->assertFalse( $factory->isEntityContentModel(
'this-does-not-exist' ) );
}
+ protected function newFactory() {
+ return $factory = new EntityContentFactory(
+ $this->newMockIdFormatter(),
+ array( 42, 1337, 9001 )
+ );
+ }
+
+ public function testGetTitleForId() {
+ $factory = $this->newFactory();
+
+ $title = $factory->getTitleForId( new EntityId( 'item', 42 ) );
+
+ $this->assertEquals( 'Nyan', $title->getText() );
+ }
+
+ public function testGetWikiPageForId() {
+ $entityId = new EntityId( 'item', 42 );
+
+ $factory = $this->newFactory();
+
+ $expectedTitle = $factory->getTitleForId( $entityId );
+
+ $wikiPage = $factory->getWikiPageForId( $entityId );
+
+ $this->assertEquals( $expectedTitle, $wikiPage->getTitle() );
+ }
+
}
--
To view, visit https://gerrit.wikimedia.org/r/64494
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I563d902b3d6a5c9b84b4a43652dc8d0f90c7d57b
Gerrit-PatchSet: 2
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Jeroen De Dauw <[email protected]>
Gerrit-Reviewer: Anja Jentzsch <[email protected]>
Gerrit-Reviewer: Ataherivand <[email protected]>
Gerrit-Reviewer: Aude <[email protected]>
Gerrit-Reviewer: Daniel Kinzler <[email protected]>
Gerrit-Reviewer: Daniel Werner <[email protected]>
Gerrit-Reviewer: Denny Vrandecic <[email protected]>
Gerrit-Reviewer: Henning Snater <[email protected]>
Gerrit-Reviewer: Jens Ohlig <[email protected]>
Gerrit-Reviewer: Jeroen De Dauw <[email protected]>
Gerrit-Reviewer: John Erling Blad <[email protected]>
Gerrit-Reviewer: Lydia Pintscher <[email protected]>
Gerrit-Reviewer: Markus Kroetzsch <[email protected]>
Gerrit-Reviewer: Nikola Smolenski <[email protected]>
Gerrit-Reviewer: Silke Meyer <[email protected]>
Gerrit-Reviewer: Tobias Gritschacher <[email protected]>
Gerrit-Reviewer: jenkins-bot
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits