|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Tests\Queryr\Resources\Builders; |
|
4
|
|
|
|
|
5
|
|
|
use DataValues\StringValue; |
|
6
|
|
|
use Queryr\Resources\Builders\SimpleItemBuilder; |
|
7
|
|
|
use Queryr\Resources\Builders\SimpleStatementsBuilder; |
|
8
|
|
|
use Queryr\Resources\SimpleItem; |
|
9
|
|
|
use Queryr\Resources\SimpleStatement; |
|
10
|
|
|
use Wikibase\DataModel\Entity\Item; |
|
11
|
|
|
use Wikibase\DataModel\Entity\PropertyId; |
|
12
|
|
|
use Wikibase\DataModel\SiteLinkList; |
|
13
|
|
|
use Wikibase\DataModel\Snak\PropertyNoValueSnak; |
|
14
|
|
|
use Wikibase\DataModel\Snak\PropertyValueSnak; |
|
15
|
|
|
use Wikibase\DataModel\Statement\Statement; |
|
16
|
|
|
use Wikibase\DataModel\Term\Fingerprint; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @covers Queryr\Resources\Builders\SimpleItemBuilder |
|
20
|
|
|
* |
|
21
|
|
|
* @licence GNU GPL v2+ |
|
22
|
|
|
* @author Jeroen De Dauw < [email protected] > |
|
23
|
|
|
*/ |
|
24
|
|
|
class SimpleItemBuilderTest extends \PHPUnit_Framework_TestCase { |
|
25
|
|
|
|
|
26
|
|
|
private function newItem() { |
|
27
|
|
|
$item = new Item(); |
|
28
|
|
|
|
|
29
|
|
|
$item->setId( 1337 ); |
|
30
|
|
|
|
|
31
|
|
|
$item->setFingerprint( $this->newFingerprint() ); |
|
32
|
|
|
$item->setSiteLinkList( $this->newSiteLinks() ); |
|
33
|
|
|
|
|
34
|
|
|
$this->addStatements( $item ); |
|
35
|
|
|
|
|
36
|
|
|
return $item; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
private function newFingerprint() { |
|
40
|
|
|
$fingerprint = new Fingerprint(); |
|
41
|
|
|
|
|
42
|
|
|
$fingerprint->setLabel( 'en', 'foo' ); |
|
43
|
|
|
$fingerprint->setLabel( 'de', 'bar' ); |
|
44
|
|
|
$fingerprint->setLabel( 'nl', 'baz' ); |
|
45
|
|
|
|
|
46
|
|
|
$fingerprint->setDescription( 'de', 'de description' ); |
|
47
|
|
|
|
|
48
|
|
|
$fingerprint->setAliasGroup( 'en', [ 'first en alias', 'second en alias' ] ); |
|
49
|
|
|
$fingerprint->setAliasGroup( 'de', [ 'first de alias', 'second de alias' ] ); |
|
50
|
|
|
|
|
51
|
|
|
return $fingerprint; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
private function newSiteLinks() { |
|
55
|
|
|
$links = new SiteLinkList(); |
|
56
|
|
|
|
|
57
|
|
|
$links->addNewSiteLink( 'enwiki', 'En Page' ); |
|
58
|
|
|
$links->addNewSiteLink( 'dewiki', 'De Page' ); |
|
59
|
|
|
|
|
60
|
|
|
return $links; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
private function addStatements( Item $item ) { |
|
64
|
|
|
$statement = new Statement( new PropertyValueSnak( 42, new StringValue( 'kittens' ) ) ); |
|
65
|
|
|
$statement->setGuid( 'first guid' ); |
|
66
|
|
|
|
|
67
|
|
|
$item->getStatements()->addStatement( $statement ); |
|
68
|
|
|
|
|
69
|
|
|
$statement = new Statement( new PropertyNoValueSnak( 23 ) ); |
|
70
|
|
|
$statement->setGuid( 'second guid' ); |
|
71
|
|
|
|
|
72
|
|
|
$item->getStatements()->addStatement( $statement ); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function testSerializationForDe() { |
|
76
|
|
|
$simpleItem = $this->buildNewSimpleItemForLanguage( 'de' ); |
|
77
|
|
|
|
|
78
|
|
|
$expected = new SimpleItem(); |
|
79
|
|
|
$expected->ids = [ |
|
|
|
|
|
|
80
|
|
|
'wikidata' => 'Q1337', |
|
81
|
|
|
'en_wikipedia' => 'En Page', |
|
82
|
|
|
'de_wikipedia' => 'De Page', |
|
83
|
|
|
]; |
|
84
|
|
|
|
|
85
|
|
|
$expected->label = 'bar'; |
|
86
|
|
|
$expected->description = 'de description'; |
|
87
|
|
|
$expected->aliases = [ 'first de alias', 'second de alias' ]; |
|
88
|
|
|
|
|
89
|
|
|
$expected->statements = [ $this->getSimpleStatement() ]; |
|
90
|
|
|
|
|
91
|
|
|
$this->assertEquals( $expected, $simpleItem ); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
private function buildNewSimpleItemForLanguage( $languageCode ) { |
|
95
|
|
|
$labelLookup = $this->getMock( 'Queryr\Resources\Builders\ResourceLabelLookup' ); |
|
|
|
|
|
|
96
|
|
|
|
|
97
|
|
|
$labelLookup->expects( $this->any() ) |
|
98
|
|
|
->method( 'getLabelByIdAndLanguage' ) |
|
99
|
|
|
->will( $this->returnValue( 'awesome label' ) ); |
|
100
|
|
|
|
|
101
|
|
|
$statementsBuilder = new SimpleStatementsBuilder( $languageCode, $labelLookup ); |
|
102
|
|
|
$itemBuilder = new SimpleItemBuilder( $languageCode, $statementsBuilder ); |
|
103
|
|
|
|
|
104
|
|
|
return $itemBuilder->buildFromItem( $this->newItem() ); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
private function getSimpleStatement() { |
|
108
|
|
|
return SimpleStatement::newInstance() |
|
109
|
|
|
->withPropertyName( 'awesome label' ) |
|
110
|
|
|
->withPropertyId( new PropertyId( 'P42' ) ) |
|
111
|
|
|
->withType( 'string' ) |
|
112
|
|
|
->withValues( [ new StringValue( 'kittens' ) ] ); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
public function testSerializationForEn() { |
|
116
|
|
|
$simpleItem = $this->buildNewSimpleItemForLanguage( 'en' ); |
|
117
|
|
|
|
|
118
|
|
|
$expected = new SimpleItem(); |
|
119
|
|
|
$expected->ids = [ |
|
|
|
|
|
|
120
|
|
|
'wikidata' => 'Q1337', |
|
121
|
|
|
'en_wikipedia' => 'En Page', |
|
122
|
|
|
]; |
|
123
|
|
|
|
|
124
|
|
|
$expected->label = 'foo'; |
|
125
|
|
|
$expected->aliases = [ 'first en alias', 'second en alias' ]; |
|
126
|
|
|
|
|
127
|
|
|
$expected->statements = [ $this->getSimpleStatement() ]; |
|
128
|
|
|
|
|
129
|
|
|
$this->assertEquals( $expected, $simpleItem ); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
public function testSerializationForNl() { |
|
133
|
|
|
$simpleItem = $this->buildNewSimpleItemForLanguage( 'nl' ); |
|
134
|
|
|
|
|
135
|
|
|
$expected = new SimpleItem(); |
|
136
|
|
|
$expected->ids = [ |
|
|
|
|
|
|
137
|
|
|
'wikidata' => 'Q1337', |
|
138
|
|
|
'en_wikipedia' => 'En Page', |
|
139
|
|
|
]; |
|
140
|
|
|
|
|
141
|
|
|
$expected->label = 'baz'; |
|
142
|
|
|
|
|
143
|
|
|
$expected->statements = [ $this->getSimpleStatement() ]; |
|
144
|
|
|
|
|
145
|
|
|
$this->assertEquals( $expected, $simpleItem ); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
} |
|
149
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..