Completed
Push — master ( 5d1976...30add5 )
by mw
13s
created

tests/phpunit/Unit/DataValueFactoryTest.php (20 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace SMW\Tests;
4
5
use SMW\DataValueFactory;
6
use SMW\DIProperty;
7
use SMW\DIWikiPage;
8
use SMWDataItem;
9
use SMWPropertyValue;
10
11
/**
12
 * @covers \SMW\DataValueFactory
13
 * @group semantic-mediawiki
14
 *
15
 * @license GNU GPL v2+
16
 * @since 1.9
17
 *
18
 * @author mwjames
19
 */
20
class DataValueFactoryTest extends \PHPUnit_Framework_TestCase {
21
22
	public function testCanConstruct() {
23
24
		$this->assertInstanceOf(
25
			'\SMW\DataValueFactory',
26
			DataValueFactory::getInstance()
27
		);
28
	}
29
30
	/**
31
	 * @dataProvider dataItemIdDataProvider
32
	 */
33
	public function testGetDataItemId( $typeId, $expectedId ) {
34
35
		$this->assertEquals(
36
			$expectedId,
37
			DataValueFactory::getDataItemId( $typeId )
0 ignored issues
show
Deprecated Code introduced by
The method SMW\DataValueFactory::getDataItemId() has been deprecated with message: since 1.9, use DataTypeRegistry::getDataItemId

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
38
		);
39
	}
40
41
	/**
42
	 * @dataProvider typeIdValueDataProvider
43
	 */
44
	public function testNewTypeIdValue( $typeId, $value, $expectedValue, $expectedInstance ) {
45
46
		$dataValue = DataValueFactory::getInstance()->newTypeIdValue( $typeId, $value );
47
48
		$this->assertInstanceOf(
49
			$expectedInstance,
50
			$dataValue
51
		);
52
53
		if ( $dataValue->getErrors() === array() ){
54
			return $this->assertEquals(
55
				$expectedValue,
56
				$dataValue->getWikiValue()
57
			);
58
		}
59
60
		$this->assertInternalType(
61
			'array',
62
			$dataValue->getErrors()
63
		);
64
	}
65
66
	/**
67
	 * @dataProvider propertyObjectValueDataProvider
68
	 */
69
	public function testNewPropertyObjectValue( $propertyName, $value, $expectedValue, $expectedInstance ) {
70
71
		$propertyDV = SMWPropertyValue::makeUserProperty( $propertyName );
72
		$propertyDI = $propertyDV->getDataItem();
73
74
		$dataValue = DataValueFactory::getInstance()->newPropertyObjectValue( $propertyDI, $value );
0 ignored issues
show
$propertyDI of type object<SMWDataItem> is not a sub-type of object<SMW\DIProperty>. It seems like you assume a child class of the class SMWDataItem to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
75
76
		// Check the returned instance
77
		$this->assertInstanceOf( $expectedInstance, $dataValue );
78
79
		if ( $dataValue->getErrors() === array() ){
80
			$this->assertInstanceOf( 'SMWDIProperty', $dataValue->getProperty() );
81
			$this->assertContains( $propertyName, $dataValue->getProperty()->getLabel() );
82
			if ( $dataValue->getDataItem()->getDIType() === SMWDataItem::TYPE_WIKIPAGE ){
83
				$this->assertEquals( $expectedValue, $dataValue->getWikiValue() );
84
			}
85
		} else {
86
			$this->assertInternalType( 'array', $dataValue->getErrors() );
87
		}
88
89
		// Check interface parameters
90
		$dataValue = DataValueFactory::getInstance()->newPropertyObjectValue(
91
			$propertyDI,
0 ignored issues
show
$propertyDI of type object<SMWDataItem> is not a sub-type of object<SMW\DIProperty>. It seems like you assume a child class of the class SMWDataItem to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
92
			$value,
93
			'FooCaption',
0 ignored issues
show
'FooCaption' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
94
			new DIWikiPage( 'Foo', NS_MAIN )
95
		);
96
97
		$this->assertInstanceOf(
98
			$expectedInstance,
99
			$dataValue
100
		);
101
	}
102
103
	/**
104
	 * @dataProvider propertyValueDataProvider
105
	 */
106
	public function testAddPropertyValueByText( $propertyName, $value, $expectedValue, $expectedInstance ) {
107
108
		$dataValue = DataValueFactory::getInstance()->newPropertyObjectValueByText( $propertyName, $value );
109
110
		// Check the returned instance
111
		$this->assertInstanceOf( $expectedInstance, $dataValue );
112
113
		if ( $dataValue->getErrors() === array() ){
114
			$this->assertInstanceOf( 'SMWDIProperty', $dataValue->getProperty() );
115
			$this->assertContains( $propertyName, $dataValue->getProperty()->getLabel() );
116
			if ( $dataValue->getDataItem()->getDIType() === SMWDataItem::TYPE_WIKIPAGE ){
117
				$this->assertEquals( $expectedValue, $dataValue->getWikiValue() );
118
			}
119
		} else {
120
			$this->assertInternalType( 'array', $dataValue->getErrors() );
121
		}
122
123
		// Check interface parameters
124
		$dataValue = DataValueFactory::getInstance()->newPropertyObjectValueByText(
125
			$propertyName,
126
			$value,
127
			'FooCaption',
128
			new DIWikiPage( 'Foo', NS_MAIN )
129
		);
130
131
		$this->assertInstanceOf(
132
			$expectedInstance,
133
			$dataValue
134
		);
135
	}
136
137
	public function testTryToCreateDataValueUsingRestrictedPropertyValue() {
138
139
		$dataValue = DataValueFactory::getInstance()->newPropertyObjectValueByText( 'Has subobject', 'Foo' );
140
141
		$this->assertInstanceOf(
142
			'\SMWErrorValue',
143
			$dataValue
144
		);
145
146
		$this->assertNotEmpty(
147
			$dataValue->getErrors()
148
		);
149
	}
150
151
	public function testToCreateDataValueUsingLegacyNewPropertyValueMethod() {
152
153
		$dataValue = DataValueFactory::getInstance()->newPropertyValue( 'Bar', 'Foo' );
0 ignored issues
show
Deprecated Code introduced by
The method SMW\DataValueFactory::newPropertyValue() has been deprecated with message: since 2.4, use DataTypeRegistry::newPropertyObjectValueByText

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
154
155
		$this->assertInstanceOf(
156
			'\SMWDataValue',
157
			$dataValue
158
		);
159
	}
160
161
	/**
162
	 * @dataProvider findTypeIdDataProvider
163
	 */
164
	public function testFindTypeID( $typeId, $expectedId ) {
165
166
		$this->assertEquals(
167
			$expectedId,
168
			DataValueFactory::findTypeID( $typeId )
0 ignored issues
show
Deprecated Code introduced by
The method SMW\DataValueFactory::findTypeID() has been deprecated with message: since 1.9, use DataTypeRegistry::findTypeId

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
169
		);
170
	}
171
172
	/**
173
	 * @dataProvider findTypeIdDataProvider
174
	 */
175
	public function testFindTypeLabel( $textId, $id ) {
176
177
		$textId = $textId === 'String' ? 'Text' : $textId;
178
179
		$this->assertEquals(
180
			$textId,
181
			DataValueFactory::findTypeLabel( $id ),
0 ignored issues
show
Deprecated Code introduced by
The method SMW\DataValueFactory::findTypeLabel() has been deprecated with message: since 1.9, use DataTypeRegistry::findTypeLabel

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
182
			'Asserts that findTypeLabel() returns a user label'
183
		);
184
185
	}
186
187
	public function testGetKnownTypeLabels() {
188
189
		$this->assertInternalType(
190
			'array',
191
			DataValueFactory::getKnownTypeLabels()
0 ignored issues
show
Deprecated Code introduced by
The method SMW\DataValueFactory::getKnownTypeLabels() has been deprecated with message: since 1.9, use DataTypeRegistry::getKnownTypeLabels

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
192
		);
193
	}
194
195
196
	public function testRegisterDatatype() {
197
198
		DataValueFactory::registerDatatype( '_foo', '\SMW\FooValue', SMWDataItem::TYPE_NOTYPE, 'FooValue' );
0 ignored issues
show
'FooValue' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Deprecated Code introduced by
The method SMW\DataValueFactory::registerDatatype() has been deprecated with message: since 1.9, use DataTypeRegistry::registerDataType

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
199
200
		$this->assertEquals(
201
			'FooValue',
202
			DataValueFactory::findTypeLabel( '_foo' )
0 ignored issues
show
Deprecated Code introduced by
The method SMW\DataValueFactory::findTypeLabel() has been deprecated with message: since 1.9, use DataTypeRegistry::findTypeLabel

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
203
		);
204
205
		DataValueFactory::getInstance()->registerDatatype( '_foo', '\SMW\FooValue', SMWDataItem::TYPE_NOTYPE, 'FooValue' );
0 ignored issues
show
'FooValue' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Deprecated Code introduced by
The method SMW\DataValueFactory::registerDatatype() has been deprecated with message: since 1.9, use DataTypeRegistry::registerDataType

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
206
207
		$this->assertEquals(
208
			'FooValue',
209
			DataValueFactory::getInstance()->findTypeLabel( '_foo' )
0 ignored issues
show
Deprecated Code introduced by
The method SMW\DataValueFactory::findTypeLabel() has been deprecated with message: since 1.9, use DataTypeRegistry::findTypeLabel

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
210
		);
211
	}
212
213
	public function testRegisterDatatypeAlias() {
214
215
		DataValueFactory::registerDatatypeAlias( '_foo', 'Bar' );
0 ignored issues
show
Deprecated Code introduced by
The method SMW\DataValueFactory::registerDatatypeAlias() has been deprecated with message: since 1.9, use DataTypeRegistry::registerDataTypeAlias

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
216
217
		$this->assertEquals(
218
			'_foo',
219
			DataValueFactory::findTypeID( 'Bar' )
0 ignored issues
show
Deprecated Code introduced by
The method SMW\DataValueFactory::findTypeID() has been deprecated with message: since 1.9, use DataTypeRegistry::findTypeId

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
220
		);
221
222
		DataValueFactory::getInstance()->registerDatatypeAlias( '_foo', 'Bar' );
0 ignored issues
show
Deprecated Code introduced by
The method SMW\DataValueFactory::registerDatatypeAlias() has been deprecated with message: since 1.9, use DataTypeRegistry::registerDataTypeAlias

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
223
224
		$this->assertEquals(
225
			'_foo',
226
			DataValueFactory::getInstance()->findTypeID( 'Bar' )
0 ignored issues
show
Deprecated Code introduced by
The method SMW\DataValueFactory::findTypeID() has been deprecated with message: since 1.9, use DataTypeRegistry::findTypeId

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
227
		);
228
	}
229
230
	/**
231
	 * Issue 673
232
	 */
233
	public function testEnforceFirstUpperCaseForDisabledCapitalLinks() {
0 ignored issues
show
testEnforceFirstUpperCaseForDisabledCapitalLinks uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

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

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
234
235
		$wgCapitalLinks = $GLOBALS['wgCapitalLinks'];
236
		$GLOBALS['wgCapitalLinks'] = false;
237
238
		$instance = DataValueFactory::getInstance();
239
240
		$dataValue = $instance->newPropertyObjectValueByText(
241
			'has type',
242
			'number',
243
			null,
244
			new DIWikiPage( 'Foo', SMW_NS_PROPERTY )
245
		);
246
247
		$this->assertEquals(
248
			'_TYPE',
249
			$dataValue->getProperty()->getKey()
250
		);
251
252
		$GLOBALS['wgCapitalLinks'] = $wgCapitalLinks;
253
	}
254
255
	public function testNewPropertyValueByLabel() {
256
257
		$dataValue = DataValueFactory::getInstance()->newPropertyValueByLabel( 'Foo' );
258
259
		$this->assertInstanceOf(
260
			'\SMWPropertyValue',
261
			$dataValue
262
		);
263
	}
264
265
	/**
266
	 * @dataProvider newDataItemValueDataProvider
267
	 */
268
	public function testNewDataItemValue( $setup ) {
269
270
		$dataValue = DataValueFactory::getInstance()->newDataItemValue(
271
			$setup['dataItem'],
272
			$setup['property'],
273
			$setup['caption']
274
		);
275
276
		$this->assertInstanceOf(
277
			'SMWDataValue',
278
			$dataValue
279
		);
280
	}
281
282
	public function newDataItemValueDataProvider() {
283
284
		$provider = array();
285
286
		$dataItem = new DIWikiPage( 'Foo', NS_MAIN );
287
		$property = new DIProperty( 'Bar' );
288
289
		// #0
290
		$provider[] = array(
291
			array(
292
				'dataItem' => $dataItem,
293
				'property' => null,
294
				'caption'  => false
295
			)
296
		);
297
298
		// #0
299
		$provider[] = array(
300
			array(
301
				'dataItem' => $dataItem,
302
				'property' => $property,
303
				'caption'  => false
304
			)
305
		);
306
307
		// #1
308
		$provider[] = array(
309
			array(
310
				'dataItem' => $dataItem,
311
				'property' => null,
312
				'caption'  => 'Foo'
313
			)
314
		);
315
316
		// #2
317
		$provider[] = array(
318
			array(
319
				'dataItem' => $dataItem,
320
				'property' => $property,
321
				'caption'  => 'Bar'
322
			)
323
		);
324
325
		return $provider;
326
	}
327
328
	public function findTypeIdDataProvider() {
329
		return array(
330
			array( 'URL'      , '_uri' ), // #0
331
			array( 'Page'     , '_wpg' ), // #1
332
			array( 'String'   , '_txt' ), // #2
333
			array( 'Text'     , '_txt' ), // #3
334
			array( 'Number'   , '_num' ), // #4
335
			array( 'Quantity' , '_qty' ), // #5
336
			array( 'Date'     , '_dat' ), // #6
337
			array( 'Email'    , '_ema' ), // #7
338
			array( ''         , ''     ), // #8
339
		);
340
	}
341
342
	public function dataItemIdDataProvider() {
343
		return array(
344
			array( '_txt' , SMWDataItem::TYPE_BLOB ), // #0
345
			array( '_wpg' , SMWDataItem::TYPE_WIKIPAGE ), // #1
346
			array( '_num' , SMWDataItem::TYPE_NUMBER ), // #2
347
			array( '_dat' , SMWDataItem::TYPE_TIME ), // #3
348
			array( '_uri' , SMWDataItem::TYPE_URI ), // #4
349
			array( '_foo' , SMWDataItem::TYPE_NOTYPE ), // #5
350
		);
351
	}
352
353
	public function typeIdValueDataProvider() {
354
		return array(
355
			array( '_txt'  , 'Bar'          , 'Bar'          , 'SMWStringValue' ), // #0
356
			array( '_txt'  , 'Bar[[ Foo ]]' , 'Bar[[ Foo ]]' , 'SMWStringValue' ), // #1
357
			array( '_txt'  , '9001'         , '9001'         , 'SMWStringValue' ), // #2
358
			array( '_txt'  , 1001           , '1001'         , 'SMWStringValue' ), // #3
359
			array( '_txt'  , '-%&$*'        , '-%&$*'        , 'SMWStringValue' ), // #4
360
			array( '_txt'  , '_Bar'         , '_Bar'         , 'SMWStringValue' ), // #5
361
			array( '_txt'  , 'bar'          , 'bar'          , 'SMWStringValue' ), // #6
362
			array( '-_txt' , 'Bar'          , 'Bar'          , 'SMWErrorValue' ), // #7
363
364
			array( '_wpg'  , 'Bar'          , 'Bar'          , 'SMWWikiPageValue' ), // #8
365
			array( '_wpg'  , 'Bar'          , 'Bar'          , 'SMWWikiPageValue' ), // #9
366
			array( '_wpg'  , 'Bar[[ Foo ]]' , 'Bar[[ Foo ]]' , 'SMWWikiPageValue' ), // #10
367
			array( '_wpg'  , '9001'         , '9001'         , 'SMWWikiPageValue' ), // #11
368
			array( '_wpg'  , 1001           , '1001'         , 'SMWWikiPageValue' ), // #12
369
			array( '_wpg'  , '-%&$*'        , '-%&$*'        , 'SMWWikiPageValue' ), // #13
370
			array( '_wpg'  , '_Bar'         , 'Bar'          , 'SMWWikiPageValue' ), // #14
371
			array( '_wpg'  , 'bar'          , 'Bar'          , 'SMWWikiPageValue' ), // #15
372
			array( '-_wpg' , 'Bar'          , 'Bar'          , 'SMWErrorValue' ), // #16
373
374
			array( '_dat' , '1 Jan 1970'    , '1 Jan 1970'   , 'SMWTimeValue' ), // #0
375
			array( '_uri' , 'Foo'           , 'Foo'          , 'SMWURIValue' ), // #0
376
			array( '_num' , 9001            , '9,001'        , 'SMWNumberValue' ), // #0
377
		);
378
	}
379
380
	public function propertyValueDataProvider() {
381
		return array(
382
			array( 'Foo'  , 'Bar'          , 'Bar'          , 'SMWDataValue' ), // #0
383
			array( 'Foo'  , 'Bar[[ Foo ]]' , 'Bar[[ Foo ]]' , 'SMWDataValue' ), // #1
384
			array( 'Foo'  , '9001'         , '9001'         , 'SMWDataValue' ), // #2
385
			array( 'Foo'  , 1001           , '1001'         , 'SMWDataValue' ), // #3
386
			array( 'Foo'  , '-%&$*'        , '-%&$*'        , 'SMWDataValue' ), // #4
387
			array( 'Foo'  , '_Bar'         , 'Bar'          , 'SMWDataValue' ), // #5
388
			array( 'Foo'  , 'bar'          , 'Bar'          , 'SMWDataValue' ), // #6
389
			array( '-Foo' , 'Bar'          , ''             , 'SMWErrorValue' ), // #7
390
			array( '_Foo' , 'Bar'          , ''             , 'SMWPropertyValue' ), // #8
391
		);
392
	}
393
394
	/**
395
	 * @return array
396
	 */
397
	public function propertyObjectValueDataProvider() {
398
		return array(
399
			array( 'Foo'  , 'Bar'          , 'Bar'          , 'SMWDataValue' ), // #0
400
			array( 'Foo'  , 'Bar[[ Foo ]]' , 'Bar[[ Foo ]]' , 'SMWDataValue' ), // #1
401
			array( 'Foo'  , '9001'         , '9001'         , 'SMWDataValue' ), // #2
402
			array( 'Foo'  , 1001           , '1001'         , 'SMWDataValue' ), // #3
403
			array( 'Foo'  , '-%&$*'        , '-%&$*'        , 'SMWDataValue' ), // #4
404
			array( 'Foo'  , '_Bar'         , 'Bar'          , 'SMWDataValue' ), // #5
405
			array( 'Foo'  , 'bar'          , 'Bar'          , 'SMWDataValue' ), // #6
406
			array( '-Foo' , 'Bar'          , 'Bar'          , 'SMWWikiPageValue' ), // #7
407
408
			// Will fail with "must be an instance of SMWDIProperty, instance of SMWDIError give"
409
			// as propertyDI isn't checked therefore addPropertyValue() should be
410
			// used as it will return a proper object
411
			// array( '_Foo' , 'Bar'          , ''             , 'SMWDIProperty' ), // #8
0 ignored issues
show
Unused Code Comprehensibility introduced by
53% 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...
412
		);
413
	}
414
415
}
416