Completed
Push — master ( 59c431...219a45 )
by mw
36:38
created

propertyValueProvider()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 41
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 28
nc 1
nop 0
dl 0
loc 41
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace SMW\Tests\DataValues\ValueFormatters;
4
5
use SMW\DataValues\ValueFormatters\PropertyValueFormatter;
6
use SMWPropertyValue as PropertyValue;
7
use SMW\DataItemFactory;
8
use SMW\Tests\TestEnvironment;
9
10
/**
11
 * @covers \SMW\DataValues\ValueFormatters\PropertyValueFormatter
12
 * @group semantic-mediawiki
13
 *
14
 * @license GNU GPL v2+
15
 * @since 2.5
16
 *
17
 * @author mwjames
18
 */
19
class PropertyValueFormatterTest extends \PHPUnit_Framework_TestCase {
20
21
	private $dataItemFactory;
22
	private $propertyLabelFinder;
23
24
	protected function setUp() {
25
		parent::setUp();
26
27
		$this->testEnvironment = new TestEnvironment();
28
		$this->dataItemFactory = new DataItemFactory();
29
30
		$this->propertyLabelFinder = $this->getMockBuilder( '\SMW\PropertyLabelFinder' )
31
			->disableOriginalConstructor()
32
			->getMock();
33
34
		$this->testEnvironment->registerObject( 'PropertyLabelFinder', $this->propertyLabelFinder );
35
	}
36
37
	public function testCanConstruct() {
38
39
		$this->assertInstanceOf(
40
			'\SMW\DataValues\ValueFormatters\PropertyValueFormatter',
41
			new PropertyValueFormatter()
42
		);
43
	}
44
45
	public function testIsFormatterForValidation() {
46
47
		$propertyValue = $this->getMockBuilder( '\SMWPropertyValue' )
48
			->disableOriginalConstructor()
49
			->getMock();
50
51
		$instance = new PropertyValueFormatter();
52
53
		$this->assertTrue(
54
			$instance->isFormatterFor( $propertyValue )
55
		);
56
	}
57
58
	public function testWithCaptionOutput() {
59
60
		$propertyValue = new PropertyValue();
61
		$propertyValue->setDataItem( $this->dataItemFactory->newDIProperty( 'Foo' ) );
62
		$propertyValue->setCaption( 'ABC[<>]' );
63
64
		$instance = new PropertyValueFormatter( $propertyValue );
65
66
		$this->assertEquals(
67
			'ABC[<>]',
68
			$instance->format( PropertyValueFormatter::WIKI_SHORT )
69
		);
70
71
		$this->assertEquals(
72
			'ABC[&lt;&gt;]',
73
			$instance->format( PropertyValueFormatter::HTML_SHORT )
74
		);
75
	}
76
77
	/**
78
	 * @dataProvider propertyValueProvider
79
	 */
80
	public function testFormat( $property, $type, $linker, $expected ) {
81
82
		$propertyValue = new PropertyValue();
83
		$propertyValue->setDataItem( $property );
84
85
		$propertyValue->setOption( PropertyValue::OPT_CONTENT_LANGUAGE, 'en' );
0 ignored issues
show
Documentation introduced by
'en' is of type string, but the function expects a object<mxied>.

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...
86
		$propertyValue->setOption( PropertyValue::OPT_USER_LANGUAGE, 'en' );
0 ignored issues
show
Documentation introduced by
'en' is of type string, but the function expects a object<mxied>.

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...
87
88
		$instance = new PropertyValueFormatter( $propertyValue );
89
90
		$this->assertEquals(
91
			$expected,
92
			$instance->format( $type, $linker )
93
		);
94
	}
95
96
	/**
97
	 * @dataProvider preferredLabelValueProvider
98
	 */
99
	public function testFormatWithPreferredLabel( $property, $preferredLabel, $type, $linker, $expected ) {
100
101
		// Ensures the mocked instance is injected and registered with the
102
		// PropertyRegistry instance
103
		\SMW\PropertyRegistry::clear();
104
105
		$this->propertyLabelFinder->expects( $this->any() )
106
			->method( 'findPreferredPropertyLabelByLanguageCode' )
107
			->will( $this->returnValue( $preferredLabel ) );
108
109
		$this->propertyLabelFinder->expects( $this->any() )
110
			->method( 'searchPropertyIdByLabel' )
111
			->will( $this->returnValue( false ) );
112
113
		$propertyValue = new PropertyValue();
114
115
		$propertyValue->setOption( 'smwgDVFeatures', SMW_DV_PROV_LHNT );
0 ignored issues
show
Documentation introduced by
SMW_DV_PROV_LHNT is of type integer, but the function expects a object<mxied>.

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...
116
		$propertyValue->setOption( PropertyValue::OPT_CONTENT_LANGUAGE, 'en' );
0 ignored issues
show
Documentation introduced by
'en' is of type string, but the function expects a object<mxied>.

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...
117
		$propertyValue->setOption( PropertyValue::OPT_USER_LANGUAGE, 'en' );
0 ignored issues
show
Documentation introduced by
'en' is of type string, but the function expects a object<mxied>.

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...
118
119
		$propertyValue->setUserValue( $property );
120
121
		$instance = new PropertyValueFormatter( $propertyValue );
122
123
		$this->assertEquals(
124
			$expected,
125
			$instance->format( $type, $linker )
126
		);
127
128
		\SMW\PropertyRegistry::clear();
129
	}
130
131
	public function testTryToFormatOnMissingDataValueThrowsException() {
132
133
		$instance = new PropertyValueFormatter();
134
135
		$this->setExpectedException( 'RuntimeException' );
136
		$instance->format( PropertyValueFormatter::VALUE );
137
	}
138
139
	public function propertyValueProvider() {
140
141
		$dataItemFactory = new DataItemFactory();
142
143
		$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...
144
			$dataItemFactory->newDIProperty( 'Foo' ),
145
			PropertyValueFormatter::VALUE,
146
			null,
147
			'Foo'
148
		);
149
150
		$provider[] = array(
151
			$dataItemFactory->newDIProperty( 'Foo' ),
152
			PropertyValueFormatter::WIKI_SHORT,
153
			null,
154
			'Foo'
155
		);
156
157
		$provider[] = array(
158
			$dataItemFactory->newDIProperty( 'Foo' ),
159
			PropertyValueFormatter::HTML_SHORT,
160
			null,
161
			'Foo'
162
		);
163
164
		$provider[] = array(
165
			$dataItemFactory->newDIProperty( 'Foo' ),
166
			PropertyValueFormatter::WIKI_LONG,
167
			null,
168
			'Property:Foo'
169
		);
170
171
		$provider[] = array(
172
			$dataItemFactory->newDIProperty( 'Foo' ),
173
			PropertyValueFormatter::HTML_LONG,
174
			null,
175
			'Property:Foo'
176
		);
177
178
		return $provider;
179
	}
180
181
	public function preferredLabelValueProvider() {
182
183
		$dataItemFactory = new DataItemFactory();
0 ignored issues
show
Unused Code introduced by
$dataItemFactory is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
184
		$linker = 'some';
185
186
		$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...
187
			'Foo',
188
			'Bar',
189
			PropertyValueFormatter::VALUE,
190
			null,
191
			'Bar'
192
		);
193
194
		$provider[] = array(
195
			'Foo',
196
			'Bar',
197
			PropertyValueFormatter::WIKI_SHORT,
198
			null,
199
			'Bar&nbsp;<span title="Foo"><sup>ᵖ</sup></span>'
200
		);
201
202
		$provider[] = array(
203
			'Foo',
204
			'Bar',
205
			PropertyValueFormatter::HTML_SHORT,
206
			null,
207
			'Bar&nbsp;<span title="Foo"><sup>ᵖ</sup></span>'
208
		);
209
210
		$provider[] = array(
211
			'Foo',
212
			'Bar',
213
			PropertyValueFormatter::WIKI_LONG,
214
			$linker,
215
			'[[:Property:Foo|Bar]]&nbsp;<span title="Foo"><sup>ᵖ</sup></span>'
216
		);
217
218
		$provider[] = array(
219
			'Foo',
220
			'Bar',
221
			PropertyValueFormatter::HTML_LONG,
222
			null,
223
			'Property:Foo&nbsp;<span title="Foo"><sup>ᵖ</sup></span>'
224
		);
225
226
		return $provider;
227
	}
228
229
}
230