Skip to content

Commit 5688aba

Browse files
authored
Merge pull request #714 from wmde/moveGetSnakListHash
Move Hashable interface from HashArray to SnakList
2 parents e306e75 + 6534583 commit 5688aba

4 files changed

Lines changed: 54 additions & 40 deletions

File tree

src/HashArray.php

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use Hashable;
88
use InvalidArgumentException;
99
use Traversable;
10-
use Wikibase\DataModel\Internal\MapValueHasher;
1110

1211
/**
1312
* Generic array object with lookups based on hashes of the elements.
@@ -27,7 +26,7 @@
2726
* @license GPL-2.0+
2827
* @author Jeroen De Dauw < jeroendedauw@gmail.com >
2928
*/
30-
abstract class HashArray extends ArrayObject implements Hashable, Comparable {
29+
abstract class HashArray extends ArrayObject implements Comparable {
3130

3231
/**
3332
* Maps element hashes to their offsets.
@@ -232,20 +231,6 @@ public function offsetUnset( $index ) {
232231
}
233232
}
234233

235-
/**
236-
* @see Hashable::getHash
237-
*
238-
* The hash is purely valuer based. Order of the elements in the array is not held into account.
239-
*
240-
* @since 0.1
241-
*
242-
* @return string
243-
*/
244-
public function getHash() {
245-
$hasher = new MapValueHasher();
246-
return $hasher->hash( $this );
247-
}
248-
249234
/**
250235
* @see Comparable::equals
251236
*

src/Snak/SnakList.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Wikibase\DataModel\Snak;
44

5+
use Hashable;
56
use InvalidArgumentException;
67
use Traversable;
78
use Wikibase\DataModel\HashArray;
@@ -17,7 +18,7 @@
1718
* @author Jeroen De Dauw < jeroendedauw@gmail.com >
1819
* @author Addshore
1920
*/
20-
class SnakList extends HashArray {
21+
class SnakList extends HashArray implements Hashable {
2122

2223
/**
2324
* @param Snak[]|Traversable $snaks
@@ -110,7 +111,9 @@ public function getSnak( $snakHash ) {
110111
/**
111112
* @see HashArray::getHash
112113
*
113-
* @since 0.5
114+
* The hash is purely value based. Order of the elements in the array is not held into account.
115+
*
116+
* @since 0.1
114117
*
115118
* @return string
116119
*/

tests/unit/HashArray/HashArrayWithoutDuplicatesTest.php

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -129,26 +129,4 @@ public function testRemoveElement( HashArray $array ) {
129129
$this->assertTrue( true );
130130
}
131131

132-
/**
133-
* @dataProvider instanceProvider
134-
* @param HashArray $array
135-
*/
136-
public function testGetHash( HashArray $array ) {
137-
$hash = $array->getHash();
138-
139-
$this->assertSame( $hash, $array->getHash() );
140-
141-
$elements = $this->elementInstancesProvider();
142-
$element = array_shift( $elements );
143-
$element = $element[0][0];
144-
145-
$array->addElement( $element );
146-
147-
if ( $array->hasElement( $element ) ) {
148-
$this->assertSame( $hash, $array->getHash() );
149-
} else {
150-
$this->assertNotSame( $hash, $array->getHash() );
151-
}
152-
}
153-
154132
}

tests/unit/Snak/SnakListTest.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Wikibase\DataModel\Tests\Snak;
44

55
use DataValues\StringValue;
6+
use Hashable;
67
use InvalidArgumentException;
78
use Wikibase\DataModel\Entity\PropertyId;
89
use Wikibase\DataModel\Snak\PropertyNoValueSnak;
@@ -293,4 +294,51 @@ public function testOrderByProperty( SnakList $snakList, SnakList $expected, arr
293294
}
294295
}
295296

297+
public function testHashableInterface() {
298+
$this->assertInstanceOf( Hashable::class, new SnakList() );
299+
}
300+
301+
public function testGetHash() {
302+
$snakList = new SnakList( [ new PropertyNoValueSnak( 1 ) ] );
303+
$hash = $snakList->getHash();
304+
305+
$this->assertInternalType( 'string', $hash, 'must be a string' );
306+
$this->assertNotSame( '', $hash, 'must not be empty' );
307+
$this->assertSame( $hash, $snakList->getHash(), 'second call must return the same hash' );
308+
309+
$otherList = new SnakList( [ new PropertyNoValueSnak( 2 ) ] );
310+
$this->assertNotSame( $hash, $otherList->getHash() );
311+
}
312+
313+
/**
314+
* This integration test (relies on SnakObject::getHash) is supposed to break whenever the hash
315+
* calculation changes.
316+
*/
317+
public function testHashStability() {
318+
$snakList = new SnakList();
319+
$this->assertSame( 'da39a3ee5e6b4b0d3255bfef95601890afd80709', $snakList->getHash() );
320+
321+
$snakList = new SnakList( [ new PropertyNoValueSnak( 1 ) ] );
322+
$this->assertSame( '4327ac5109aaf437ccce05580c563a5857d96c82', $snakList->getHash() );
323+
}
324+
325+
/**
326+
* @dataProvider provideEqualSnakLists
327+
*/
328+
public function testGivenEqualSnakLists_getHashIsTheSame( SnakList $self, SnakList $other ) {
329+
$this->assertSame( $self->getHash(), $other->getHash() );
330+
}
331+
332+
public function provideEqualSnakLists() {
333+
$empty = new SnakList();
334+
$oneSnak = new SnakList( [ new PropertyNoValueSnak( 1 ) ] );
335+
336+
return [
337+
'same empty object' => [ $empty, $empty ],
338+
'same non-empty object' => [ $oneSnak, $oneSnak ],
339+
'equal empty objects' => [ $empty, new SnakList() ],
340+
'equal non-empty objects' => [ $oneSnak, new SnakList( [ new PropertyNoValueSnak( 1 ) ] ) ],
341+
];
342+
}
343+
296344
}

0 commit comments

Comments
 (0)