Skip to content

Commit

Permalink
Fix StatementList not reindexing associative arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
thiemowmde committed Nov 26, 2014
1 parent 2b4530d commit 29997a9
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 49 deletions.
4 changes: 4 additions & 0 deletions RELEASE-NOTES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Wikibase DataModel release notes

## Version 2.4.1 (2014-11-26)

* Fixed `StatementList` not reindexing array keys

## Version 2.4.0 (2014-11-23)

* `Property` now implements the deprecated claim related methods defined in `Entity`
Expand Down
2 changes: 1 addition & 1 deletion WikibaseDataModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
return 1;
}

define( 'WIKIBASE_DATAMODEL_VERSION', '2.4.0' );
define( 'WIKIBASE_DATAMODEL_VERSION', '2.4.1' );

if ( defined( 'MEDIAWIKI' ) ) {
call_user_func( function() {
Expand Down
15 changes: 2 additions & 13 deletions src/Statement/StatementList.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,6 @@ class StatementList implements IteratorAggregate, Comparable, Countable {
* @throws InvalidArgumentException
*/
public function __construct( $statements = array() ) {
$this->assertAreStatements( $statements );

if ( is_array( $statements ) ) {
$this->statements = $statements;
}
else {
foreach ( $statements as $statement ) {
$this->statements[] = $statement;
}
}
}

private function assertAreStatements( $statements ) {
if ( !is_array( $statements ) && !( $statements instanceof Traversable ) ) {
throw new InvalidArgumentException( '$statements must be an array or an instance of Traversable' );
}
Expand All @@ -62,6 +49,8 @@ private function assertAreStatements( $statements ) {
if ( !( $statement instanceof Statement ) ) {
throw new InvalidArgumentException( 'Every element in $statements must be an instance of Statement' );
}

$this->statements[] = $statement;
}
}

Expand Down
77 changes: 42 additions & 35 deletions tests/unit/Statement/StatementListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ public function testGivenNoStatements_getPropertyIdsReturnsEmptyArray() {

public function testGivenStatements_getPropertyIdsReturnsArrayWithoutDuplicates() {
$list = new StatementList( array(
$this->getStubStatement( 1, 'kittens' ),
$this->getStubStatement( 3, 'foo' ),
$this->getStubStatement( 2, 'bar' ),
$this->getStubStatement( 2, 'baz' ),
$this->getStubStatement( 1, 'bah' ),
$this->getStatement( 1, 'kittens' ),
$this->getStatement( 3, 'foo' ),
$this->getStatement( 2, 'bar' ),
$this->getStatement( 2, 'baz' ),
$this->getStatement( 1, 'bah' ),
) );

$this->assertEquals(
Expand All @@ -45,7 +45,21 @@ public function testGivenStatements_getPropertyIdsReturnsArrayWithoutDuplicates(
);
}

private function getStubStatement( $propertyId, $guid, $rank = Statement::RANK_NORMAL ) {
public function testGivenStatementsWithArrayKeys_reindexesArray() {
$statement = $this->getStatement( 1, 'guid' );
$list = new StatementList( array( 'ignore-me' => $statement ) );

$this->assertSame( array( 0 => $statement ), $list->toArray() );
}

/**
* @param int $propertyId
* @param string $guid
* @param int $rank
*
* @return Statement
*/
private function getStatement( $propertyId, $guid, $rank = Statement::RANK_NORMAL ) {
$statement = $this->getMockBuilder( 'Wikibase\DataModel\Statement\Statement' )
->disableOriginalConstructor()->getMock();

Expand All @@ -65,7 +79,7 @@ private function getStubStatement( $propertyId, $guid, $rank = Statement::RANK_N
}

public function testCanIterate() {
$statement = $this->getStubStatement( 1, 'kittens' );
$statement = $this->getStatement( 1, 'kittens' );
$list = new StatementList( array( $statement ) );

foreach ( $list as $statementFormList ) {
Expand All @@ -75,27 +89,27 @@ public function testCanIterate() {

public function testGetBestStatementPerProperty() {
$list = new StatementList( array(
$this->getStubStatement( 1, 'one', Statement::RANK_PREFERRED ),
$this->getStubStatement( 1, 'two', Statement::RANK_NORMAL ),
$this->getStubStatement( 1, 'three', Statement::RANK_PREFERRED ),
$this->getStatement( 1, 'one', Statement::RANK_PREFERRED ),
$this->getStatement( 1, 'two', Statement::RANK_NORMAL ),
$this->getStatement( 1, 'three', Statement::RANK_PREFERRED ),

$this->getStubStatement( 2, 'four', Statement::RANK_DEPRECATED ),
$this->getStatement( 2, 'four', Statement::RANK_DEPRECATED ),

$this->getStubStatement( 3, 'five', Statement::RANK_DEPRECATED ),
$this->getStubStatement( 3, 'six', Statement::RANK_NORMAL ),
$this->getStatement( 3, 'five', Statement::RANK_DEPRECATED ),
$this->getStatement( 3, 'six', Statement::RANK_NORMAL ),

$this->getStubStatement( 4, 'seven', Statement::RANK_PREFERRED ),
$this->getStubStatement( 4, 'eight', Claim::RANK_TRUTH ),
$this->getStatement( 4, 'seven', Statement::RANK_PREFERRED ),
$this->getStatement( 4, 'eight', Claim::RANK_TRUTH ),
) );

$this->assertEquals(
array(
$this->getStubStatement( 1, 'one', Statement::RANK_PREFERRED ),
$this->getStubStatement( 1, 'three', Statement::RANK_PREFERRED ),
$this->getStatement( 1, 'one', Statement::RANK_PREFERRED ),
$this->getStatement( 1, 'three', Statement::RANK_PREFERRED ),

$this->getStubStatement( 3, 'six', Statement::RANK_NORMAL ),
$this->getStatement( 3, 'six', Statement::RANK_NORMAL ),

$this->getStubStatement( 4, 'eight', Claim::RANK_TRUTH ),
$this->getStatement( 4, 'eight', Claim::RANK_TRUTH ),
),
$list->getBestStatementPerProperty()->toArray()
);
Expand Down Expand Up @@ -298,22 +312,15 @@ public function testGivenIdenticalLists_equalsReturnsTrue( array $statements ) {

public function statementArrayProvider() {
return array(
array(
array(
$this->getStatementWithSnak( 1, 'foo' ),
$this->getStatementWithSnak( 2, 'bar' ),
)
),

array(
array(
$this->getStatementWithSnak( 1, 'foo' ),
)
),

array(
array()
),
array( array(
$this->getStatementWithSnak( 1, 'foo' ),
$this->getStatementWithSnak( 2, 'bar' ),
) ),
array( array(
$this->getStatementWithSnak( 1, 'foo' ),
) ),
array( array(
) ),
);
}

Expand Down

1 comment on commit 29997a9

@JeroenDeDauw
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has been released

Please sign in to comment.