Skip to content

Commit df04592

Browse files
committed
Merge pull request #597 from wmde/jsonTests
More tests for ItemId/PropertyId::unserialize
2 parents 7242a0c + ae2dbd8 commit df04592

2 files changed

Lines changed: 40 additions & 6 deletions

File tree

tests/unit/Entity/ItemIdTest.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,27 @@ public function testSerialize() {
8484
$this->assertSame( '["item","Q1"]', $id->serialize() );
8585
}
8686

87-
public function testUnserialize() {
87+
/**
88+
* @dataProvider serializationProvider
89+
*/
90+
public function testUnserialize( $json, $expected ) {
8891
$id = new ItemId( 'Q1' );
89-
$id->unserialize( '["item","Q2"]' );
90-
$this->assertSame( 'Q2', $id->getSerialization() );
92+
$id->unserialize( $json );
93+
$this->assertSame( $expected, $id->getSerialization() );
94+
}
95+
96+
public function serializationProvider() {
97+
return array(
98+
array( '["item","Q2"]', 'Q2' ),
99+
100+
// All these cases are kind of an injection vector and allow constructing invalid ids.
101+
array( '["string","Q2"]', 'Q2' ),
102+
array( '["","string"]', 'string' ),
103+
array( '["",""]', '' ),
104+
array( '["",2]', 2 ),
105+
array( '["",null]', null ),
106+
array( '', null ),
107+
);
91108
}
92109

93110
/**

tests/unit/Entity/PropertyIdTest.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,27 @@ public function testSerialize() {
8484
$this->assertSame( '["property","P1"]', $id->serialize() );
8585
}
8686

87-
public function testUnserialize() {
87+
/**
88+
* @dataProvider serializationProvider
89+
*/
90+
public function testUnserialize( $json, $expected ) {
8891
$id = new PropertyId( 'P1' );
89-
$id->unserialize( '["property","P2"]' );
90-
$this->assertSame( 'P2', $id->getSerialization() );
92+
$id->unserialize( $json );
93+
$this->assertSame( $expected, $id->getSerialization() );
94+
}
95+
96+
public function serializationProvider() {
97+
return array(
98+
array( '["property","P2"]', 'P2' ),
99+
100+
// All these cases are kind of an injection vector and allow constructing invalid ids.
101+
array( '["string","P2"]', 'P2' ),
102+
array( '["","string"]', 'string' ),
103+
array( '["",""]', '' ),
104+
array( '["",2]', 2 ),
105+
array( '["",null]', null ),
106+
array( '', null ),
107+
);
91108
}
92109

93110
/**

0 commit comments

Comments
 (0)