Skip to content

Commit f6ac19e

Browse files
committed
Move Hashable interface from HashArray to SnakList
1 parent d0017a8 commit f6ac19e

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 Wikibase\DataModel\HashArray;
67
use Wikibase\DataModel\Internal\MapValueHasher;
78

@@ -15,7 +16,7 @@
1516
* @author Jeroen De Dauw < jeroendedauw@gmail.com >
1617
* @author Addshore
1718
*/
18-
class SnakList extends HashArray {
19+
class SnakList extends HashArray implements Hashable {
1920

2021
/**
2122
* @see GenericArrayObject::getObjectType
@@ -93,7 +94,9 @@ public function getSnak( $snakHash ) {
9394
/**
9495
* @see HashArray::getHash
9596
*
96-
* @since 0.5
97+
* The hash is purely value based. Order of the elements in the array is not held into account.
98+
*
99+
* @since 0.1
97100
*
98101
* @return string
99102
*/

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;
@@ -292,4 +293,51 @@ public function testOrderByProperty( SnakList $snakList, SnakList $expected, arr
292293
}
293294
}
294295

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

0 commit comments

Comments
 (0)