Completed
Push — master ( 2472c5...a814e8 )
by mw
35:03
created

testGetPreferredPropertyLabel()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 18
nc 1
nop 0
dl 0
loc 26
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace SMW\Tests;
4
5
use SMW\DataItemFactory;
6
use SMW\PropertySpecificationLookup;
7
use SMWContainerSemanticData as ContainerSemanticData;
8
use SMWDataItem as DataItem;
9
10
/**
11
 * @covers \SMW\PropertySpecificationLookup
12
 * @group semantic-mediawiki
13
 *
14
 * @license GNU GPL v2+
15
 * @since 2.4
16
 *
17
 * @author mwjames
18
 */
19
class PropertySpecificationLookupTest extends \PHPUnit_Framework_TestCase {
20
21
	private $blobStore;
22
	private $dataItemFactory;
23
	private $testEnvironment;
24
	private $cachedPropertyValuesPrefetcher;
25
	private $intermediaryMemoryCache;
26
27
	protected function setUp() {
28
		parent::setUp();
29
30
		$this->cachedPropertyValuesPrefetcher = $this->getMockBuilder( '\SMW\CachedPropertyValuesPrefetcher' )
31
			->disableOriginalConstructor()
32
			->getMock();
33
34
		$this->intermediaryMemoryCache = $this->getMockBuilder( '\Onoi\Cache\Cache' )
35
			->disableOriginalConstructor()
36
			->getMock();
37
38
		$this->blobStore = $this->getMockBuilder( '\Onoi\BlobStore\BlobStore' )
39
			->disableOriginalConstructor()
40
			->getMock();
41
42
		$this->dataItemFactory = new DataItemFactory();
43
		$this->testEnvironment = new TestEnvironment();
44
	}
45
46
	public function testCanConstruct() {
47
48
		$this->assertInstanceOf(
49
			'\SMW\PropertySpecificationLookup',
50
			new PropertySpecificationLookup( $this->cachedPropertyValuesPrefetcher, $this->intermediaryMemoryCache )
51
		);
52
	}
53
54
	public function testGetFieldList() {
55
56
		$property = $this->dataItemFactory->newDIProperty( 'RecordProperty' );
57
58
		$this->cachedPropertyValuesPrefetcher->expects( $this->once() )
59
			->method( 'getPropertyValues' )
60
			->with(
61
				$this->equalTo( $property->getDiWikiPage() ),
62
				$this->equalTo( $this->dataItemFactory->newDIProperty( '_LIST' ) ),
63
				$this->anything() )
64
			->will(
65
				$this->returnValue( array(
66
					$this->dataItemFactory->newDIBlob( 'Foo' ),
67
					$this->dataItemFactory->newDIBlob( 'abc;123' ) ) ) );
68
69
		$instance = new PropertySpecificationLookup(
70
			$this->cachedPropertyValuesPrefetcher,
71
			$this->intermediaryMemoryCache
72
		);
73
74
		$this->assertEquals(
75
			'abc;123',
76
			$instance->getFieldListBy( $property )
77
		);
78
	}
79
80
	public function testGetPreferredPropertyLabel() {
81
82
		$property = $this->dataItemFactory->newDIProperty( 'SomeProperty' );
83
		$property->setPropertyTypeId( '_mlt_rec' );
84
85
		$this->cachedPropertyValuesPrefetcher->expects( $this->once() )
86
			->method( 'getPropertyValues' )
87
			->with(
88
				$this->equalTo( $property->getDiWikiPage() ),
89
				$this->equalTo( $this->dataItemFactory->newDIProperty( '_PPLB' ) ),
90
				$this->anything() );
91
92
		$this->intermediaryMemoryCache->expects( $this->once() )
93
			->method( 'fetch' )
94
			->will( $this->returnValue( false ) );
95
96
		$instance = new PropertySpecificationLookup(
97
			$this->cachedPropertyValuesPrefetcher,
98
			$this->intermediaryMemoryCache
99
		);
100
101
		$this->assertEquals(
102
			'',
103
			$instance->getPreferredPropertyLabelBy( $property )
104
		);
105
	}
106
107
	public function testGetPropertyFromDisplayTitle() {
108
109
		$property = $this->dataItemFactory->newDIProperty( 'Foo' );
110
111
		$this->cachedPropertyValuesPrefetcher->expects( $this->once() )
112
			->method( 'queryPropertyValuesFor' )
113
			->will( $this->returnValue( array( $this->dataItemFactory->newDIWikiPage( 'Foo' ) ) ) );
114
115
		$instance = new PropertySpecificationLookup(
116
			$this->cachedPropertyValuesPrefetcher,
117
			$this->intermediaryMemoryCache
118
		);
119
120
		$this->assertEquals(
121
			$property,
122
			$instance->getPropertyFromDisplayTitle( 'abc' )
123
		);
124
	}
125
126
	public function testHasUniquenessConstraint() {
127
128
		$property = $this->dataItemFactory->newDIProperty( 'Foo' );
129
130
		$this->cachedPropertyValuesPrefetcher->expects( $this->once() )
131
			->method( 'getPropertyValues' )
132
			->with(
133
				$this->equalTo( $property->getDiWikiPage() ),
134
				$this->equalTo( $this->dataItemFactory->newDIProperty( '_PVUC' ) ),
135
				$this->anything() )
136
			->will( $this->returnValue( array( $this->dataItemFactory->newDIBoolean( true ) ) ) );
137
138
		$instance = new PropertySpecificationLookup(
139
			$this->cachedPropertyValuesPrefetcher,
140
			$this->intermediaryMemoryCache
141
		);
142
143
		$this->assertTrue(
144
			$instance->hasUniquenessConstraintBy( $property )
145
		);
146
	}
147
148
	public function testGetExternalFormatterUri() {
149
150
		$property = $this->dataItemFactory->newDIProperty( 'Foo' );
151
152
		$this->cachedPropertyValuesPrefetcher->expects( $this->once() )
153
			->method( 'getPropertyValues' )
154
			->with(
155
				$this->equalTo( $property->getDiWikiPage() ),
156
				$this->equalTo( $this->dataItemFactory->newDIProperty( '_PEFU' ) ),
157
				$this->anything() )
158
			->will( $this->returnValue( array( $this->dataItemFactory->newDIUri( 'http', 'example.org/$1' ) ) ) );
159
160
		$instance = new PropertySpecificationLookup(
161
			$this->cachedPropertyValuesPrefetcher,
162
			$this->intermediaryMemoryCache
163
		);
164
165
		$this->assertInstanceOf(
166
			DataItem::class,
167
			$instance->getExternalFormatterUriBy( $property )
168
		);
169
	}
170
171
	public function testGetAllowedPattern() {
172
173
		$property = $this->dataItemFactory->newDIProperty( 'Has allowed pattern' );
174
175
		$this->cachedPropertyValuesPrefetcher->expects( $this->once() )
176
			->method( 'getPropertyValues' )
177
			->with(
178
				$this->equalTo( $property->getDiWikiPage() ),
179
				$this->equalTo( $this->dataItemFactory->newDIProperty( '_PVAP' ) ),
180
				$this->anything() )
181
			->will(
182
				$this->returnValue( array( $this->dataItemFactory->newDIBlob( 'IPv4' ) ) ) );
183
184
		$instance = new PropertySpecificationLookup(
185
			$this->cachedPropertyValuesPrefetcher,
186
			$this->intermediaryMemoryCache
187
		);
188
189
		$this->assertEquals(
190
			'IPv4',
191
			$instance->getAllowedPatternBy( $property )
192
		);
193
	}
194
195
	public function testGetAllowedValues() {
196
197
		$expected =  array(
198
			$this->dataItemFactory->newDIBlob( 'A' ),
199
			$this->dataItemFactory->newDIBlob( 'B' )
200
		);
201
202
		$property = $this->dataItemFactory->newDIProperty( 'Has allowed values' );
203
204
		$this->cachedPropertyValuesPrefetcher->expects( $this->once() )
205
			->method( 'getPropertyValues' )
206
			->with(
207
				$this->equalTo( $property->getDiWikiPage() ),
208
				$this->equalTo( $this->dataItemFactory->newDIProperty( '_PVAL' ) ),
209
				$this->anything() )
210
			->will( $this->returnValue( $expected ) );
211
212
		$instance = new PropertySpecificationLookup(
213
			$this->cachedPropertyValuesPrefetcher,
214
			$this->intermediaryMemoryCache
215
		);
216
217
		$this->assertEquals(
218
			$expected,
219
			$instance->getAllowedValuesBy( $property )
220
		);
221
	}
222
223
	public function testGetDisplayPrecision() {
224
225
		$property = $this->dataItemFactory->newDIProperty( 'Foo' );
226
227
		$this->cachedPropertyValuesPrefetcher->expects( $this->once() )
228
			->method( 'getPropertyValues' )
229
			->with(
230
				$this->equalTo( $property->getDiWikiPage() ),
231
				$this->equalTo( $this->dataItemFactory->newDIProperty( '_PREC' ) ),
232
				$this->anything() )
233
			->will( $this->returnValue( array( $this->dataItemFactory->newDINumber( -2.3 ) ) ) );
234
235
		$instance = new PropertySpecificationLookup(
236
			$this->cachedPropertyValuesPrefetcher,
237
			$this->intermediaryMemoryCache
238
		);
239
240
		$this->assertEquals(
241
			2,
242
			$instance->getDisplayPrecisionBy( $property )
243
		);
244
	}
245
246
	public function testgetDisplayUnits() {
247
248
		$property = $this->dataItemFactory->newDIProperty( 'Foo' );
249
250
		$this->cachedPropertyValuesPrefetcher->expects( $this->once() )
251
			->method( 'getPropertyValues' )
252
			->with(
253
				$this->equalTo( $property->getDiWikiPage() ),
254
				$this->equalTo( $this->dataItemFactory->newDIProperty( '_UNIT' ) ),
255
				$this->anything() )
256
			->will( $this->returnValue( array(
257
				$this->dataItemFactory->newDIBlob( 'abc,def' ),
258
				$this->dataItemFactory->newDIBlob( '123' ) ) ) );
259
260
		$instance = new PropertySpecificationLookup(
261
			$this->cachedPropertyValuesPrefetcher,
262
			$this->intermediaryMemoryCache
263
		);
264
265
		$this->assertEquals(
266
			array( 'abc', 'def', '123' ),
267
			$instance->getDisplayUnitsBy( $property )
268
		);
269
	}
270
271
	public function testGetPropertyDescriptionForPredefinedProperty() {
272
273
		$property = $this->dataItemFactory->newDIProperty( 'Foo' );
274
275
		$container = $this->getMockBuilder( '\Onoi\BlobStore\Container' )
276
			->disableOriginalConstructor()
277
			->getMock();
278
279
		$this->blobStore->expects( $this->once() )
280
			->method( 'read' )
281
			->will( $this->returnValue( $container ) );
282
283
		$this->cachedPropertyValuesPrefetcher->expects( $this->once() )
284
			->method( 'getBlobStore' )
285
			->will( $this->returnValue( $this->blobStore ) );
286
287
		$instance = new PropertySpecificationLookup(
288
			$this->cachedPropertyValuesPrefetcher,
289
			$this->intermediaryMemoryCache
290
		);
291
292
		$this->assertInternalType(
293
			'string',
294
			$instance->getPropertyDescriptionBy( $property )
295
		);
296
	}
297
298
	public function testGetPropertyDescriptionForPredefinedPropertyViaCacheForLanguageCode() {
299
300
		$property = $this->dataItemFactory->newDIProperty( 'Foo' );
301
302
		$container = $this->getMockBuilder( '\Onoi\BlobStore\Container' )
303
			->disableOriginalConstructor()
304
			->getMock();
305
306
		$container->expects( $this->once() )
307
			->method( 'has' )
308
			->will( $this->returnValue( true ) );
309
310
		$container->expects( $this->once() )
311
			->method( 'get' )
312
			->with( $this->stringContains( 'pdesc:en:0' ) )
313
			->will( $this->returnValue( 1001 ) );
314
315
		$this->blobStore->expects( $this->once() )
316
			->method( 'read' )
317
			->will( $this->returnValue( $container ) );
318
319
		$this->cachedPropertyValuesPrefetcher->expects( $this->once() )
320
			->method( 'getBlobStore' )
321
			->will( $this->returnValue( $this->blobStore ) );
322
323
		$instance = new PropertySpecificationLookup(
324
			$this->cachedPropertyValuesPrefetcher,
325
			$this->intermediaryMemoryCache
326
		);
327
328
		$instance->setLanguageCode( 'en' );
329
330
		$this->assertEquals(
331
			1001,
332
			$instance->getPropertyDescriptionBy( $property )
333
		);
334
	}
335
336
	public function testTryToGetLocalPropertyDescriptionForUserdefinedProperty() {
337
338
		$property = $this->dataItemFactory->newDIProperty( 'Foo' );
339
340
		$pdesc = $this->dataItemFactory->newDIProperty( '_PDESC' );
341
		$pdesc->setPropertyTypeId( '_mlt_rec' );
342
343
		$container = $this->getMockBuilder( '\Onoi\BlobStore\Container' )
344
			->disableOriginalConstructor()
345
			->getMock();
346
347
		$this->blobStore->expects( $this->once() )
348
			->method( 'read' )
349
			->will( $this->returnValue( $container ) );
350
351
		$this->cachedPropertyValuesPrefetcher->expects( $this->once() )
352
			->method( 'getPropertyValues' )
353
			->with(
354
				$this->equalTo( $property->getDiWikiPage() ),
355
				$this->anything(),
356
				$this->anything() )
357
			->will( $this->returnValue( array(
358
				$this->dataItemFactory->newDIContainer( ContainerSemanticData::makeAnonymousContainer() ) ) ) );
359
360
		$this->cachedPropertyValuesPrefetcher->expects( $this->once() )
361
			->method( 'getBlobStore' )
362
			->will( $this->returnValue( $this->blobStore ) );
363
364
		$instance = new PropertySpecificationLookup(
365
			$this->cachedPropertyValuesPrefetcher,
366
			$this->intermediaryMemoryCache
367
		);
368
369
		$this->assertInternalType(
370
			'string',
371
			$instance->getPropertyDescriptionBy( $property )
372
		);
373
	}
374
375
}
376