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
|
|
|
|