1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SEQL\Tests; |
4
|
|
|
|
5
|
|
|
use SEQL\QueryEncoder; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @covers \SEQL\QueryEncoder |
9
|
|
|
* @group semantic-external-query-lookup |
10
|
|
|
* |
11
|
|
|
* @license GNU GPL v2+ |
12
|
|
|
* @since 1.0 |
13
|
|
|
* |
14
|
|
|
* @author mwjames |
15
|
|
|
*/ |
16
|
|
|
class QueryEncoderTest extends \PHPUnit_Framework_TestCase { |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @dataProvider queryElementProvider |
20
|
|
|
*/ |
21
|
|
|
public function testEncode( $sortKeys, $extraPrintouts, $expectedEncode, $expectedRawEncode ) { |
22
|
|
|
|
23
|
|
|
$query = $this->getMockBuilder( '\SMWQuery' ) |
24
|
|
|
->disableOriginalConstructor() |
25
|
|
|
->getMock(); |
26
|
|
|
|
27
|
|
|
$query->expects( $this->any() ) |
28
|
|
|
->method( 'getQueryString' ) |
29
|
|
|
->will( $this->returnValue( '[[Foo::bar]]' ) ); |
30
|
|
|
|
31
|
|
|
$query->expects( $this->any() ) |
32
|
|
|
->method( 'getLimit' ) |
33
|
|
|
->will( $this->returnValue( 42 ) ); |
34
|
|
|
|
35
|
|
|
$query->expects( $this->any() ) |
36
|
|
|
->method( 'getOffset' ) |
37
|
|
|
->will( $this->returnValue( 0 ) ); |
38
|
|
|
|
39
|
|
|
$query->expects( $this->any() ) |
40
|
|
|
->method( 'getMainlabel' ) |
41
|
|
|
->will( $this->returnValue( '' ) ); |
42
|
|
|
|
43
|
|
|
$query->expects( $this->any() ) |
44
|
|
|
->method( 'getSortKeys' ) |
45
|
|
|
->will( $this->returnValue( $sortKeys ) ); |
46
|
|
|
|
47
|
|
|
$query->expects( $this->any() ) |
48
|
|
|
->method( 'getExtraPrintouts' ) |
49
|
|
|
->will( $this->returnValue( $extraPrintouts ) ); |
50
|
|
|
|
51
|
|
|
$this->assertSame( |
52
|
|
|
$expectedEncode, |
53
|
|
|
QueryEncoder::encode( $query ) |
54
|
|
|
); |
55
|
|
|
|
56
|
|
|
$this->assertSame( |
57
|
|
|
$expectedRawEncode, |
58
|
|
|
QueryEncoder::rawUrlEncode( $query ) |
59
|
|
|
); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function queryElementProvider() { |
63
|
|
|
|
64
|
|
|
#0 |
65
|
|
|
$provider[] = array( |
|
|
|
|
66
|
|
|
array(), |
67
|
|
|
array(), |
68
|
|
|
'[[Foo::bar]]|limit=42|offset=0|mainlabel=', |
69
|
|
|
'%5B%5BFoo%3A%3Abar%5D%5D%7Climit%3D42%7Coffset%3D0%7Cmainlabel%3D' |
70
|
|
|
); |
71
|
|
|
|
72
|
|
|
#1 |
73
|
|
|
$provider[] = array( |
74
|
|
|
array( 'Foobar' => 'DESC' ), |
75
|
|
|
array(), |
76
|
|
|
'[[Foo::bar]]|limit=42|offset=0|mainlabel=|sort=Foobar|order=desc', |
77
|
|
|
'%5B%5BFoo%3A%3Abar%5D%5D%7Climit%3D42%7Coffset%3D0%7Cmainlabel%3D%7Csort%3DFoobar%7Corder%3Ddesc' |
78
|
|
|
); |
79
|
|
|
|
80
|
|
|
#2 |
81
|
|
|
$provider[] = array( |
82
|
|
|
array( 'Foobar' => 'DESC', 'Foobaz' => 'ASC' ), |
83
|
|
|
array(), |
84
|
|
|
'[[Foo::bar]]|limit=42|offset=0|mainlabel=|sort=Foobar,Foobaz|order=desc,asc', |
85
|
|
|
'%5B%5BFoo%3A%3Abar%5D%5D%7Climit%3D42%7Coffset%3D0%7Cmainlabel%3D%7Csort%3DFoobar%2CFoobaz%7Corder%3Ddesc%2Casc' |
86
|
|
|
); |
87
|
|
|
|
88
|
|
|
#3 |
89
|
|
|
$printRequest = $this->getMockBuilder( '\SMW\Query\PrintRequest' ) |
90
|
|
|
->disableOriginalConstructor() |
91
|
|
|
->getMock(); |
92
|
|
|
|
93
|
|
|
$printRequest->expects( $this->any() ) |
94
|
|
|
->method( 'getSerialisation' ) |
95
|
|
|
->will( $this->returnValue( '?ABC' ) ); |
96
|
|
|
|
97
|
|
|
$provider[] = array( |
98
|
|
|
array(), |
99
|
|
|
array( $printRequest ), |
100
|
|
|
'[[Foo::bar]]|?ABC|limit=42|offset=0|mainlabel=', |
101
|
|
|
'%5B%5BFoo%3A%3Abar%5D%5D%7C%3FABC%7Climit%3D42%7Coffset%3D0%7Cmainlabel%3D' |
102
|
|
|
); |
103
|
|
|
|
104
|
|
|
#4 (#show returns with an extra =) |
105
|
|
|
$printRequest = $this->getMockBuilder( '\SMW\Query\PrintRequest' ) |
106
|
|
|
->disableOriginalConstructor() |
107
|
|
|
->getMock(); |
108
|
|
|
|
109
|
|
|
$printRequest->expects( $this->any() ) |
110
|
|
|
->method( 'getSerialisation' ) |
111
|
|
|
->will( $this->returnValue( '?ABC=' ) ); |
112
|
|
|
|
113
|
|
|
$provider[] = array( |
114
|
|
|
array(), |
115
|
|
|
array( $printRequest ), |
116
|
|
|
'[[Foo::bar]]|?ABC|limit=42|offset=0|mainlabel=', |
117
|
|
|
'%5B%5BFoo%3A%3Abar%5D%5D%7C%3FABC%7Climit%3D42%7Coffset%3D0%7Cmainlabel%3D' |
118
|
|
|
); |
119
|
|
|
|
120
|
|
|
return $provider; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
} |
124
|
|
|
|
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.