1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PPP\Wikidata\TreeSimplifier; |
4
|
|
|
|
5
|
|
|
use DataValues\TimeValue; |
6
|
|
|
use DateTime; |
7
|
|
|
use PPP\DataModel\AbstractNode; |
8
|
|
|
use PPP\DataModel\IntersectionNode; |
9
|
|
|
use PPP\DataModel\JsonLdResourceNode; |
10
|
|
|
use PPP\DataModel\MissingNode; |
11
|
|
|
use PPP\DataModel\ResourceListNode; |
12
|
|
|
use PPP\DataModel\StringResourceNode; |
13
|
|
|
use PPP\DataModel\TripleNode; |
14
|
|
|
use PPP\DataModel\UnionNode; |
15
|
|
|
use PPP\Wikidata\WikibaseResourceNode; |
16
|
|
|
use Wikibase\DataModel\Entity\EntityIdValue; |
17
|
|
|
use Wikibase\DataModel\Entity\ItemId; |
18
|
|
|
use Wikibase\DataModel\Entity\PropertyId; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @covers PPP\Wikidata\TreeSimplifier\SpecificTripleNodeSimplifier |
22
|
|
|
* |
23
|
|
|
* @licence AGPLv3+ |
24
|
|
|
* @author Thomas Pellissier Tanon |
25
|
|
|
*/ |
26
|
|
|
class SpecificTripleNodeSimplifierTest extends NodeSimplifierBaseTest { |
27
|
|
|
|
28
|
|
|
protected function buildSimplifier() { |
29
|
|
|
$resourceListNodeParserMock = $this->getMockBuilder('PPP\Wikidata\ValueParsers\ResourceListNodeParser') |
30
|
|
|
->disableOriginalConstructor() |
31
|
|
|
->getMock(); |
32
|
|
|
$resourceListForEntityPropertyMock = $this->getMockBuilder('PPP\Wikidata\TreeSimplifier\ResourceListForEntityProperty') |
33
|
|
|
->disableOriginalConstructor() |
34
|
|
|
->getMock(); |
35
|
|
|
|
36
|
|
|
date_default_timezone_set('UTC'); |
37
|
|
|
return new SpecificTripleNodeSimplifier($resourceListNodeParserMock, $resourceListForEntityPropertyMock, new DateTime('2015-03-12')); |
|
|
|
|
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function simplifiableProvider() { |
41
|
|
|
return array( |
42
|
|
|
array( |
43
|
|
|
new TripleNode(new ResourceListNode(), new ResourceListNode(), new MissingNode()) |
44
|
|
|
) |
45
|
|
|
); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function nonSimplifiableProvider() { |
49
|
|
|
return array( |
50
|
|
|
array( |
51
|
|
|
new MissingNode() |
52
|
|
|
) |
53
|
|
|
); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @dataProvider simplifiedTripleProvider |
58
|
|
|
*/ |
59
|
|
|
public function testSimplify(AbstractNode $outputNode, TripleNode $inputNode) { |
60
|
|
|
$resourceListNodeParserMock = $this->getMockBuilder('PPP\Wikidata\ValueParsers\ResourceListNodeParser') |
61
|
|
|
->disableOriginalConstructor() |
62
|
|
|
->getMock(); |
63
|
|
|
$resourceListNodeParserMock->expects($this->any()) |
64
|
|
|
->method('parse') |
65
|
|
|
->with($this->equalTo(new ResourceListNode(array(new StringResourceNode('Douglas Adams'))))) |
66
|
|
|
->will($this->returnValue(new ResourceListNode(array(new WikibaseResourceNode('Douglas Adams', new EntityIdValue(new ItemId('Q42'))))))); |
67
|
|
|
$resourceListForEntityPropertyMock = $this->getMockBuilder('PPP\Wikidata\TreeSimplifier\ResourceListForEntityProperty') |
68
|
|
|
->disableOriginalConstructor() |
69
|
|
|
->getMock(); |
70
|
|
|
$resourceListForEntityPropertyMock->expects($this->any()) |
71
|
|
|
->method('getForEntityProperty') |
72
|
|
|
->with($this->equalTo(new ItemId('Q42')), $this->equalTo('P569')) |
73
|
|
|
->will($this->returnValue(new ResourceListNode(array(new WikibaseResourceNode('', new TimeValue('+00000001952-03-11T00:00:00Z', 0, 0, 0, TimeValue::PRECISION_DAY, 'http://www.wikidata.org/entity/Q1985786')))))); |
74
|
|
|
|
75
|
|
|
date_default_timezone_set('UTC'); |
76
|
|
|
$simplifier = new SpecificTripleNodeSimplifier($resourceListNodeParserMock, $resourceListForEntityPropertyMock, new DateTime('2015-03-12')); |
77
|
|
|
|
78
|
|
|
$this->assertEquals( |
79
|
|
|
$outputNode, |
80
|
|
|
$simplifier->simplify($inputNode) |
81
|
|
|
); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function simplifiedTripleProvider() { |
85
|
|
|
return array( |
86
|
|
|
array( |
87
|
|
|
new TripleNode( |
88
|
|
|
new ResourceListNode(array(new StringResourceNode('a'))), |
89
|
|
|
new ResourceListNode(array(new StringResourceNode('foo'))), |
90
|
|
|
new MissingNode() |
91
|
|
|
), |
92
|
|
|
new TripleNode( |
93
|
|
|
new ResourceListNode(array(new StringResourceNode('a'))), |
94
|
|
|
new ResourceListNode(array(new StringResourceNode('foo'))), |
95
|
|
|
new MissingNode() |
96
|
|
|
) |
97
|
|
|
), |
98
|
|
|
array( |
99
|
|
|
new ResourceListNode(array(new WikibaseResourceNode('Douglas Adams', new EntityIdValue(new ItemId('Q42'))))), |
100
|
|
|
new TripleNode( |
101
|
|
|
new ResourceListNode(array(new StringResourceNode('Douglas Adams'))), |
102
|
|
|
new ResourceListNode(array(new StringResourceNode('name'))), |
103
|
|
|
new MissingNode() |
104
|
|
|
) |
105
|
|
|
), |
106
|
|
|
array( |
107
|
|
|
new ResourceListNode(array(new WikibaseResourceNode('Douglas Adams', new EntityIdValue(new ItemId('Q42'))))), |
108
|
|
|
new TripleNode( |
109
|
|
|
new ResourceListNode(array(new StringResourceNode('Douglas Adams'))), |
110
|
|
|
new ResourceListNode(array(new StringResourceNode('definition'))), |
111
|
|
|
new MissingNode() |
112
|
|
|
) |
113
|
|
|
), |
114
|
|
|
array( |
115
|
|
|
new UnionNode(array( |
116
|
|
|
new ResourceListNode(array(new WikibaseResourceNode('Douglas Adams', new EntityIdValue(new ItemId('Q42'))))), |
117
|
|
|
new TripleNode( |
118
|
|
|
new ResourceListNode(array(new StringResourceNode('Douglas Adams'))), |
119
|
|
|
new ResourceListNode(array(new StringResourceNode('foo'))), |
120
|
|
|
new MissingNode() |
121
|
|
|
) |
122
|
|
|
)), |
123
|
|
|
new TripleNode( |
124
|
|
|
new ResourceListNode(array(new StringResourceNode('Douglas Adams'))), |
125
|
|
|
new ResourceListNode(array(new StringResourceNode('name'), new StringResourceNode('foo'))), |
126
|
|
|
new MissingNode() |
127
|
|
|
) |
128
|
|
|
), |
129
|
|
|
array( |
130
|
|
|
new IntersectionNode(array( |
131
|
|
|
new TripleNode( |
132
|
|
|
new ResourceListNode(array(new StringResourceNode('Douglas Adams'))), |
133
|
|
|
new ResourceListNode(array(new WikibaseResourceNode('', new EntityIdValue(new PropertyId('P40'))))), |
134
|
|
|
new MissingNode() |
135
|
|
|
), |
136
|
|
|
new TripleNode( |
137
|
|
|
new MissingNode(), |
138
|
|
|
new ResourceListNode(array(new WikibaseResourceNode('', new EntityIdValue(new PropertyId('P21'))))), |
139
|
|
|
new ResourceListNode(array(new WikibaseResourceNode('', new EntityIdValue(new ItemId('Q6581097'))))) |
140
|
|
|
) |
141
|
|
|
)), |
142
|
|
|
new TripleNode( |
143
|
|
|
new ResourceListNode(array(new StringResourceNode('Douglas Adams'))), |
144
|
|
|
new ResourceListNode(array(new StringResourceNode('son'))), |
145
|
|
|
new MissingNode() |
146
|
|
|
) |
147
|
|
|
), |
148
|
|
|
array( |
149
|
|
|
new IntersectionNode(array( |
150
|
|
|
new TripleNode( |
151
|
|
|
new ResourceListNode(array(new StringResourceNode('Douglas Adams'))), |
152
|
|
|
new ResourceListNode(array(new WikibaseResourceNode('', new EntityIdValue(new PropertyId('P40'))))), |
153
|
|
|
new MissingNode() |
154
|
|
|
), |
155
|
|
|
new TripleNode( |
156
|
|
|
new MissingNode(), |
157
|
|
|
new ResourceListNode(array(new WikibaseResourceNode('', new EntityIdValue(new PropertyId('P21'))))), |
158
|
|
|
new ResourceListNode(array(new WikibaseResourceNode('', new EntityIdValue(new ItemId('Q6581072'))))) |
159
|
|
|
) |
160
|
|
|
)), |
161
|
|
|
new TripleNode( |
162
|
|
|
new ResourceListNode(array(new StringResourceNode('Douglas Adams'))), |
163
|
|
|
new ResourceListNode(array(new StringResourceNode('daughter'))), |
164
|
|
|
new MissingNode() |
165
|
|
|
) |
166
|
|
|
), |
167
|
|
|
array( |
168
|
|
|
new ResourceListNode(array( |
169
|
|
|
new JsonLdResourceNode( |
170
|
|
|
'63', |
171
|
|
|
(object) array( |
172
|
|
|
'@context' => 'http://schema.org', |
173
|
|
|
'@type' => 'Duration', |
174
|
|
|
'name' => '63', |
175
|
|
|
'http://www.w3.org/1999/02/22-rdf-syntax-ns#value' => (object) array( |
176
|
|
|
'@type' => 'Duration', |
177
|
|
|
'@value' => 'P63Y0M1D' |
178
|
|
|
) |
179
|
|
|
) |
180
|
|
|
) |
181
|
|
|
)), |
182
|
|
|
new TripleNode( |
183
|
|
|
new ResourceListNode(array(new StringResourceNode('Douglas Adams'))), |
184
|
|
|
new ResourceListNode(array(new StringResourceNode('age'))), |
185
|
|
|
new MissingNode() |
186
|
|
|
) |
187
|
|
|
), |
188
|
|
|
); |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|
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.