Foxtrott has submitted this change and it was merged. Change subject: Add 'SG\PropertyRegistry' ......................................................................
Add 'SG\PropertyRegistry' - Fixed Deprecated use of wfMsg - Added unit test - Fixed wrong invocation for "registerPropertyAliases" Change-Id: I9e9a0b6fbeef1b26ec4cb0bce2a752a2de693d04 --- M .gitignore M SemanticGlossary.php M SemanticGlossaryBackend.php M composer.json A phpunit.xml.dist A src/PropertyRegistry.php A tests/bootstrap.php A tests/phpunit/PropertyRegistryTest.php 8 files changed, 225 insertions(+), 29 deletions(-) Approvals: Foxtrott: Verified; Looks good to me, approved diff --git a/.gitignore b/.gitignore index da5d6dd..949b9d8 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,10 @@ *~ *.kate-swp .*.swp + +vendor/ +extensions/ +build/ + +composer.phar +composer.lock \ No newline at end of file diff --git a/SemanticGlossary.php b/SemanticGlossary.php index 36bf63c..164197b 100644 --- a/SemanticGlossary.php +++ b/SemanticGlossary.php @@ -34,7 +34,7 @@ /** * The Semantic Glossary version */ - define( 'SG_VERSION', '1.0.0' ); + define( 'SG_VERSION', '1.0.1-dev' ); // register the extension $GLOBALS[ 'wgExtensionCredits' ][ 'semantic' ][] = array( @@ -60,15 +60,13 @@ $autoloadClasses = array( 'SemanticGlossaryBackend' => $dir . '/SemanticGlossaryBackend.php', 'SemanticGlossaryCacheHandling' => $dir . '/SemanticGlossaryCacheHandling.php', + 'SG\PropertyRegistry' => $dir . '/src/PropertyRegistry.php', ); $GLOBALS[ 'wgAutoloadClasses' ] = array_merge( $GLOBALS[ 'wgAutoloadClasses' ], $autoloadClasses ); // register hook handlers $hooks = array( - 'smwInitProperties' => array( 'SemanticGlossaryBackend::registerProperties' ), - 'smwInitDatatypes' => array( 'SemanticGlossaryBackend::registerPropertyAliases' ), - 'SMWStore::updateDataBefore' => array( 'SemanticGlossaryCacheHandling::purgeCacheForData' ), // invalidate on update 'smwDeleteSemanticData' => array( 'SemanticGlossaryCacheHandling::purgeCacheForSubject' ), // invalidate on delete 'TitleMoveComplete' => array( 'SemanticGlossaryCacheHandling::purgeCacheForTitle' ), // move annotations @@ -81,4 +79,13 @@ define( 'SG_PROP_GLL', 'Glossary-Link' ); define( 'SG_PROP_GLS', 'Glossary-Style' ); + /** + * Register properties + * + * @since 1.0 + */ + $GLOBALS['wgHooks']['smwInitProperties'][] = function () { + return \SG\PropertyRegistry::getInstance()->registerPropertiesAndAliases(); + }; + } ); diff --git a/SemanticGlossaryBackend.php b/SemanticGlossaryBackend.php index 2164a11..68fc0dc 100644 --- a/SemanticGlossaryBackend.php +++ b/SemanticGlossaryBackend.php @@ -193,29 +193,4 @@ return true; } - /** - * Hook handler for registering semantic properties - * - * @return bool - */ - static function registerProperties() { - SMWDIProperty::registerProperty( '___glt', '_str', SG_PROP_GLT, true ); - SMWDIProperty::registerProperty( '___gld', '_txt', SG_PROP_GLD, true ); - SMWDIProperty::registerProperty( '___gll', '_str', SG_PROP_GLL, true ); - SMWDIProperty::registerProperty( '___gls', '_txt', SG_PROP_GLS, true ); - return true; - } - - /** - * Hook handler for registering property aliases - * - * @return bool - */ - static function registerPropertyAliases() { - SMWDIProperty::registerPropertyAlias( '___glt', wfMsg( 'semanticglossary-prop-glt' ) ); - SMWDIProperty::registerPropertyAlias( '___gld', wfMsg( 'semanticglossary-prop-gld' ) ); - SMWDIProperty::registerPropertyAlias( '___gll', wfMsg( 'semanticglossary-prop-gll' ) ); - SMWDIProperty::registerPropertyAlias( '___gls', wfMsg( 'semanticglossary-prop-gls' ) ); - return true; - } } diff --git a/composer.json b/composer.json index aa2b80a..04fe1ec 100644 --- a/composer.json +++ b/composer.json @@ -38,6 +38,10 @@ "autoload" : { "files": [ "SemanticGlossary.php" + ], + "classmap": [ + "src/", + "tests/phpunit/" ] } } diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 0000000..a548810 --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,25 @@ +<phpunit backupGlobals="false" + backupStaticAttributes="false" + bootstrap="tests/bootstrap.php" + cacheTokens="false" + colors="true" + convertErrorsToExceptions="true" + convertNoticesToExceptions="true" + convertWarningsToExceptions="true" + stopOnError="false" + stopOnFailure="false" + stopOnIncomplete="false" + stopOnSkipped="false" + strict="true" + verbose="true"> + <testsuites> + <testsuite name="SemanticGlossary"> + <directory>tests/phpunit</directory> + </testsuite> + </testsuites> + <filter> + <whitelist addUncoveredFilesFromWhitelist="true"> + <directory suffix=".php">src</directory> + </whitelist> + </filter> +</phpunit> diff --git a/src/PropertyRegistry.php b/src/PropertyRegistry.php new file mode 100644 index 0000000..99181df --- /dev/null +++ b/src/PropertyRegistry.php @@ -0,0 +1,98 @@ +<?php + +namespace SG; + +use SMW\DIProperty; + +/** + * @ingroup SG + * + * @licence GNU GPL v2+ + * @since 1.0 + * + * @author mwjames + */ +class PropertyRegistry { + + const SG_TERM = '___glt'; + const SG_DEFINITION = '___gld'; + const SG_LINK = '___gll'; + const SG_STYLE = '___gls'; + + protected static $instance = null; + + /** + * @since 1.0 + * + * @return PropertyRegistry + */ + public static function getInstance() { + + if ( self::$instance === null ) { + self::$instance = new self(); + } + + return self::$instance; + } + + /** + * @since 1.0 + */ + public static function clear() { + self::$instance = null; + } + + /** + * @since 1.0 + * + * @return boolean + */ + public function registerPropertiesAndAliases() { + + $propertyDefinitions = array( + self::SG_TERM => array( + 'label' => SG_PROP_GLT, + 'type' => '_txt', + 'alias' => wfMessage( 'semanticglossary-prop-glt' )->text() + ), + self::SG_DEFINITION => array( + 'label' => SG_PROP_GLD, + 'type' => '_txt', + 'alias' => wfMessage( 'semanticglossary-prop-gld' )->text() + ), + self::SG_LINK => array( + 'label' => SG_PROP_GLL, + 'type' => '_txt', + 'alias' => wfMessage( 'semanticglossary-prop-gll' )->text() + ), + self::SG_STYLE => array( + 'label' => SG_PROP_GLS, + 'type' => '_txt', + 'alias' => wfMessage( 'semanticglossary-prop-gls' )->text() + ) + ); + + return $this->registerPropertiesFromList( $propertyDefinitions ); + } + + protected function registerPropertiesFromList( array $propertyList ) { + + foreach ( $propertyList as $propertyId => $definition ) { + + DIProperty::registerProperty( + $propertyId, + $definition['type'], + $definition['label'], + true + ); + + DIProperty::registerPropertyAlias( + $propertyId, + $definition['alias'] + ); + } + + return true; + } + +} diff --git a/tests/bootstrap.php b/tests/bootstrap.php new file mode 100644 index 0000000..d7ebbed --- /dev/null +++ b/tests/bootstrap.php @@ -0,0 +1,21 @@ +<?php + +/** + * PHPUnit test bootstrap file + * + * @licence GNU GPL v2+ + * @author Jeroen De Dauw < jeroended...@gmail.com > + */ + +if ( php_sapi_name() !== 'cli' ) { + die( 'Not an entry point' ); +} + +$pwd = getcwd(); +chdir( __DIR__ . '/..' ); +passthru( 'composer update' ); +chdir( $pwd ); + +if ( !is_readable( __DIR__ . '/../vendor/autoload.php' ) ) { + die( 'You need to install this package with Composer before you can run the tests' ); +} diff --git a/tests/phpunit/PropertyRegistryTest.php b/tests/phpunit/PropertyRegistryTest.php new file mode 100644 index 0000000..49b3a3c --- /dev/null +++ b/tests/phpunit/PropertyRegistryTest.php @@ -0,0 +1,59 @@ +<?php + +namespace SG\Tests; + +use SG\PropertyRegistry; +use SMW\DIProperty; + +/** + * @covers \SG\PropertyRegistry + * + * @ingroup Test + * + * @group SG + * @group SGExtension + * + * @licence GNU GPL v2+ + * @since 1.0 + * + * @author mwjames + */ +class PropertyRegistryTest extends \PHPUnit_Framework_TestCase { + + public function testCanConstruct() { + $this->assertInstanceOf( + '\SG\PropertyRegistry', + PropertyRegistry::getInstance() + ); + } + + public function testRegisterPropertiesAndAliases() { + PropertyRegistry::clear(); + $this->assertTrue( PropertyRegistry::getInstance()->registerPropertiesAndAliases() ); + } + + /** + * @dataProvider propertyDefinitionDataProvider + */ + public function testRegisteredPropertyById( $id, $label ) { + + $property = new DIProperty( $id ); + + $this->assertInstanceOf( '\SMW\DIProperty', $property ); + $this->assertEquals( $label, $property->getLabel() ); + $this->assertTrue( $property->isShown() ); + } + + public function propertyDefinitionDataProvider() { + + $provider = array(); + + $provider[] = array( PropertyRegistry::SG_TERM, SG_PROP_GLT ); + $provider[] = array( PropertyRegistry::SG_DEFINITION, SG_PROP_GLD ); + $provider[] = array( PropertyRegistry::SG_LINK, SG_PROP_GLL ); + $provider[] = array( PropertyRegistry::SG_STYLE, SG_PROP_GLS ); + + return $provider; + } + +} -- To view, visit https://gerrit.wikimedia.org/r/117159 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: I9e9a0b6fbeef1b26ec4cb0bce2a752a2de693d04 Gerrit-PatchSet: 3 Gerrit-Project: mediawiki/extensions/SemanticGlossary Gerrit-Branch: master Gerrit-Owner: Mwjames <jamesin.hongkon...@gmail.com> Gerrit-Reviewer: Foxtrott <s7ep...@gmail.com> Gerrit-Reviewer: Mwjames <jamesin.hongkon...@gmail.com> Gerrit-Reviewer: jenkins-bot <> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits