EntitySerializationRoundtripTest   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 14

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 14
dl 0
loc 76
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A itemProvider() 0 32 1
A testItemSerializationRoundtrips() 0 9 1
A propertyProvider() 0 5 1
A testPropertySerializationRoundtrips() 0 9 1
A newSerializerFactory() 0 3 1
A newDeserializerFactory() 0 3 1
1
<?php
2
3
namespace Tests\Wikibase\DataModel;
4
5
use DataValues\Deserializers\DataValueDeserializer;
6
use DataValues\Serializers\DataValueSerializer;
7
use PHPUnit\Framework\TestCase;
8
use Wikibase\DataModel\DeserializerFactory;
9
use Wikibase\DataModel\Entity\BasicEntityIdParser;
10
use Wikibase\DataModel\Entity\Item;
11
use Wikibase\DataModel\Entity\ItemId;
12
use Wikibase\DataModel\Entity\Property;
13
use Wikibase\DataModel\SerializerFactory;
14
use Wikibase\DataModel\Snak\PropertyNoValueSnak;
15
16
/**
17
 * @license GPL-2.0-or-later
18
 * @author Thomas Pellissier Tanon
19
 * @author Thiemo Kreuz
20
 */
21
class EntitySerializationRoundtripTest extends TestCase {
22
23
	public function itemProvider() {
24
		$empty = new Item( new ItemId( 'Q42' ) );
25
26
		$withLabels = new Item();
27
		$withLabels->setLabel( 'en', 'Nyan Cat' );
28
		$withLabels->setLabel( 'fr', 'Nyan Cat' );
29
30
		$withDescriptions = new Item();
31
		$withDescriptions->setDescription( 'en', 'Nyan Cat' );
32
		$withDescriptions->setDescription( 'fr', 'Nyan Cat' );
33
34
		$withAliases = new Item();
35
		$withAliases->setAliases( 'en', [ 'Cat', 'My cat' ] );
36
		$withAliases->setAliases( 'fr', [ 'Cat' ] );
37
38
		$withStatements = new Item();
39
		$withStatements->getStatements()->addNewStatement(
40
			new PropertyNoValueSnak( 42 ), null, null, 'guid'
41
		);
42
43
		$withSiteLinks = new Item();
44
		$withSiteLinks->getSiteLinkList()->addNewSiteLink( 'enwiki', 'Nyan Cat' );
45
46
		return [
47
			[ $empty ],
48
			[ $withLabels ],
49
			[ $withDescriptions ],
50
			[ $withAliases ],
51
			[ $withStatements ],
52
			[ $withSiteLinks ],
53
		];
54
	}
55
56
	/**
57
	 * @dataProvider itemProvider
58
	 */
59
	public function testItemSerializationRoundtrips( Item $item ) {
60
		$serializer = $this->newSerializerFactory()->newItemSerializer();
61
		$deserializer = $this->newDeserializerFactory()->newItemDeserializer();
62
63
		$serialization = $serializer->serialize( $item );
64
		$newEntity = $deserializer->deserialize( $serialization );
65
66
		$this->assertTrue( $item->equals( $newEntity ) );
67
	}
68
69
	public function propertyProvider() {
70
		return [
71
			[ Property::newFromType( 'string' ) ],
72
		];
73
	}
74
75
	/**
76
	 * @dataProvider propertyProvider
77
	 */
78
	public function testPropertySerializationRoundtrips( Property $property ) {
79
		$serializer = $this->newSerializerFactory()->newPropertySerializer();
80
		$deserializer = $this->newDeserializerFactory()->newPropertyDeserializer();
81
82
		$serialization = $serializer->serialize( $property );
83
		$newEntity = $deserializer->deserialize( $serialization );
84
85
		$this->assertTrue( $property->equals( $newEntity ) );
86
	}
87
88
	private function newSerializerFactory() {
89
		return new SerializerFactory( new DataValueSerializer() );
90
	}
91
92
	private function newDeserializerFactory() {
93
		return new DeserializerFactory( new DataValueDeserializer(), new BasicEntityIdParser() );
94
	}
95
96
}
97