Completed
Push — master ( 17dff3...f2d821 )
by mw
42:32 queued 07:38
created

SomePropertyTest::testGetFingerprint()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 3
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace SMW\Tests\Query\Language;
4
5
use SMW\DIProperty;
6
use SMW\DIWikiPage;
7
use SMW\Query\Language\NamespaceDescription;
8
use SMW\Query\Language\SomeProperty;
9
use SMW\Query\Language\ThingDescription;
10
use SMW\Query\Language\ValueDescription;
11
use SMW\Query\Language\Conjunction;
12
13
/**
14
 * @covers \SMW\Query\Language\SomeProperty
15
 * @group semantic-mediawiki
16
 *
17
 * @license GNU GPL v2+
18
 * @since 2.1
19
 *
20
 * @author mwjames
21
 */
22
class SomePropertyTest extends \PHPUnit_Framework_TestCase {
23
24
	public function testCanConstruct() {
25
26
		$property = $this->getMockBuilder( '\SMW\DIProperty' )
27
			->disableOriginalConstructor()
28
			->getMock();
29
30
		$description = $this->getMockBuilder( '\SMW\Query\Language\Description' )
31
			->disableOriginalConstructor()
32
			->getMockForAbstractClass();
33
34
		$this->assertInstanceOf(
35
			'SMW\Query\Language\SomeProperty',
36
			new SomeProperty( $property, $description )
37
		);
38
39
		// Legacy
40
		$this->assertInstanceOf(
41
			'SMW\Query\Language\SomeProperty',
42
			new \SMWSomeProperty( $property, $description )
43
		);
44
	}
45
46
	/**
47
	 * @dataProvider somePropertyProvider
48
	 */
49
	public function testCommonMethods( $property, $description, $expected ) {
50
51
		$instance = new SomeProperty( $property, $description );
52
53
		$this->assertEquals(
54
			$expected['property'],
55
			$instance->getProperty()
56
		);
57
58
		$this->assertEquals(
59
			$expected['description'],
60
			$instance->getDescription()
61
		);
62
63
		$this->assertEquals(
64
			$expected['queryString'],
65
			$instance->getQueryString()
66
		);
67
68
		$this->assertEquals(
69
			$expected['queryStringAsValue'],
70
			$instance->getQueryString( true )
71
		);
72
73
		$this->assertEquals(
74
			$expected['isSingleton'],
75
			$instance->isSingleton()
76
		);
77
78
		$this->assertEquals(
79
			array(),
80
			$instance->getPrintRequests()
81
		);
82
83
		$this->assertEquals(
84
			$expected['size'],
85
			$instance->getSize()
86
		);
87
88
		$this->assertEquals(
89
			$expected['depth'],
90
			$instance->getDepth()
91
		);
92
93
		$this->assertEquals(
94
			$expected['queryFeatures'],
95
			$instance->getQueryFeatures()
96
		);
97
	}
98
99
	/**
100
	 * @dataProvider comparativeHashProvider
101
	 */
102
	public function testGetFingerprint( $description, $compareTo, $expected ) {
103
104
		$this->assertEquals(
105
			$expected,
106
			$description->getFingerprint() === $compareTo->getFingerprint()
107
		);
108
	}
109
110
	public function somePropertyProvider() {
111
112
		#0
113
		$property = new DIProperty( 'Foo' );
114
115
		$description = new ValueDescription(
116
			new DIWikiPage( 'Bar', NS_MAIN ),
117
			$property
118
		);
119
120
		$provider[] = array(
0 ignored issues
show
Coding Style Comprehensibility introduced by
$provider was never initialized. Although not strictly required by PHP, it is generally a good practice to add $provider = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
121
			$property,
122
			$description,
123
			array(
124
				'property'    => $property,
125
				'description' => $description,
126
				'queryString' => "[[Foo::Bar]]",
127
				'queryStringAsValue' => "<q>[[Foo::Bar]]</q>",
128
				'isSingleton' => false,
129
				'queryFeatures' => 1,
130
				'size'  => 2,
131
				'depth' => 1
132
			)
133
		);
134
135
		#1
136
		$property = new DIProperty( 'Foo' );
137
138
		$description = new SomeProperty(
139
			new DIProperty( 'Yui' ),
140
			new ValueDescription( new DIWikiPage( 'Bar', NS_MAIN ), null )
141
		);
142
143
		$provider[] = array(
144
			$property,
145
			$description,
146
			array(
147
				'property'    => $property,
148
				'description' => $description,
149
				'queryString' => "[[Foo.Yui::Bar]]",
150
				'queryStringAsValue' => "<q>[[Foo.Yui::Bar]]</q>",
151
				'isSingleton' => false,
152
				'queryFeatures' => 1,
153
				'size'  => 3,
154
				'depth' => 2
155
			)
156
		);
157
158
		#2
159
		$property = new DIProperty( 'Foo' );
160
161
		$description = new SomeProperty(
162
			new DIProperty( 'Yui' ),
163
			new NamespaceDescription( NS_MAIN )
164
		);
165
166
		$provider[] = array(
167
			$property,
168
			$description,
169
			array(
170
				'property'    => $property,
171
				'description' => $description,
172
				'queryString' => "[[Foo.Yui:: <q>[[:+]]</q> ]]",
173
				'queryStringAsValue' => "<q>[[Foo.Yui:: <q>[[:+]]</q> ]]</q>",
174
				'isSingleton' => false,
175
				'queryFeatures' => 9,
176
				'size'  => 3,
177
				'depth' => 2
178
			)
179
		);
180
181
		#3, 1096
182
		$property = new DIProperty( 'Foo' );
183
184
		$description = new SomeProperty(
185
			new DIProperty( 'Yui' ),
186
			new SomeProperty(
187
				new DIProperty( 'Bar', true ),
188
				new NamespaceDescription( NS_MAIN )
189
			)
190
		);
191
192
		$provider[] = array(
193
			$property,
194
			$description,
195
			array(
196
				'property'    => $property,
197
				'description' => $description,
198
				'queryString' => "[[Foo.Yui.-Bar:: <q>[[:+]]</q> ]]",
199
				'queryStringAsValue' => "<q>[[Foo.Yui.-Bar:: <q>[[:+]]</q> ]]</q>",
200
				'isSingleton' => false,
201
				'queryFeatures' => 9,
202
				'size'  => 4,
203
				'depth' => 3
204
			)
205
		);
206
207
		#4, 1096
208
		$property = new DIProperty( 'Foo' );
209
210
		$description = new SomeProperty(
211
			new DIProperty( 'Yui' ),
212
			new SomeProperty(
213
				new DIProperty( '_SOBJ', true ),
214
				new NamespaceDescription( NS_MAIN )
215
			)
216
		);
217
218
		$provider[] = array(
219
			$property,
220
			$description,
221
			array(
222
				'property'    => $property,
223
				'description' => $description,
224
				'queryString' => "[[Foo.Yui.-Has subobject:: <q>[[:+]]</q> ]]",
225
				'queryStringAsValue' => "<q>[[Foo.Yui.-Has subobject:: <q>[[:+]]</q> ]]</q>",
226
				'isSingleton' => false,
227
				'queryFeatures' => 9,
228
				'size'  => 4,
229
				'depth' => 3
230
			)
231
		);
232
233
		return $provider;
234
	}
235
236
	public function testPrune() {
237
238
		$property = new DIProperty( 'Foo' );
239
240
		$description = new ValueDescription(
241
			new DIWikiPage( 'Bar', NS_MAIN ),
242
			$property
243
		);
244
245
		$instance = new SomeProperty( $property, $description );
246
247
		$maxsize  = 2;
248
		$maxDepth = 2;
249
		$log      = array();
250
251
		$this->assertEquals(
252
			$instance,
253
			$instance->prune( $maxsize, $maxDepth, $log )
254
		);
255
256
		$this->assertEquals( 0, $maxsize );
257
		$this->assertEquals( 1, $maxDepth );
258
259
		$maxsize  = 0;
260
		$maxDepth = 1;
261
		$log      = array();
262
263
		$this->assertEquals(
264
			new ThingDescription(),
265
			$instance->prune( $maxsize, $maxDepth, $log )
266
		);
267
	}
268
269
	public function comparativeHashProvider() {
270
271
		// Same property, different description === different hash
272
		$provider[] = array(
0 ignored issues
show
Coding Style Comprehensibility introduced by
$provider was never initialized. Although not strictly required by PHP, it is generally a good practice to add $provider = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
273
			new SomeProperty(
274
				new DIProperty( 'Foo' ),
275
				new NamespaceDescription( NS_HELP )
276
			),
277
			new SomeProperty(
278
				new DIProperty( 'Foo' ),
279
				new NamespaceDescription( NS_MAIN )
280
			),
281
			false
282
		);
283
284
		// Inverse property, same description === different hash
285
		$provider[] = array(
286
			new SomeProperty(
287
				new DIProperty( 'Foo', true ),
288
				new NamespaceDescription( NS_MAIN )
289
			),
290
			new SomeProperty(
291
				new DIProperty( 'Foo' ),
292
				new NamespaceDescription( NS_MAIN )
293
			),
294
			false
295
		);
296
297
		// Same property, different description === different hash
298
		$provider[] = array(
299
			new SomeProperty(
300
				new DIProperty( 'Foo', true ),
301
				new NamespaceDescription( NS_MAIN )
302
			),
303
			new SomeProperty(
304
				new DIProperty( 'Foo' ),
305
				new ThingDescription()
306
			),
307
			false
308
		);
309
310
		// Property.chain, different description === different hash
311
		$provider[] = array(
312
			new SomeProperty(
313
				new DIProperty( 'Foo', true ),
314
				new ThingDescription()
315
			),
316
			new SomeProperty(
317
				new DIProperty( 'Foo' ),
318
				new SomeProperty(
319
					new DIProperty( 'Foo' ),
320
					new ThingDescription()
321
				)
322
			),
323
			false
324
		);
325
326
		// Property.chain, same description === same hash
327
		$provider[] = array(
328
			new SomeProperty(
329
				new DIProperty( 'Foo' ),
330
				new SomeProperty(
331
					new DIProperty( 'Foo' ),
332
					new ThingDescription()
333
				)
334
			),
335
			new SomeProperty(
336
				new DIProperty( 'Foo' ),
337
				new SomeProperty(
338
					new DIProperty( 'Foo' ),
339
					new ThingDescription()
340
				)
341
			),
342
			true
343
		);
344
345
		// Property.chain, different description (inverse prop) === different hash
346
		$provider[] = array(
347
			new SomeProperty(
348
				new DIProperty( 'Foo' ),
349
				new SomeProperty(
350
					new DIProperty( 'Foo' ),
351
					new ThingDescription()
352
				)
353
			),
354
			new SomeProperty(
355
				new DIProperty( 'Foo' ),
356
				new SomeProperty(
357
					new DIProperty( 'Foo', true ),
358
					new ThingDescription()
359
				)
360
			),
361
			false
362
		);
363
364
		// Property.chain, different description === different hash
365
		$provider[] = array(
366
			new SomeProperty(
367
				new DIProperty( 'Foo' ),
368
				new ThingDescription()
369
			),
370
			new SomeProperty(
371
				new DIProperty( 'Foo' ),
372
				new SomeProperty(
373
					new DIProperty( 'Foo' ),
374
					new SomeProperty(
375
						new DIProperty( 'Foo' ),
376
						new ThingDescription()
377
					)
378
				)
379
			),
380
			false
381
		);
382
383
		// Property.chain, different description === different hash
384
		// "[[Foo.Foo::Foo]]" !== "[[Foo.Foo.Foo::Foo]]"
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
385
		$provider[] = array(
386
			new SomeProperty(
387
				new DIProperty( 'Foo' ),
388
				new SomeProperty(
389
					new DIProperty( 'Foo' ),
390
					new SomeProperty(
391
						new DIProperty( 'Foo' ),
392
						new ValueDescription( new DIWikiPage( 'Foo', NS_MAIN ) )
393
					)
394
				)
395
			),
396
			new SomeProperty(
397
				new DIProperty( 'Foo' ),
398
				new SomeProperty(
399
					new DIProperty( 'Foo' ),
400
					new SomeProperty(
401
						new DIProperty( 'Foo' ),
402
						new SomeProperty(
403
							new DIProperty( 'Foo' ),
404
							new ValueDescription( new DIWikiPage( 'Foo', NS_MAIN ) )
405
						)
406
					)
407
				)
408
			),
409
			false
410
		);
411
412
		return $provider;
413
	}
414
415
}
416