Skip to content

Commit

Permalink
Merge pull request #709 from wmde/refListTestMutations
Browse files Browse the repository at this point in the history
Add missing ReferenceList::addReference test case
  • Loading branch information
JeroenDeDauw committed Jan 6, 2017
2 parents 38b0008 + 38b2f82 commit 3441060
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
4 changes: 4 additions & 0 deletions src/ReferenceList.php
Expand Up @@ -100,6 +100,10 @@ public function addNewReference( $snaks = [] /*...*/ ) {
* @param int $index
*/
private function insertReferenceAtIndex( Reference $reference, $index ) {
if ( !is_int( $index ) ) {
throw new InvalidArgumentException( '$index must be an integer' );
}

$this->references = array_merge(
array_slice( $this->references, 0, $index ),
[ spl_object_hash( $reference ) => $reference ],
Expand Down
22 changes: 17 additions & 5 deletions tests/unit/ReferenceListTest.php
Expand Up @@ -143,7 +143,7 @@ public function testRemoveReference( ReferenceList $array ) {
$array->removeReference( $element );

$this->assertFalse( $array->hasReference( $element ) );
$this->assertEquals( --$elementCount, count( $array ) );
$this->assertSame( --$elementCount, count( $array ) );
}
}

Expand Down Expand Up @@ -180,13 +180,13 @@ public function testAddReferenceOnEmptyList() {
}

private function assertSameReferenceOrder( ReferenceList $expectedList, ReferenceList $references ) {
$this->assertEquals(
$this->assertSame(
iterator_to_array( $expectedList ),
iterator_to_array( $references )
);
}

public function testAddReferenceOnNonEmptyList() {
public function testAddReferenceAtTheEnd() {
$reference1 = new Reference( [ new PropertyNoValueSnak( 1 ) ] );
$reference2 = new Reference( [ new PropertyNoValueSnak( 2 ) ] );
$reference3 = new Reference( [ new PropertyNoValueSnak( 3 ) ] );
Expand All @@ -200,6 +200,18 @@ public function testAddReferenceOnNonEmptyList() {
$this->assertSameReferenceOrder( $expectedList, $references );
}

public function testAddReferenceBetweenExistingReferences() {
$reference1 = new Reference( [ new PropertyNoValueSnak( 1 ) ] );
$reference2 = new Reference( [ new PropertyNoValueSnak( 2 ) ] );
$list = new ReferenceList( [ $reference1, $reference2 ] );

$reference3 = new Reference( [ new PropertyNoValueSnak( 3 ) ] );
$list->addReference( $reference3, 1 );

$this->assertCount( 3, $list );
$this->assertSame( 1, $list->indexOf( $reference3 ) );
}

public function testAddReferenceIgnoresIdenticalObjects() {
$list = new ReferenceList();
$reference = new Reference( [ new PropertyNoValueSnak( 1 ) ] );
Expand Down Expand Up @@ -294,7 +306,7 @@ public function testIndexOf( ReferenceList $array ) {

$i = 0;
foreach ( $array as $reference ) {
$this->assertEquals( $i++, $array->indexOf( $reference ) );
$this->assertSame( $i++, $array->indexOf( $reference ) );
}
}

Expand Down Expand Up @@ -329,7 +341,7 @@ public function testGetValueHashReturnsString( ReferenceList $array ) {
*/
public function testGetValueHashIsTheSameForClone( ReferenceList $array ) {
$copy = unserialize( serialize( $array ) );
$this->assertEquals( $array->getValueHash(), $copy->getValueHash() );
$this->assertSame( $array->getValueHash(), $copy->getValueHash() );
}

/**
Expand Down

0 comments on commit 3441060

Please sign in to comment.