Skip to content

Commit

Permalink
Avoid static methods when registering Properties (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
s7eph4n authored and mwjames committed Mar 24, 2018
1 parent 94847ea commit d52f9ef
Show file tree
Hide file tree
Showing 11 changed files with 126 additions and 130 deletions.
17 changes: 0 additions & 17 deletions ISSUE_TEMPLATE.md

This file was deleted.

4 changes: 1 addition & 3 deletions SemanticGlossary.php
Expand Up @@ -31,9 +31,7 @@ public static function initExtension() {

$GLOBALS[ 'wgExtensionFunctions' ][] = function () {

$hookRegistry = new HookRegistry(
ApplicationFactory::getInstance()->getStore()
);
$hookRegistry = new HookRegistry();

$hookRegistry->register();
};
Expand Down
10 changes: 5 additions & 5 deletions src/Cache/CacheInvalidator.php
Expand Up @@ -3,7 +3,7 @@
namespace SG\Cache;

use SG\SemanticDataComparator;
use SG\PropertyRegistry;
use SG\PropertyRegistrationHelper;

use SMW\Store;
use SMW\SemanticData;
Expand Down Expand Up @@ -157,10 +157,10 @@ private function hasSemanticDataDeviation( Store $store, SemanticData $semanticD

$dataComparator = new SemanticDataComparator( $store, $semanticData );

return $dataComparator->compareForProperty( PropertyRegistry::SG_TERM ) ||
$dataComparator->compareForProperty( PropertyRegistry::SG_DEFINITION ) ||
$dataComparator->compareForProperty( PropertyRegistry::SG_LINK ) ||
$dataComparator->compareForProperty( PropertyRegistry::SG_STYLE );
return $dataComparator->compareForProperty( PropertyRegistrationHelper::SG_TERM ) ||
$dataComparator->compareForProperty( PropertyRegistrationHelper::SG_DEFINITION ) ||
$dataComparator->compareForProperty( PropertyRegistrationHelper::SG_LINK ) ||
$dataComparator->compareForProperty( PropertyRegistrationHelper::SG_STYLE );
}

private function purgeCache( DIWikiPage $subject ) {
Expand Down
10 changes: 5 additions & 5 deletions src/Cache/ElementsCacheBuilder.php
Expand Up @@ -2,7 +2,7 @@

namespace SG\Cache;

use SG\PropertyRegistry;
use SG\PropertyRegistrationHelper;
use SMW\DataValueFactory;
use SMW\Store;
use SMW\DIProperty;
Expand Down Expand Up @@ -127,7 +127,7 @@ private function buildQuery() {
$dataValueFactory = DataValueFactory::getInstance();

// build term data item and data value for later use
$this->mDiTerm = new DIProperty( PropertyRegistry::SG_TERM );
$this->mDiTerm = new DIProperty( PropertyRegistrationHelper::SG_TERM );
$this->mDvTerm = $dataValueFactory->newDataValueByType( '_txt' );
$this->mDvTerm->setProperty( $this->mDiTerm );

Expand All @@ -136,7 +136,7 @@ private function buildQuery() {
$prTerm = new PrintRequest( PrintRequest::PRINT_PROP, null, $pvTerm );

// build definition data item and data value for later use
$this->mDiDefinition = new DIProperty( PropertyRegistry::SG_DEFINITION );
$this->mDiDefinition = new DIProperty( PropertyRegistrationHelper::SG_DEFINITION );
$this->mDvDefinition = $dataValueFactory->newDataValueByType( '_txt' );
$this->mDvDefinition->setProperty( $this->mDiDefinition );

Expand All @@ -145,7 +145,7 @@ private function buildQuery() {
$prDefinition = new PrintRequest( PrintRequest::PRINT_PROP, null, $pvDefinition );

// build link data item and data value for later use
$this->mDiLink = new DIProperty( PropertyRegistry::SG_LINK );
$this->mDiLink = new DIProperty( PropertyRegistrationHelper::SG_LINK );
$this->mDvLink = $dataValueFactory->newDataValueByType( '_txt' );
$this->mDvLink->setProperty( $this->mDiLink );

Expand All @@ -154,7 +154,7 @@ private function buildQuery() {
$prLink = new PrintRequest( PrintRequest::PRINT_PROP, null, $pvLink );

// build style data item and data value for later use
$this->mDiStyle = new DIProperty( PropertyRegistry::SG_STYLE );
$this->mDiStyle = new DIProperty( PropertyRegistrationHelper::SG_STYLE );
$this->mDvStyle = $dataValueFactory->newDataValueByType( '_txt' );
$this->mDvStyle->setProperty( $this->mDiStyle );

Expand Down
18 changes: 8 additions & 10 deletions src/HookRegistry.php
Expand Up @@ -2,7 +2,6 @@

namespace SG;

use SMW\Store;
use SMW\DIWikiPage;
use Hooks;

Expand All @@ -21,11 +20,9 @@ class HookRegistry {

/**
* @since 1.0
*
* @param Store $store
*/
public function __construct( Store $store ) {
$this->addCallbackHandlers( $store );
public function __construct() {
$this->addCallbackHandlers();
}

/**
Expand Down Expand Up @@ -59,15 +56,16 @@ public function register() {
}
}

private function addCallbackHandlers( $store ) {

$propertyRegistry = new PropertyRegistry();
private function addCallbackHandlers() {

/**
* @see https://github.com/SemanticMediaWiki/SemanticMediaWiki/blob/master/docs/technical/hooks.md
*/
$this->handlers['SMW::Property::initProperties'] = function () use ( $propertyRegistry ) {
return PropertyRegistry::getInstance()->register();
$this->handlers['SMW::Property::initProperties'] = function ( $propertyRegistry ) {

$propertyRegistrationHelper = new PropertyRegistrationHelper( $propertyRegistry );
return $propertyRegistrationHelper->registerProperties();

};

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Maintenance/GlossaryCacheRebuilder.php
Expand Up @@ -2,7 +2,7 @@

namespace SG\Maintenance;

use SG\PropertyRegistry;
use SG\PropertyRegistrationHelper;
use SG\Cache\GlossaryCache;

use SMWUpdateJob as UpdateJob;
Expand Down Expand Up @@ -113,7 +113,7 @@ private function updateSelectedPages( array $pages ) {
private function buildQuery() {

$description = new SomeProperty(
new DIProperty( PropertyRegistry::SG_TERM ),
new DIProperty( PropertyRegistrationHelper::SG_TERM ),
new ThingDescription()
);

Expand Down
36 changes: 12 additions & 24 deletions src/PropertyRegistry.php → src/PropertyRegistrationHelper.php
Expand Up @@ -2,7 +2,7 @@

namespace SG;

use SMW\DIProperty;
use SMW\PropertyRegistry;

define( 'SG_PROP_GLT', 'Glossary-Term' );
define( 'SG_PROP_GLD', 'Glossary-Definition' );
Expand All @@ -17,42 +17,30 @@
*
* @author mwjames
*/
class PropertyRegistry {
class PropertyRegistrationHelper {

const SG_TERM = '___glt';
const SG_DEFINITION = '___gld';
const SG_LINK = '___gll';
const SG_STYLE = '___gls';

protected static $instance = null;
private $propertyRegistry;

/**
* @since 1.0
* PropertyRegistry constructor.
*
* @return PropertyRegistry
*/
public static function getInstance() {

if ( self::$instance === null ) {
self::$instance = new self();
}

return self::$instance;
}

/**
* @since 1.0
* @param PropertyRegistry $propertyRegistry
*/
public static function clear() {
self::$instance = null;
public function __construct( PropertyRegistry $propertyRegistry ) {
$this->propertyRegistry = $propertyRegistry;
}

/**
* @since 1.0
*
* @return boolean
*/
public function register() {
public function registerProperties() {

$propertyDefinitions = array(
self::SG_TERM => array(
Expand Down Expand Up @@ -81,21 +69,21 @@ public function register() {
}

/**
* @param array $propertyList
* @param string[][] $propertyList
* @return bool
*/
protected function registerPropertiesFromList(array $propertyList ) {
protected function registerPropertiesFromList( array $propertyList ) {

foreach ( $propertyList as $propertyId => $definition ) {

\SMW\PropertyRegistry::getInstance()->registerProperty(
$this->propertyRegistry->registerProperty(
$propertyId,
$definition['type'],
$definition['label'],
true
);

\SMW\PropertyRegistry::getInstance()->registerPropertyAlias(
$this->propertyRegistry->registerPropertyAlias(
$propertyId,
$definition['alias']
);
Expand Down
6 changes: 3 additions & 3 deletions tests/phpunit/Integration/MwDBSQLStoreIntegrationTest.php
Expand Up @@ -2,7 +2,7 @@

namespace SG\Tests\Integration;

use SG\PropertyRegistry;
use SG\PropertyRegistrationHelper;

use SMW\Tests\MwDBaseUnitTestCase;
use SMW\Tests\Utils\UtilityFactory;
Expand Down Expand Up @@ -55,7 +55,7 @@ public function testPageCreateDeleteStoreIntegration() {

$values = $this->getStore()->getPropertyValues(
DIWikiPage::newFromTitle( $title ),
new DIProperty( PropertyRegistry::SG_TERM )
new DIProperty( PropertyRegistrationHelper::SG_TERM )
);

$this->assertNotEmpty( $values );
Expand All @@ -65,7 +65,7 @@ public function testPageCreateDeleteStoreIntegration() {

$values = $this->getStore()->getPropertyValues(
DIWikiPage::newFromTitle( $title ),
new DIProperty( PropertyRegistry::SG_TERM )
new DIProperty( PropertyRegistrationHelper::SG_TERM )
);

$this->assertEmpty( $values );
Expand Down
6 changes: 3 additions & 3 deletions tests/phpunit/Unit/Cache/CacheInvalidatorTest.php
Expand Up @@ -2,7 +2,7 @@

namespace SG\Tests\Cache;

use SG\PropertyRegistry;
use SG\PropertyRegistrationHelper;
use SG\Cache\CacheInvalidator;
use SG\Cache\GlossaryCache;

Expand Down Expand Up @@ -64,12 +64,12 @@ public function testInvalidateOnUpdateWithDifferentSubobjectData() {
$subobject->setSemanticData( '_999999' );

$subobject->getSemanticData()->addPropertyObjectValue(
new DIProperty( PropertyRegistry::SG_TERM ),
new DIProperty( PropertyRegistrationHelper::SG_TERM ),
new DIBlob( 'Foo' )
);

$subobject->getSemanticData()->addPropertyObjectValue(
new DIProperty( PropertyRegistry::SG_DEFINITION ),
new DIProperty( PropertyRegistrationHelper::SG_DEFINITION ),
new DIBlob( 'Bar' )
);

Expand Down
87 changes: 87 additions & 0 deletions tests/phpunit/Unit/PropertyRegistrationHelperTest.php
@@ -0,0 +1,87 @@
<?php

namespace SG\Tests;

use SG\PropertyRegistrationHelper;
use SMW\DIProperty;

/**
* @covers \SG\PropertyRegistrationHelper
* @group semantic-glossary
*
* @license GNU GPL v2+
* @since 1.0
*
* @author mwjames
*/
class PropertyRegistrationHelperTest extends \PHPUnit_Framework_TestCase {

public function testCanConstruct() {

$propertyRegistry =
$this->getMockBuilder( '\SMW\PropertyRegistry' )
->disableOriginalConstructor()
->getMock();

$propertyRegistrationHelper = new PropertyRegistrationHelper( $propertyRegistry );

$this->assertInstanceOf(
'\SG\PropertyRegistrationHelper',
$propertyRegistrationHelper
);
}

public function testRegisterProperties() {

$propertyRegistry =
$this->getMockBuilder( '\SMW\PropertyRegistry' )
->disableOriginalConstructor()
->getMock();

$propertyRegistry
->expects( $this->exactly( 4 ) )
->method( 'registerProperty' );

$propertyRegistry
->expects( $this->exactly( 4 ) )
->method( 'registerPropertyAlias' );


$propertyRegistrationHelper = new PropertyRegistrationHelper( $propertyRegistry );

$this->assertTrue(
$propertyRegistrationHelper->registerProperties()
);
}

/**
* @dataProvider propertyDefinitionDataProvider
*
* @param string $id
* @param string $label
*/
public function testRegisteredPropertyById( $id, $label ) {

$property = new DIProperty( $id );

$this->assertInstanceOf( '\SMW\DIProperty', $property );
$this->assertEquals( $label, $property->getLabel() );
$this->assertTrue( $property->isShown() );
}

/**
* @return string[][]
*/
public function propertyDefinitionDataProvider() {

$provider = array();

$provider[] = array( PropertyRegistrationHelper::SG_TERM, SG_PROP_GLT );
$provider[] = array( PropertyRegistrationHelper::SG_DEFINITION, SG_PROP_GLD );
$provider[] = array( PropertyRegistrationHelper::SG_LINK, SG_PROP_GLL );
$provider[] = array( PropertyRegistrationHelper::SG_STYLE, SG_PROP_GLS );

return $provider;
}

}

0 comments on commit d52f9ef

Please sign in to comment.