1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
require_once __DIR__ . '/Expectation/Json.php'; |
4
|
|
|
require_once __DIR__ . '/Expectation/Object.php'; |
5
|
|
|
|
6
|
|
|
use GroupByInc\API\Model\BannerZone; |
7
|
|
|
use GroupByInc\API\Model\Cluster; |
8
|
|
|
use GroupByInc\API\Model\ClusterRecord; |
9
|
|
|
use GroupByInc\API\Model\ContentZone; |
10
|
|
|
use GroupByInc\API\Model\Metadata; |
11
|
|
|
use GroupByInc\API\Model\Navigation; |
12
|
|
|
use GroupByInc\API\Model\PageInfo; |
13
|
|
|
use GroupByInc\API\Model\Record; |
14
|
|
|
use GroupByInc\API\Model\RecordZone; |
15
|
|
|
use GroupByInc\API\Model\RefinementMatch; |
16
|
|
|
use GroupByInc\API\Model\RefinementRange; |
17
|
|
|
use GroupByInc\API\Model\RefinementsResult; |
18
|
|
|
use GroupByInc\API\Model\RefinementValue; |
19
|
|
|
use GroupByInc\API\Model\Results; |
20
|
|
|
use GroupByInc\API\Model\RichContentZone; |
21
|
|
|
use GroupByInc\API\Model\Template; |
22
|
|
|
use GroupByInc\API\Request\RestrictNavigation; |
23
|
|
|
use GroupByInc\API\Util\SerializerFactory; |
24
|
|
|
use JMS\Serializer\Serializer; |
25
|
|
|
|
26
|
|
|
class JsonDeserializeTest extends PHPUnit_Framework_TestCase |
|
|
|
|
27
|
|
|
{ |
28
|
|
|
|
29
|
|
|
/** @var Serializer */ |
30
|
|
|
private static $serializer; |
31
|
|
|
|
32
|
|
|
public static function setUpBeforeClass() |
33
|
|
|
{ |
34
|
|
|
self::$serializer = SerializerFactory::build(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
private function deserialize($json, $namespacedClass) |
38
|
|
|
{ |
39
|
|
|
return self::$serializer->deserialize($json, $namespacedClass, 'json'); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function testDeserializeRefinementRange() |
43
|
|
|
{ |
44
|
|
|
/** @var RefinementRange $refRange */ |
45
|
|
|
$refRange = $this->deserialize(Json::$REFINEMENT_RANGE, 'GroupByInc\API\Model\RefinementRange'); |
46
|
|
|
$this->assertEquals(Object::$REFINEMENT_RANGE, $refRange); |
47
|
|
|
|
48
|
|
|
$json = json_decode(Json::$REFINEMENT_RANGE); |
49
|
|
|
$this->assertEquals($json->high, $refRange->getHigh()); |
50
|
|
|
$this->assertEquals($json->low, $refRange->getLow()); |
51
|
|
|
$this->assertEquals($json->type, $refRange->getType()); |
52
|
|
|
$this->assertEquals($json->count, $refRange->getCount()); |
53
|
|
|
$this->assertEquals(true, $refRange->isRange()); |
54
|
|
|
$this->assertEquals($json->exclude, $refRange->isExclude()); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function testDeserializeRefinementValue() |
58
|
|
|
{ |
59
|
|
|
/** @var RefinementValue $refValue */ |
60
|
|
|
$refValue = $this->deserialize(Json::$REFINEMENT_VALUE, 'GroupByInc\API\Model\RefinementValue'); |
61
|
|
|
$this->assertEquals(Object::$REFINEMENT_VALUE, $refValue); |
62
|
|
|
|
63
|
|
|
$json = json_decode(Json::$REFINEMENT_VALUE); |
64
|
|
|
$this->assertEquals($json->value, $refValue->getValue()); |
65
|
|
|
$this->assertEquals($json->type, $refValue->getType()); |
66
|
|
|
$this->assertEquals($json->count, $refValue->getCount()); |
67
|
|
|
$this->assertEquals(false, $refValue->isRange()); |
68
|
|
|
$this->assertEquals($json->exclude, $refValue->isExclude()); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function testDeserializeMetadata() |
72
|
|
|
{ |
73
|
|
|
/** @var Metadata $metadata */ |
74
|
|
|
$metadata = $this->deserialize(Json::$METADATA, 'GroupByInc\API\Model\Metadata'); |
75
|
|
|
$this->assertEquals(Object::$METADATA, $metadata); |
76
|
|
|
|
77
|
|
|
$json = json_decode(Json::$METADATA); |
78
|
|
|
$this->assertEquals($json->key, $metadata->getKey()); |
79
|
|
|
$this->assertEquals($json->value, $metadata->getValue()); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function testDeserializeNavigation() |
83
|
|
|
{ |
84
|
|
|
/** @var Navigation $navigation */ |
85
|
|
|
$navigation = $this->deserialize(Json::$NAVIGATION, 'GroupByInc\API\Model\Navigation'); |
86
|
|
|
$this->assertEquals(Object::$NAVIGATION, $navigation); |
87
|
|
|
|
88
|
|
|
$json = json_decode(Json::$NAVIGATION); |
89
|
|
|
$this->assertEquals($json->name, $navigation->getName()); |
90
|
|
|
$this->assertEquals($json->displayName, $navigation->getDisplayName()); |
91
|
|
|
$this->assertEquals($json->type, $navigation->getType()); |
92
|
|
|
$this->assertEquals(Object::$SORT, $navigation->getSort()); |
93
|
|
|
$this->assertEquals([Object::$METADATA], $navigation->getMetadata()); |
94
|
|
|
$this->assertEquals([Object::$REFINEMENT_RANGE, Object::$REFINEMENT_VALUE], $navigation->getRefinements()); |
95
|
|
|
$this->assertEquals($json->moreRefinements, $navigation->getMoreRefinements()); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
View Code Duplication |
public function testDeserializeClusterRecord() |
|
|
|
|
99
|
|
|
{ |
100
|
|
|
/** @var ClusterRecord $clusterRecord */ |
101
|
|
|
$clusterRecord = $this->deserialize(Json::$CLUSTER_RECORD, 'GroupByInc\API\Model\ClusterRecord'); |
102
|
|
|
$this->assertEquals(Object::$CLUSTER_RECORD, $clusterRecord); |
103
|
|
|
|
104
|
|
|
$json = json_decode(Json::$CLUSTER_RECORD); |
105
|
|
|
$this->assertEquals($json->snippet, $clusterRecord->getSnippet()); |
106
|
|
|
$this->assertEquals($json->title, $clusterRecord->getTitle()); |
107
|
|
|
$this->assertEquals($json->url, $clusterRecord->getUrl()); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
View Code Duplication |
public function testDeserializeCluster() |
|
|
|
|
111
|
|
|
{ |
112
|
|
|
/** @var Cluster $cluster */ |
113
|
|
|
$cluster = $this->deserialize(Json::$CLUSTER, 'GroupByInc\API\Model\Cluster'); |
114
|
|
|
$this->assertEquals(Object::$CLUSTER, $cluster); |
115
|
|
|
|
116
|
|
|
$json = json_decode(Json::$CLUSTER); |
117
|
|
|
$this->assertEquals($json->term, $cluster->getTerm()); |
118
|
|
|
$this->assertEquals([Object::$CLUSTER_RECORD], $cluster->getRecords()); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public function testDeserializeRefinementMatchValue() |
122
|
|
|
{ |
123
|
|
|
/** @var RefinementMatch\Value $refinementMatchValue */ |
124
|
|
|
$refinementMatchValue = $this->deserialize(Json::$REFINEMENT_MATCH_VALUE, |
125
|
|
|
'GroupByInc\API\Model\RefinementMatch\Value'); |
126
|
|
|
$this->assertEquals(Object::$REFINEMENT_MATCH_VALUE, $refinementMatchValue); |
127
|
|
|
|
128
|
|
|
$json = json_decode(Json::$REFINEMENT_MATCH_VALUE); |
129
|
|
|
$this->assertEquals($json->count, $refinementMatchValue->getCount()); |
130
|
|
|
$this->assertEquals($json->value, $refinementMatchValue->getValue()); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
View Code Duplication |
public function testDeserializeRefinementMatch() |
|
|
|
|
134
|
|
|
{ |
135
|
|
|
/** @var RefinementMatch $refinementMatch */ |
136
|
|
|
$refinementMatch = $this->deserialize(Json::$REFINEMENT_MATCH, 'GroupByInc\API\Model\RefinementMatch'); |
137
|
|
|
$this->assertEquals(Object::$REFINEMENT_MATCH, $refinementMatch); |
138
|
|
|
|
139
|
|
|
$json = json_decode(Json::$REFINEMENT_MATCH); |
140
|
|
|
$this->assertEquals($json->name, $refinementMatch->getName()); |
141
|
|
|
$this->assertEquals([Object::$REFINEMENT_MATCH_VALUE], $refinementMatch->getValues()); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
View Code Duplication |
public function testDeserializeRecord() |
|
|
|
|
145
|
|
|
{ |
146
|
|
|
/** @var Record $record */ |
147
|
|
|
$record = $this->deserialize(Json::$RECORD, 'GroupByInc\API\Model\Record'); |
148
|
|
|
$this->assertEquals(Object::$RECORD, $record); |
149
|
|
|
|
150
|
|
|
$json = json_decode(Json::$RECORD); |
151
|
|
|
$this->assertNotEmpty($record->getAllMeta()); |
152
|
|
|
$this->assertEquals($json->_snippet, $record->getSnippet()); |
153
|
|
|
$this->assertEquals($json->_t, $record->getTitle()); |
154
|
|
|
$this->assertEquals($json->_u, $record->getUrl()); |
155
|
|
|
$this->assertEquals([Object::$REFINEMENT_MATCH], $record->getRefinementMatches()); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
public function testDeserializePageInfo() |
159
|
|
|
{ |
160
|
|
|
/** @var PageInfo $pageInfo */ |
161
|
|
|
$pageInfo = $this->deserialize(Json::$PAGE_INFO, 'GroupByInc\API\Model\PageInfo'); |
162
|
|
|
$this->assertEquals(Object::$PAGE_INFO, $pageInfo); |
163
|
|
|
|
164
|
|
|
$json = json_decode(Json::$PAGE_INFO); |
165
|
|
|
$this->assertEquals($json->recordStart, $pageInfo->getRecordStart()); |
166
|
|
|
$this->assertEquals($json->recordEnd, $pageInfo->getRecordEnd()); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
View Code Duplication |
public function testDeserializeContentZone() |
|
|
|
|
170
|
|
|
{ |
171
|
|
|
/** @var ContentZone $contentZone */ |
172
|
|
|
$contentZone = $this->deserialize(Json::$CONTENT_ZONE, 'GroupByInc\API\Model\ContentZone'); |
173
|
|
|
$this->assertEquals(Object::$CONTENT_ZONE, $contentZone); |
174
|
|
|
|
175
|
|
|
$json = json_decode(Json::$CONTENT_ZONE); |
176
|
|
|
$this->assertEquals($json->name, $contentZone->getName()); |
177
|
|
|
$this->assertEquals($json->content, $contentZone->getContent()); |
178
|
|
|
$this->assertEquals($json->type, $contentZone->getType()); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
View Code Duplication |
public function testDeserializeRecordZone() |
|
|
|
|
182
|
|
|
{ |
183
|
|
|
/** @var RecordZone $recordZone */ |
184
|
|
|
$recordZone = $this->deserialize(Json::$RECORD_ZONE, 'GroupByInc\API\Model\RecordZone'); |
185
|
|
|
$this->assertEquals(Object::$RECORD_ZONE, $recordZone); |
186
|
|
|
|
187
|
|
|
$json = json_decode(Json::$RECORD_ZONE); |
188
|
|
|
$this->assertEquals($json->name, $recordZone->getName()); |
189
|
|
|
$this->assertEquals($json->query, $recordZone->getQuery()); |
190
|
|
|
$this->assertEquals([Object::$RECORD], $recordZone->getRecords()); |
191
|
|
|
$this->assertEquals($json->type, $recordZone->getType()); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
View Code Duplication |
public function testDeserializeBannerZone() |
|
|
|
|
195
|
|
|
{ |
196
|
|
|
/** @var BannerZone $bannerZone */ |
197
|
|
|
$bannerZone = $this->deserialize(Json::$BANNER_ZONE, 'GroupByInc\API\Model\BannerZone'); |
198
|
|
|
$this->assertEquals(Object::$BANNER_ZONE, $bannerZone); |
199
|
|
|
|
200
|
|
|
$json = json_decode(Json::$BANNER_ZONE); |
201
|
|
|
$this->assertEquals($json->name, $bannerZone->getName()); |
202
|
|
|
$this->assertEquals($json->bannerUrl, $bannerZone->getBannerUrl()); |
203
|
|
|
$this->assertEquals($json->type, $bannerZone->getType()); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
View Code Duplication |
public function testDeserializeRichContentZone() |
|
|
|
|
207
|
|
|
{ |
208
|
|
|
/** @var RichContentZone $richContentZone */ |
209
|
|
|
$richContentZone = $this->deserialize(Json::$RICH_CONTENT_ZONE, 'GroupByInc\API\Model\RichContentZone'); |
210
|
|
|
$this->assertEquals(Object::$RICH_CONTENT_ZONE, $richContentZone); |
211
|
|
|
|
212
|
|
|
$json = json_decode(Json::$RICH_CONTENT_ZONE); |
213
|
|
|
$this->assertEquals($json->name, $richContentZone->getName()); |
214
|
|
|
$this->assertEquals($json->richContent, $richContentZone->getRichContent()); |
215
|
|
|
$this->assertEquals($json->type, $richContentZone->getType()); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
public function testDeserializeTemplate() |
219
|
|
|
{ |
220
|
|
|
/** @var Template $template */ |
221
|
|
|
$template = $this->deserialize(Json::$TEMPLATE, 'GroupByInc\API\Model\Template'); |
222
|
|
|
$this->assertEquals(Object::$TEMPLATE, $template); |
223
|
|
|
|
224
|
|
|
$json = json_decode(Json::$TEMPLATE); |
225
|
|
|
$this->assertEquals($json->name, $template->getName()); |
226
|
|
|
$this->assertEquals($json->ruleName, $template->getRuleName()); |
227
|
|
|
$zones = $template->getZones(); |
228
|
|
|
$this->assertEquals(Object::$CONTENT_ZONE, $zones["content_zone"]); |
229
|
|
|
$this->assertEquals(Object::$RECORD_ZONE, $zones["record_zone"]); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
public function testDeserializeRestrictNavigation() |
233
|
|
|
{ |
234
|
|
|
/** @var RestrictNavigation $restrictNavigation */ |
235
|
|
|
$restrictNavigation = $this->deserialize(Json::$RESTRICT_NAVIGATION, 'GroupByInc\API\Request\RestrictNavigation'); |
236
|
|
|
$this->assertEquals(Object::$RESTRICT_NAVIGATION, $restrictNavigation); |
237
|
|
|
|
238
|
|
|
$json = json_decode(Json::$RESTRICT_NAVIGATION); |
239
|
|
|
$this->assertEquals($json->name, $restrictNavigation->getName()); |
240
|
|
|
$this->assertEquals($json->count, $restrictNavigation->getCount()); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
public function testDeserializeResults() |
244
|
|
|
{ |
245
|
|
|
/** @var Results $results */ |
246
|
|
|
$results = $this->deserialize(Json::$RESULTS, 'GroupByInc\API\Model\Results'); |
247
|
|
|
$this->assertEquals(Object::$RESULTS, $results); |
248
|
|
|
|
249
|
|
|
$json = json_decode(Json::$RESULTS); |
250
|
|
|
$this->assertEquals($json->id, $results->getId()); |
251
|
|
|
$this->assertEquals($json->area, $results->getArea()); |
252
|
|
|
$this->assertEquals($json->query, $results->getQuery()); |
253
|
|
|
$this->assertEquals($json->correctedQuery, $results->getCorrectedQuery()); |
254
|
|
|
$this->assertEquals($json->errors, $results->getErrors()); |
255
|
|
|
$this->assertEquals($json->originalQuery, $results->getOriginalQuery()); |
256
|
|
|
$this->assertEquals($json->redirect, $results->getRedirect()); |
257
|
|
|
$this->assertEquals($json->biasingProfile, $results->getBiasingProfile()); |
258
|
|
|
$this->assertEquals($json->totalRecordCount, $results->getTotalRecordCount()); |
259
|
|
|
$this->assertEquals($json->didYouMean, $results->getDidYouMean()); |
260
|
|
|
$this->assertEquals($json->relatedQueries, $results->getRelatedQueries()); |
261
|
|
|
$this->assertEquals($json->rewrites, $results->getRewrites()); |
262
|
|
|
$this->assertEquals(Object::$PAGE_INFO, $results->getPageInfo()); |
263
|
|
|
$this->assertEquals(Object::$TEMPLATE, $results->getTemplate()); |
264
|
|
|
$this->assertEquals([Object::$RECORD], $results->getRecords()); |
265
|
|
|
$this->assertEquals([Object::$NAVIGATION], $results->getAvailableNavigation()); |
266
|
|
|
$this->assertEquals([Object::$METADATA], $results->getSiteParams()); |
267
|
|
|
$this->assertEquals([Object::$NAVIGATION], $results->getSelectedNavigation()); |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
public function testDeserializeRefinementsResult() |
271
|
|
|
{ |
272
|
|
|
/** @var RefinementsResult $refinementsResult */ |
273
|
|
|
$refinementsResult = $this->deserialize(Json::$REFINEMENT_RESULTS, 'GroupByInc\API\Model\RefinementsResult'); |
274
|
|
|
$this->assertEquals(Object::$REFINEMENTS_RESULT, $refinementsResult); |
275
|
|
|
|
276
|
|
|
$json = json_decode(Json::$REFINEMENT_RESULTS); |
277
|
|
|
$this->assertEquals($json->errors, $refinementsResult->getErrors()); |
278
|
|
|
$this->assertEquals(Object::$NAVIGATION, $refinementsResult->getNavigation()); |
279
|
|
|
} |
280
|
|
|
} |
281
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.