|
6 | 6 | use DataValues\Serializers\DataValueSerializer; |
7 | 7 | use Wikibase\DataModel\DeserializerFactory; |
8 | 8 | use Wikibase\DataModel\Entity\BasicEntityIdParser; |
9 | | -use Wikibase\DataModel\Entity\EntityDocument; |
10 | 9 | use Wikibase\DataModel\Entity\Item; |
11 | 10 | use Wikibase\DataModel\Entity\ItemId; |
12 | 11 | use Wikibase\DataModel\Entity\Property; |
|
16 | 15 | /** |
17 | 16 | * @license GPL-2.0+ |
18 | 17 | * @author Thomas Pellissier Tanon |
| 18 | + * @author Thiemo Mättig |
19 | 19 | */ |
20 | 20 | class EntitySerializationRoundtripTest extends \PHPUnit_Framework_TestCase { |
21 | 21 |
|
22 | | - /** |
23 | | - * @dataProvider entityProvider |
24 | | - */ |
25 | | - public function testEntitySerializationRoundtrips( EntityDocument $entity ) { |
26 | | - $serializerFactory = new SerializerFactory( new DataValueSerializer() ); |
27 | | - $deserializerFactory = new DeserializerFactory( |
28 | | - new DataValueDeserializer(), |
29 | | - new BasicEntityIdParser() |
30 | | - ); |
31 | | - |
32 | | - $serialization = $serializerFactory->newEntitySerializer()->serialize( $entity ); |
33 | | - $newEntity = $deserializerFactory->newEntityDeserializer()->deserialize( $serialization ); |
34 | | - $this->assertTrue( $entity->equals( $newEntity ) ); |
| 22 | + public function itemProvider() { |
| 23 | + $empty = new Item( new ItemId( 'Q42' ) ); |
| 24 | + |
| 25 | + $withLabels = new Item(); |
| 26 | + $withLabels->setLabel( 'en', 'Nyan Cat' ); |
| 27 | + $withLabels->setLabel( 'fr', 'Nyan Cat' ); |
| 28 | + |
| 29 | + $withDescriptions = new Item(); |
| 30 | + $withDescriptions->setDescription( 'en', 'Nyan Cat' ); |
| 31 | + $withDescriptions->setDescription( 'fr', 'Nyan Cat' ); |
| 32 | + |
| 33 | + $withAliases = new Item(); |
| 34 | + $withAliases->setAliases( 'en', [ 'Cat', 'My cat' ] ); |
| 35 | + $withAliases->setAliases( 'fr', [ 'Cat' ] ); |
| 36 | + |
| 37 | + $withStatements = new Item(); |
| 38 | + $withStatements->getStatements()->addNewStatement( new PropertyNoValueSnak( 42 ), null, null, 'guid' ); |
| 39 | + |
| 40 | + $withSiteLinks = new Item(); |
| 41 | + $withSiteLinks->getSiteLinkList()->addNewSiteLink( 'enwiki', 'Nyan Cat' ); |
| 42 | + |
| 43 | + return [ |
| 44 | + [ $empty ], |
| 45 | + [ $withLabels ], |
| 46 | + [ $withDescriptions ], |
| 47 | + [ $withAliases ], |
| 48 | + [ $withStatements ], |
| 49 | + [ $withSiteLinks ], |
| 50 | + ]; |
35 | 51 | } |
36 | 52 |
|
37 | | - public function entityProvider() { |
38 | | - $entities = []; |
| 53 | + /** |
| 54 | + * @dataProvider itemProvider |
| 55 | + */ |
| 56 | + public function testItemSerializationRoundtrips( Item $item ) { |
| 57 | + $serializer = $this->newSerializerFactory()->newItemSerializer(); |
| 58 | + $deserializer = $this->newDeserializerFactory()->newItemDeserializer(); |
39 | 59 |
|
40 | | - $entity = new Item( new ItemId( 'Q42' ) ); |
41 | | - $entities[] = [ $entity ]; |
| 60 | + $serialization = $serializer->serialize( $item ); |
| 61 | + $newEntity = $deserializer->deserialize( $serialization ); |
42 | 62 |
|
43 | | - $entity = new Item(); |
44 | | - $entity->setLabel( 'en', 'Nyan Cat' ); |
45 | | - $entity->setLabel( 'fr', 'Nyan Cat' ); |
46 | | - $entities[] = [ $entity ]; |
| 63 | + $this->assertTrue( $item->equals( $newEntity ) ); |
| 64 | + } |
47 | 65 |
|
48 | | - $entity = new Item(); |
49 | | - $entity->setDescription( 'en', 'Nyan Cat' ); |
50 | | - $entity->setDescription( 'fr', 'Nyan Cat' ); |
51 | | - $entities[] = [ $entity ]; |
| 66 | + public function propertyProvider() { |
| 67 | + return [ |
| 68 | + [ Property::newFromType( 'string' ) ], |
| 69 | + ]; |
| 70 | + } |
52 | 71 |
|
53 | | - $entity = new Item(); |
54 | | - $entity->setAliases( 'en', [ 'Cat', 'My cat' ] ); |
55 | | - $entity->setAliases( 'fr', [ 'Cat' ] ); |
56 | | - $entities[] = [ $entity ]; |
| 72 | + /** |
| 73 | + * @dataProvider propertyProvider |
| 74 | + */ |
| 75 | + public function testPropertySerializationRoundtrips( Property $property ) { |
| 76 | + $serializer = $this->newSerializerFactory()->newPropertySerializer(); |
| 77 | + $deserializer = $this->newDeserializerFactory()->newPropertyDeserializer(); |
57 | 78 |
|
58 | | - $entity = new Item(); |
59 | | - $entity->getStatements()->addNewStatement( new PropertyNoValueSnak( 42 ), null, null, 'guid' ); |
60 | | - $entities[] = [ $entity ]; |
| 79 | + $serialization = $serializer->serialize( $property ); |
| 80 | + $newEntity = $deserializer->deserialize( $serialization ); |
61 | 81 |
|
62 | | - $item = new Item(); |
63 | | - $item->getSiteLinkList()->addNewSiteLink( 'enwiki', 'Nyan Cat' ); |
64 | | - $entities[] = [ $item ]; |
| 82 | + $this->assertTrue( $property->equals( $newEntity ) ); |
| 83 | + } |
65 | 84 |
|
66 | | - $entities[] = [ Property::newFromType( 'string' ) ]; |
| 85 | + private function newSerializerFactory() { |
| 86 | + return new SerializerFactory( new DataValueSerializer() ); |
| 87 | + } |
67 | 88 |
|
68 | | - return $entities; |
| 89 | + private function newDeserializerFactory() { |
| 90 | + return new DeserializerFactory( new DataValueDeserializer(), new BasicEntityIdParser() ); |
69 | 91 | } |
70 | 92 |
|
71 | 93 | } |
0 commit comments