| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Wikibase\DataModel\Tests\HashArray; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Hashable; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Wikibase\DataModel\Fixtures\HashArrayElement; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Wikibase\DataModel\Fixtures\HashArrayWithoutDuplicates; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Wikibase\DataModel\HashArray; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Wikibase\DataModel\Snak\PropertyNoValueSnak; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * @covers Wikibase\DataModel\HashArray | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * @group Wikibase | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * @group WikibaseDataModel | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * @group HashArray | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * @license GPL-2.0+ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * @author Jeroen De Dauw < [email protected] > | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  */ | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 21 |  |  | class HashArrayWithoutDuplicatesTest extends HashArrayTest { | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  | 	public function instanceProvider() { | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  | 		return [ | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  | 			[ new HashArrayWithoutDuplicates( HashArrayElement::getInstances() ) ], | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  | 		]; | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  | 	} | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  | 	public function elementInstancesProvider() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | 		return $this->arrayWrap( array_merge( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 			$this->arrayWrap( HashArrayElement::getInstances() ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | 			[ HashArrayElement::getInstances() ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 		) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 	 * @dataProvider instanceProvider | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 	 * @param HashArray $array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 	public function testAddElement( HashArray $array ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | 		$elementCount = $array->count(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | 		$elements = $this->elementInstancesProvider(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 		$element = array_shift( $elements ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 		$element = $element[0][0]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 		if ( !$array->hasElement( $element ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 			++$elementCount; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 		$this->assertEquals( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 			!$array->hasElement( $element ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 			$array->addElement( $element ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 			'Adding an element should only work if its not already there' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 		$this->assertEquals( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 			$elementCount, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 			$array->count(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 			'Element count should only increase if the element was not there yet' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 		$this->assertFalse( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 			$array->addElement( $element ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 			'Adding an already present element should not work' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 		$this->assertEquals( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 			$elementCount, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 			$array->count(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 			'Element count should not increase if the element is already there' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 	 * @dataProvider instanceProvider | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 	 * @param HashArray $array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 	public function testHasElement( HashArray $array ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 		/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 		 * @var Hashable $hashable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 		 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 		foreach ( iterator_to_array( $array ) as $hashable ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 			$this->assertTrue( $array->hasElement( $hashable ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 			$this->assertTrue( $array->hasElementHash( $hashable->getHash() ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 			$array->removeElement( $hashable ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 			$this->assertFalse( $array->hasElement( $hashable ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 			$this->assertFalse( $array->hasElementHash( $hashable->getHash() ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 		$this->assertTrue( true ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 	 * @dataProvider instanceProvider | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 	 * @param HashArray $array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 	public function testRemoveElement( HashArray $array ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 		$elementCount = $array->count(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  | 		/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  | 		 * @var Hashable $element | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | 		 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | 		foreach ( iterator_to_array( $array ) as $element ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  | 			$this->assertTrue( $array->hasElement( $element ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  | 			if ( $elementCount % 2 === 0 ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  | 				$array->removeElement( $element ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  | 			else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  | 				$array->removeByElementHash( $element->getHash() ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  | 			$this->assertFalse( $array->hasElement( $element ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  | 			$this->assertEquals( --$elementCount, $array->count() ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  | 		$element = new PropertyNoValueSnak( 42 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  | 		$array->removeElement( $element ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  | 		$array->removeByElementHash( $element->getHash() ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  | 		$this->assertTrue( true ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 126 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 127 |  |  |  |