1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SMW\Tests\Query\Language; |
4
|
|
|
|
5
|
|
|
use SMW\DIProperty; |
6
|
|
|
use SMW\DIWikiPage; |
7
|
|
|
use SMW\Query\Language\ValueDescription; |
8
|
|
|
use SMWDINumber as DINumber; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @covers \SMW\Query\Language\ValueDescription |
12
|
|
|
* @group semantic-mediawiki |
13
|
|
|
* |
14
|
|
|
* @license GNU GPL v2+ |
15
|
|
|
* @since 2.1 |
16
|
|
|
* |
17
|
|
|
* @author mwjames |
18
|
|
|
*/ |
19
|
|
|
class ValueDescriptionTest extends \PHPUnit_Framework_TestCase { |
20
|
|
|
|
21
|
|
|
public function testCanConstruct() { |
22
|
|
|
|
23
|
|
|
$dataItem = $this->getMockBuilder( '\SMW\DIWikiPage' ) |
24
|
|
|
->disableOriginalConstructor() |
25
|
|
|
->getMock(); |
26
|
|
|
|
27
|
|
|
$this->assertInstanceOf( |
28
|
|
|
'SMW\Query\Language\ValueDescription', |
29
|
|
|
new ValueDescription( $dataItem ) |
30
|
|
|
); |
31
|
|
|
|
32
|
|
|
// Legacy |
33
|
|
|
$this->assertInstanceOf( |
34
|
|
|
'SMW\Query\Language\ValueDescription', |
35
|
|
|
new \SMWValueDescription( $dataItem ) |
36
|
|
|
); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @dataProvider valueDescriptionProvider |
41
|
|
|
*/ |
42
|
|
|
public function testCommonMethods( $dataItem, $property, $comparator, $expected ) { |
43
|
|
|
|
44
|
|
|
$instance = new ValueDescription( $dataItem, $property, $comparator ); |
45
|
|
|
|
46
|
|
|
$this->assertEquals( |
47
|
|
|
$expected['comparator'], |
48
|
|
|
$instance->getComparator() |
49
|
|
|
); |
50
|
|
|
|
51
|
|
|
$this->assertEquals( |
52
|
|
|
$expected['dataItem'], |
53
|
|
|
$instance->getDataItem() |
54
|
|
|
); |
55
|
|
|
|
56
|
|
|
$this->assertEquals( |
57
|
|
|
$expected['property'], |
58
|
|
|
$instance->getProperty() |
59
|
|
|
); |
60
|
|
|
|
61
|
|
|
$this->assertEquals( |
62
|
|
|
$expected['queryString'], |
63
|
|
|
$instance->getQueryString() |
64
|
|
|
); |
65
|
|
|
|
66
|
|
|
$this->assertEquals( |
67
|
|
|
$expected['queryStringAsValue'], |
68
|
|
|
$instance->getQueryString( true ) |
69
|
|
|
); |
70
|
|
|
|
71
|
|
|
$this->assertEquals( |
72
|
|
|
$expected['isSingleton'], |
73
|
|
|
$instance->isSingleton() |
74
|
|
|
); |
75
|
|
|
|
76
|
|
|
$this->assertEquals( |
77
|
|
|
array(), |
78
|
|
|
$instance->getPrintRequests() |
79
|
|
|
); |
80
|
|
|
|
81
|
|
|
$this->assertEquals( |
82
|
|
|
1, |
83
|
|
|
$instance->getSize() |
84
|
|
|
); |
85
|
|
|
|
86
|
|
|
$this->assertEquals( |
87
|
|
|
0, |
88
|
|
|
$instance->getDepth() |
89
|
|
|
); |
90
|
|
|
|
91
|
|
|
$this->assertEquals( |
92
|
|
|
0, |
93
|
|
|
$instance->getQueryFeatures() |
94
|
|
|
); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @dataProvider comparativeHashProvider |
99
|
|
|
*/ |
100
|
|
|
public function testGetFingerprint( $description, $compareTo, $expected ) { |
101
|
|
|
|
102
|
|
|
$this->assertEquals( |
103
|
|
|
$expected, |
104
|
|
|
$description->getFingerprint() === $compareTo->getFingerprint() |
105
|
|
|
); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function valueDescriptionProvider() { |
109
|
|
|
|
110
|
|
|
$dataItem = new DIWikiPage( 'Foo', NS_MAIN ); |
111
|
|
|
|
112
|
|
|
$provider[] = array( |
|
|
|
|
113
|
|
|
$dataItem, |
114
|
|
|
null, |
115
|
|
|
SMW_CMP_EQ, |
116
|
|
|
array( |
117
|
|
|
'comparator' => SMW_CMP_EQ, |
118
|
|
|
'dataItem' => $dataItem, |
119
|
|
|
'property' => null, |
120
|
|
|
'queryString' => '[[:Foo]]', |
121
|
|
|
'queryStringAsValue' => 'Foo', |
122
|
|
|
'isSingleton' => true |
123
|
|
|
) |
124
|
|
|
); |
125
|
|
|
|
126
|
|
|
$provider['page.1'] = array( |
127
|
|
|
$dataItem, |
128
|
|
|
null, |
129
|
|
|
SMW_CMP_LEQ, |
130
|
|
|
array( |
131
|
|
|
'comparator' => SMW_CMP_LEQ, |
132
|
|
|
'dataItem' => $dataItem, |
133
|
|
|
'property' => null, |
134
|
|
|
'queryString' => '[[≤Foo]]', |
135
|
|
|
'queryStringAsValue' => '≤Foo', |
136
|
|
|
'isSingleton' => false |
137
|
|
|
) |
138
|
|
|
); |
139
|
|
|
|
140
|
|
|
$property = DIProperty::newFromUserLabel( 'Foo' )->setPropertyTypeId( '_num' ); |
141
|
|
|
$dataItem = new DINumber( 9001 ); |
142
|
|
|
|
143
|
|
|
$provider['num.1'] = array( |
144
|
|
|
$dataItem, |
145
|
|
|
$property, |
146
|
|
|
SMW_CMP_LEQ, |
147
|
|
|
array( |
148
|
|
|
'comparator' => SMW_CMP_LEQ, |
149
|
|
|
'dataItem' => $dataItem, |
150
|
|
|
'property' => $property, |
151
|
|
|
'queryString' => '[[≤9001]]', |
152
|
|
|
'queryStringAsValue' => '≤9001', |
153
|
|
|
'isSingleton' => false |
154
|
|
|
) |
155
|
|
|
); |
156
|
|
|
|
157
|
|
|
$property = DIProperty::newFromUserLabel( 'Foo' )->setPropertyTypeId( '_num' ); |
158
|
|
|
$dataItem = new DINumber( 9001.356 ); |
159
|
|
|
|
160
|
|
|
$provider['num.2'] = array( |
161
|
|
|
$dataItem, |
162
|
|
|
$property, |
163
|
|
|
SMW_CMP_GEQ, |
164
|
|
|
array( |
165
|
|
|
'comparator' => SMW_CMP_GEQ, |
166
|
|
|
'dataItem' => $dataItem, |
167
|
|
|
'property' => $property, |
168
|
|
|
'queryString' => '[[≥9001.356]]', |
169
|
|
|
'queryStringAsValue' => '≥9001.356', |
170
|
|
|
'isSingleton' => false |
171
|
|
|
) |
172
|
|
|
); |
173
|
|
|
|
174
|
|
|
return $provider; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
public function comparativeHashProvider() { |
178
|
|
|
|
179
|
|
|
$provider[] = array( |
|
|
|
|
180
|
|
|
new ValueDescription( |
181
|
|
|
new DIWikiPage( 'Foo', NS_MAIN ), null, SMW_CMP_EQ |
182
|
|
|
), |
183
|
|
|
new ValueDescription( |
184
|
|
|
new DIWikiPage( 'Foo', NS_MAIN ), null, SMW_CMP_EQ |
185
|
|
|
), |
186
|
|
|
true |
187
|
|
|
); |
188
|
|
|
|
189
|
|
|
$provider[] = array( |
190
|
|
|
new ValueDescription( |
191
|
|
|
new DIWikiPage( 'Foo', NS_MAIN ), null, SMW_CMP_EQ |
192
|
|
|
), |
193
|
|
|
new ValueDescription( |
194
|
|
|
new DIWikiPage( 'Foo', NS_MAIN ), null, SMW_CMP_LEQ |
195
|
|
|
), |
196
|
|
|
false |
197
|
|
|
); |
198
|
|
|
|
199
|
|
|
$provider[] = array( |
200
|
|
|
new ValueDescription( |
201
|
|
|
new DIWikiPage( 'Foo', NS_MAIN ), null, SMW_CMP_EQ |
202
|
|
|
), |
203
|
|
|
new ValueDescription( |
204
|
|
|
new DIWikiPage( 'Foo', NS_MAIN ), new DIProperty( 'Bar' ), SMW_CMP_EQ |
205
|
|
|
), |
206
|
|
|
false |
207
|
|
|
); |
208
|
|
|
|
209
|
|
|
// Inverse case |
210
|
|
|
$provider[] = array( |
211
|
|
|
new ValueDescription( |
212
|
|
|
new DIWikiPage( 'Foo', NS_MAIN ), new DIProperty( 'Bar', true ), SMW_CMP_EQ |
213
|
|
|
), |
214
|
|
|
new ValueDescription( |
215
|
|
|
new DIWikiPage( 'Foo', NS_MAIN ), new DIProperty( 'Bar' ), SMW_CMP_EQ |
216
|
|
|
), |
217
|
|
|
false |
218
|
|
|
); |
219
|
|
|
|
220
|
|
|
return $provider; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
} |
224
|
|
|
|
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:
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 thebar
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.