1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SEQL\ByAskApiHttpRequest\Tests; |
4
|
|
|
|
5
|
|
|
use SEQL\ByAskApiHttpRequest\JsonResponseParser; |
6
|
|
|
use SMW\DIProperty; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @covers \SEQL\ByAskApiHttpRequest\JsonResponseParser |
10
|
|
|
* @group semantic-external-query-lookup |
11
|
|
|
* |
12
|
|
|
* @license GNU GPL v2+ |
13
|
|
|
* @since 1.0 |
14
|
|
|
* |
15
|
|
|
* @author mwjames |
16
|
|
|
*/ |
17
|
|
|
class JsonResponseParserTest extends \PHPUnit_Framework_TestCase { |
18
|
|
|
|
19
|
|
|
public function testCanConstruct() { |
20
|
|
|
|
21
|
|
|
$dataValueDeserializer = $this->getMockBuilder( '\SEQL\DataValueDeserializer' ) |
22
|
|
|
->disableOriginalConstructor() |
23
|
|
|
->getMock(); |
24
|
|
|
|
25
|
|
|
$this->assertInstanceOf( |
26
|
|
|
'\SEQL\ByAskApiHttpRequest\JsonResponseParser', |
27
|
|
|
new JsonResponseParser( $dataValueDeserializer ) |
28
|
|
|
); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @dataProvider resultProvider |
33
|
|
|
*/ |
34
|
|
|
public function testDoParse( $result, $rawResponseResult, $hasFurtherResults, $property ) { |
35
|
|
|
|
36
|
|
|
$dataValueDeserializer = $this->getMockBuilder( '\SEQL\DataValueDeserializer' ) |
37
|
|
|
->disableOriginalConstructor() |
38
|
|
|
->getMock(); |
39
|
|
|
|
40
|
|
|
$instance = new JsonResponseParser( $dataValueDeserializer ); |
41
|
|
|
|
42
|
|
|
$instance->doParse( $result ); |
43
|
|
|
|
44
|
|
|
$this->assertEquals( |
45
|
|
|
$rawResponseResult, |
46
|
|
|
$instance->getRawResponseResult() |
47
|
|
|
); |
48
|
|
|
|
49
|
|
|
$this->assertEquals( |
50
|
|
|
$hasFurtherResults, |
51
|
|
|
$instance->hasFurtherResults() |
52
|
|
|
); |
53
|
|
|
|
54
|
|
|
if ( $property !== null ) { |
55
|
|
|
$this->assertEquals( |
56
|
|
|
$property, |
57
|
|
|
$instance->findPropertyFromInMemoryExternalRepositoryCache( $property ) |
58
|
|
|
); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function testDoParseForRedirect() { |
63
|
|
|
|
64
|
|
|
$result = array( |
65
|
|
|
'query' => array( |
66
|
|
|
'printrequests' => array( |
67
|
|
|
array( 'label' => 'Foo', 'mode' => 2, 'redi' => 'Bar' , 'typeid' => '_wpg' ) |
68
|
|
|
), |
69
|
|
|
'results' => array() |
70
|
|
|
) |
71
|
|
|
); |
72
|
|
|
|
73
|
|
|
$rawResponseResult = array( |
74
|
|
|
'printrequests' => array( |
75
|
|
|
array( 'label' => 'Foo', 'mode' => 2, 'redi' => 'Bar' , 'typeid' => '_wpg' ) |
76
|
|
|
), |
77
|
|
|
'results' => array() |
78
|
|
|
); |
79
|
|
|
|
80
|
|
|
$property = new DIProperty( 'Foo' ); |
81
|
|
|
$property->setPropertyTypeId( '_wpg' ); |
82
|
|
|
$property->setInterwiki( 'abc' ); |
83
|
|
|
|
84
|
|
|
$dataValueDeserializer = $this->getMockBuilder( '\SEQL\DataValueDeserializer' ) |
85
|
|
|
->disableOriginalConstructor() |
86
|
|
|
->getMock(); |
87
|
|
|
|
88
|
|
|
$dataValueDeserializer->expects( $this->once() ) |
89
|
|
|
->method( 'getQuerySource' ) |
90
|
|
|
->will( $this->returnValue( 'abc' ) ); |
91
|
|
|
|
92
|
|
|
$instance = new JsonResponseParser( $dataValueDeserializer ); |
93
|
|
|
|
94
|
|
|
$instance->doParse( $result ); |
95
|
|
|
|
96
|
|
|
$this->assertEquals( |
97
|
|
|
$rawResponseResult, |
98
|
|
|
$instance->getRawResponseResult() |
99
|
|
|
); |
100
|
|
|
|
101
|
|
|
$this->assertEquals( |
102
|
|
|
$property, |
103
|
|
|
$instance->findPropertyFromInMemoryExternalRepositoryCache( new DIProperty( 'Bar' ) ) |
104
|
|
|
); |
105
|
|
|
|
106
|
|
|
$this->assertEquals( |
107
|
|
|
$property, |
108
|
|
|
$instance->findPropertyFromInMemoryExternalRepositoryCache( new DIProperty( 'Foo' ) ) |
109
|
|
|
); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function resultProvider() { |
113
|
|
|
|
114
|
|
|
#0 |
115
|
|
|
$provider[] = array( |
|
|
|
|
116
|
|
|
array( 'query' => array() ), |
117
|
|
|
array(), |
118
|
|
|
false, |
119
|
|
|
null |
120
|
|
|
); |
121
|
|
|
|
122
|
|
|
#1 |
123
|
|
|
$provider[] = array( |
124
|
|
|
array( |
125
|
|
|
'query-continue-offset' => 3, |
126
|
|
|
'query' => array() |
127
|
|
|
), |
128
|
|
|
array(), |
129
|
|
|
true, |
130
|
|
|
null |
131
|
|
|
); |
132
|
|
|
|
133
|
|
|
#2 |
134
|
|
|
$provider[] = array( |
135
|
|
|
array( |
136
|
|
|
'query-continue-offset' => 3, |
137
|
|
|
'query' => array( |
138
|
|
|
'printrequests' => array( |
139
|
|
|
array( 'label' => 'Category', 'mode' => 0 ) |
140
|
|
|
) |
141
|
|
|
) |
142
|
|
|
), |
143
|
|
|
array( |
144
|
|
|
'printrequests' => array( |
145
|
|
|
array( 'label' => 'Category', 'mode' => 0 ) |
146
|
|
|
) |
147
|
|
|
), |
148
|
|
|
true, |
149
|
|
|
new DIProperty( '_INST' ) |
150
|
|
|
); |
151
|
|
|
|
152
|
|
|
#3 |
153
|
|
|
$provider[] = array( |
154
|
|
|
array( |
155
|
|
|
'query' => array( |
156
|
|
|
'printrequests' => array( |
157
|
|
|
array( 'label' => 'Category', 'mode' => 0 ) |
158
|
|
|
), |
159
|
|
|
'results' => array() |
160
|
|
|
), |
161
|
|
|
), |
162
|
|
|
array( |
163
|
|
|
'printrequests' => array( |
164
|
|
|
array( 'label' => 'Category', 'mode' => 0 ) |
165
|
|
|
), |
166
|
|
|
'results' => array() |
167
|
|
|
), |
168
|
|
|
false, |
169
|
|
|
new DIProperty( '_INST' ) |
170
|
|
|
); |
171
|
|
|
|
172
|
|
|
return $provider; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
} |
176
|
|
|
|
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.