Jeroen De Dauw has uploaded a new change for review. https://gerrit.wikimedia.org/r/73005
Change subject: Added groundwork for the dependency injection system ...................................................................... Added groundwork for the dependency injection system Change-Id: I2dfa4dc4820f5154b8342b4d85ac7fc98c6c2546 --- A Tests/Phpunit/Wikibase/Query/DIC/DependencyBuilderTest.php A Tests/Phpunit/Wikibase/Query/DIC/DependencyManagerTest.php A Tests/Phpunit/Wikibase/Query/DIC/ExtensionAccessTest.php A Tests/Phpunit/Wikibase/Query/DIC/ExtensionRegistryTest.php R Tests/Phpunit/Wikibase/Query/QueryEntityTest.php M WikibaseQuery.php A src/Wikibase/Query/Api/EntitiesByPropertyValue.php A src/Wikibase/Query/Api/EntitiesByPropertyValueWrapper.php A src/Wikibase/Query/DIC/DependencyBuilder.php A src/Wikibase/Query/DIC/DependencyManager.php A src/Wikibase/Query/DIC/ExtensionAccess.php A src/Wikibase/Query/DIC/ExtensionRegistry.php A src/Wikibase/Query/WikibaseQuery.php 13 files changed, 420 insertions(+), 25 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/WikibaseQuery refs/changes/05/73005/1 diff --git a/Tests/Phpunit/Wikibase/Query/DIC/DependencyBuilderTest.php b/Tests/Phpunit/Wikibase/Query/DIC/DependencyBuilderTest.php new file mode 100644 index 0000000..3732d0b --- /dev/null +++ b/Tests/Phpunit/Wikibase/Query/DIC/DependencyBuilderTest.php @@ -0,0 +1,21 @@ +<?php + +namespace Tests\Phpunit\Wikibase\Query\DIC; + +/** + * @covers Wikibase\Query\DIC\DependencyBuilder + * + * @file + * @ingroup WikibaseQuery + * @group WikibaseQuery + * + * @licence GNU GPL v2+ + * @author Jeroen De Dauw < [email protected] > + */ +class DependencyBuilderTest extends \PHPUnit_Framework_TestCase { + + public function testTrue() { + $this->assertTrue( true ); // TODO + } + +} diff --git a/Tests/Phpunit/Wikibase/Query/DIC/DependencyManagerTest.php b/Tests/Phpunit/Wikibase/Query/DIC/DependencyManagerTest.php new file mode 100644 index 0000000..4a848c8 --- /dev/null +++ b/Tests/Phpunit/Wikibase/Query/DIC/DependencyManagerTest.php @@ -0,0 +1,93 @@ +<?php + +namespace Tests\Phpunit\Wikibase\Query\DIC; + +use Wikibase\Query\DIC\DependencyManager; + +/** + * @covers Wikibase\Query\DIC\DependencyManager + * + * @file + * @ingroup WikibaseQuery + * @group WikibaseQuery + * + * @licence GNU GPL v2+ + * @author Jeroen De Dauw < [email protected] > + */ +class DependencyManagerTest extends \PHPUnit_Framework_TestCase { + + public function testCanConstruct() { + new DependencyManager(); + $this->assertTrue( true ); + } + + public function testRegisterBuilder() { + $dependencyManager = new DependencyManager(); + + $fooBuilder = $this->getMock( 'Wikibase\Query\DIC\DependencyBuilder' ); + + $dependencyManager->registerBuilder( 'foo', $fooBuilder ); + + $this->assertTrue( true ); + } + + public function testNewObject() { + $dependencyManager = new DependencyManager(); + + $expectedObject = new \stdClass(); + $expectedObject->awesomeness = 9001; + + $fooBuilder = $this->getMock( 'Wikibase\Query\DIC\DependencyBuilder' ); + $fooBuilder->expects( $this->once() ) + ->method( 'buildObject' ) + ->with( $this->equalTo( $dependencyManager ) ) + ->will( $this->returnValue( $expectedObject ) ); + + $dependencyManager->registerBuilder( 'foo', $fooBuilder ); + $actualObject = $dependencyManager->newObject( 'foo' ); + + $this->assertEquals( $expectedObject, $actualObject ); + } + + /** + * @dataProvider nonStringProvider + */ + public function testRegisterBuilderRequiresNonEmptyStringKey( $nonString ) { + $dependencyManager = new DependencyManager(); + + $fooBuilder = $this->getMock( 'Wikibase\Query\DIC\DependencyBuilder' ); + + $this->setExpectedException( 'InvalidArgumentException' ); + $dependencyManager->registerBuilder( $nonString, $fooBuilder ); + } + + /** + * @dataProvider nonStringProvider + */ + public function testNewObjectRequiresNonEmptyStringKey( $nonString ) { + $dependencyManager = new DependencyManager(); + + $this->setExpectedException( 'InvalidArgumentException' ); + $dependencyManager->newObject( $nonString ); + } + + public function nonStringProvider() { + return array( + array( 4 ), + array( 4.2 ), + array( null ), + array( array() ), + array( true ), + array( '' ) + ); + } + + public function testNewObjectWithNonRegisteredKey() { + $dependencyManager = new DependencyManager(); + + $this->setExpectedException( 'OutOfBoundsException' ); + + $dependencyManager->newObject( 'notRegistered' ); + } + +} diff --git a/Tests/Phpunit/Wikibase/Query/DIC/ExtensionAccessTest.php b/Tests/Phpunit/Wikibase/Query/DIC/ExtensionAccessTest.php new file mode 100644 index 0000000..cdb05c3 --- /dev/null +++ b/Tests/Phpunit/Wikibase/Query/DIC/ExtensionAccessTest.php @@ -0,0 +1,21 @@ +<?php + +namespace Tests\Phpunit\Wikibase\Query\DIC; + +/** + * @covers Wikibase\Query\DIC\ExtensionAccess + * + * @file + * @ingroup WikibaseQuery + * @group WikibaseQuery + * + * @licence GNU GPL v2+ + * @author Jeroen De Dauw < [email protected] > + */ +class ExtensionAccessTest extends \PHPUnit_Framework_TestCase { + + public function testTrue() { + $this->assertTrue( true ); // TODO + } + +} diff --git a/Tests/Phpunit/Wikibase/Query/DIC/ExtensionRegistryTest.php b/Tests/Phpunit/Wikibase/Query/DIC/ExtensionRegistryTest.php new file mode 100644 index 0000000..9373079 --- /dev/null +++ b/Tests/Phpunit/Wikibase/Query/DIC/ExtensionRegistryTest.php @@ -0,0 +1,21 @@ +<?php + +namespace Tests\Phpunit\Wikibase\Query\DIC; + +/** + * @covers Wikibase\Query\DIC\ExtensionRegistry + * + * @file + * @ingroup WikibaseQuery + * @group WikibaseQuery + * + * @licence GNU GPL v2+ + * @author Jeroen De Dauw < [email protected] > + */ +class ExtensionRegistryTest extends \PHPUnit_Framework_TestCase { + + public function testTrue() { + $this->assertTrue( true ); // TODO + } + +} diff --git a/Tests/Phpunit/QueryEntityTest.php b/Tests/Phpunit/Wikibase/Query/QueryEntityTest.php similarity index 94% rename from Tests/Phpunit/QueryEntityTest.php rename to Tests/Phpunit/Wikibase/Query/QueryEntityTest.php index aa2c2c7..ae78f89 100644 --- a/Tests/Phpunit/QueryEntityTest.php +++ b/Tests/Phpunit/Wikibase/Query/QueryEntityTest.php @@ -1,11 +1,12 @@ <?php -namespace Wikibase\Test; +namespace Tests\Phpunit\Wikibase\Query; use Ask\Language\Description\AnyValue; use Ask\Language\Option\QueryOptions; use Ask\Language\Query; use Wikibase\Query\QueryEntity; +use Wikibase\Test\EntityTest; /** * @covers Wikibase\Query\QueryEntity diff --git a/WikibaseQuery.php b/WikibaseQuery.php index a877cf6..0af4e2a 100644 --- a/WikibaseQuery.php +++ b/WikibaseQuery.php @@ -62,6 +62,30 @@ throw new Exception( 'Wikibase Query depends on the Wikibase QueryEngine component.' ); } +// @codeCoverageIgnoreStart +spl_autoload_register( function ( $className ) { + $className = ltrim( $className, '\\' ); + $fileName = ''; + $namespace = ''; + + if ( $lastNsPos = strripos( $className, '\\') ) { + $namespace = substr( $className, 0, $lastNsPos ); + $className = substr( $className, $lastNsPos + 1 ); + $fileName = str_replace( '\\', '/', $namespace ) . '/'; + } + + $fileName .= str_replace( '_', '/', $className ) . '.php'; + + $namespaceSegments = explode( '\\', $namespace ); + + if ( $namespaceSegments[0] === 'Wikibase' && count( $namespaceSegments ) > 1 && $namespaceSegments[1] === 'Query' ) { + if ( count( $namespaceSegments ) === 2 || $namespaceSegments[2] !== 'Tests' ) { + require_once __DIR__ . '/src/' . $fileName; + } + } +} ); +// @codeCoverageIgnoreEnd + call_user_func( function() { global $wgExtensionCredits, $wgExtensionMessagesFiles, $wgHooks, $wgWBRepoSettings; global $wgExtraNamespaces, $wgContentHandlers; @@ -78,30 +102,6 @@ ); $wgExtensionMessagesFiles['WikibaseQuery'] = __DIR__ . '/WikibaseQuery.i18n.php'; - - // @codeCoverageIgnoreStart - spl_autoload_register( function ( $className ) { - $className = ltrim( $className, '\\' ); - $fileName = ''; - $namespace = ''; - - if ( $lastNsPos = strripos( $className, '\\') ) { - $namespace = substr( $className, 0, $lastNsPos ); - $className = substr( $className, $lastNsPos + 1 ); - $fileName = str_replace( '\\', '/', $namespace ) . '/'; - } - - $fileName .= str_replace( '_', '/', $className ) . '.php'; - - $namespaceSegments = explode( '\\', $namespace ); - - if ( $namespaceSegments[0] === 'Wikibase' && count( $namespaceSegments ) > 1 && $namespaceSegments[1] === 'Query' ) { - if ( count( $namespaceSegments ) === 2 || $namespaceSegments[2] !== 'Tests' ) { - require_once __DIR__ . '/src/' . $fileName; - } - } - } ); - // @codeCoverageIgnoreEnd /** * Hook to add PHPUnit test cases. diff --git a/src/Wikibase/Query/Api/EntitiesByPropertyValue.php b/src/Wikibase/Query/Api/EntitiesByPropertyValue.php new file mode 100644 index 0000000..94fe802 --- /dev/null +++ b/src/Wikibase/Query/Api/EntitiesByPropertyValue.php @@ -0,0 +1,18 @@ +<?php + +namespace Wikibase\Query\Api; + +/** + * @since 0.1 + * + * @file + * @ingroup WikibaseQuery + * + * @licence GNU GPL v2+ + * @author Jeroen De Dauw < [email protected] > + */ +class EntitiesByPropertyValue { + + + +} diff --git a/src/Wikibase/Query/Api/EntitiesByPropertyValueWrapper.php b/src/Wikibase/Query/Api/EntitiesByPropertyValueWrapper.php new file mode 100644 index 0000000..7feec9d --- /dev/null +++ b/src/Wikibase/Query/Api/EntitiesByPropertyValueWrapper.php @@ -0,0 +1,102 @@ +<?php + +namespace Wikibase\Query\Api; + +/** + * @since 0.1 + * + * @file + * @ingroup WikibaseQuery + * + * @licence GNU GPL v2+ + * @author Jeroen De Dauw < [email protected] > + */ +class EntitiesByPropertyValueWrapper extends \ApiBase { + + /** + * @see ApiBase::execute + * + * @since 0.1 + */ + public function execute() { + + } + + /** + * @see ApiBase::getAllowedParams + * + * @since 0.1 + * + * @return array + */ + public function getAllowedParams() { + return array( + 'parser' => array( + ApiBase::PARAM_TYPE => $this->getFactory()->getParserIds(), + ApiBase::PARAM_REQUIRED => true, + ), + 'values' => array( + ApiBase::PARAM_TYPE => 'string', + ApiBase::PARAM_REQUIRED => true, + ApiBase::PARAM_ISMULTI => true, + ), + 'options' => array( + ApiBase::PARAM_TYPE => 'string', + ApiBase::PARAM_REQUIRED => false, + ), + ); + } + + /** + * @see ApiBase::getParamDescription + * + * @since 0.1 + * + * @return array + */ + public function getParamDescription() { + return array( + 'parser' => 'Id of the ValueParser to use', + 'values' => 'The values to parse', + 'options' => 'The options the parser should use. Provided as a JSON object.', + ); + } + + /** + * @see ApiBase::getDescription + * + * @since 0.1 + * + * @return string + */ + public function getDescription() { + return array( + 'API module for parsing values using a ValueParser.' + ); + } + + /** + * @see ApiBase::getExamples + * + * @since 0.1 + * + * @return array + */ + protected function getExamples() { + return array( + // 'ex' => 'desc' // TODO + ); + } + + /** + * @see ApiBase::getHelpUrls + * + * @since 0.1 + * + * @return string + */ + public function getHelpUrls() { + return ''; // TODO + } + +} diff --git a/src/Wikibase/Query/DIC/DependencyBuilder.php b/src/Wikibase/Query/DIC/DependencyBuilder.php new file mode 100644 index 0000000..d97959d --- /dev/null +++ b/src/Wikibase/Query/DIC/DependencyBuilder.php @@ -0,0 +1,18 @@ +<?php + +namespace Wikibase\Query\DIC; + +/** + * @since 1.0 + * + * @file + * @ingroup WikibaseQuery + * + * @licence GNU GPL v2+ + * @author Jeroen De Dauw < [email protected] > + */ +abstract class DependencyBuilder { + + public abstract function buildObject( DependencyManager $dependencyManager ); + +} diff --git a/src/Wikibase/Query/DIC/DependencyManager.php b/src/Wikibase/Query/DIC/DependencyManager.php new file mode 100644 index 0000000..b9a02f9 --- /dev/null +++ b/src/Wikibase/Query/DIC/DependencyManager.php @@ -0,0 +1,45 @@ +<?php + +namespace Wikibase\Query\DIC; + +use InvalidArgumentException; +use OutOfBoundsException; + +/** + * @since 1.0 + * + * @file + * @ingroup WikibaseQuery + * + * @licence GNU GPL v2+ + * @author Jeroen De Dauw < [email protected] > + */ +class DependencyManager { + + /** + * @var DependencyBuilder[] + */ + protected $builders = array(); + + public function registerBuilder( $objectKey, DependencyBuilder $builder ) { + $this->assertIsValidObjectKey( $objectKey ); + $this->builders[$objectKey] = $builder; + } + + public function newObject( $objectKey ) { + $this->assertIsValidObjectKey( $objectKey ); + + if ( !array_key_exists( $objectKey, $this->builders ) ) { + throw new OutOfBoundsException( "No '$objectKey' builder has been registered'" ); + } + + return $this->builders[$objectKey]->buildObject( $this ); + } + + protected function assertIsValidObjectKey( $objectKey ) { + if ( !is_string( $objectKey ) || $objectKey === '' ) { + throw new InvalidArgumentException( '$objectKey needs to be a string' ); + } + } + +} diff --git a/src/Wikibase/Query/DIC/ExtensionAccess.php b/src/Wikibase/Query/DIC/ExtensionAccess.php new file mode 100644 index 0000000..370dcff --- /dev/null +++ b/src/Wikibase/Query/DIC/ExtensionAccess.php @@ -0,0 +1,24 @@ +<?php + +namespace Wikibase\Query\DIC; + +/** + * @since 1.0 + * + * @file + * @ingroup WikibaseQuery + * + * @licence GNU GPL v2+ + * @author Jeroen De Dauw < [email protected] > + */ +class ExtensionAccess { + + public static function setRegistry() { + + } + + public function getRegistry() { + + } + +} diff --git a/src/Wikibase/Query/DIC/ExtensionRegistry.php b/src/Wikibase/Query/DIC/ExtensionRegistry.php new file mode 100644 index 0000000..9ce34a6 --- /dev/null +++ b/src/Wikibase/Query/DIC/ExtensionRegistry.php @@ -0,0 +1,18 @@ +<?php + +namespace Wikibase\Query\DIC; + +/** + * @since 1.0 + * + * @file + * @ingroup WikibaseQuery + * + * @licence GNU GPL v2+ + * @author Jeroen De Dauw < [email protected] > + */ +class ExtensionRegistry { + + + +} \ No newline at end of file diff --git a/src/Wikibase/Query/WikibaseQuery.php b/src/Wikibase/Query/WikibaseQuery.php new file mode 100644 index 0000000..ccfccc5 --- /dev/null +++ b/src/Wikibase/Query/WikibaseQuery.php @@ -0,0 +1,13 @@ +<?php + +namespace Wikibase\Query; + +use Wikibase\Query\Api\EntitiesByPropertyValue; + +class WikibaseQuery { + + public function newEntitiesByPropertyValueModule() { + return new EntitiesByPropertyValue(); + } + +} -- To view, visit https://gerrit.wikimedia.org/r/73005 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I2dfa4dc4820f5154b8342b4d85ac7fc98c6c2546 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/WikibaseQuery Gerrit-Branch: master Gerrit-Owner: Jeroen De Dauw <[email protected]> _______________________________________________ MediaWiki-commits mailing list [email protected] https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits
