EntityDeserializerTest   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 9
dl 0
loc 60
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 4 1
A testGivenLegacySerialization_itemIsDeserialized() 0 3 1
A testGivenCurrentSerialization_itemIsDeserialized() 0 3 1
A assertDeserializesToItem() 0 5 1
A newLegacySerialization() 0 3 1
A newCurrentSerialization() 0 3 1
A getSerializationFromFile() 0 4 1
A testGivenGeneratedSerialization_itemIsDeserialized() 0 3 1
A newTestItem() 0 10 1
1
<?php
2
3
namespace Tests\Integration\Wikibase\InternalSerialization\Deserializers;
4
5
use Deserializers\Deserializer;
6
use Serializers\Serializer;
7
use Tests\Integration\Wikibase\InternalSerialization\TestFactoryBuilder;
8
use Wikibase\DataModel\Entity\Item;
9
use Wikibase\DataModel\Entity\ItemId;
10
11
/**
12
 * @covers Wikibase\InternalSerialization\Deserializers\EntityDeserializer
13
 *
14
 * @license GPL-2.0-or-later
15
 * @author Jeroen De Dauw < [email protected] >
16
 */
17
class EntityDeserializerTest extends \PHPUnit\Framework\TestCase {
18
19
	/**
20
	 * @var Deserializer
21
	 */
22
	private $deserializer;
23
24
	/**
25
	 * @var Serializer
26
	 */
27
	private $currentSerializer;
28
29
	protected function setUp() : void {
30
		$this->deserializer = TestFactoryBuilder::newDeserializerFactoryWithDataValueSupport()->newEntityDeserializer();
31
		$this->currentSerializer = TestFactoryBuilder::newSerializerFactory()->newEntitySerializer();
32
	}
33
34
	public function testGivenLegacySerialization_itemIsDeserialized() {
35
		$this->assertDeserializesToItem( $this->newLegacySerialization() );
36
	}
37
38
	public function testGivenCurrentSerialization_itemIsDeserialized() {
39
		$this->assertDeserializesToItem( $this->newCurrentSerialization() );
40
	}
41
42
	private function assertDeserializesToItem( $serialization ) {
43
		$item = $this->deserializer->deserialize( $serialization );
44
45
		$this->assertInstanceOf( Item::class, $item );
46
	}
47
48
	private function newLegacySerialization() {
49
		return $this->getSerializationFromFile( 'items/legacy/recent/Q1.json' );
50
	}
51
52
	private function newCurrentSerialization() {
53
		return $this->getSerializationFromFile( 'items/current/Q1.json' );
54
	}
55
56
	private function getSerializationFromFile( $file ) {
57
		$itemJson = file_get_contents( __DIR__ . '/../../data/' . $file );
58
		return json_decode( $itemJson, true );
59
	}
60
61
	public function testGivenGeneratedSerialization_itemIsDeserialized() {
62
		$this->assertDeserializesToItem( $this->currentSerializer->serialize( $this->newTestItem() ) );
63
	}
64
65
	private function newTestItem() {
66
		$item = new Item( new ItemId( 'Q42' ) );
67
68
		$item->setLabel( 'en', 'foo' );
69
		$item->setLabel( 'de', 'bar' );
70
71
		$item->getSiteLinkList()->addNewSiteLink( 'wiki', 'page' );
72
73
		return $item;
74
	}
75
76
}
77