1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PPP\Wikidata\TreeSimplifier; |
4
|
|
|
|
5
|
|
|
use DataValues\StringValue; |
6
|
|
|
use PPP\DataModel\AbstractNode; |
7
|
|
|
use PPP\DataModel\MissingNode; |
8
|
|
|
use PPP\DataModel\ResourceListNode; |
9
|
|
|
use PPP\DataModel\StringResourceNode; |
10
|
|
|
use PPP\DataModel\TripleNode; |
11
|
|
|
use PPP\Wikidata\WikibaseResourceNode; |
12
|
|
|
use Wikibase\DataModel\Entity\EntityIdValue; |
13
|
|
|
use Wikibase\DataModel\Entity\Item; |
14
|
|
|
use Wikibase\DataModel\Entity\ItemId; |
15
|
|
|
use Wikibase\DataModel\Entity\PropertyId; |
16
|
|
|
use Wikibase\DataModel\Snak\PropertySomeValueSnak; |
17
|
|
|
use Wikibase\DataModel\Snak\PropertyValueSnak; |
18
|
|
|
use Wikibase\DataModel\Statement\Statement; |
19
|
|
|
use Wikibase\EntityStore\InMemory\InMemoryEntityStore; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @covers PPP\Wikidata\TreeSimplifier\MissingObjectTripleNodeSimplifier |
23
|
|
|
* |
24
|
|
|
* @licence AGPLv3+ |
25
|
|
|
* @author Thomas Pellissier Tanon |
26
|
|
|
*/ |
27
|
|
|
class MissingObjectTripleNodeSimplifierTest extends NodeSimplifierBaseTest { |
28
|
|
|
|
29
|
|
|
public function buildSimplifier() { |
30
|
|
|
$resourceListNodeParserMock = $this->getMockBuilder('PPP\Wikidata\ValueParsers\ResourceListNodeParser') |
31
|
|
|
->disableOriginalConstructor() |
32
|
|
|
->getMock(); |
33
|
|
|
$resourceListForEntityPropertyMock = $this->getMockBuilder('PPP\Wikidata\TreeSimplifier\ResourceListForEntityProperty') |
34
|
|
|
->disableOriginalConstructor() |
35
|
|
|
->getMock(); |
36
|
|
|
return new MissingObjectTripleNodeSimplifier($resourceListNodeParserMock, $resourceListForEntityPropertyMock); |
|
|
|
|
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @see NodeSimplifierBaseTest::simplifiableProvider |
41
|
|
|
*/ |
42
|
|
|
public function simplifiableProvider() { |
43
|
|
|
return array( |
44
|
|
|
array( |
45
|
|
|
new TripleNode( |
46
|
|
|
new ResourceListNode(array(new WikibaseResourceNode('', new EntityIdValue(new ItemId('Q42'))))), |
47
|
|
|
new ResourceListNode(array(new WikibaseResourceNode('', new EntityIdValue(new PropertyId('P214'))))), |
48
|
|
|
new MissingNode() |
49
|
|
|
) |
50
|
|
|
), |
51
|
|
|
array( |
52
|
|
|
new TripleNode( |
53
|
|
|
new ResourceListNode(array(new StringResourceNode('Douglas Adams'))), |
54
|
|
|
new ResourceListNode(array(new StringResourceNode('VIAF'))), |
55
|
|
|
new MissingNode() |
56
|
|
|
) |
57
|
|
|
), |
58
|
|
|
); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @see NodeSimplifierBaseTest::nonSimplifiableProvider |
63
|
|
|
*/ |
64
|
|
|
public function nonSimplifiableProvider() { |
65
|
|
|
return array( |
66
|
|
|
array( |
67
|
|
|
new MissingNode() |
68
|
|
|
), |
69
|
|
|
array( |
70
|
|
|
new TripleNode( |
71
|
|
|
new MissingNode(), |
72
|
|
|
new ResourceListNode(array(new WikibaseResourceNode('', new EntityIdValue(new PropertyId('P214'))))), |
73
|
|
|
new ResourceListNode(array(new WikibaseResourceNode('', new StringValue('113230702')))) |
74
|
|
|
) |
75
|
|
|
), |
76
|
|
|
array( |
77
|
|
|
new TripleNode( |
78
|
|
|
new ResourceListNode(array(new WikibaseResourceNode('', new EntityIdValue(new ItemId('Q42'))))), |
79
|
|
|
new MissingNode(), |
80
|
|
|
new ResourceListNode(array(new WikibaseResourceNode('', new StringValue('113230702')))) |
81
|
|
|
) |
82
|
|
|
), |
83
|
|
|
); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @dataProvider simplificationProvider |
88
|
|
|
*/ |
89
|
|
|
public function testSimplification(TripleNode $queryNode, AbstractNode $responseNodes, Item $item, PropertyId $propertyId) { |
90
|
|
|
$resourceListNodeParserMock = $this->getMockBuilder('PPP\Wikidata\ValueParsers\ResourceListNodeParser') |
91
|
|
|
->disableOriginalConstructor() |
92
|
|
|
->getMock(); |
93
|
|
|
$resourceListNodeParserMock->expects($this->any()) |
94
|
|
|
->method('parse') |
95
|
|
|
->will($this->onConsecutiveCalls( |
96
|
|
|
new ResourceListNode(array(new WikibaseResourceNode('', new EntityIdValue($item->getId())))), |
|
|
|
|
97
|
|
|
new ResourceListNode(array(new WikibaseResourceNode('', new EntityIdValue($propertyId)))) |
98
|
|
|
)); |
99
|
|
|
|
100
|
|
|
$simplifier = new MissingObjectTripleNodeSimplifier( |
101
|
|
|
$resourceListNodeParserMock, |
102
|
|
|
new ResourceListForEntityProperty(new InMemoryEntityStore(array($item))) |
103
|
|
|
); |
104
|
|
|
$this->assertEquals( |
105
|
|
|
$responseNodes, |
106
|
|
|
$simplifier->simplify($queryNode) |
107
|
|
|
); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function simplificationProvider() { |
111
|
|
|
$list = array(); |
112
|
|
|
|
113
|
|
|
//Value |
114
|
|
|
$douglasAdamItem = new Item(); |
115
|
|
|
$douglasAdamItem->setId(new ItemId('Q42')); |
116
|
|
|
$birthPlaceStatement = new Statement( |
117
|
|
|
new PropertyValueSnak(new PropertyId('P214'), new StringValue('113230702')) |
118
|
|
|
); |
119
|
|
|
$birthPlaceStatement->setGuid('42'); |
120
|
|
|
$douglasAdamItem->getStatements()->addStatement($birthPlaceStatement); |
121
|
|
|
$list[] = array( |
122
|
|
|
new TripleNode( |
123
|
|
|
new ResourceListNode(array(new WikibaseResourceNode('', new EntityIdValue(new ItemId('Q42'))))), |
124
|
|
|
new ResourceListNode(array(new WikibaseResourceNode('', new EntityIdValue(new PropertyId('P214'))))), |
125
|
|
|
new MissingNode() |
126
|
|
|
), |
127
|
|
|
new ResourceListNode(array( |
128
|
|
|
new ResourceListNode(array( |
129
|
|
|
new WikibaseResourceNode( |
130
|
|
|
'', |
131
|
|
|
new StringValue('113230702'), |
132
|
|
|
new ItemId('Q42'), |
133
|
|
|
new PropertyId('P214') |
134
|
|
|
) |
135
|
|
|
)) |
136
|
|
|
)), |
137
|
|
|
$douglasAdamItem, |
138
|
|
|
new PropertyId('P214') |
139
|
|
|
); |
140
|
|
|
|
141
|
|
|
//SomeValue |
142
|
|
|
$douglasAdamItem = new Item(); |
143
|
|
|
$douglasAdamItem->setId(new ItemId('Q42')); |
144
|
|
|
$birthPlaceStatement = new Statement(new PropertySomeValueSnak(new PropertyId('P19'))); |
145
|
|
|
$birthPlaceStatement->setGuid('42'); |
146
|
|
|
$douglasAdamItem->getStatements()->addStatement($birthPlaceStatement); |
147
|
|
|
$list[] = array( |
148
|
|
|
new TripleNode( |
149
|
|
|
new ResourceListNode(array(new WikibaseResourceNode('', new EntityIdValue(new ItemId('Q42'))))), |
150
|
|
|
new ResourceListNode(array(new WikibaseResourceNode('', new EntityIdValue(new PropertyId('P19'))))), |
151
|
|
|
new MissingNode() |
152
|
|
|
), |
153
|
|
|
new ResourceListNode(array()), |
154
|
|
|
$douglasAdamItem, |
155
|
|
|
new PropertyId('P19') |
156
|
|
|
); |
157
|
|
|
|
158
|
|
|
//No result |
159
|
|
|
$douglasAdamItem = new Item(); |
160
|
|
|
$douglasAdamItem->setId(new ItemId('Q42')); |
161
|
|
|
$list[] = array( |
162
|
|
|
new TripleNode( |
163
|
|
|
new ResourceListNode(array(new WikibaseResourceNode('', new EntityIdValue(new ItemId('Q42'))))), |
164
|
|
|
new ResourceListNode(array(new WikibaseResourceNode('', new EntityIdValue(new PropertyId('P19'))))), |
165
|
|
|
new MissingNode() |
166
|
|
|
), |
167
|
|
|
new ResourceListNode(array()), |
168
|
|
|
$douglasAdamItem, |
169
|
|
|
new PropertyId('P19') |
170
|
|
|
); |
171
|
|
|
|
172
|
|
|
//Parsing |
173
|
|
|
$douglasAdamItem = new Item(); |
174
|
|
|
$douglasAdamItem->setId(new ItemId('Q42')); |
175
|
|
|
$list[] = array( |
176
|
|
|
new TripleNode( |
177
|
|
|
new ResourceListNode(array(new StringResourceNode('Douglas Adams'))), |
178
|
|
|
new ResourceListNode(array(new StringResourceNode('VIAF'))), |
179
|
|
|
new MissingNode() |
180
|
|
|
), |
181
|
|
|
new ResourceListNode(array()), |
182
|
|
|
$douglasAdamItem, |
183
|
|
|
new PropertyId('P214') |
184
|
|
|
); |
185
|
|
|
|
186
|
|
|
return $list; |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.