Completed
Pull Request — develop (#24)
by Ben
06:57 queued 04:18
created

UrlFunctionsTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 198
Duplicated Lines 11.62 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 6
dl 23
loc 198
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 7 1
A testAddToUrl() 0 22 1
A testAddRangeToUrl() 10 10 1
A testRemoveFromUrl() 0 19 1
B testRefinementAdditionWithMapping() 0 36 1
B testRefinementAdditionWithoutMapping() 0 29 1
B testRefinementAdditionWithMappingMulti() 0 43 1
A testRefinementAdditionWithCategoryExpansion() 13 13 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
2
use GroupByInc\API\Model\Navigation;
3
use GroupByInc\API\Request\SelectedRefinementRange;
4
use GroupByInc\API\Request\SelectedRefinementValue;
5
use GroupByInc\API\Url\UrlBeautifier;
6
use GroupByInc\API\Url\UrlFunctions;
7
8
class UrlFunctionsTest 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...
9
{
10
11
    const DEFAULT_BEAUTIFIER = "default";
12
    const DEFAULT_NAVIGATION = "default";
13
    const HEIGHT_NAVIGATION = "height";
14
    const CATEGORY_NAVIGATION = "category";
15
16
    /** @var UrlBeautifier $beautifier */
17
    private $beautifier;
18
19
    public function setUp()
20
    {
21
        UrlBeautifier::createUrlBeautifier(self::DEFAULT_BEAUTIFIER);
22
        $this->beautifier = UrlBeautifier::getUrlBeautifiers()[self::DEFAULT_BEAUTIFIER];
23
        $this->beautifier->addRefinementMapping('h', 'height')
24
            ->setSearchMapping('q');
25
    }
26
27
    public function testAddToUrl()
28
    {
29
        $navigations = [];
30
        $this->beautifier->addRefinementMapping('c', 'category');
31
        $refinement = new SelectedRefinementValue();
32
        $refinement->setValue('3.2m');
33
        $url = UrlFunctions::toUrlAdd(self::DEFAULT_BEAUTIFIER, 'toast', $navigations, self::HEIGHT_NAVIGATION, $refinement);
34
        $this->assertEquals('/3.2m/toast/hq', $url);
35
36
        $nav1 = new Navigation();
37
        $nav1->setName(self::HEIGHT_NAVIGATION)->setDisplayName("Height")
38
            ->setType(Navigation\Type::String)
39
            ->setRefinements([$refinement]);
0 ignored issues
show
Documentation introduced by
array($refinement) is of type array<integer,object<Gro...ctedRefinementValue>"}>, but the function expects a array<integer,object<Gro...\API\Model\Refinement>>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
40
41
        array_push($navigations, $nav1);
42
43
        $refinement = new SelectedRefinementValue();
44
        $refinement->setValue('toys');
45
        $url = UrlFunctions::toUrlAdd(self::DEFAULT_BEAUTIFIER, 'toast', $navigations, self::CATEGORY_NAVIGATION, $refinement);
46
        $this->assertEquals('/3.2m/toast/toys/hqc', $url);
47
        $this->assertEquals(1, count($navigations));
48
    }
49
50 View Code Duplication
    public function testAddRangeToUrl()
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...
51
    {
52
        $navigations = [];
53
        $this->beautifier->addRefinementMapping('c', 'category');
54
        $refinement = new SelectedRefinementRange();
55
        $refinement->setLow('12')
56
            ->setHigh('34');
57
        $url = UrlFunctions::toUrlAdd(self::DEFAULT_BEAUTIFIER, 'toast', $navigations, self::CATEGORY_NAVIGATION, $refinement);
58
        $this->assertEquals('/toast/q?refinements=%7Ecategory%3A12..34', $url);
59
    }
60
    public function testRemoveFromUrl()
61
    {
62
        $navigations = [];
63
        $this->beautifier->addRefinementMapping('c', 'category');
64
        $refinement1 = new SelectedRefinementValue();
65
        $refinement1->setValue('3.2m');
66
67
        $nav1 = new Navigation();
68
        $nav1->setName(self::HEIGHT_NAVIGATION)->setDisplayName("Height")
69
            ->setType(Navigation\Type::String)
70
            ->setRefinements([$refinement1]);
0 ignored issues
show
Documentation introduced by
array($refinement1) is of type array<integer,object<Gro...ctedRefinementValue>"}>, but the function expects a array<integer,object<Gro...\API\Model\Refinement>>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
71
72
        array_push($navigations, $nav1);
73
74
        $this->assertEquals(1, count($navigations));
75
        $url = UrlFunctions::toUrlRemove(self::DEFAULT_BEAUTIFIER, 'toast', $navigations, self::HEIGHT_NAVIGATION, $refinement1);
76
        $this->assertEquals('/toast/q', $url);
77
        $this->assertEquals(0, count($navigations));
78
    }
79
80
    public function testRefinementAdditionWithMapping() {
81
        $navigations = [];
82
83
        $this->beautifier->addRefinementMapping('g', "gender");
84
        $this->beautifier->addRefinementMapping('t', "product");
85
        $this->beautifier->addRefinementMapping('s', "primarysport");
86
        $this->beautifier->addRefinementMapping('c', "simpleColorDesc");
87
        $this->beautifier->addRefinementMapping('l', "collections");
88
        $this->beautifier->addRefinementMapping('f', "league");
89
        $this->beautifier->setAppend("/index.html");
90
91
        $refinement1 = new SelectedRefinementValue();
92
        $refinement1->setValue("Women");
93
94
        $nav1 = new Navigation();
95
        $nav1->setName("gender")->setDisplayName("Gender")
96
            ->setType(Navigation\Type::String)
97
            ->setRefinements([$refinement1]);
0 ignored issues
show
Documentation introduced by
array($refinement1) is of type array<integer,object<Gro...ctedRefinementValue>"}>, but the function expects a array<integer,object<Gro...\API\Model\Refinement>>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
98
99
        $refinement2 = new SelectedRefinementValue();
100
        $refinement2->setValue("Pink");
101
102
        $nav2 = new Navigation();
103
        $nav2->setName("simpleColorDesc")->setDisplayName("Color")
104
            ->setType(Navigation\Type::String)
105
            ->setRefinements([$refinement2]);
0 ignored issues
show
Documentation introduced by
array($refinement2) is of type array<integer,object<Gro...ctedRefinementValue>"}>, but the function expects a array<integer,object<Gro...\API\Model\Refinement>>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
106
107
        array_push($navigations, $nav1, $nav2);
108
109
110
        $refinement3 = new SelectedRefinementValue();
111
        $refinement3->setValue("Clothing");
112
113
        $url = UrlFunctions::toUrlAdd(self::DEFAULT_BEAUTIFIER, '', $navigations, 'product', $refinement3);
114
        $this->assertEquals("/Women/Clothing/Pink/gtc/index.html", $url);
115
    }
116
117
    public function testRefinementAdditionWithoutMapping() {
118
        $navigations = [];
119
120
        $this->beautifier->setAppend("/index.html");
121
122
        $refinement1 = new SelectedRefinementValue();
123
        $refinement1->setValue("Women");
124
125
        $nav1 = new Navigation();
126
        $nav1->setName("gender")->setDisplayName("Gender")
127
            ->setType(Navigation\Type::String)
128
            ->setRefinements([$refinement1]);
0 ignored issues
show
Documentation introduced by
array($refinement1) is of type array<integer,object<Gro...ctedRefinementValue>"}>, but the function expects a array<integer,object<Gro...\API\Model\Refinement>>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
129
130
        $refinement2 = new SelectedRefinementValue();
131
        $refinement2->setValue("Pink");
132
133
        $nav2 = new Navigation();
134
        $nav2->setName("simpleColorDesc")->setDisplayName("Color")
135
            ->setType(Navigation\Type::String)
136
            ->setRefinements([$refinement2]);
0 ignored issues
show
Documentation introduced by
array($refinement2) is of type array<integer,object<Gro...ctedRefinementValue>"}>, but the function expects a array<integer,object<Gro...\API\Model\Refinement>>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
137
138
        array_push($navigations, $nav1, $nav2);
139
140
        $refinement3 = new SelectedRefinementValue();
141
        $refinement3->setValue("Clothing");
142
143
        $url = UrlFunctions::toUrlAdd(self::DEFAULT_BEAUTIFIER, '', $navigations, 'product', $refinement3);
144
        $this->assertEquals("/index.html?refinements=%7Egender%3DWomen%7EsimpleColorDesc%3DPink%7Eproduct%3DClothing", $url);
145
    }
146
147
    public function testRefinementAdditionWithMappingMulti() {
148
        $navigations = [];
149
150
        $this->beautifier->addRefinementMapping('g', "gender");
151
        $this->beautifier->addRefinementMapping('t', "product");
152
        $this->beautifier->addRefinementMapping('s', "primarysport");
153
        $this->beautifier->addRefinementMapping('c', "simpleColorDesc");
154
        $this->beautifier->addRefinementMapping('l', "collections");
155
        $this->beautifier->addRefinementMapping('f', "league");
156
        $this->beautifier->setAppend("/index.html");
157
158
        $refinement1 = new SelectedRefinementValue();
159
        $refinement1->setValue("Women");
160
161
        $nav1 = new Navigation();
162
        $nav1->setName("gender")->setDisplayName("Gender")
163
            ->setType(Navigation\Type::String)
164
            ->setRefinements([$refinement1]);
0 ignored issues
show
Documentation introduced by
array($refinement1) is of type array<integer,object<Gro...ctedRefinementValue>"}>, but the function expects a array<integer,object<Gro...\API\Model\Refinement>>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
165
166
        $refinement2 = new SelectedRefinementValue();
167
        $refinement2->setValue("Pink");
168
169
        $nav2 = new Navigation();
170
        $nav2->setName("simpleColorDesc")->setDisplayName("Color")
171
            ->setType(Navigation\Type::String)
172
            ->setRefinements([$refinement2]);
0 ignored issues
show
Documentation introduced by
array($refinement2) is of type array<integer,object<Gro...ctedRefinementValue>"}>, but the function expects a array<integer,object<Gro...\API\Model\Refinement>>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
173
174
        array_push($navigations, $nav1, $nav2);
175
176
177
        $refinement3 = new SelectedRefinementValue();
178
        $refinement3->setValue("Men");
179
180
        $url = UrlFunctions::toUrlAdd(self::DEFAULT_BEAUTIFIER, '', $navigations, 'gender', $refinement3);
181
        $this->assertEquals("/Women/Men/Pink/ggc/index.html", $url);
182
183
        $refinement4 = new SelectedRefinementValue();
184
        $refinement4->setValue("Kid");
185
186
187
        $url = UrlFunctions::toUrlAdd(self::DEFAULT_BEAUTIFIER, '', $navigations, 'gender', $refinement4);
188
        $this->assertEquals("/Women/Kid/Pink/ggc/index.html", $url);
189
    }
190
191 View Code Duplication
    public function testRefinementAdditionWithCategoryExpansion () {
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...
192
        $navigations = [];
193
194
        $this->beautifier->addRefinementMapping('s', "size");
195
        $this->beautifier->setAppend("/index.html");
196
197
        $refinement = new SelectedRefinementValue();
198
        $refinement->setValue("Category Root~Athletics~Men's~Sneakers");
199
200
201
        $url = UrlFunctions::toUrlAdd(self::DEFAULT_BEAUTIFIER, '', $navigations, 'category_leaf_expanded', $refinement);
202
        $this->assertEquals("/index.html?refinements=%7Ecategory_leaf_expanded%3DCategory+Root%7EAthletics%7EMen%27s%7ESneakers", $url);
203
    }
204
205
}