Completed
Push — master ( 9eae06...a41cb5 )
by mw
16s
created

RecordValueTest::testParseValue()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 36
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 23
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 36
rs 8.8571
1
<?php
2
3
namespace SMW\Tests\DataValues;
4
5
use SMWRecordValue as RecordValue;
6
use SMW\DataItemFactory;
7
use SMW\Tests\TestEnvironment;
8
9
/**
10
 * @covers \SMWRecordValue
11
 * @group semantic-mediawiki
12
 *
13
 * @license GNU GPL v2+
14
 * @since 2.1
15
 *
16
 * @author mwjames
17
 */
18
class RecordValueTest extends \PHPUnit_Framework_TestCase {
19
20
	private $testEnvironment;
21
	private $dataItemFactory;
22
23
	protected function setUp() {
24
		parent::setUp();
25
26
		$this->testEnvironment = new TestEnvironment();
27
		$this->dataItemFactory = new DataItemFactory();
28
29
		$this->propertySpecificationLookup = $this->getMockBuilder( '\SMW\PropertySpecificationLookup' )
30
			->disableOriginalConstructor()
31
			->getMock();
32
33
		$this->testEnvironment->registerObject( 'PropertySpecificationLookup', $this->propertySpecificationLookup );
34
	}
35
36
	protected function tearDown() {
37
		$this->testEnvironment->tearDown();
38
		parent::tearDown();
39
	}
40
41
	public function testCanConstruct() {
42
43
		$this->assertInstanceOf(
44
			'\SMWRecordValue',
45
			new RecordValue()
46
		);
47
	}
48
49
	public function testGetPropertyDataItems() {
50
51
		$expected = array(
52
			$this->dataItemFactory->newDIProperty( 'Bar' ),
53
			$this->dataItemFactory->newDIProperty( 'Foobar' )
54
		);
55
56
		$store = $this->getMockBuilder( '\SMW\Store' )
57
			->disableOriginalConstructor()
58
			->setMethods( array( 'getRedirectTarget' ) )
59
			->getMockForAbstractClass();
60
61
		$store->expects( $this->atLeastOnce() )
62
			->method( 'getPropertyValues' )
63
			->will( $this->returnValue( array( $this->dataItemFactory->newDIBlob( 'Bar;Foobar' ) ) ) );
64
65
		$store->expects( $this->any() )
66
			->method( 'getRedirectTarget' )
67
			->will( $this->returnArgument( 0 ) );
68
69
		$this->testEnvironment->registerObject( 'Store', $store );
70
71
		$instance = new RecordValue();
72
		$instance->setProperty(
73
			$this->dataItemFactory->newDIProperty( 'Foo' )
74
		);
75
76
		$this->assertEquals(
77
			$expected,
78
			$instance->getPropertyDataItems()
79
		);
80
81
		$this->assertEquals(
82
			$this->dataItemFactory->newDIProperty( 'Foobar' ),
83
			$instance->getPropertyDataItemByIndex( 'Foobar' )
84
		);
85
	}
86
87
	public function testParseValue() {
88
89
		$store = $this->getMockBuilder( '\SMW\Store' )
90
			->disableOriginalConstructor()
91
			->setMethods( array( 'getRedirectTarget' ) )
92
			->getMockForAbstractClass();
93
94
		$store->expects( $this->atLeastOnce() )
95
			->method( 'getPropertyValues' )
96
			->will( $this->returnValue( array( $this->dataItemFactory->newDIBlob( 'Bar;Foobar' ) ) ) );
97
98
		$store->expects( $this->any() )
99
			->method( 'getRedirectTarget' )
100
			->will( $this->returnArgument( 0 ) );
101
102
		$this->testEnvironment->registerObject( 'Store', $store );
103
104
		$instance = new RecordValue();
105
		$instance->setProperty(
106
			$this->dataItemFactory->newDIProperty( 'Foo' )
107
		);
108
109
		$instance->setUserValue( '123;abc' );
110
		$container = $instance->getDataItem();
111
112
		$this->assertInstanceOf(
113
			'\SMWDIContainer',
114
			$container
115
		);
116
117
		$semanticData = $container->getSemanticData();
118
119
		$this->assertTrue(
120
			$semanticData->hasProperty( $this->dataItemFactory->newDIProperty( 'Foobar' ) )
121
		);
122
	}
123
124
	public function testParseValueOnMissingValues() {
125
126
		$instance = new RecordValue();
127
		$instance->setProperty(
128
			$this->dataItemFactory->newDIProperty( 'Foo' )
129
		);
130
131
		$instance->setUserValue( '' );
132
133
		$this->assertInstanceOf(
134
			'\SMWDIError',
135
			$instance->getDataItem()
136
		);
137
	}
138
139
	public function testParseValueWithErroredDv() {
140
141
		$store = $this->getMockBuilder( '\SMW\Store' )
142
			->disableOriginalConstructor()
143
			->setMethods( array( 'getRedirectTarget' ) )
144
			->getMockForAbstractClass();
145
146
		$store->expects( $this->atLeastOnce() )
147
			->method( 'getPropertyValues' )
148
			->will( $this->returnValue( array( $this->dataItemFactory->newDIBlob( 'Bar;Foobar' ) ) ) );
149
150
		$store->expects( $this->any() )
151
			->method( 'getRedirectTarget' )
152
			->will( $this->returnArgument( 0 ) );
153
154
		$this->testEnvironment->registerObject( 'Store', $store );
155
156
		$instance = new RecordValue();
157
		$instance->setProperty(
158
			$this->dataItemFactory->newDIProperty( 'Foo' )
159
		);
160
161
		$instance->setUserValue( 'Foo;<>Foo' );
162
163
		$this->assertInstanceOf(
164
			'\SMWDIError',
165
			$instance->getDataItem()
166
		);
167
168
		$this->assertEquals(
169
			array( '[2,"smw_notitle","<>Foo"]' ),
170
			$instance->getErrors()
171
		);
172
	}
173
174
	public function testGetValuesFromStringWithEncodedSemicolon() {
175
176
		$instance = new RecordValue();
177
178
		$this->assertEquals(
179
			array( 'abc', '1;2', 3 ),
180
			$instance->getValuesFromString( 'abc;1\;2;3' )
181
		);
182
	}
183
184
	/**
185
	 * @dataProvider valueProvider
186
	 */
187
	public function testGetQueryDescription( $properties, $value, $expected ) {
188
189
		$instance = new RecordValue( '_rec' );
190
		$instance->setFieldProperties( $properties );
191
192
		$description = $instance->getQueryDescription( htmlspecialchars( $value ) );
193
194
		$this->assertEquals(
195
			$expected['description'],
196
			$description->getQueryString()
197
		);
198
	}
199
200
	/**
201
	 * @dataProvider valueProvider
202
	 */
203
	public function testGetWikiValue( $properties, $value, $expected ) {
204
205
		$instance = new RecordValue( '_rec' );
206
		$instance->setFieldProperties( $properties );
207
208
		$instance->setUserValue( $value );
209
210
		$this->assertEquals(
211
			$expected['wikivalue'],
212
			$instance->getWikiValue()
213
		);
214
	}
215
216
	public function valueProvider() {
217
218
		$dataItemFactory = new DataItemFactory();
219
220
		$properties = array(
221
			$dataItemFactory->newDIProperty( 'Foo' ),
222
			$dataItemFactory->newDIProperty( 'Bar' ),
223
			'InvalidFieldPropertyNotSet'
224
		);
225
226
		$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...
227
			$properties,
228
			"Title without special characters;2001",
229
			array(
230
				'description' => "[[Foo::Title without special characters]] [[Bar::2001]]",
231
				'wikivalue'   => "Title without special characters; 2001"
232
			)
233
234
		);
235
236
		$provider[] = array(
237
			$properties,
238
			"Title with $&%'* special characters;(..&^%..)",
239
			array(
240
				'description' => "[[Foo::Title with $&%'* special characters]] [[Bar::(..&^%..)]]",
241
				'wikivalue'   => "Title with $&%'* special characters; (..&^%..)"
242
			)
243
		);
244
245
		$provider[] = array(
246
			$properties,
247
			" Title with space before ; After the divider ",
248
			array(
249
				'description' => "[[Foo::Title with space before]] [[Bar::After the divider]]",
250
				'wikivalue'   => "Title with space before; After the divider"
251
			)
252
		);
253
254
		$provider[] = array(
255
			$properties,
256
			" Title with backslash\; escape ; After the divider ",
257
			array(
258
				'description' => "[[Foo::Title with backslash; escape]] [[Bar::After the divider]]",
259
				'wikivalue'   => "Title with backslash\; escape; After the divider"
260
			)
261
		);
262
263
		return $provider;
264
	}
265
266
}
267