Completed
Push — master ( 0cf5bd...a3625c )
by mw
11s
created

testhasChangeToNotifyAbout_ChangeNotificationForSubobjectRelatedValue()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 98
Code Lines 68

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 98
rs 8.3352
c 0
b 0
f 0
cc 1
eloc 68
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace SMW\Notifications\Tests\ChangeNotification;
4
5
use SMW\Notifications\ChangeNotification\ChangeNotificationFilter;
6
use SMW\DIWikiPage;
7
use SMWDIBlob as DIBlob;
8
use SMW\Tests\TestEnvironment;
9
10
/**
11
 * @covers \SMW\Notifications\ChangeNotification\ChangeNotificationFilter
12
 * @group semantic-notifications
13
 *
14
 * @license GNU GPL v2+
15
 * @since 1.0
16
 *
17
 * @author mwjames
18
 */
19
class ChangeNotificationFilterTest extends \PHPUnit_Framework_TestCase {
20
21
	private $store;
22
	private $cachedPropertyValuesPrefetcher;
23
	private $testEnvironment;
24
25
	protected function setUp() {
26
27
		$this->store = $this->getMockBuilder( '\SMW\SQLStore\SQLStore' )
28
			->disableOriginalConstructor()
29
			->getMock();
30
31
		$this->cachedPropertyValuesPrefetcher = $this->getMockBuilder( '\SMW\CachedPropertyValuesPrefetcher' )
32
			->disableOriginalConstructor()
33
			->getMock();
34
35
		$this->testEnvironment = new TestEnvironment();
36
37
		$this->testEnvironment->registerObject( 'Store', $this->store );
38
		$this->testEnvironment->registerObject( 'CachedPropertyValuesPrefetcher', $this->cachedPropertyValuesPrefetcher );
39
	}
40
41
	protected function tearDown() {
42
		$this->testEnvironment->tearDown();
43
	}
44
45
	public function testCanConstruct() {
46
47
		$this->assertInstanceOf(
48
			ChangeNotificationFilter::class,
49
			new ChangeNotificationFilter( DIWikiPage::newFromText( __METHOD__ ), $this->store )
50
		);
51
	}
52
53
	public function testTryToGetEventRecordOnNullType() {
54
55
		$instance = new ChangeNotificationFilter(
56
			DIWikiPage::newFromText( __METHOD__ ),
57
			$this->store
58
		);
59
60
		$this->assertEmpty(
61
			$instance->getEventRecord( true )
62
		);
63
	}
64
65
	public function testGetEventRecordOnPropertyChange() {
66
67
		$subject = DIWikiPage::newFromText( __METHOD__, SMW_NS_PROPERTY );
68
		$dataItem = DIWikiPage::newFromText( 'FOO', SMW_NS_PROPERTY );
69
70
		$orderedDiffByTable = array(
71
			'fpt_foo' => array(
72
				'property' => array(
73
					'key'  => '_FOO',
74
					'p_id' => 29
75
				),
76
				'insert' => array(
77
					array(
78
						's_id' => 201,
79
						'o_serialized' => '1/2016/6/1/11/1/48/0',
80
						'o_sortkey' => '2457540.9595833'
81
					)
82
				),
83
				'delete' => array(
84
					array(
85
						's_id' => 202,
86
						'o_serialized' => '1/2016/6/1/11/1/59/0',
87
						'o_sortkey' => '2457540.9582292'
88
					)
89
				)
90
			)
91
		);
92
93
		$idTable = $this->getMockBuilder( '\stdClass' )
94
			->disableOriginalConstructor()
95
			->setMethods( array( 'getDataItemById' ) )
96
			->getMock();
97
98
		$idTable->expects( $this->any() )
99
			->method( 'getDataItemById' )
100
			->will( $this->returnValue( $dataItem ) );
101
102
		$this->store->expects( $this->any() )
103
			->method( 'getObjectIds' )
104
			->will( $this->returnValue( $idTable ) );
105
106
		$compositePropertyTableDiffIterator = $this->getMockBuilder( '\SMW\SQLStore\CompositePropertyTableDiffIterator' )
107
			->disableOriginalConstructor()
108
			->setMethods( array( 'getOrderedDiffByTable' ) )
109
			->getMock();
110
111
		$compositePropertyTableDiffIterator->expects( $this->any() )
112
			->method( 'getOrderedDiffByTable' )
113
			->will( $this->returnValue( $orderedDiffByTable ) );
114
115
		$instance = new ChangeNotificationFilter(
116
			$subject,
117
			$this->store
118
		);
119
120
		$instance->hasChangeToNotifyAbout(
121
			$compositePropertyTableDiffIterator
122
		);
123
124
		$this->assertEquals(
125
			array(
126
				'agent' => null,
127
				'extra' => array(
128
					'notifyAgent' => false,
129
					'revid' => 0,
130
					'properties' => array(
131
						'FOO#102#' => $dataItem
132
					),
133
					'subSemanticDataMatch' => array(),
134
					'subject' => $subject
135
				),
136
				'title' => $subject->getTitle(),
137
				'type' => 'smw-specification-change'
138
			),
139
			$instance->getEventRecord( true )
140
		);
141
	}
142
143
	public function testCannotNotifyOnChangeWhen_MDAT_REDI() {
144
145
		$subject = DIWikiPage::newFromText( __METHOD__ );
146
147
		$orderedDiffByTable = array(
148
			'fpt_mdat' => array(
149
				'property' => array(
150
					'key'  => '_MDAT',
151
					'p_id' => 29
152
				)
153
			),
154
			'fpt_redi' => array(
155
				'property' => array(
156
					'key'  => '_REDI',
157
					'p_id' => 290
158
				)
159
			)
160
		);
161
162
		$compositePropertyTableDiffIterator = $this->getMockBuilder( '\SMW\SQLStore\CompositePropertyTableDiffIterator' )
163
			->disableOriginalConstructor()
164
			->setMethods( array( 'getOrderedDiffByTable' ) )
165
			->getMock();
166
167
		$compositePropertyTableDiffIterator->expects( $this->any() )
168
			->method( 'getOrderedDiffByTable' )
169
			->will( $this->returnValue( $orderedDiffByTable ) );
170
171
		$instance = new ChangeNotificationFilter(
172
			$subject,
173
			$this->store
174
		);
175
176
		$result = $instance->hasChangeToNotifyAbout(
177
			$compositePropertyTableDiffIterator
178
		);
179
180
		$this->assertFalse(
181
			$result
182
		);
183
	}
184
185
	public function testhasChangeToNotifyAbout_ChangeNotificationForAnyValue() {
186
187
		$subject = DIWikiPage::newFromText( __METHOD__ );
188
189
		$orderedDiffByTable = array(
190
			'fpt_foo' => array(
191
				'property' => array(
192
					'key'  => '_FOO',
193
					'p_id' => 29
194
				),
195
				'insert' => array(
196
					array(
197
						's_id' => 201,
198
						'o_serialized' => '1/2016/6/1/11/1/48/0',
199
						'o_sortkey' => '2457540.9595833'
200
					)
201
				)
202
			)
203
		);
204
205
		$idTable = $this->getMockBuilder( '\stdClass' )
206
			->disableOriginalConstructor()
207
			->setMethods( array( 'getDataItemById' ) )
208
			->getMock();
209
210
		$idTable->expects( $this->any() )
211
			->method( 'getDataItemById' )
212
			->will( $this->returnValue( DIWikiPage::newFromText( 'FOO', SMW_NS_PROPERTY ) ) );
213
214
		$this->store->expects( $this->any() )
215
			->method( 'getObjectIds' )
216
			->will( $this->returnValue( $idTable ) );
217
218
		$this->cachedPropertyValuesPrefetcher->expects( $this->any() )
219
			->method( 'getPropertyValues' )
220
			->will( $this->returnValue( array( new DIBlob( '+' ) ) ) ); //Any value
221
222
		$compositePropertyTableDiffIterator = $this->getMockBuilder( '\SMW\SQLStore\CompositePropertyTableDiffIterator' )
223
			->disableOriginalConstructor()
224
			->setMethods( array( 'getOrderedDiffByTable' ) )
225
			->getMock();
226
227
		$compositePropertyTableDiffIterator->expects( $this->any() )
228
			->method( 'getOrderedDiffByTable' )
229
			->will( $this->returnValue( $orderedDiffByTable ) );
230
231
		$agent = $this->getMockBuilder( '\User' )
232
			->disableOriginalConstructor()
233
			->getMock();
234
235
		$instance = new ChangeNotificationFilter(
236
			$subject,
237
			$this->store
238
		);
239
240
		$instance->setAgent( $agent );
241
242
		$result = $instance->hasChangeToNotifyAbout(
243
			$compositePropertyTableDiffIterator
244
		);
245
246
		$this->assertTrue(
247
			$result
248
		);
249
	}
250
251
	public function testhasChangeToNotifyAbout_ChangeNotificationForDistinctValue() {
252
253
		$subject = DIWikiPage::newFromText( __METHOD__ );
254
255
		$orderedDiffByTable = array(
256
			'fpt_date' => array(
257
				'property' => array(
258
					'key'  => '_TEXT',
259
					'p_id' => 29
260
				),
261
				'insert' => array(
262
					array(
263
						's_id' => 201,
264
						'o_blob' => 'DistinctText',
265
						'o_hash' => ''
266
					)
267
				),
268
				'delete' => array(
269
					array(
270
						's_id' => 201,
271
						'o_blob' => 'Text',
272
						'o_hash' => ''
273
					)
274
				)
275
			)
276
		);
277
278
		$idTable = $this->getMockBuilder( '\stdClass' )
279
			->disableOriginalConstructor()
280
			->setMethods( array( 'getDataItemById' ) )
281
			->getMock();
282
283
		$idTable->expects( $this->any() )
284
			->method( 'getDataItemById' )
285
			->will( $this->returnValue( DIWikiPage::newFromText( '_TEXT', SMW_NS_PROPERTY ) ) );
286
287
		$this->store->expects( $this->any() )
288
			->method( 'getObjectIds' )
289
			->will( $this->returnValue( $idTable ) );
290
291
		$this->cachedPropertyValuesPrefetcher->expects( $this->any() )
292
			->method( 'getPropertyValues' )
293
			->will( $this->returnValue( array( new DIBlob( 'DistinctText' ) ) ) ); //Distinct value
294
295
		$compositePropertyTableDiffIterator = $this->getMockBuilder( '\SMW\SQLStore\CompositePropertyTableDiffIterator' )
296
			->disableOriginalConstructor()
297
			->setMethods( array( 'getOrderedDiffByTable' ) )
298
			->getMock();
299
300
		$compositePropertyTableDiffIterator->expects( $this->any() )
301
			->method( 'getOrderedDiffByTable' )
302
			->will( $this->returnValue( $orderedDiffByTable ) );
303
304
		$agent = $this->getMockBuilder( '\User' )
305
			->disableOriginalConstructor()
306
			->getMock();
307
308
		$instance = new ChangeNotificationFilter(
309
			$subject,
310
			$this->store
311
		);
312
313
		$instance->setAgent( $agent );
314
315
		$result = $instance->hasChangeToNotifyAbout(
316
			$compositePropertyTableDiffIterator
317
		);
318
319
		$this->assertTrue(
320
			$result
321
		);
322
	}
323
324
	public function testhasChangeToNotifyAbout_ChangeNotificationForPageValue() {
325
326
		$subject = DIWikiPage::newFromText( __METHOD__ );
327
328
		$orderedDiffByTable = array(
329
			'tab_id_page' => array(
330
				'insert' => array(
331
					array(
332
						'p_id' => 29,
333
						's_id' => 201,
334
						'o_id' => 1001
335
					)
336
				),
337
				'delete' => array(
338
					array(
339
						'p_id' => 29,
340
						's_id' => 201,
341
						'o_id' => 42
342
					)
343
				)
344
			)
345
		);
346
347
		$idTable = $this->getMockBuilder( '\stdClass' )
348
			->disableOriginalConstructor()
349
			->setMethods( array( 'getDataItemById' ) )
350
			->getMock();
351
352
		$idTable->expects( $this->any() )
353
			->method( 'getDataItemById' )
354
			->will( $this->returnValue( DIWikiPage::newFromText( 'BAR', SMW_NS_PROPERTY ) ) );
355
356
		$this->store->expects( $this->any() )
357
			->method( 'getObjectIds' )
358
			->will( $this->returnValue( $idTable ) );
359
360
		$this->cachedPropertyValuesPrefetcher->expects( $this->any() )
361
			->method( 'getPropertyValues' )
362
			->will( $this->returnValue( array( new DIBlob( 'BAR' ) ) ) ); //Distinct value
363
364
		$compositePropertyTableDiffIterator = $this->getMockBuilder( '\SMW\SQLStore\CompositePropertyTableDiffIterator' )
365
			->disableOriginalConstructor()
366
			->setMethods( array( 'getOrderedDiffByTable' ) )
367
			->getMock();
368
369
		$compositePropertyTableDiffIterator->expects( $this->any() )
370
			->method( 'getOrderedDiffByTable' )
371
			->will( $this->returnValue( $orderedDiffByTable ) );
372
373
		$agent = $this->getMockBuilder( '\User' )
374
			->disableOriginalConstructor()
375
			->getMock();
376
377
		$instance = new ChangeNotificationFilter(
378
			$subject,
379
			$this->store
380
		);
381
382
		$instance->setAgent( $agent );
383
384
		$result = $instance->hasChangeToNotifyAbout(
385
			$compositePropertyTableDiffIterator
386
		);
387
388
		$this->assertTrue(
389
			$result
390
		);
391
	}
392
393
	public function testhasChangeToNotifyAbout_ChangeNotificationForSubobjectRelatedValue() {
394
395
		$subject = DIWikiPage::newFromText( __METHOD__ );
396
		$dataItem = DIWikiPage::newFromText( 'BAR', SMW_NS_PROPERTY );
397
398
		$orderedDiffByTable = array(
399
			'tab_id_page' => array(
400
				'insert' => array(
401
					array(
402
						'p_id' => 29,
403
						's_id' => 201,
404
						'o_id' => 1001
405
					)
406
				),
407
				'delete' => array(
408
					array(
409
						'p_id' => 29,
410
						's_id' => 201,
411
						'o_id' => 42
412
					)
413
				)
414
			)
415
		);
416
417
		$subSemanticData = $this->getMockBuilder( '\SMW\SemanticData' )
418
			->disableOriginalConstructor()
419
			->getMock();
420
421
		$subSemanticData->expects( $this->any() )
422
			->method( 'hasProperty' )
423
			->will( $this->returnValue( true ) );
424
425
		$subSemanticData->expects( $this->any() )
426
			->method( 'getSubject' )
427
			->will( $this->returnValue( new DIWikiPage( 'BAR', SMW_NS_PROPERTY, '', 'ooooooo' ) ) );
428
429
		$subSemanticData->expects( $this->any() )
430
			->method( 'getPropertyValues' )
431
			->will( $this->returnValue( array( new DIBlob( 'BAR' ) ) ) ); //Distinct value
432
433
		$semanticData = $this->getMockBuilder( '\SMW\SemanticData' )
434
			->disableOriginalConstructor()
435
			->getMock();
436
437
		$semanticData->expects( $this->any() )
438
			->method( 'getSubSemanticData' )
439
			->will( $this->returnValue( array( $subSemanticData ) ) );
440
441
		$idTable = $this->getMockBuilder( '\stdClass' )
442
			->disableOriginalConstructor()
443
			->setMethods( array( 'getDataItemById' ) )
444
			->getMock();
445
446
		$idTable->expects( $this->any() )
447
			->method( 'getDataItemById' )
448
			->will( $this->returnValue( $dataItem ) );
449
450
		$this->store->expects( $this->any() )
451
			->method( 'getObjectIds' )
452
			->will( $this->returnValue( $idTable ) );
453
454
		$this->store->expects( $this->any() )
455
			->method( 'getSemanticData' )
456
			->with( $this->equalTo( $dataItem ) )
457
			->will( $this->returnValue( $semanticData ) );
458
459
		$this->cachedPropertyValuesPrefetcher->expects( $this->any() )
460
			->method( 'getPropertyValues' )
461
			->will( $this->returnValue( array() ) );
462
463
		$compositePropertyTableDiffIterator = $this->getMockBuilder( '\SMW\SQLStore\CompositePropertyTableDiffIterator' )
464
			->disableOriginalConstructor()
465
			->setMethods( array( 'getOrderedDiffByTable' ) )
466
			->getMock();
467
468
		$compositePropertyTableDiffIterator->expects( $this->any() )
469
			->method( 'getOrderedDiffByTable' )
470
			->will( $this->returnValue( $orderedDiffByTable ) );
471
472
		$agent = $this->getMockBuilder( '\User' )
473
			->disableOriginalConstructor()
474
			->getMock();
475
476
		$instance = new ChangeNotificationFilter(
477
			$subject,
478
			$this->store
479
		);
480
481
		$instance->setAgent( $agent );
482
483
		$result = $instance->hasChangeToNotifyAbout(
484
			$compositePropertyTableDiffIterator
485
		);
486
487
		$this->assertTrue(
488
			$result
489
		);
490
	}
491
492
}
493