-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Don't use numeric IDs for internal PHP serialization. #716
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -88,7 +88,7 @@ public function testHashStability() { | |
$hash = $snak->getHash(); | ||
|
||
// @codingStandardsIgnoreStart | ||
$expected = sha1( 'C:41:"Wikibase\DataModel\Snak\PropertyValueSnak":53:{a:2:{i:0;i:1;i:1;C:22:"DataValues\StringValue":1:{a}}}' ); | ||
$expected = sha1( 'C:41:"Wikibase\DataModel\Snak\PropertyValueSnak":58:{a:2:{i:0;s:2:"P1";i:1;C:22:"DataValues\StringValue":1:{a}}}' ); | ||
// @codingStandardsIgnoreEnd | ||
$this->assertSame( $expected, $hash ); | ||
} | ||
|
@@ -121,15 +121,62 @@ public function notEqualsProvider() { | |
]; | ||
} | ||
|
||
public function testSerialize() { | ||
$snak = new PropertyValueSnak( new PropertyId( 'P1' ), new StringValue( 'a' ) ); | ||
$this->assertSame( 'a:2:{i:0;i:1;i:1;C:22:"DataValues\StringValue":1:{a}}', $snak->serialize() ); | ||
public function provideDataToSerialize() { | ||
$p2 = new PropertyId( 'P2' ); | ||
$p2foo = new PropertyId( 'foo:P2' ); | ||
$value = new StringValue( 'b' ); | ||
|
||
return [ | ||
'string' => [ | ||
'a:2:{i:0;s:2:"P2";i:1;C:22:"DataValues\StringValue":1:{b}}', | ||
new PropertyValueSnak( $p2, $value ), | ||
], | ||
'foreign' => [ | ||
'a:2:{i:0;s:6:"foo:P2";i:1;C:22:"DataValues\StringValue":1:{b}}', | ||
new PropertyValueSnak( $p2foo, $value ), | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider provideDataToSerialize | ||
*/ | ||
public function testSerialize( $expected, Snak $snak ) { | ||
$serialized = $snak->serialize(); | ||
$this->assertSame( $expected, $serialized ); | ||
|
||
$snak2 = new PropertyValueSnak( new PropertyId( 'P1' ), new StringValue( 'a' ) ); | ||
$snak2->unserialize( $serialized ); | ||
$this->assertTrue( $snak->equals( $snak2 ), 'round trip' ); | ||
} | ||
|
||
public function testUnserialize() { | ||
public function provideDataToUnserialize() { | ||
$p2 = new PropertyId( 'P2' ); | ||
$p2foo = new PropertyId( 'foo:P2' ); | ||
$value = new StringValue( 'b' ); | ||
|
||
return [ | ||
'legacy' => [ | ||
new PropertyValueSnak( $p2, $value ), | ||
'a:2:{i:0;i:2;i:1;C:22:"DataValues\StringValue":1:{b}}' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It does not affect test result but I believe to be 100% accurate "i:2" here should become "i:1". Minor thing, I am going to fix this, and squash into other commits. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please ignore this comment. I looked at wrong spot in the old code. All good here. |
||
], | ||
'current' => [ | ||
new PropertyValueSnak( $p2, $value ), | ||
'a:2:{i:0;s:2:"P2";i:1;C:22:"DataValues\StringValue":1:{b}}' | ||
], | ||
'foreign' => [ | ||
new PropertyValueSnak( $p2foo, $value ), | ||
'a:2:{i:0;s:6:"foo:P2";i:1;C:22:"DataValues\StringValue":1:{b}}' | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider provideDataToUnserialize | ||
*/ | ||
public function testUnserialize( $expected, $serialized ) { | ||
$snak = new PropertyValueSnak( new PropertyId( 'P1' ), new StringValue( 'a' ) ); | ||
$snak->unserialize( 'a:2:{i:0;i:2;i:1;C:22:"DataValues\StringValue":1:{b}}' ); | ||
$expected = new PropertyValueSnak( new PropertyId( 'P2' ), new StringValue( 'b' ) ); | ||
$snak->unserialize( $serialized ); | ||
$this->assertTrue( $snak->equals( $expected ) ); | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder is this correct to change this number here. Change here is indeed breaking change but the interface of the methods does not change. So I guess version number change is to show that serialization format has changed in the release 7.0. I am find with that I just wonder if this is "how it should be done" (tm).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe the returned value is a crucial part of the contract of a method. If the format of the return value changes, it's a different method. The idea of the since tag is to make this obvious.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have some places where we bumped the number but then mention the method was already there in some other form or something along those lines
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea, something like this seems reaonable.