Jeroen De Dauw has uploaded a new change for review.

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


Change subject: Initial work on IndexDefinition
......................................................................

Initial work on IndexDefinition

Change-Id: I8554272ac10b11477b9a2404ee2d440f38df99b1
---
A src/IndexDefinition.php
A tests/phpunit/IndexDefinitionTest.php
2 files changed, 194 insertions(+), 0 deletions(-)


  git pull 
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/WikibaseDatabase 
refs/changes/35/76535/1

diff --git a/src/IndexDefinition.php b/src/IndexDefinition.php
new file mode 100644
index 0000000..905305e
--- /dev/null
+++ b/src/IndexDefinition.php
@@ -0,0 +1,98 @@
+<?php
+
+namespace Wikibase\Database;
+
+use InvalidArgumentException;
+
+/**
+ * Definition of a database index. Immutable.
+ *
+ * @since 0.1
+ *
+ * @file
+ * @ingroup WikibaseDatabase
+ *
+ * @licence GNU GPL v2+
+ * @author Jeroen De Dauw < [email protected] >
+ * @author Denny Vrandecic < [email protected] >
+ */
+class IndexDefinition {
+
+       protected $name;
+       protected $columns;
+
+       /**
+        * @param string $name
+        * @param int[] $columns array with string column names => int size, 0 
for unrestricted size
+        *
+        * @throws InvalidArgumentException
+        */
+       public function __construct( $name, $columns ) {
+               $this->assertIsValidIndexName( $name );
+               $this->assertAreValidColumns( $columns );
+
+               $this->name = $name;
+               $this->columns = $columns;
+       }
+
+       protected function assertIsValidIndexName( $indexName ) {
+               if ( !$this->isAlphanumericOrUnderscoreString( $indexName ) ) {
+                       throw new InvalidArgumentException( 'The index name 
needs to be an alphanumeric string that can contain underscores' );
+               }
+       }
+
+       protected function isAlphanumericOrUnderscoreString( $string ) {
+               if ( !is_string( $string ) ) {
+                       return false;
+               }
+
+               if ( $string === '' ) {
+                       return false;
+               }
+
+               if ( !ctype_alnum( str_replace( array( '_' ), '', $string ) ) ) 
{
+                       return false;
+               }
+
+               return true;
+       }
+
+       protected function assertIsValidColumnName( $columnName ) {
+               if ( !$this->isAlphanumericOrUnderscoreString( $columnName ) ) {
+                       throw new InvalidArgumentException( 'The column name 
needs to be an alphanumeric string that can contain underscores' );
+               }
+       }
+
+       protected function assertAreValidColumns( $columns ) {
+               if ( !is_array( $columns ) ) {
+                       throw new InvalidArgumentException( 'Cannot construct 
IndexDefinition with non-array $columns' );
+               }
+
+               if ( $columns === array() ) {
+                       throw new InvalidArgumentException( 'The list of 
columns cannot be empty' );
+               }
+
+               foreach ( $columns as $columnName => $indexSize ) {
+                       $this->assertIsValidColumnName( $columnName );
+
+                       if ( !is_int( $indexSize ) || ( $indexSize < 0 ) ) {
+                               throw new InvalidArgumentException( 'All index 
sizes need to be positive integers' );
+                       }
+               }
+       }
+
+       /**
+        * @return string
+        */
+       public function getName() {
+               return $this->name;
+       }
+
+       /**
+        * @return int[] array with string column names => int size, 0 for 
unrestricted size
+        */
+       public function getColumns() {
+               return $this->columns;
+       }
+
+}
diff --git a/tests/phpunit/IndexDefinitionTest.php 
b/tests/phpunit/IndexDefinitionTest.php
new file mode 100644
index 0000000..9bfdaf7
--- /dev/null
+++ b/tests/phpunit/IndexDefinitionTest.php
@@ -0,0 +1,96 @@
+<?php
+
+namespace Wikibase\Database\Tests;
+
+use Wikibase\Database\IndexDefinition;
+
+/**
+ * @covers Wikibase\Database\IndexDefinition
+ *
+ * @file
+ * @since 0.1
+ *
+ * @ingroup WikibaseDatabaseTest
+ *
+ * @group Wikibase
+ * @group WikibaseDatabase
+ *
+ * @licence GNU GPL v2+
+ * @author Jeroen De Dauw < [email protected] >
+ * @author Denny Vrandecic < [email protected] >
+ */
+class IndexDefinitionTest extends \PHPUnit_Framework_TestCase {
+
+       public function testCanGetName() {
+               $indexName = 'foo_bar';
+
+               $index = new IndexDefinition( $indexName, array( 'a' => 0 ) );
+
+               $this->assertEquals( $indexName, $index->getName() );
+       }
+
+       /**
+        * @dataProvider invalidNameProvider
+        */
+       public function testCannotSetInvalidName( $invalidName ) {
+               $this->setExpectedException( 'InvalidArgumentException' );
+               new IndexDefinition( $invalidName, array( 'a' => 0 ) );
+       }
+
+       public function invalidNameProvider() {
+               return array(
+                       array( null ),
+                       array( array() ),
+                       array( false ),
+                       array( 42 ),
+                       array( 4.2 ),
+                       array( (object)array( 'foo' => 'bar' ) ),
+                       array( '' ),
+                       array( 'foo bar' ),
+                       array( ' ' ),
+                       array( '.' ),
+                       array( '42?' ),
+                       array( '$' ),
+                       array( '"foo"' ),
+               );
+       }
+
+       public function testCanGetColumns() {
+               $indexName = 'foo_bar';
+               $columns = array(
+                       'foo' => 10
+               );
+
+               $index = new IndexDefinition( $indexName, $columns );
+
+               $this->assertEquals( $columns, $index->getColumns() );
+       }
+
+       /**
+        * @dataProvider invalidColumnsProvider
+        */
+       public function testCannotSetInvalidColumns( $invalidColumns ) {
+               $this->setExpectedException( 'InvalidArgumentException' );
+               new IndexDefinition( 'name', $invalidColumns );
+       }
+
+       public function invalidColumnsProvider() {
+               return array(
+                       array( null ),
+                       array( array() ),
+                       array( false ),
+                       array( 42 ),
+                       array( 4.2 ),
+                       array( (object)array( 'foo' => 'bar' ) ),
+                       array( '' ),
+                       array( '"foo"' ),
+                       array( array( 'name' ) ),
+                       array( array( 0 => 'name' ) ),
+                       array( array( 'name' => 'name' ) ),
+                       array( array( 'name' => 4.2 ) ),
+                       array( array( 'name' => -1 ) ),
+                       array( array( 'name' => -1337 ) ),
+               );
+       }
+
+}

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I8554272ac10b11477b9a2404ee2d440f38df99b1
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/WikibaseDatabase
Gerrit-Branch: master
Gerrit-Owner: Jeroen De Dauw <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to