Completed
Push — develop ( 50dbcf...bff7e0 )
by Ben
13s
created

JsonDeserializeTest   C

Complexity

Total Complexity 20

Size/Duplication

Total Lines 255
Duplicated Lines 34.9 %

Coupling/Cohesion

Components 1
Dependencies 23

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 23
dl 89
loc 255
rs 5.5
c 0
b 0
f 0

20 Methods

Rating   Name   Duplication   Size   Complexity  
A setUpBeforeClass() 0 4 1
A deserialize() 0 4 1
A testDeserializeRefinementRange() 0 14 1
A testDeserializeRefinementValue() 0 13 1
A testDeserializeMetadata() 0 10 1
A testDeserializeNavigation() 0 15 1
A testDeserializeClusterRecord() 11 11 1
A testDeserializeCluster() 10 10 1
A testDeserializeRefinementMatchValue() 0 11 1
A testDeserializeRefinementMatch() 10 10 1
A testDeserializeRecord() 13 13 1
A testDeserializePageInfo() 0 10 1
A testDeserializeContentZone() 11 11 1
A testDeserializeRecordZone() 12 12 1
A testDeserializeBannerZone() 11 11 1
A testDeserializeRichContentZone() 11 11 1
A testDeserializeTemplate() 0 13 1
A testDeserializeRestrictNavigation() 0 10 1
B testDeserializeResults() 0 26 1
A testDeserializeRefinementsResult() 0 10 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 26 and the first side effect is on line 3.

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.

Loading history...
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
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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