|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the php-phantomjs. |
|
5
|
|
|
* |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
7
|
|
|
* file that was distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
namespace JonnyW\PhantomJs\Tests\Unit\Parser; |
|
10
|
|
|
|
|
11
|
|
|
use JonnyW\PhantomJs\Parser\JsonParser; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* PHP PhantomJs |
|
15
|
|
|
* |
|
16
|
|
|
* @author Jon Wenmoth <[email protected]> |
|
17
|
|
|
*/ |
|
18
|
|
|
class JsonParserTest extends \PHPUnit_Framework_TestCase |
|
19
|
|
|
{ |
|
20
|
|
|
|
|
21
|
|
|
/*****************/ |
|
22
|
|
|
/***** TESTS *****/ |
|
23
|
|
|
/*****************/ |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Test parse returns array if data |
|
27
|
|
|
* is null. |
|
28
|
|
|
* |
|
29
|
|
|
* @access public |
|
30
|
|
|
* @return void |
|
31
|
|
|
*/ |
|
32
|
|
|
public function testParseReturnsArrayIfDataIsNull() |
|
33
|
|
|
{ |
|
34
|
|
|
$data = null; |
|
35
|
|
|
|
|
36
|
|
|
$jsonParser = $this->getJsonParser(); |
|
37
|
|
|
|
|
38
|
|
|
$this->assertInternalType('array', $jsonParser->parse($data)); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Test parse returns array if data |
|
43
|
|
|
* is not a string. |
|
44
|
|
|
* |
|
45
|
|
|
* @access public |
|
46
|
|
|
* @return void |
|
47
|
|
|
*/ |
|
48
|
|
|
public function testParseReturnsArrayIfDataIsNotAString() |
|
49
|
|
|
{ |
|
50
|
|
|
$data = new \stdClass(); |
|
51
|
|
|
|
|
52
|
|
|
$jsonParser = $this->getJsonParser(); |
|
53
|
|
|
|
|
54
|
|
|
$this->assertInternalType('array', $jsonParser->parse($data)); |
|
|
|
|
|
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Test parse returns array if data |
|
59
|
|
|
* is invalid JSON format. |
|
60
|
|
|
* |
|
61
|
|
|
* @access public |
|
62
|
|
|
* @return void |
|
63
|
|
|
*/ |
|
64
|
|
|
public function testParseReturnsArrayIfDataIsInvalidJsonFormat() |
|
65
|
|
|
{ |
|
66
|
|
|
$data = 'Invalid JSON format'; |
|
67
|
|
|
|
|
68
|
|
|
$jsonParser = $this->getJsonParser(); |
|
69
|
|
|
|
|
70
|
|
|
$this->assertInternalType('array', $jsonParser->parse($data)); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Test parse returns array if data |
|
75
|
|
|
* is broken json format. |
|
76
|
|
|
* |
|
77
|
|
|
* @access public |
|
78
|
|
|
* @return void |
|
79
|
|
|
*/ |
|
80
|
|
|
public function testParseReturnsArrayIfDataIsBrokenJsonFormat() |
|
81
|
|
|
{ |
|
82
|
|
|
$data = '{data: Unquoted string}'; |
|
83
|
|
|
|
|
84
|
|
|
$jsonParser = $this->getJsonParser(); |
|
85
|
|
|
|
|
86
|
|
|
$this->assertInternalType('array', $jsonParser->parse($data)); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Test parse returns array if data |
|
91
|
|
|
* is valid JSON object. |
|
92
|
|
|
* |
|
93
|
|
|
* @access public |
|
94
|
|
|
* @return void |
|
95
|
|
|
*/ |
|
96
|
|
|
public function testParseReturnsArrayIfDataIsValidJsonObject() |
|
97
|
|
|
{ |
|
98
|
|
|
$data = '{"data": "Test data"}'; |
|
99
|
|
|
|
|
100
|
|
|
$jsonParser = $this->getJsonParser(); |
|
101
|
|
|
|
|
102
|
|
|
$this->assertInternalType('array', $jsonParser->parse($data)); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Test parse returns array if data |
|
107
|
|
|
* is valid JSON array. |
|
108
|
|
|
* |
|
109
|
|
|
* @access public |
|
110
|
|
|
* @return void |
|
111
|
|
|
*/ |
|
112
|
|
|
public function testParseReturnsArrayIfDataIsValidJsonArray() |
|
113
|
|
|
{ |
|
114
|
|
|
$data = '["Test data"]'; |
|
115
|
|
|
|
|
116
|
|
|
$jsonParser = $this->getJsonParser(); |
|
117
|
|
|
|
|
118
|
|
|
$this->assertInternalType('array', $jsonParser->parse($data)); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Test parse successfully parses data |
|
123
|
|
|
* if data is valid JSON object. |
|
124
|
|
|
* |
|
125
|
|
|
* @access public |
|
126
|
|
|
* @return void |
|
127
|
|
|
*/ |
|
128
|
|
|
public function testParseSuccessfullyParsesDataIfDataIsValidJsonObject() |
|
129
|
|
|
{ |
|
130
|
|
|
$data = '{"data": "Test data"}'; |
|
131
|
|
|
|
|
132
|
|
|
$jsonParser = $this->getJsonParser(); |
|
133
|
|
|
$parsedData = $jsonParser->parse($data); |
|
134
|
|
|
|
|
135
|
|
|
$expectedData = array( |
|
136
|
|
|
'data' => 'Test data' |
|
137
|
|
|
); |
|
138
|
|
|
|
|
139
|
|
|
$this->assertSame($parsedData, $expectedData); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Test parse successfully parses data |
|
144
|
|
|
* if data is valid JSON array. |
|
145
|
|
|
* |
|
146
|
|
|
* @access public |
|
147
|
|
|
* @return void |
|
148
|
|
|
*/ |
|
149
|
|
|
public function testParseSuccessfullyParsesDataIfDataIsValidJsonArray() |
|
150
|
|
|
{ |
|
151
|
|
|
$data = '["Test data"]'; |
|
152
|
|
|
|
|
153
|
|
|
$jsonParser = $this->getJsonParser(); |
|
154
|
|
|
$parsedData = $jsonParser->parse($data); |
|
155
|
|
|
|
|
156
|
|
|
$expectedData = array( |
|
157
|
|
|
'Test data' |
|
158
|
|
|
); |
|
159
|
|
|
|
|
160
|
|
|
$this->assertSame($parsedData, $expectedData); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* Test parse successfully parses |
|
165
|
|
|
* multidimensional data if data is |
|
166
|
|
|
* valid JSON format. |
|
167
|
|
|
* |
|
168
|
|
|
* @access public |
|
169
|
|
|
* @return void |
|
170
|
|
|
*/ |
|
171
|
|
|
public function testParseSuccessfullyParsesMultidimensionalDataIfDataIsValidJsonFormat() |
|
172
|
|
|
{ |
|
173
|
|
|
$data = '{ |
|
174
|
|
|
"data": { |
|
175
|
|
|
"data": { "data": "Test data" }, |
|
176
|
|
|
"more_data": "More test data" |
|
177
|
|
|
} |
|
178
|
|
|
}'; |
|
179
|
|
|
|
|
180
|
|
|
$jsonParser = $this->getJsonParser(); |
|
181
|
|
|
$parsedData = $jsonParser->parse($data); |
|
182
|
|
|
|
|
183
|
|
|
$expectedData = array( |
|
184
|
|
|
'data' => array( |
|
185
|
|
|
'data' => array( 'data' => 'Test data' ), |
|
186
|
|
|
'more_data' => 'More test data' |
|
187
|
|
|
) |
|
188
|
|
|
); |
|
189
|
|
|
|
|
190
|
|
|
$this->assertSame($parsedData, $expectedData); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** +++++++++++++++++++++++++++++++++++ **/ |
|
194
|
|
|
/** ++++++++++ TEST ENTITIES ++++++++++ **/ |
|
195
|
|
|
/** +++++++++++++++++++++++++++++++++++ **/ |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* Get JSON parser instance. |
|
199
|
|
|
* |
|
200
|
|
|
* @access protected |
|
201
|
|
|
* @return \JonnyW\PhantomJs\Parser\JsonParser |
|
202
|
|
|
*/ |
|
203
|
|
|
protected function getJsonParser() |
|
204
|
|
|
{ |
|
205
|
|
|
$jsonParser = new JsonParser(); |
|
206
|
|
|
|
|
207
|
|
|
return $jsonParser; |
|
208
|
|
|
} |
|
209
|
|
|
} |
|
210
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: