simplifiableProvider()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 17
nc 1
nop 0
1
<?php
2
3
namespace PPP\Wikidata\TreeSimplifier;
4
5
use Ask\Language\Description\AnyValue;
6
use Ask\Language\Description\Conjunction;
7
use Ask\Language\Description\Description;
8
use Ask\Language\Description\Disjunction;
9
use Ask\Language\Description\SomeProperty;
10
use Ask\Language\Description\ValueDescription;
11
use Ask\Language\Option\QueryOptions;
12
use DataValues\StringValue;
13
use PPP\DataModel\AbstractNode;
14
use PPP\DataModel\IntersectionNode;
15
use PPP\DataModel\MissingNode;
16
use PPP\DataModel\ResourceListNode;
17
use PPP\DataModel\StringResourceNode;
18
use PPP\DataModel\TripleNode;
19
use PPP\DataModel\UnionNode;
20
use PPP\Module\TreeSimplifier\NodeSimplifierFactory;
21
use PPP\Wikidata\WikibaseResourceNode;
22
use Wikibase\DataModel\Entity\EntityIdValue;
23
use Wikibase\DataModel\Entity\ItemId;
24
use Wikibase\DataModel\Entity\Property;
25
use Wikibase\DataModel\Entity\PropertyId;
26
27
/**
28
 * @covers PPP\Wikidata\TreeSimplifier\MissingSubjectTripleNodeSimplifier
29
 *
30
 * @licence AGPLv3+
31
 * @author Thomas Pellissier Tanon
32
 */
33
class MissingSubjectTripleNodeSimplifierTest extends NodeSimplifierBaseTest {
34
35
	public function buildSimplifier() {
36
		$nodeSimplifierFactoryMock = $this->getMockBuilder('PPP\Module\TreeSimplifier\NodeSimplifierFactory')
37
			->disableOriginalConstructor()
38
			->getMock();
39
		$entityStoreMock = $this->getMock('Wikibase\EntityStore\EntityStore');
40
		$resourceListNodeParserMock = $this->getMockBuilder('PPP\Wikidata\ValueParsers\ResourceListNodeParser')
41
			->disableOriginalConstructor()
42
			->getMock();
43
44
		return new MissingSubjectTripleNodeSimplifier($nodeSimplifierFactoryMock, $entityStoreMock, $resourceListNodeParserMock);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \PPP\Wikidata...rceListNodeParserMock); (PPP\Wikidata\TreeSimplif...ectTripleNodeSimplifier) is incompatible with the return type declared by the abstract method PPP\Wikidata\TreeSimplif...seTest::buildSimplifier of type PPP\Wikidata\TreeSimplifier\NodeSimplifier.

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:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
45
	}
46
47
	public function simplifiableProvider() {
48
		return array(
49
			array(
50
				new TripleNode(
51
					new MissingNode(),
52
					new ResourceListNode(array(new WikibaseResourceNode('', new EntityIdValue(new PropertyId('P214'))))),
53
					new ResourceListNode(array(new WikibaseResourceNode('', new StringValue('113230702'))))
54
				)
55
			),
56
			array(
57
				new TripleNode(
58
					new MissingNode(),
59
					new ResourceListNode(array(new StringResourceNode('VIAF'))),
60
					new ResourceListNode(array(new StringResourceNode('113230702')))
61
				)
62
			),
63
			array(
64
				new UnionNode(array(new IntersectionNode(array(new TripleNode(
65
					new MissingNode(),
66
					new ResourceListNode(array(new StringResourceNode('VIAF'))),
67
					new ResourceListNode(array(new StringResourceNode('113230702')))
68
				)))))
69
			),
70
		);
71
	}
72
73
	public function nonSimplifiableProvider() {
74
		return array(
75
			array(
76
				new MissingNode()
77
			),
78
			array(
79
				new TripleNode(
80
					new ResourceListNode(array(new WikibaseResourceNode('', new EntityIdValue(new ItemId('Q42'))))),
81
					new MissingNode(),
82
						new ResourceListNode(array(new WikibaseResourceNode('', new StringValue('113230702'))))
83
				)
84
			),
85
			array(
86
				new TripleNode(
87
					new ResourceListNode(array(new WikibaseResourceNode('', new EntityIdValue(new ItemId('Q42'))))),
88
					new ResourceListNode(array(new WikibaseResourceNode('', new EntityIdValue(new PropertyId('P214'))))),
89
					new MissingNode()
90
				)
91
			),
92
			array(
93
				new UnionNode(array(new IntersectionNode(array(new TripleNode(
94
					new ResourceListNode(array(new WikibaseResourceNode('', new EntityIdValue(new ItemId('Q42'))))),
95
					new ResourceListNode(array(new WikibaseResourceNode('', new EntityIdValue(new PropertyId('P214'))))),
96
					new MissingNode()
97
				)))))
98
			),
99
		);
100
	}
101
102
	/**
103
	 * @dataProvider simplifiedTripleProvider
104
	 */
105
	public function testSimplify(AbstractNode $queryNode, ResourceListNode $responseNodes, Description $description, array $queryResult, ResourceListNode $parsedPredicates, ResourceListNode $parsedObjects, array $entities) {
106
		$itemIdForQueryLookupMock = $this->getMockBuilder('Wikibase\EntityStore\ItemIdForQueryLookup')
107
			->disableOriginalConstructor()
108
			->getMock();
109
		$itemIdForQueryLookupMock->expects($this->any())
110
			->method('getItemIdsForQuery')
111
			->with($this->equalTo($description), $this->equalTo(new QueryOptions(50, 0)))
112
			->will($this->returnValue($queryResult));
113
114
		$entityStoreMock = $this->getMock(
115
			'Wikibase\EntityStore\InMemory\InMemoryEntityStore',
116
			array('getItemIdForQueryLookup'),
117
			array($entities)
118
		);
119
		$entityStoreMock->expects($this->any())
120
			->method('getItemIdForQueryLookup')
121
			->will($this->returnValue($itemIdForQueryLookupMock));
122
123
		$resourceListNodeParserMock = $this->getMockBuilder('PPP\Wikidata\ValueParsers\ResourceListNodeParser')
124
			->disableOriginalConstructor()
125
			->getMock();
126
		$resourceListNodeParserMock->expects($this->any())
127
			->method('parse')
128
			->will($this->onConsecutiveCalls(
129
				$parsedPredicates,
130
				$parsedObjects,
131
				$parsedPredicates,
132
				$parsedObjects,
133
				$parsedPredicates,
134
				$parsedObjects
135
			));
136
137
		$simplifier = new MissingSubjectTripleNodeSimplifier(
138
			new NodeSimplifierFactory(),
139
			$entityStoreMock,
140
			$resourceListNodeParserMock
141
		);
142
143
		$this->assertEquals(
144
			$responseNodes,
145
			$simplifier->simplify($queryNode)
146
		);
147
	}
148
149
	public function simplifiedTripleProvider() {
150
		return array(
151
			array(
152
				new TripleNode(
153
					new MissingNode(),
154
					new ResourceListNode(array(new WikibaseResourceNode('', new EntityIdValue(new PropertyId('P214'))))),
155
					new ResourceListNode(array(new WikibaseResourceNode('', new StringValue('113230702'))))
156
				),
157
				new ResourceListNode(array(
158
					new ResourceListNode(array(new WikibaseResourceNode('', new EntityIdValue(new ItemId('Q42')))))
159
				)),
160
				new SomeProperty(new EntityIdValue(new PropertyId('P214')), new ValueDescription(new StringValue('113230702'))),
161
				array(
162
					new ItemId('Q42')
163
				),
164
				new ResourceListNode(array(new WikibaseResourceNode('', new EntityIdValue(new PropertyId('P214'))))),
165
				new ResourceListNode(array(new WikibaseResourceNode('', new StringValue('113230702')))),
166
				array(
167
					new Property(new PropertyId('P214'), null, 'string')
168
				)
169
			),
170
			array(
171
				new UnionNode(array(new TripleNode(
172
					new MissingNode(),
173
					new ResourceListNode(array(new WikibaseResourceNode('', new EntityIdValue(new PropertyId('P214'))))),
174
					new ResourceListNode(array(
175
						new WikibaseResourceNode('', new StringValue('113230702')),
176
						new WikibaseResourceNode('', new StringValue('113230700'))
177
					))
178
				))),
179
				new ResourceListNode(array(
180
					new ResourceListNode(array(new WikibaseResourceNode('', new EntityIdValue(new ItemId('Q42')))))
181
				)),
182
				new SomeProperty(
183
					new EntityIdValue(new PropertyId('P214')),
184
					new Disjunction(array(
185
						new ValueDescription(new StringValue('113230702')),
186
						new ValueDescription(new StringValue('113230700'))
187
					))
188
				),
189
				array(
190
					new ItemId('Q42')
191
				),
192
				new ResourceListNode(array(new WikibaseResourceNode('', new EntityIdValue(new PropertyId('P214'))))),
193
				new ResourceListNode(array(
194
					new WikibaseResourceNode('', new StringValue('113230702')),
195
					new WikibaseResourceNode('', new StringValue('113230700'))
196
				)),
197
				array(
198
					new Property(new PropertyId('P214'), null, 'string')
199
				)
200
			),
201
			array(
202
				new IntersectionNode(array(new TripleNode(
203
					new MissingNode(),
204
					new ResourceListNode(array(
205
						new WikibaseResourceNode('', new EntityIdValue(new PropertyId('P213'))),
206
						new WikibaseResourceNode('', new EntityIdValue(new PropertyId('P214')))
207
					)),
208
					new ResourceListNode(array(new WikibaseResourceNode('', new StringValue('113230702'))))
209
				))),
210
				new ResourceListNode(array(
211
					new ResourceListNode(array(new WikibaseResourceNode('', new EntityIdValue(new ItemId('Q42')))))
212
				)),
213
				new Disjunction(array(
214
					new SomeProperty(new EntityIdValue(new PropertyId('P213')), new ValueDescription(new StringValue('113230702'))),
215
					new SomeProperty(new EntityIdValue(new PropertyId('P214')), new ValueDescription(new StringValue('113230702')))
216
				)),
217
				array(
218
					new ItemId('Q42')
219
				),
220
				new ResourceListNode(array(
221
					new WikibaseResourceNode('', new EntityIdValue(new PropertyId('P213'))),
222
					new WikibaseResourceNode('', new EntityIdValue(new PropertyId('P214')))
223
				)),
224
				new ResourceListNode(array(new WikibaseResourceNode('', new StringValue('113230702')))),
225
				array(
226
					new Property(new PropertyId('P213'), null, 'string'),
227
					new Property(new PropertyId('P214'), null, 'string')
228
				)
229
			),
230
			array(
231
				new TripleNode(
232
					new MissingNode(),
233
					new ResourceListNode(array(
234
						new WikibaseResourceNode('', new EntityIdValue(new PropertyId('P213'))),
235
						new WikibaseResourceNode('', new EntityIdValue(new PropertyId('P214')))
236
					)),
237
					new ResourceListNode(array(new StringResourceNode('491268')))
238
				),
239
				new ResourceListNode(array(
240
					new ResourceListNode(array(new WikibaseResourceNode('', new EntityIdValue(new ItemId('Q456')))))
241
				)),
242
				new Disjunction(array(
243
					new SomeProperty(new EntityIdValue(new PropertyId('P213')), new ValueDescription(new StringValue('491268'))),
244
					new SomeProperty(new EntityIdValue(new PropertyId('P214')), new ValueDescription(new StringValue('491268')))
245
				)),
246
				array(
247
					new ItemId('Q456')
248
				),
249
				new ResourceListNode(array(
250
					new WikibaseResourceNode('', new EntityIdValue(new PropertyId('P213'))),
251
					new WikibaseResourceNode('', new EntityIdValue(new PropertyId('P214')))
252
				)),
253
				new ResourceListNode(array(new WikibaseResourceNode('491268', new StringValue('491268')))),
254
				array(
255
					new Property(new PropertyId('P213'), null, 'string'),
256
					new Property(new PropertyId('P214'), null, 'string')
257
				)
258
			),
259
			array(
260
				new IntersectionNode(array(
261
					new UnionNode(array(
262
						new TripleNode(
263
							new MissingNode(),
264
							new ResourceListNode(array(new WikibaseResourceNode('', new EntityIdValue(new PropertyId('P214'))))),
265
							new ResourceListNode(array(new StringResourceNode('491268')))
266
						),
267
						new TripleNode(
268
							new MissingNode(),
269
							new ResourceListNode(array(new WikibaseResourceNode('', new EntityIdValue(new PropertyId('P214'))))),
270
							new ResourceListNode(array(new StringResourceNode('491268')))
271
						),
272
					)),
273
					new TripleNode(
274
						new MissingNode(),
275
						new ResourceListNode(array(new WikibaseResourceNode('', new EntityIdValue(new PropertyId('P214'))))),
276
						new ResourceListNode(array(new StringResourceNode('491268')))
277
					),
278
				)),
279
				new ResourceListNode(array(
280
					new ResourceListNode(array(new WikibaseResourceNode('', new EntityIdValue(new ItemId('Q456')))))
281
				)),
282
				new Conjunction(array(
283
					new Disjunction(array(
284
						new SomeProperty(new EntityIdValue(new PropertyId('P214')), new ValueDescription(new StringValue('491268'))),
285
						new SomeProperty(new EntityIdValue(new PropertyId('P214')), new ValueDescription(new StringValue('491268')))
286
					)),
287
					new SomeProperty(new EntityIdValue(new PropertyId('P214')), new ValueDescription(new StringValue('491268')))
288
				)),
289
				array(
290
					new ItemId('Q456')
291
				),
292
				new ResourceListNode(array(new WikibaseResourceNode('', new EntityIdValue(new PropertyId('P214'))))),
293
				new ResourceListNode(array(new WikibaseResourceNode('491268', new StringValue('491268')))),
294
				array(
295
					new Property(new PropertyId('P214'), null, 'string')
296
				)
297
			),
298
			array(
299
				new TripleNode(
300
					new MissingNode(),
301
					new UnionNode(array(
302
						new ResourceListNode(),
303
						new ResourceListNode(array(new StringResourceNode('VIAF')))
304
					)),
305
					new ResourceListNode(array(new StringResourceNode('113230702')))
306
				),
307
				new ResourceListNode(array(
308
					new ResourceListNode(array(new WikibaseResourceNode('', new EntityIdValue(new ItemId('Q42')))))
309
				)),
310
				new SomeProperty(new EntityIdValue(new PropertyId('P214')), new ValueDescription(new StringValue('113230702'))),
311
				array(
312
					new ItemId('Q42')
313
				),
314
				new ResourceListNode(array(new WikibaseResourceNode('', new EntityIdValue(new PropertyId('P214'))))),
315
				new ResourceListNode(array(new WikibaseResourceNode('', new StringValue('113230702')))),
316
				array(
317
					new Property(new PropertyId('P214'), null, 'string')
318
				)
319
			),
320
			array(
321
				new TripleNode(
322
					new MissingNode(),
323
					new ResourceListNode(array(new WikibaseResourceNode('', new EntityIdValue(new PropertyId('P214'))))),
324
					new ResourceListNode()
325
				),
326
				new ResourceListNode(),
327
				new AnyValue(),
328
				array(
329
					new ItemId('Q42')
330
				),
331
				new ResourceListNode(array(new WikibaseResourceNode('', new EntityIdValue(new PropertyId('P214'))))),
332
				new ResourceListNode(),
333
				array(
334
					new Property(new PropertyId('P214'), null, 'string')
335
				)
336
			),
337
			array(
338
				new TripleNode(
339
					new MissingNode(),
340
					new ResourceListNode(),
341
					new ResourceListNode(array(new StringResourceNode('491268')))
342
				),
343
				new ResourceListNode(),
344
				new AnyValue(),
345
				array(
346
					new ItemId('Q42')
347
				),
348
				new ResourceListNode(),
349
				new ResourceListNode(array(new StringResourceNode('491268'))),
350
				array()
351
			),
352
			array(
353
				new UnionNode(array()),
354
				new ResourceListNode(),
355
				new AnyValue(),
356
				array(
357
					new ItemId('Q42')
358
				),
359
				new ResourceListNode(),
360
				new ResourceListNode(),
361
				array()
362
			),
363
			array(
364
				new UnionNode(array(
365
					new TripleNode(
366
						new MissingNode(),
367
						new ResourceListNode(),
368
						new ResourceListNode(array(new StringResourceNode('491268')))
369
					),
370
				)),
371
				new ResourceListNode(),
372
				new AnyValue(),
373
				array(
374
					new ItemId('Q42')
375
				),
376
				new ResourceListNode(),
377
				new ResourceListNode(array(new StringResourceNode('491268'))),
378
				array()
379
			),
380
		);
381
	}
382
}
383