Skip to content

Commit

Permalink
Merge pull request #142 from wmde/hash
Browse files Browse the repository at this point in the history
Fix hashes after serialization change
  • Loading branch information
lucaswerkmeister authored Oct 21, 2022
2 parents a81654e + 7e1ebd0 commit 0d3d024
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/DataValues/QuantityValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,26 @@ public function __unserialize( array $data ): void {
$this->__construct( $amount, $unit, $upperBound, $lowerBound );
}

public function getSerializationForHash(): string {
// mimic a legacy serialization of __serialize() (amount + unit + upperBound + lowerBound)
$amountSerialization = method_exists( $this->amount, 'getSerializationForHash' )
? $this->amount->getSerializationForHash()
: serialize( $this->amount );
$unitSerialization = serialize( $this->unit );
$upperBoundSerialization = method_exists( $this->upperBound, 'getSerializationForHash' )
? $this->upperBound->getSerializationForHash()
: serialize( $this->upperBound );
$lowerBoundSerialization = method_exists( $this->lowerBound, 'getSerializationForHash' )
? $this->lowerBound->getSerializationForHash()
: serialize( $this->lowerBound );

$data = 'a:4:{i:0;' . $amountSerialization . 'i:1;' . $unitSerialization .
'i:2;' . $upperBoundSerialization . 'i:3;' . $lowerBoundSerialization . '}';

return 'C:' . strlen( static::class ) . ':"' . static::class .
'":' . strlen( $data ) . ':{' . $data . '}';
}

/**
* Returns this quantity's upper bound.
*
Expand Down
13 changes: 13 additions & 0 deletions src/DataValues/UnboundedQuantityValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,19 @@ public function __unserialize( array $data ): void {
$this->__construct( $amount, $unit );
}

public function getSerializationForHash(): string {
// mimic a legacy serialization of __serialize() (amount + unit)
$amountSerialization = method_exists( $this->amount, 'getSerializationForHash' )
? $this->amount->getSerializationForHash()
: serialize( $this->amount );
$unitSerialization = serialize( $this->unit );

$data = 'a:2:{i:0;' . $amountSerialization . 'i:1;' . $unitSerialization . '}';

return 'C:' . strlen( static::class ) . ':"' . static::class .
'":' . strlen( $data ) . ':{' . $data . '}';
}

/**
* @see DataValue::getType
*
Expand Down
22 changes: 22 additions & 0 deletions tests/DataValues/QuantityValueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -458,4 +458,26 @@ public function transformProvider() {
];
}

/** @dataProvider instanceWithHashProvider */
public function testGetHashStability( QuantityValue $quantity, string $hash ) {
$this->assertSame( $hash, $quantity->getHash() );
}

public function instanceWithHashProvider(): iterable {
// all hashes obtained from data-values/data-values==3.0.0 data-values/number==0.11.1 under PHP 7.2.34
yield '10+-1' => [
QuantityValue::newFromNumber( '+10', '1', '+11', '+9' ),
'2eb812346e6bbc1bc47ed7da130bfa5d',
];
yield '500 miles, or a little bit more' => [
QuantityValue::newFromNumber(
'+558.84719',
'http://www.wikidata.org/entity/Q253276',
'+558.84719',
'+558.84719'
),
'5fb8c57f9ba8225146b03abdbb45c431',
];
}

}
17 changes: 17 additions & 0 deletions tests/DataValues/UnboundedQuantityValueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -319,4 +319,21 @@ public function transformProvider() {
];
}

/** @dataProvider instanceWithHashProvider */
public function testGetHashStability( UnboundedQuantityValue $quantity, string $hash ) {
$this->assertSame( $hash, $quantity->getHash() );
}

public function instanceWithHashProvider(): iterable {
// all hashes obtained from data-values/data-values==3.0.0 data-values/number==0.11.1 under PHP 7.2.34
yield '10+-1' => [
UnboundedQuantityValue::newFromNumber( '+10', '1' ),
'6fc1f325788cfea1bc9c869fbbfe7824',
];
yield '500 miles, or a little bit more' => [
UnboundedQuantityValue::newFromNumber( '+558.84719', 'http://www.wikidata.org/entity/Q253276' ),
'8caf675b471b8579d8adae2e9721dbc9',
];
}

}

0 comments on commit 0d3d024

Please sign in to comment.