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 resultProvider() { |
63
|
|
|
|
64
|
|
|
#0 |
65
|
|
|
$provider[] = array( |
|
|
|
|
66
|
|
|
array( 'query' => array() ), |
67
|
|
|
array(), |
68
|
|
|
false, |
69
|
|
|
null |
70
|
|
|
); |
71
|
|
|
|
72
|
|
|
#1 |
73
|
|
|
$provider[] = array( |
74
|
|
|
array( |
75
|
|
|
'query-continue-offset' => 3, |
76
|
|
|
'query' => array() |
77
|
|
|
), |
78
|
|
|
array(), |
79
|
|
|
true, |
80
|
|
|
null |
81
|
|
|
); |
82
|
|
|
|
83
|
|
|
#2 |
84
|
|
|
$provider[] = array( |
85
|
|
|
array( |
86
|
|
|
'query-continue-offset' => 3, |
87
|
|
|
'query' => array( |
88
|
|
|
'printrequests' => array( |
89
|
|
|
array( 'label' => 'Category', 'mode' => 0 ) |
90
|
|
|
) |
91
|
|
|
) |
92
|
|
|
), |
93
|
|
|
array( |
94
|
|
|
'printrequests' => array( |
95
|
|
|
array( 'label' => 'Category', 'mode' => 0 ) |
96
|
|
|
) |
97
|
|
|
), |
98
|
|
|
true, |
99
|
|
|
new DIProperty( '_INST' ) |
100
|
|
|
); |
101
|
|
|
|
102
|
|
|
#3 |
103
|
|
|
$provider[] = array( |
104
|
|
|
array( |
105
|
|
|
'query' => array( |
106
|
|
|
'printrequests' => array( |
107
|
|
|
array( 'label' => 'Category', 'mode' => 0 ) |
108
|
|
|
), |
109
|
|
|
'results' => array() |
110
|
|
|
), |
111
|
|
|
), |
112
|
|
|
array( |
113
|
|
|
'printrequests' => array( |
114
|
|
|
array( 'label' => 'Category', 'mode' => 0 ) |
115
|
|
|
), |
116
|
|
|
'results' => array() |
117
|
|
|
), |
118
|
|
|
false, |
119
|
|
|
null |
120
|
|
|
); |
121
|
|
|
|
122
|
|
|
|
123
|
|
|
return $provider; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
} |
127
|
|
|
|
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.