jenkins-bot has submitted this change and it was merged. Change subject: Added indexes field to TableDefinition ......................................................................
Added indexes field to TableDefinition Change-Id: Icdfe6f59ed5ebf0e2c9e2aa63bf98a92c36d0b1a --- M src/TableDefinition.php M tests/phpunit/TableDefinitionTest.php 2 files changed, 143 insertions(+), 8 deletions(-) Approvals: Denny Vrandecic: Looks good to me, approved jenkins-bot: Verified diff --git a/src/TableDefinition.php b/src/TableDefinition.php index ca86411..dc1c1d3 100644 --- a/src/TableDefinition.php +++ b/src/TableDefinition.php @@ -34,21 +34,42 @@ /** * @since 0.1 * + * @var IndexDefinition[] + */ + private $indexes; + + /** + * @since 0.1 + * * @param string $name * @param FieldDefinition[] $fields + * @param IndexDefinition[] $indexes * * @throws InvalidArgumentException */ - public function __construct( $name, array $fields ) { + public function __construct( $name, array $fields, array $indexes = array() ) { + $this->setName( $name ); + $this->setFields( $fields ); + $this->setIndexes( $indexes ); + } + + protected function setName( $name ) { if ( !is_string( $name ) ) { throw new InvalidArgumentException( 'The table $name needs to be a string' ); } + $this->name = $name; + } + + /** + * @param FieldDefinition[] $fields + * + * @throws InvalidArgumentException + */ + protected function setFields( array $fields ) { if ( empty( $fields ) ) { throw new InvalidArgumentException( 'The table $fields list cannot be empty' ); } - - $this->name = $name; $this->fields = array(); @@ -62,6 +83,23 @@ } $this->fields[$field->getName()] = $field; + } + } + + /** + * @param IndexDefinition[] $indexes + * + * @throws InvalidArgumentException + */ + protected function setIndexes( array $indexes ) { + $this->indexes = array(); + + foreach ( $indexes as $index ) { + if ( !( $index instanceof IndexDefinition ) ) { + throw new InvalidArgumentException( 'All table indexes should be of type IndexDefinition' ); + } + + $this->indexes[$index->getName()] = $index; } } @@ -90,6 +128,19 @@ } /** + * Returns the indexes that this table has. + * The array keys in the returned array correspond to the names + * of the indexes defined by the value they point to. + * + * @since 0.1 + * + * @return IndexDefinition[] + */ + public function getIndexes() { + return $this->indexes; + } + + /** * Returns if the table has a field with the provided name. * * @since 0.1 @@ -112,7 +163,7 @@ * @return TableDefinition */ public function mutateName( $cloneName ) { - return new self( $cloneName, $this->fields ); + return new self( $cloneName, $this->fields, $this->indexes ); } /** @@ -125,9 +176,7 @@ * @return TableDefinition */ public function mutateFields( array $fields ) { - return new self( $this->name, $fields ); + return new self( $this->name, $fields, $this->indexes ); } - - // TODO: multiple field indices } diff --git a/tests/phpunit/TableDefinitionTest.php b/tests/phpunit/TableDefinitionTest.php index 2f2f670..30ff9ad 100644 --- a/tests/phpunit/TableDefinitionTest.php +++ b/tests/phpunit/TableDefinitionTest.php @@ -3,6 +3,7 @@ namespace Wikibase\Database\Tests; use Wikibase\Database\FieldDefinition; +use Wikibase\Database\IndexDefinition; use Wikibase\Database\TableDefinition; /** @@ -36,7 +37,31 @@ array( new FieldDefinition( 'o', FieldDefinition::TYPE_TEXT ), new FieldDefinition( 'h', FieldDefinition::TYPE_TEXT ), - new FieldDefinition( 'i', FieldDefinition::TYPE_INTEGER, false, 42 ), + new FieldDefinition( 'i', FieldDefinition::TYPE_INTEGER, FieldDefinition::NOT_NULL, 42 ), + ) + ); + + $instances[] = new TableDefinition( + 'spam', + array( + new FieldDefinition( 'o', FieldDefinition::TYPE_TEXT ), + new FieldDefinition( 'h', FieldDefinition::TYPE_TEXT ), + new FieldDefinition( 'i', FieldDefinition::TYPE_INTEGER, FieldDefinition::NOT_NULL, 42 ), + ), + array( + new IndexDefinition( 'o', array( 'o' => 0 ) ), + ) + ); + + $instances[] = new TableDefinition( + 'spam', + array( + new FieldDefinition( 'o', FieldDefinition::TYPE_TEXT ), + ), + array( + new IndexDefinition( 'o', array( 'o' => 0 ) ), + new IndexDefinition( 'h', array( 'h' => 42 ) ), + new IndexDefinition( 'foo', array( 'bar' => 5, 'baz' => 0 ) ), ) ); @@ -146,4 +171,65 @@ $this->assertEquals( $table->getName(), $newTable->getName() ); } + /** + * @dataProvider instanceProvider + * + * @param TableDefinition $table + */ + public function testReturnTypeOfGetIndexes( TableDefinition $table ) { + $this->assertInternalType( 'array', $table->getIndexes() ); + $this->assertContainsOnlyInstancesOf( 'Wikibase\Database\IndexDefinition', $table->getIndexes() ); + + $newTable = new TableDefinition( $table->getName(), $table->getFields(), $table->getIndexes() ); + + $this->assertEquals( + $table->getIndexes(), + $newTable->getIndexes(), + 'The TableDefinition indexes are set and obtained correctly' + ); + } + + /** + * @dataProvider invalidIndexesProvider + */ + public function testCannotConstructWithInvalidIndexList( array $invalidIndexList ) { + $this->setExpectedException( 'InvalidArgumentException' ); + + new TableDefinition( + 'foo', + array( new FieldDefinition( 'h', FieldDefinition::TYPE_TEXT ) ), + $invalidIndexList + ); + } + + public function invalidIndexesProvider() { + $mockIndexDefinition = $this->getMockBuilder( 'Wikibase\Database\IndexDefinition' ) + ->disableOriginalConstructor()->getMock(); + + $argLists = array(); + + $argLists[] = array( array( + null + ) ); + + $argLists[] = array( array( + 'foo bar' + ) ); + + $argLists[] = array( array( + $mockIndexDefinition, + array() + ) ); + + $argLists[] = array( array( + $mockIndexDefinition, + $mockIndexDefinition, + 4.2, + $mockIndexDefinition, + $mockIndexDefinition + ) ); + + return $argLists; + } + } -- To view, visit https://gerrit.wikimedia.org/r/76728 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: Icdfe6f59ed5ebf0e2c9e2aa63bf98a92c36d0b1a Gerrit-PatchSet: 2 Gerrit-Project: mediawiki/extensions/WikibaseDatabase Gerrit-Branch: master Gerrit-Owner: Jeroen De Dauw <jeroended...@gmail.com> Gerrit-Reviewer: Denny Vrandecic <denny.vrande...@wikimedia.de> Gerrit-Reviewer: jenkins-bot _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits