Completed
Pull Request — master (#221)
by no
12:30 queued 09:16
created

testPropertySerializationRoundtrips()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 1
eloc 6
nc 1
nop 1
1
<?php
2
3
namespace Tests\Wikibase\DataModel;
4
5
use DataValues\Deserializers\DataValueDeserializer;
6
use DataValues\Serializers\DataValueSerializer;
7
use Wikibase\DataModel\DeserializerFactory;
8
use Wikibase\DataModel\Entity\BasicEntityIdParser;
9
use Wikibase\DataModel\Entity\Item;
10
use Wikibase\DataModel\Entity\ItemId;
11
use Wikibase\DataModel\Entity\Property;
12
use Wikibase\DataModel\SerializerFactory;
13
use Wikibase\DataModel\Snak\PropertyNoValueSnak;
14
15
/**
16
 * @license GPL-2.0+
17
 * @author Thomas Pellissier Tanon
18
 * @author Thiemo Mättig
19
 */
20
class EntitySerializationRoundtripTest extends \PHPUnit_Framework_TestCase {
21
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
		];
51
	}
52
53
	/**
54
	 * @dataProvider itemProvider
55
	 */
56
	public function testItemSerializationRoundtrips( Item $item ) {
57
		$serializer = $this->newSerializerFactory()->newItemSerializer();
58
		$deserializer = $this->newDeserializerFactory()->newItemDeserializer();
59
60
		$serialization = $serializer->serialize( $item );
61
		$newEntity = $deserializer->deserialize( $serialization );
62
63
		$this->assertTrue( $item->equals( $newEntity ) );
64
	}
65
66
	public function propertyProvider() {
67
		return [
68
			[ Property::newFromType( 'string' ) ],
69
		];
70
	}
71
72
	/**
73
	 * @dataProvider propertyProvider
74
	 */
75
	public function testPropertySerializationRoundtrips( Property $property ) {
76
		$serializer = $this->newSerializerFactory()->newPropertySerializer();
77
		$deserializer = $this->newDeserializerFactory()->newPropertyDeserializer();
78
79
		$serialization = $serializer->serialize( $property );
80
		$newEntity = $deserializer->deserialize( $serialization );
81
82
		$this->assertTrue( $property->equals( $newEntity ) );
83
	}
84
85
	private function newSerializerFactory() {
86
		return new SerializerFactory( new DataValueSerializer() );
87
	}
88
89
	private function newDeserializerFactory() {
90
		return new DeserializerFactory( new DataValueDeserializer(), new BasicEntityIdParser() );
91
	}
92
93
}
94