diff --git a/src/ByPropertyIdArray.php b/src/ByPropertyIdArray.php deleted file mode 100644 index fe52e95d..00000000 --- a/src/ByPropertyIdArray.php +++ /dev/null @@ -1,487 +0,0 @@ - o3 (p2) - * o3 (p2) ---> move to index 1 -/ o2 (p2) - * - * Example of moving an object that triggers moving the whole "property group": - * o1 (p1) /-> o3 (p2) - * o2 (p2) | o2 (p2) - * o3 (p2) ---> move to index 0 -/ o1 (p1) - * - * @since 0.2 - * - * @licence GNU GPL v2+ - * @author H. Snater < mediawiki@snater.com > - */ -class ByPropertyIdArray extends ArrayObject { - - /** - * @var array[]|null - */ - private $byId = null; - - /** - * @see ArrayObject::__construct - * - * @param array|object|null $input - */ - public function __construct( $input = null ) { - parent::__construct( (array)$input ); - } - - /** - * Builds the index for doing look-ups by property id. - * - * @since 0.2 - */ - public function buildIndex() { - $this->byId = array(); - - foreach ( $this as $object ) { - $propertyId = $object->getPropertyId()->getSerialization(); - - if ( !array_key_exists( $propertyId, $this->byId ) ) { - $this->byId[$propertyId] = array(); - } - - $this->byId[$propertyId][] = $object; - } - } - - /** - * Checks whether id indexed array has been generated. - * - * @throws RuntimeException - */ - private function assertIndexIsBuild() { - if ( $this->byId === null ) { - throw new RuntimeException( 'Index not build, call buildIndex first' ); - } - } - - /** - * Returns the property ids used for indexing. - * - * @since 0.2 - * - * @return PropertyId[] - * @throws RuntimeException - */ - public function getPropertyIds() { - $this->assertIndexIsBuild(); - - return array_map( - function( $serializedPropertyId ) { - return new PropertyId( $serializedPropertyId ); - }, - array_keys( $this->byId ) - ); - } - - /** - * Returns the objects featuring the provided property id in the index. - * - * @since 0.2 - * - * @param PropertyId $propertyId - * - * @throws OutOfBoundsException - * @throws RuntimeException - * @return object[] - */ - public function getByPropertyId( PropertyId $propertyId ) { - $this->assertIndexIsBuild(); - - if ( !( array_key_exists( $propertyId->getSerialization(), $this->byId ) ) ) { - throw new OutOfBoundsException( "Object with propertyId \"$propertyId\" not found" ); - } - - return $this->byId[$propertyId->getSerialization()]; - } - - /** - * Returns the absolute index of an object or false if the object could not be found. - * @since 0.5 - * - * @param object $object - * - * @return bool|int - * @throws RuntimeException - */ - public function getFlatArrayIndexOfObject( $object ) { - $this->assertIndexIsBuild(); - - $i = 0; - foreach ( $this as $o ) { - if ( $o === $object ) { - return $i; - } - $i++; - } - return false; - } - - /** - * Returns the objects in a flat array (using the indexed form for generating the array). - * @since 0.5 - * - * @return object[] - * @throws RuntimeException - */ - public function toFlatArray() { - $this->assertIndexIsBuild(); - - $array = array(); - foreach ( $this->byId as $objects ) { - $array = array_merge( $array, $objects ); - } - return $array; - } - - /** - * Returns the absolute numeric indices of objects featuring the same property id. - * - * @param PropertyId $propertyId - * - * @throws RuntimeException - * @return int[] - */ - private function getFlatArrayIndices( PropertyId $propertyId ) { - $this->assertIndexIsBuild(); - - $propertyIndices = array(); - $i = 0; - - foreach ( $this->byId as $serializedPropertyId => $objects ) { - if ( $serializedPropertyId === $propertyId->getSerialization() ) { - $propertyIndices = range( $i, $i + count( $objects ) - 1 ); - break; - } else { - $i += count( $objects ); - } - } - - return $propertyIndices; - } - - /** - * Moves an object within its "property group". - * - * @param object $object - * @param int $toIndex Absolute index within a "property group". - * - * @throws OutOfBoundsException - */ - private function moveObjectInPropertyGroup( $object, $toIndex ) { - $currentIndex = $this->getFlatArrayIndexOfObject( $object ); - - if ( $toIndex === $currentIndex ) { - return; - } - - $propertyId = $object->getPropertyId(); - - $numericIndices = $this->getFlatArrayIndices( $propertyId ); - $lastIndex = $numericIndices[count( $numericIndices ) - 1]; - - if ( $toIndex > $lastIndex + 1 || $toIndex < $numericIndices[0] ) { - throw new OutOfBoundsException( 'Object cannot be moved to ' . $toIndex ); - } - - if ( $toIndex >= $lastIndex ) { - $this->moveObjectToEndOfPropertyGroup( $object ); - } else { - $this->removeObject( $object ); - - $propertyGroup = array_combine( - $this->getFlatArrayIndices( $propertyId ), - $this->getByPropertyId( $propertyId ) - ); - - $insertBefore = $propertyGroup[$toIndex]; - $this->insertObjectAtIndex( $object, $this->getFlatArrayIndexOfObject( $insertBefore ) ); - } - } - - /** - * Moves an object to the end of its "property group". - * - * @param object $object - */ - private function moveObjectToEndOfPropertyGroup( $object ) { - $this->removeObject( $object ); - - /** @var PropertyId $propertyId */ - $propertyId = $object->getPropertyId(); - $propertyIdSerialization = $propertyId->getSerialization(); - - $propertyGroup = in_array( $propertyIdSerialization, $this->getPropertyIds() ) - ? $this->getByPropertyId( $propertyId ) - : array(); - - $propertyGroup[] = $object; - $this->byId[$propertyIdSerialization] = $propertyGroup; - - $this->exchangeArray( $this->toFlatArray() ); - } - - /** - * Removes an object from the array structures. - * - * @param object $object - */ - private function removeObject( $object ) { - $flatArray = $this->toFlatArray(); - $this->exchangeArray( $flatArray ); - $this->offsetUnset( array_search( $object, $flatArray ) ); - $this->buildIndex(); - } - - /** - * Inserts an object at a specific index. - * - * @param object $object - * @param int $index Absolute index within the flat list of objects. - */ - private function insertObjectAtIndex( $object, $index ) { - $flatArray = $this->toFlatArray(); - - $this->exchangeArray( array_merge( - array_slice( $flatArray, 0, $index ), - array( $object ), - array_slice( $flatArray, $index ) - ) ); - - $this->buildIndex(); - } - - /** - * @param PropertyId $propertyId - * @param int $toIndex - */ - private function movePropertyGroup( PropertyId $propertyId, $toIndex ) { - if ( $this->getPropertyGroupIndex( $propertyId ) === $toIndex ) { - return; - } - - $insertBefore = null; - - $oldIndex = $this->getPropertyGroupIndex( $propertyId ); - $byIdClone = $this->byId; - - // Remove "property group" to calculate the groups new index: - unset( $this->byId[$propertyId->getSerialization()] ); - - if ( $toIndex > $oldIndex ) { - // If the group shall be moved towards the bottom, the number of objects within the - // group needs to be subtracted from the absolute toIndex: - $toIndex -= count( $byIdClone[$propertyId->getSerialization()] ); - } - - foreach ( $this->getPropertyIds() as $pId ) { - // Accepting other than the exact index by using <= letting the "property group" "latch" - // in the next slot. - if ( $toIndex <= $this->getPropertyGroupIndex( $pId ) ) { - $insertBefore = $pId; - break; - } - } - - $serializedPropertyId = $propertyId->getSerialization(); - $this->byId = array(); - - foreach ( $byIdClone as $serializedPId => $objects ) { - $pId = new PropertyId( $serializedPId ); - if ( $pId->equals( $propertyId ) ) { - continue; - } elseif ( $pId->equals( $insertBefore ) ) { - $this->byId[$serializedPropertyId] = $byIdClone[$serializedPropertyId]; - } - $this->byId[$serializedPId] = $objects; - } - - if ( $insertBefore === null ) { - $this->byId[$serializedPropertyId] = $byIdClone[$serializedPropertyId]; - } - - $this->exchangeArray( $this->toFlatArray() ); - } - - /** - * Returns the index of a "property group" (the first object in the flat array that features - * the specified property). Returns false if property id could not be found. - * - * @param PropertyId $propertyId - * - * @return bool|int - */ - private function getPropertyGroupIndex( PropertyId $propertyId ) { - $i = 0; - - foreach ( $this->byId as $serializedPropertyId => $objects ) { - $pId = new PropertyId( $serializedPropertyId ); - if ( $pId->equals( $propertyId ) ) { - return $i; - } - $i += count( $objects ); - } - - return false; - } - - /** - * Moves an existing object to a new index. Specifying an index outside the object's "property - * group" will move the object to the edge of the "property group" and shift the whole group - * to achieve the designated index for the object to move. - * @since 0.5 - * - * @param object $object - * @param int $toIndex Absolute index where to move the object to. - * - * @throws OutOfBoundsException - * @throws RuntimeException - */ - public function moveObjectToIndex( $object, $toIndex ) { - $this->assertIndexIsBuild(); - - if ( !in_array( $object, $this->toFlatArray() ) ) { - throw new OutOfBoundsException( 'Object not present in array' ); - } elseif ( $toIndex < 0 || $toIndex > count( $this ) ) { - throw new OutOfBoundsException( 'Specified index is out of bounds' ); - } elseif ( $this->getFlatArrayIndexOfObject( $object ) === $toIndex ) { - return; - } - - // Determine whether to simply reindex the object within its "property group": - $propertyIndices = $this->getFlatArrayIndices( $object->getPropertyId() ); - - if ( in_array( $toIndex, $propertyIndices ) ) { - $this->moveObjectInPropertyGroup( $object, $toIndex ); - } else { - $edgeIndex = ( $toIndex <= $propertyIndices[0] ) - ? $propertyIndices[0] - : $propertyIndices[count( $propertyIndices ) - 1]; - - $this->moveObjectInPropertyGroup( $object, $edgeIndex ); - $this->movePropertyGroup( $object->getPropertyId(), $toIndex ); - } - - $this->exchangeArray( $this->toFlatArray() ); - } - - /** - * Adds an object at a specific index. If no index is specified, the object will be append to - * the end of its "property group" or - if no objects featuring the same property exist - to the - * absolute end of the array. - * Specifying an index outside a "property group" will place the new object at the specified - * index with the existing "property group" objects being shifted towards the new object. - * - * @since 0.5 - * - * @param object $object - * @param int|null $index Absolute index where to place the new object. - * - * @throws RuntimeException - */ - public function addObjectAtIndex( $object, $index = null ) { - $this->assertIndexIsBuild(); - - $propertyId = $object->getPropertyId(); - $validIndices = $this->getFlatArrayIndices( $propertyId ); - - if ( count( $this ) === 0 ) { - // Array is empty, just append object. - $this->append( $object ); - } elseif ( empty( $validIndices ) ) { - // No objects featuring that property exist. The object may be inserted at a place - // between existing "property groups". - $this->append( $object ); - if ( $index !== null ) { - $this->buildIndex(); - $this->moveObjectToIndex( $object, $index ); - } - } else { - // Objects featuring the same property as the object which is about to be added already - // exist in the array. - $this->addObjectToPropertyGroup( $object, $index ); - } - - $this->buildIndex(); - } - - /** - * Adds an object to an existing property group at the specified absolute index. - * - * @param object $object - * @param int|null $index - * - * @throws OutOfBoundsException - */ - private function addObjectToPropertyGroup( $object, $index = null ) { - /** @var PropertyId $propertyId */ - $propertyId = $object->getPropertyId(); - $validIndices = $this->getFlatArrayIndices( $propertyId ); - - if ( empty( $validIndices ) ) { - throw new OutOfBoundsException( 'No objects featuring the object\'s property exist' ); - } - - // Add index to allow placing object after the last object of the "property group": - $validIndices[] = $validIndices[count( $validIndices ) - 1] + 1; - - if ( $index === null ) { - // If index is null, append object to "property group". - $index = $validIndices[count( $validIndices ) - 1]; - } - - if ( in_array( $index, $validIndices ) ) { - // Add object at index within "property group". - $this->byId[$propertyId->getSerialization()][] = $object; - $this->exchangeArray( $this->toFlatArray() ); - $this->moveObjectToIndex( $object, $index ); - - } else { - // Index is out of the "property group"; The whole group needs to be moved. - $this->movePropertyGroup( $propertyId, $index ); - - // Move new object to the edge of the "property group" to receive its designated - // index: - if ( $index < $validIndices[0] ) { - array_unshift( $this->byId[$propertyId->getSerialization()], $object ); - } else { - $this->byId[$propertyId->getSerialization()][] = $object; - } - } - - $this->exchangeArray( $this->toFlatArray() ); - } - -} diff --git a/tests/unit/ByPropertyIdArrayTest.php b/tests/unit/ByPropertyIdArrayTest.php deleted file mode 100644 index cb79bfe5..00000000 --- a/tests/unit/ByPropertyIdArrayTest.php +++ /dev/null @@ -1,400 +0,0 @@ - - * @author H. Snater < mediawiki@snater.com > - */ -class ByPropertyIdArrayTest extends \PHPUnit_Framework_TestCase { - - public function testArrayObjectNotConstructedFromObject() { - $statement1 = new Statement( new PropertyNoValueSnak( 1 ) ); - $statement1->setGuid( '1' ); - $statement2 = new Statement( new PropertyNoValueSnak( 2 ) ); - $statement2->setGuid( '2' ); - - $object = new ArrayObject(); - $object->append( $statement1 ); - - $byPropertyIdArray = new ByPropertyIdArray( $object ); - // According to the documentation append() "cannot be called when the ArrayObject was - // constructed from an object." This test makes sure it was not constructed from an object. - $byPropertyIdArray->append( $statement2 ); - - $this->assertCount( 2, $byPropertyIdArray ); - } - - /** - * Returns an accessible ReflectionMethod of ByPropertyIdArray. - * - * @param string $methodName - * @return ReflectionMethod - */ - protected static function getMethod( $methodName ) { - $class = new ReflectionClass( 'Wikibase\DataModel\ByPropertyIdArray' ); - $method = $class->getMethod( $methodName ); - $method->setAccessible( true ); - return $method; - } - - public function listProvider() { - $lists = array(); - - $snaks = array( - new PropertyNoValueSnak( new PropertyId( 'P42' ) ), - new PropertySomeValueSnak( new PropertyId( 'P42' ) ), - new PropertySomeValueSnak( new PropertyId( 'P10' ) ), - new PropertyValueSnak( new PropertyId( 'P10' ), new StringValue( 'ohi' ) ), - new PropertySomeValueSnak( new PropertyId( 'P1' ) ), - ); - - $lists[] = $snaks; - - $lists[] = array_map( - function( Snak $snak ) { - return new Statement( $snak ); - }, - $snaks - ); - - $argLists = array(); - - foreach ( $lists as $list ) { - $argLists[] = array( $list ); - } - - return $argLists; - } - - /** - * @return Statement[] - */ - protected function statementsProvider() { - $snaks = array( - new PropertyNoValueSnak( new PropertyId( 'P1' ) ), - new PropertySomeValueSnak( new PropertyId( 'P1' ) ), - new PropertyValueSnak( new PropertyId( 'P2' ), new StringValue( 'a' ) ), - new PropertyValueSnak( new PropertyId( 'P2' ), new StringValue( 'b' ) ), - new PropertyValueSnak( new PropertyId( 'P2' ), new StringValue( 'c' ) ), - new PropertySomeValueSnak( new PropertyId( 'P3' ) ), - ); - - return array_map( - function( Snak $snak ) { - return new Statement( $snak ); - }, - $snaks - ); - } - - /** - * @dataProvider listProvider - * @param PropertyIdProvider[] $objects - */ - public function testGetIds( array $objects ) { - $indexedArray = new ByPropertyIdArray( $objects ); - - $expected = array(); - - foreach ( $objects as $object ) { - $expected[] = $object->getPropertyId(); - } - - $expected = array_unique( $expected ); - - $indexedArray->buildIndex(); - - $this->assertEquals( - array_values( $expected ), - array_values( $indexedArray->getPropertyIds() ) - ); - } - - /** - * @dataProvider listProvider - * @param PropertyIdProvider[] $objects - */ - public function testGetById( array $objects ) { - $indexedArray = new ByPropertyIdArray( $objects ); - - $ids = array(); - - foreach ( $objects as $object ) { - $ids[] = $object->getPropertyId(); - } - - $ids = array_unique( $ids ); - - $indexedArray->buildIndex(); - - $allObtainedObjects = array(); - - foreach ( $ids as $id ) { - foreach ( $indexedArray->getByPropertyId( $id ) as $obtainedObject ) { - $allObtainedObjects[] = $obtainedObject; - $this->assertEquals( $id, $obtainedObject->getPropertyId() ); - } - } - - $this->assertEquals( - array_values( $objects ), - array_values( $allObtainedObjects ) - ); - } - - /** - * @dataProvider listProvider - * @param PropertyIdProvider[] $objects - */ - public function testRemoveObject( array $objects ) { - $lastIndex = count( $objects ) - 1; - $indexedArray = new ByPropertyIdArray( $objects ); - $indexedArray->buildIndex(); - - $removeObject = self::getMethod( 'removeObject' ); - - $removeObject->invokeArgs( $indexedArray, array( $objects[0] ) ); - $removeObject->invokeArgs( $indexedArray, array( $objects[$lastIndex] ) ); - - $this->assertFalse( - in_array( $objects[0], $indexedArray->getByPropertyId( $objects[0]->getPropertyId() ) ) - ); - - $this->assertFalse( in_array( - $objects[$lastIndex], - $indexedArray->getByPropertyId( $objects[1]->getPropertyId() ) - ) ); - - $this->assertFalse( in_array( $objects[0], $indexedArray->toFlatArray() ) ); - $this->assertFalse( in_array( $objects[$lastIndex], $indexedArray->toFlatArray() ) ); - } - - public function testGetByNotSetIdThrowsException() { - $indexedArray = new ByPropertyIdArray(); - $indexedArray->buildIndex(); - - $this->setExpectedException( 'OutOfBoundsException' ); - - $indexedArray->getByPropertyId( PropertyId::newFromNumber( 9000 ) ); - } - - public function testNotBuildExceptionIsThrownForByPropertyId() { - $indexedArray = new ByPropertyIdArray(); - - $this->setExpectedException( 'RuntimeException' ); - $indexedArray->getByPropertyId( PropertyId::newFromNumber( 9000 ) ); - } - - public function testNotBuildExceptionIsThrownForGetPropertyIds() { - $indexedArray = new ByPropertyIdArray(); - - $this->setExpectedException( 'RuntimeException' ); - $indexedArray->getPropertyIds(); - } - - /** - * @dataProvider listProvider - */ - public function testGetFlatArrayIndexOfObject( array $objects ) { - $indexedArray = new ByPropertyIdArray( $objects ); - $indexedArray->buildIndex(); - - $indicesSource = array(); - $indicesDestination = array(); - - $i = 0; - foreach ( $objects as $object ) { - $indicesSource[$i++] = $object; - $indicesDestination[$indexedArray->getFlatArrayIndexOfObject( $object )] = $object; - } - - $this->assertEquals( $indicesSource, $indicesDestination ); - } - - /** - * @dataProvider listProvider - */ - public function testToFlatArray( array $objects ) { - $indexedArray = new ByPropertyIdArray( $objects ); - $indexedArray->buildIndex(); - - $this->assertEquals( $objects, $indexedArray->toFlatArray() ); - } - - public function moveProvider() { - $c = $this->statementsProvider(); - $argLists = array(); - - $argLists[] = array( $c, $c[0], 0, $c ); - $argLists[] = array( $c, $c[0], 1, array( $c[1], $c[0], $c[2], $c[3], $c[4], $c[5] ) ); - $argLists[] = array( $c, $c[0], 2, array( $c[1], $c[0], $c[2], $c[3], $c[4], $c[5] ) ); - $argLists[] = array( $c, $c[0], 3, array( $c[2], $c[3], $c[4], $c[1], $c[0], $c[5] ) ); - $argLists[] = array( $c, $c[0], 4, array( $c[2], $c[3], $c[4], $c[1], $c[0], $c[5] ) ); - $argLists[] = array( $c, $c[0], 5, array( $c[2], $c[3], $c[4], $c[1], $c[0], $c[5] ) ); - $argLists[] = array( $c, $c[0], 6, array( $c[2], $c[3], $c[4], $c[5], $c[1], $c[0] ) ); - - $argLists[] = array( $c, $c[1], 0, array( $c[1], $c[0], $c[2], $c[3], $c[4], $c[5] ) ); - $argLists[] = array( $c, $c[1], 1, $c ); - $argLists[] = array( $c, $c[1], 2, $c ); - $argLists[] = array( $c, $c[1], 3, array( $c[2], $c[3], $c[4], $c[0], $c[1], $c[5] ) ); - $argLists[] = array( $c, $c[1], 4, array( $c[2], $c[3], $c[4], $c[0], $c[1], $c[5] ) ); - $argLists[] = array( $c, $c[1], 5, array( $c[2], $c[3], $c[4], $c[0], $c[1], $c[5] ) ); - $argLists[] = array( $c, $c[1], 6, array( $c[2], $c[3], $c[4], $c[5], $c[0], $c[1] ) ); - - $argLists[] = array( $c, $c[2], 0, array( $c[2], $c[3], $c[4], $c[0], $c[1], $c[5] ) ); - $argLists[] = array( $c, $c[2], 1, $c ); - $argLists[] = array( $c, $c[2], 2, $c ); - $argLists[] = array( $c, $c[2], 3, array( $c[0], $c[1], $c[3], $c[2], $c[4], $c[5] ) ); - $argLists[] = array( $c, $c[2], 4, array( $c[0], $c[1], $c[3], $c[4], $c[2], $c[5] ) ); - $argLists[] = array( $c, $c[2], 5, array( $c[0], $c[1], $c[3], $c[4], $c[2], $c[5] ) ); - $argLists[] = array( $c, $c[2], 6, array( $c[0], $c[1], $c[5], $c[3], $c[4], $c[2] ) ); - - $argLists[] = array( $c, $c[3], 0, array( $c[3], $c[2], $c[4], $c[0], $c[1], $c[5] ) ); - $argLists[] = array( $c, $c[3], 1, array( $c[0], $c[1], $c[3], $c[2], $c[4], $c[5] ) ); - $argLists[] = array( $c, $c[3], 2, array( $c[0], $c[1], $c[3], $c[2], $c[4], $c[5] ) ); - $argLists[] = array( $c, $c[3], 3, $c ); - $argLists[] = array( $c, $c[3], 4, array( $c[0], $c[1], $c[2], $c[4], $c[3], $c[5] ) ); - $argLists[] = array( $c, $c[3], 5, array( $c[0], $c[1], $c[2], $c[4], $c[3], $c[5] ) ); - $argLists[] = array( $c, $c[3], 6, array( $c[0], $c[1], $c[5], $c[2], $c[4], $c[3] ) ); - - $argLists[] = array( $c, $c[4], 0, array( $c[4], $c[2], $c[3], $c[0], $c[1], $c[5] ) ); - $argLists[] = array( $c, $c[4], 1, array( $c[0], $c[1], $c[4], $c[2], $c[3], $c[5] ) ); - $argLists[] = array( $c, $c[4], 2, array( $c[0], $c[1], $c[4], $c[2], $c[3], $c[5] ) ); - $argLists[] = array( $c, $c[4], 3, array( $c[0], $c[1], $c[2], $c[4], $c[3], $c[5] ) ); - $argLists[] = array( $c, $c[4], 4, $c ); - $argLists[] = array( $c, $c[4], 5, $c ); - $argLists[] = array( $c, $c[4], 6, array( $c[0], $c[1], $c[5], $c[2], $c[3], $c[4] ) ); - - $argLists[] = array( $c, $c[5], 0, array( $c[5], $c[0], $c[1], $c[2], $c[3], $c[4] ) ); - $argLists[] = array( $c, $c[5], 1, array( $c[0], $c[1], $c[5], $c[2], $c[3], $c[4] ) ); - $argLists[] = array( $c, $c[5], 2, array( $c[0], $c[1], $c[5], $c[2], $c[3], $c[4] ) ); - $argLists[] = array( $c, $c[5], 3, $c ); - $argLists[] = array( $c, $c[5], 4, $c ); - $argLists[] = array( $c, $c[5], 5, $c ); - $argLists[] = array( $c, $c[5], 6, $c ); - - return $argLists; - } - - /** - * @dataProvider moveProvider - * @param object[] $objectsSource - * @param object $object - * @param int $toIndex - * @param object[] $objectsDestination - */ - public function testMoveObjectToIndex( - array $objectsSource, - $object, - $toIndex, - array $objectsDestination - ) { - $indexedArray = new ByPropertyIdArray( $objectsSource ); - $indexedArray->buildIndex(); - - $indexedArray->moveObjectToIndex( $object, $toIndex ); - - // Not using $indexedArray->toFlatArray() here to test whether native array has been - // exchanged: - $reindexedArray = array(); - foreach ( $indexedArray as $o ) { - $reindexedArray[] = $o; - } - - $this->assertEquals( $objectsDestination, $reindexedArray ); - } - - public function testMoveThrowingOutOfBoundsExceptionIfObjectNotPresent() { - $statements = $this->statementsProvider(); - $indexedArray = new ByPropertyIdArray( $statements ); - $indexedArray->buildIndex(); - - $this->setExpectedException( 'OutOfBoundsException' ); - - $indexedArray->moveObjectToIndex( new Statement( new PropertyNoValueSnak( new PropertyId( 'P9999' ) ) ), 0 ); - } - - public function testMoveThrowingOutOfBoundsExceptionOnInvalidIndex() { - $statements = $this->statementsProvider(); - $indexedArray = new ByPropertyIdArray( $statements ); - $indexedArray->buildIndex(); - - $this->setExpectedException( 'OutOfBoundsException' ); - - $indexedArray->moveObjectToIndex( $statements[0], 9999 ); - } - - public function addProvider() { - $c = $this->statementsProvider(); - - $argLists = array(); - - $argLists[] = array( array(), $c[0], null, array( $c[0] ) ); - $argLists[] = array( array(), $c[0], 1, array( $c[0] ) ); - $argLists[] = array( array( $c[0] ), $c[2], 0, array( $c[2], $c[0] ) ); - $argLists[] = array( array( $c[2], $c[1] ), $c[0], 0, array( $c[0], $c[1], $c[2] ) ); - $argLists[] = array( - array( $c[0], $c[1], $c[3] ), - $c[5], - 1, - array( $c[0], $c[1], $c[5], $c[3] ) - ); - $argLists[] = array( - array( $c[0], $c[1], $c[5], $c[3] ), - $c[2], - 2, - array( $c[0], $c[1], $c[2], $c[3], $c[5] ) - ); - $argLists[] = array( - array( $c[0], $c[1], $c[2], $c[3], $c[5] ), - $c[4], - null, - array( $c[0], $c[1], $c[2], $c[3], $c[4], $c[5] ) - ); - - return $argLists; - } - - /** - * @dataProvider addProvider - * @param object[] $objectsSource - * @param object $object - * @param int|null $index - * @param object[] $objectsDestination - */ - public function testAddObjectAtIndex( - array $objectsSource, - $object, - $index, - array $objectsDestination - ) { - $indexedArray = new ByPropertyIdArray( $objectsSource ); - $indexedArray->buildIndex(); - - $indexedArray->addObjectAtIndex( $object, $index ); - - $this->assertEquals( $objectsDestination, $indexedArray->toFlatArray() ); - } - -}