1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SMW\Tests\Query; |
4
|
|
|
|
5
|
|
|
use SMW\Query\QueryComparator; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @covers \SMW\Query\QueryComparator |
9
|
|
|
* @group semantic-mediawiki |
10
|
|
|
* |
11
|
|
|
* @license GNU GPL v2+ |
12
|
|
|
* @since 2.3 |
13
|
|
|
* |
14
|
|
|
* @author mwjames |
15
|
|
|
*/ |
16
|
|
|
class QueryComparatorTest extends \PHPUnit_Framework_TestCase { |
17
|
|
|
|
18
|
|
|
public function testCanConstruct() { |
19
|
|
|
|
20
|
|
|
$comparatorList = ''; |
21
|
|
|
|
22
|
|
|
$this->assertInstanceOf( |
23
|
|
|
'\SMW\Query\QueryComparator', |
24
|
|
|
new QueryComparator( $comparatorList, false ) |
25
|
|
|
); |
26
|
|
|
|
27
|
|
|
$this->assertInstanceOf( |
28
|
|
|
'\SMW\Query\QueryComparator', |
29
|
|
|
QueryComparator::getInstance() |
30
|
|
|
); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @dataProvider stringComparatorProvider |
35
|
|
|
*/ |
36
|
|
|
public function testGetComparatorFromString( $stringComparator, $expected ) { |
37
|
|
|
|
38
|
|
|
$comparatorList = ''; |
39
|
|
|
|
40
|
|
|
$instance = new QueryComparator( $comparatorList, false ); |
41
|
|
|
|
42
|
|
|
$this->assertEquals( |
43
|
|
|
$expected, |
44
|
|
|
$instance->getComparatorFromString( $stringComparator ) |
45
|
|
|
); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @dataProvider stringComparatorProvider |
50
|
|
|
*/ |
51
|
|
|
public function testGetStringForComparator( $stringComparator, $comparator ) { |
52
|
|
|
|
53
|
|
|
$comparatorList = ''; |
54
|
|
|
|
55
|
|
|
$instance = new QueryComparator( $comparatorList, false ); |
56
|
|
|
|
57
|
|
|
$this->assertEquals( |
58
|
|
|
$stringComparator, |
59
|
|
|
$instance->getStringForComparator( $comparator ) |
60
|
|
|
); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @dataProvider extractStringComparatorProvider |
65
|
|
|
*/ |
66
|
|
|
public function testExtractComparatorFromString( $string, $expectedString, $expectedComparator ) { |
67
|
|
|
|
68
|
|
|
$comparatorList = ''; |
69
|
|
|
|
70
|
|
|
$instance = new QueryComparator( $comparatorList, true ); |
71
|
|
|
|
72
|
|
|
$this->assertEquals( |
73
|
|
|
$expectedComparator, |
74
|
|
|
$instance->extractComparatorFromString( $string ) |
75
|
|
|
); |
76
|
|
|
|
77
|
|
|
$this->assertEquals( |
78
|
|
|
$expectedString, |
79
|
|
|
$string |
80
|
|
|
); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @dataProvider containsComparatorProvider |
85
|
|
|
*/ |
86
|
|
|
public function testContainsComparator( $string, $comparator, $expected ) { |
87
|
|
|
|
88
|
|
|
$comparatorList = ''; |
89
|
|
|
|
90
|
|
|
$instance = new QueryComparator( $comparatorList, true ); |
91
|
|
|
|
92
|
|
|
$this->assertEquals( |
93
|
|
|
$expected, |
94
|
|
|
$instance->containsComparator( $string, $comparator ) |
95
|
|
|
); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function stringComparatorProvider() { |
99
|
|
|
|
100
|
|
|
$provider[] = array( |
|
|
|
|
101
|
|
|
'!~', |
102
|
|
|
SMW_CMP_NLKE |
103
|
|
|
); |
104
|
|
|
|
105
|
|
|
return $provider; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function extractStringComparatorProvider() { |
109
|
|
|
|
110
|
|
|
$provider[] = array( |
|
|
|
|
111
|
|
|
'!~Foo', |
112
|
|
|
'Foo', |
113
|
|
|
SMW_CMP_NLKE |
114
|
|
|
); |
115
|
|
|
|
116
|
|
|
$provider[] = array( |
117
|
|
|
'<Foo', |
118
|
|
|
'Foo', |
119
|
|
|
SMW_CMP_LESS |
120
|
|
|
); |
121
|
|
|
|
122
|
|
|
return $provider; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function containsComparatorProvider() { |
126
|
|
|
|
127
|
|
|
$provider[] = array( |
|
|
|
|
128
|
|
|
'~someThing', |
129
|
|
|
SMW_CMP_EQ, |
130
|
|
|
false |
131
|
|
|
); |
132
|
|
|
|
133
|
|
|
$provider[] = array( |
134
|
|
|
'someThing', |
135
|
|
|
SMW_CMP_EQ, |
136
|
|
|
true |
137
|
|
|
); |
138
|
|
|
|
139
|
|
|
$provider[] = array( |
140
|
|
|
'!~someThing', |
141
|
|
|
SMW_CMP_NLKE, |
142
|
|
|
true |
143
|
|
|
); |
144
|
|
|
|
145
|
|
|
$provider[] = array( |
146
|
|
|
'!~someThing', |
147
|
|
|
SMW_CMP_LIKE, |
148
|
|
|
false |
149
|
|
|
); |
150
|
|
|
|
151
|
|
|
$provider[] = array( |
152
|
|
|
'>>someThing', |
153
|
|
|
SMW_CMP_LESS, |
154
|
|
|
false |
155
|
|
|
); |
156
|
|
|
|
157
|
|
|
$provider[] = array( |
158
|
|
|
'<<someThing', |
159
|
|
|
SMW_CMP_LESS, |
160
|
|
|
true |
161
|
|
|
); |
162
|
|
|
|
163
|
|
|
return $provider; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
} |
167
|
|
|
|
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.