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

UrlBeautifierTest   D

Complexity

Total Complexity 82

Size/Duplication

Total Lines 667
Duplicated Lines 33.58 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 82
lcom 1
cbo 4
dl 224
loc 667
rs 4.5931
c 0
b 0
f 0

73 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 6 1
A getBeautifier() 0 5 1
A testStopVowels() 0 16 3
A testMultipleBeautifiers() 0 10 1
A testQueryUrl() 0 6 1
A testRefinementsUrl() 0 6 1
A testMultipleRefinements() 0 6 1
A setUpTestHeightAndCategoryRefinements() 0 6 1
A testFullSearchUrl() 0 7 1
A testDetailQuery() 0 5 1
A testSearchQuery() 8 8 1
A testInvalidReferenceBlock() 0 5 1
A testRange() 0 11 1
A testDeepSearchQuery() 9 9 1
A testSearchUrlBackAndForth() 0 16 1
A testExistingMapping() 0 11 2
A testEmptyQueryString() 0 4 1
A testAddSameRefinementMultipleTimes() 8 8 1
A testAppend() 7 7 1
A testAppendWithSlash() 7 7 1
A testUnappend() 12 12 1
A assertNavigation() 0 5 1
A testUnappendWithSlash() 12 12 1
A testUnmappedToUrl() 0 8 1
A testUnmappedToUrlWithModifiedName() 8 8 1
A testUnmappedFromUrl() 0 14 1
A testCanonical() 0 8 1
A testSearchWithSlash() 0 5 1
A testRefinementWithSlash() 0 5 1
A testUnencodePlus() 0 7 1
A assertFromUrl() 11 18 4
A assertFromUrlWithSlash() 0 7 1
A assertFromUrlWithOneReplace() 0 6 1
A setSearchAndIndex() 0 5 1
A assertToAndFromUrl() 10 16 3
A assertFromUrlWithMultipleReplace() 0 8 1
A assertFromUrlWithOneInsert() 0 5 1
A assertFromUrlWithReplaceAndInsertionsOrderMatters() 0 14 1
A assertFromUrlBadReplace() 0 5 1
A assertFailingQuery() 0 9 2
A assertFromUrlBadInsert() 0 5 1
A assertFromUrlBadInsert2() 0 5 1
A assertFromUrlReplaceBadIndex() 0 5 1
A assertFromUrlReplaceBadReplacementString() 0 5 1
A assertFromUrlReplaceBadIndex3() 0 5 1
A assertFromUrlReplaceBadIndex4() 0 5 1
A assertFromUrlReplaceNoIndex() 0 5 1
A assertFromUrlReplaceValidEdgeIndex() 0 5 1
A assertFromUrlInsertBadIndex() 0 5 1
A assertFromUrlInsertMalformedIndex() 0 5 1
A assertFromUrlInsertBadIndex3() 0 5 1
A assertFromUrlInsertNoIndex() 0 5 1
A assertFromUrlInsertValidEdgeIndex() 0 5 1
A assertFromUrlWithReplace() 8 8 1
A assertFromUrlWithReplaceFullUrl() 9 9 1
A testSimpleToUrlOneReplace() 8 8 1
A testSimpleToUrlMultipleReplace() 9 9 1
A testSimpleToUrlReplaceWithEmpty() 8 8 1
A testSimpleToUrlMultipleReplaceWithEmpty() 9 9 1
A testSimpleToUrlMultipleReplaceOrderMatters() 0 8 1
A testToUrlWithReplace() 12 12 1
A testToUrlWithReplaceDash() 12 12 1
A testToUrlWithReplaceWithRefinement() 8 8 1
A setUpTestHeightCategoryAndSearch() 0 5 1
A testToUrlWithUnmappedRefinements() 0 15 1
A assertToAndFromUrlWithRefinementSpecificReplacements() 0 12 1
A assertToAndFromUrlWithMultipleRefinementSpecificReplacements() 0 14 1
A assertToAndFromUrlWithReplaceWithSpecialChar() 9 9 1
A assertToAndFromUrlWithReplaceWithSpecialChar2() 8 8 1
A assertToAndFromUrlWithReplaceWithRegexSpecialChar() 8 8 1
A assertToAndFromUrlWithReplaceWithRegexSpecialChar2() 8 8 1
A assertToAndFromUrlWithReplaceWithSameChar() 8 8 1
A assertToAndFromUrlWithNullSearchString() 8 8 1

How to fix   Duplicated Code    Complexity   

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:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like UrlBeautifierTest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use UrlBeautifierTest, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
use GroupByInc\API\Model\Navigation;
4
use GroupByInc\API\Url\Beauty;
5
use GroupByInc\API\Url\UrlBeautifier;
6
7
class UrlBeautifierTest 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...
8
{
9
    const DEFAULT_BEAUTIFIER = "default";
10
11
    /** @var UrlBeautifier */
12
    private $beautifier;
13
14
    public function setUp()
15
    {
16
        UrlBeautifier::createUrlBeautifier(self::DEFAULT_BEAUTIFIER);
17
        $this->beautifier = $this->getBeautifier(self::DEFAULT_BEAUTIFIER);
18
        $this->beautifier->clearSavedFields();
19
    }
20
21
    private function getBeautifier($identifier)
22
    {
23
        $beautifiers = UrlBeautifier::getUrlBeautifiers();
24
        return $beautifiers[$identifier];
25
    }
26
27
    public function testStopVowels()
28
    {
29
        try {
30
            $this->beautifier->addRefinementMapping("u", "test");
31
            $this->fail("Should throw exception for vowels");
32
        } catch (RuntimeException $e) {
33
            // expected
34
        }
35
36
        try {
37
            $this->beautifier->setSearchMapping("e");
38
            $this->fail("Should throw exception for vowels");
39
        } catch (RuntimeException $e) {
40
            // expected
41
        }
42
    }
43
44
    public function testMultipleBeautifiers()
45
    {
46
        $identifier = "default2";
47
        UrlBeautifier::createUrlBeautifier($identifier);
48
49
        $beautifier2 = $this->getBeautifier($identifier);
50
        $this->beautifier->addRefinementMapping("t", "test");
51
        $this->assertEquals("/value/t", $this->beautifier->toUrl(null, "test=value"));
52
        $this->assertEquals("?refinements=%7Etest%3Dvalue", $beautifier2->toUrl(null, "test=value"));
53
    }
54
55
    public function testQueryUrl()
56
    {
57
        $this->beautifier->setSearchMapping("q");
58
        $url = $this->beautifier->toUrl("this is a test", null);
59
        $this->assertEquals("/this+is+a+test/q", $url);
60
    }
61
62
    public function testRefinementsUrl()
63
    {
64
        $this->beautifier->addRefinementMapping("t", "test");
65
        $url = $this->beautifier->toUrl("", "test=value");
66
        $this->assertEquals("/value/t", $url);
67
    }
68
69
    public function testMultipleRefinements()
70
    {
71
        $this->setUpTestHeightAndCategoryRefinements();
72
        $url = $this->beautifier->toUrl("", "test=value~height=20in~category=computer accessories");
73
        $this->assertEquals("/value/20in/computer+accessories/thc", $url);
74
    }
75
76
    private function setUpTestHeightAndCategoryRefinements()
77
    {
78
        $this->beautifier->addRefinementMapping("t", "test")
79
            ->addRefinementMapping("h", "height")
80
            ->addRefinementMapping("c", "category");
81
    }
82
83
    public function testFullSearchUrl()
84
    {
85
        $this->beautifier->setSearchMapping("q")
86
            ->addRefinementMapping("t", "test");
87
        $url = $this->beautifier->toUrl("this is a test", "test=value");
88
        $this->assertEquals("/this+is+a+test/value/qt", $url);
89
    }
90
91
    public function testDetailQuery()
92
    {
93
        $query = $this->beautifier->fromUrl("http://example.com/details?p=4&id=243478931&b=test");
94
        $this->assertEquals("~id=243478931", $query->getRefinementString());
95
    }
96
97 View Code Duplication
    public function testSearchQuery()
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...
98
    {
99
        $this->beautifier->setSearchMapping("q")
100
            ->addRefinementMapping("t", "test");
101
        $query = $this->beautifier->fromUrl("http://example.com/this%20is%20a%20test/value/qt");
102
        $this->assertEquals("this is a test", $query->getQuery());
103
        $this->assertEquals("~test=value", $query->getRefinementString());
104
    }
105
106
    public function testInvalidReferenceBlock()
107
    {
108
        $query = $this->beautifier->fromUrl("http://example.com/this%20is%20a%20test/value/qtrs", null);
109
        $this->assertEquals(null, $query);
110
    }
111
112
    public function testRange()
113
    {
114
        $refinement = "test=bob~price:10..20";
115
        $expectedUrl = "/bob/t?refinements=%7Eprice%3A10..20";
116
117
        $this->beautifier->addRefinementMapping("t", "test");
118
        $this->assertEquals($expectedUrl, $this->beautifier->toUrl(null, $refinement));
119
        $this->beautifier->addRefinementMapping("p", "price");
120
        $actual = $this->beautifier->toUrl(null, $refinement);
121
        $this->assertEquals($expectedUrl, $actual);
122
    }
123
124 View Code Duplication
    public function testDeepSearchQuery()
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...
125
    {
126
        $this->beautifier->setSearchMapping("q")
127
            ->addRefinementMapping("t", "test");
128
        $query = $this->beautifier->fromUrl("http://example.com/path/to/search/this%20is%20a%20test/value/qt");
129
130
        $this->assertEquals("this is a test", $query->getQuery());
131
        $this->assertEquals("~test=value", $query->getRefinementString());
132
    }
133
134
    public function testSearchUrlBackAndForth()
135
    {
136
        $url = "/this%20is%20a%20test/value/qt";
137
        $this->beautifier->setSearchMapping("q")
138
            ->addRefinementMapping("t", "test");
139
140
        $query = $this->beautifier->fromUrl($url);
141
        $this->assertEquals("this is a test", $query->getQuery());
142
143
        /** @var Navigation[] $navigations */
144
        $navigations = array_values($query->getNavigations());
145
        $this->assertEquals("test", $navigations[0]->getName());
146
        /** @var SelectedRefinementValue $valueRefinement */
147
        $valueRefinement = $navigations[0]->getRefinements()[0];
148
        $this->assertEquals("value", $valueRefinement->getValue());
149
    }
150
151
    public function testExistingMapping()
152
    {
153
        try {
154
            $this->beautifier->setSearchMapping("q")
155
                ->addRefinementMapping("q", "quasorLightLevel");
156
            $this->fail("should throw exception");
157
        } catch (RuntimeException $e) {
158
            // expected
159
            $this->assertEquals("This token: q is already mapped to: search", $e->getMessage());
160
        }
161
    }
162
163
    public function testEmptyQueryString()
164
    {
165
        $this->beautifier->fromUrl("");
166
    }
167
168 View Code Duplication
    public function testAddSameRefinementMultipleTimes()
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...
169
    {
170
        $this->setUpTestHeightAndCategoryRefinements();
171
        $this->beautifier->setAppend(".html");
172
        $url = $this->beautifier->toUrl("",
173
            "test=value~test=value~test=value2~height=20in~category=computer+accessories");
174
        $this->assertEquals("/value/value2/20in/computer%2Baccessories/tthc.html", $url);
175
    }
176
177 View Code Duplication
    public function testAppend()
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...
178
    {
179
        $this->beautifier->setAppend(".html");
180
        $this->setUpTestHeightAndCategoryRefinements();
181
        $url = $this->beautifier->toUrl("", "test=value~height=20in~category=computer accessories");
182
        $this->assertEquals("/value/20in/computer+accessories/thc.html", $url);
183
    }
184
185 View Code Duplication
    public function testAppendWithSlash()
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...
186
    {
187
        $this->beautifier->setAppend("/index.html");
188
        $this->setUpTestHeightAndCategoryRefinements();
189
        $url = $this->beautifier->toUrl("", "test=value~height=20in~category=computer accessories");
190
        $this->assertEquals("/value/20in/computer+accessories/thc/index.html", $url);
191
    }
192
193 View Code Duplication
    public function testUnappend()
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...
194
    {
195
        $this->beautifier->setAppend(".html")
196
            ->addRefinementMapping("t", "test")
197
            ->addRefinementMapping("h", "height");
198
        $query = $this->beautifier->fromUrl("/value/20in/th.html");
199
200
        $navigations = array_values($query->getNavigations());
201
        $this->assertEquals(2, count($navigations));
202
        $this->assertNavigation("test", "=value", $navigations[0]);
203
        $this->assertNavigation("height", "=20in", $navigations[1]);
204
    }
205
206
    public function assertNavigation($expectedNavName, $expectedValue, Navigation $navigation)
207
    {
208
        $this->assertEquals($expectedNavName, $navigation->getName());
209
        $this->assertEquals($expectedValue, $navigation->getRefinements()[0]->toTildeString());
210
    }
211
212 View Code Duplication
    public function testUnappendWithSlash()
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...
213
    {
214
        $this->beautifier->setAppend("/index.html")
215
            ->addRefinementMapping("t", "test")
216
            ->addRefinementMapping("h", "height");
217
        $query = $this->beautifier->fromUrl("/value/20in/th/index.html");
218
219
        $navigations = array_values($query->getNavigations());
220
        $this->assertEquals(2, count($navigations));
221
        $this->assertNavigation("test", "=value", $navigations[0]);
222
        $this->assertNavigation("height", "=20in", $navigations[1]);
223
    }
224
225
    public function testUnmappedToUrl()
226
    {
227
        $this->setUpTestHeightAndCategoryRefinements();
228
        $url = $this->beautifier->toUrl("",
229
            "test=value~height=20in~category2=mice~cat3=wireless mice~category=computer accessories");
230
        $this->assertEquals(
231
            "/value/20in/computer+accessories/thc?refinements=%7Ecategory2%3Dmice%7Ecat3%3Dwireless+mice", $url);
232
    }
233
234 View Code Duplication
    public function testUnmappedToUrlWithModifiedName()
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...
235
    {
236
        $this->setUpTestHeightAndCategoryRefinements();
237
        $this->beautifier->setRefinementsQueryParamName("r");
238
        $url = $this->beautifier->toUrl("",
239
            "test=value~height=20in~category2=mice~cat3=wireless mice~category=computer accessories");
240
        $this->assertEquals("/value/20in/computer+accessories/thc?r=%7Ecategory2%3Dmice%7Ecat3%3Dwireless+mice", $url);
241
    }
242
243
    public function testUnmappedFromUrl()
244
    {
245
        $this->setUpTestHeightAndCategoryRefinements();
246
        $query = $this->beautifier->fromUrl(
247
            "/value/20in/computer%20accessories/thc?refinements=category2%3Dmice%7Ecat3%3Dwireless%20mice");
248
249
        $navigations = array_values($query->getNavigations());
250
        $this->assertEquals(5, count($navigations));
251
        $this->assertNavigation("test", "=value", $navigations[0]);
252
        $this->assertNavigation("height", "=20in", $navigations[1]);
253
        $this->assertNavigation("category", "=computer accessories", $navigations[2]);
254
        $this->assertNavigation("category2", "=mice", $navigations[3]);
255
        $this->assertNavigation("cat3", "=wireless mice", $navigations[4]);
256
    }
257
258
    public function testCanonical()
259
    {
260
        $this->setUpTestHeightAndCategoryRefinements();
261
        $this->assertEquals($this->beautifier->toUrl(null,
262
            "~height=20in~category2=mice~cat3=wireless mice~test=value~category=computer accessories"),
263
            $this->beautifier->toUrl(null,
264
                "~height=20in~category=computer accessories~test=value~category2=mice~cat3=wireless mice"));
265
    }
266
267
    public function testSearchWithSlash()
268
    {
269
        $this->beautifier->setSearchMapping("q");
270
        $this->assertEquals("/photo%2Fcommodity/q", $this->beautifier->toUrl("photo/commodity", null));
271
    }
272
273
    public function testRefinementWithSlash()
274
    {
275
        $this->beautifier->addRefinementMapping("t", "test");
276
        $this->assertEquals("/photo%2Fcommodity/t", $this->beautifier->toUrl(null, "test=photo/commodity"));
277
    }
278
279
    public function testUnencodePlus()
280
    {
281
        $this->beautifier->setSearchMapping("q")
282
            ->addRefinementMapping("d", "department")
283
            ->setAppend("/index.html");
284
        $this->assertFromUrl("/aoeu/laptop/MAGNOLIA+HOME+THEATRE/qd/index.html", null, "MAGNOLIA HOME THEATRE");
285
    }
286
287
    private function assertFromUrl($url, $searchString)
288
    {
289
        $query = $this->beautifier->fromUrl($url);
290
        if (!empty($searchString)) {
291
            $this->assertEquals($searchString, $query->getQuery());
292
        }
293 View Code Duplication
        if (func_num_args() > 2) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
294
            /** @var string[] $expectedRefinements */
295
            $expectedRefinements = array_slice(func_get_args(), 2);
296
            /** @var Navigation[] $navigations */
297
            $navigations = array_values($query->getNavigations());
298
            for ($i = 0; $i < count($expectedRefinements); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
299
                /** @var SelectedRefinementValue $valueRefinement */
300
                $valueRefinement = $navigations[$i]->getRefinements()[0];
301
                $this->assertEquals($expectedRefinements[$i], $valueRefinement->getValue());
302
            }
303
        }
304
    }
305
306
    public function assertFromUrlWithSlash()
307
    {
308
        $this->beautifier->setSearchMapping("q")
309
            ->addRefinementMapping("d", "department")
310
            ->setAppend("/index.html");
311
        $this->assertFromUrl("/taylor/PHOTO%252FCOMMODITIES/qd/index.html", null, "PHOTO/COMMODITIES");
312
    }
313
314
    public function assertFromUrlWithOneReplace()
315
    {
316
        $this->setSearchAndIndex();
317
        $this->beautifier->addReplacementRule("&", " ");
318
        $this->assertToAndFromUrl("black&decker", null);
319
    }
320
321
    private function setSearchAndIndex()
322
    {
323
        $this->beautifier->setSearchMapping("q")
324
            ->setAppend("/index.html");
325
    }
326
327
    private function assertToAndFromUrl($searchString, $refinementString)
328
    {
329
        $url = $this->beautifier->toUrl($searchString, $refinementString);
330
        $query = $this->beautifier->fromUrl($url);
331
        $this->assertEquals($searchString, $query->getQuery());
332 View Code Duplication
        if (func_num_args() > 2) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
333
            $expectedRefinements = array_slice(func_get_args(), 2);
334
            /** @var Navigation[] $navigations */
335
            $navigations = array_values($query->getNavigations());
336
            for ($i = 0; $i < count($expectedRefinements); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
337
                /** @var SelectedRefinementValue $valueRefinement */
338
                $valueRefinement = $navigations[$i]->getRefinements()[0];
339
                $this->assertEquals($expectedRefinements[$i], $valueRefinement->getValue());
340
            }
341
        }
342
    }
343
344
    public function assertFromUrlWithMultipleReplace()
345
    {
346
        $this->setSearchAndIndex();
347
        $this->beautifier->addReplacementRule("&", " ")
348
            ->addReplacementRule("B", "b")
349
            ->addReplacementRule("D", "d");
350
        $this->assertToAndFromUrl("Black&Decker", null);
351
    }
352
353
    public function assertFromUrlWithOneInsert()
354
    {
355
        $this->setSearchAndIndex();
356
        $this->assertFromUrl("/black+decker/q/index.html?z=i1-1", "1black decker");
357
    }
358
359
    public function assertFromUrlWithReplaceAndInsertionsOrderMatters()
360
    {
361
        $this->setSearchAndIndex();
362
        $this->beautifier->addReplacementRule("d", "D")
363
            ->addReplacementRule("1", null)
364
            ->addReplacementRule("2", null)
365
            ->addReplacementRule("3", null)
366
            ->addReplacementRule("&", " ")
367
            ->addReplacementRule("b", "B");
368
        $searchString = "123black&decker";
369
        $expected = "/Black+Decker/q";
370
        $this->assertEquals($expected, substr($this->beautifier->toUrl($searchString, null), 0, strlen($expected)));
371
        $this->assertToAndFromUrl($searchString, null);
372
    }
373
374
    public function assertFromUrlBadReplace()
375
    {
376
        $this->setSearchAndIndex();
377
        $this->assertFailingQuery("/black+decker/q/index.html?z=2-B--");
378
    }
379
380
    private function assertFailingQuery($uri)
381
    {
382
        try {
383
            $this->beautifier->fromUrl($uri);
384
            $this->fail("Expected an exception");
385
        } catch (RuntimeException $e) {
386
            // expected
387
        }
388
    }
389
390
    public function assertFromUrlBadInsert()
391
    {
392
        $this->setSearchAndIndex();
393
        $this->assertFailingQuery("/black+decker/q/index.html?z=c2-B");
394
    }
395
396
    public function assertFromUrlBadInsert2()
397
    {
398
        $this->setSearchAndIndex();
399
        $this->assertFailingQuery("/black+decker/q/index.html?z=ii2-B");
400
    }
401
402
    public function assertFromUrlReplaceBadIndex()
403
    {
404
        $this->setSearchAndIndex();
405
        $this->assertFromUrl("/black+decker/q/index.html?z=26-R", "black decker");
406
    }
407
408
    public function assertFromUrlReplaceBadReplacementString()
409
    {
410
        $this->setSearchAndIndex();
411
        $this->assertFailingQuery("/black+decker/q/index.html?z=-1-R");
412
    }
413
414
    public function assertFromUrlReplaceBadIndex3()
415
    {
416
        $this->setSearchAndIndex();
417
        $this->assertFromUrl("/black+decker/q/index.html?z=0-R", "black decker");
418
    }
419
420
    public function assertFromUrlReplaceBadIndex4()
421
    {
422
        $this->setSearchAndIndex();
423
        $this->assertFromUrl("/black+decker/q/index.html?z=13-R", "black decker");
424
    }
425
426
    public function assertFromUrlReplaceNoIndex()
427
    {
428
        $this->setSearchAndIndex();
429
        $this->assertFailingQuery("/black+decker/q/index.html?z=-R");
430
    }
431
432
    public function assertFromUrlReplaceValidEdgeIndex()
433
    {
434
        $this->setSearchAndIndex();
435
        $this->assertFromUrl("/black+decker/q/index.html?z=12-R", "black deckeR");
436
    }
437
438
    public function assertFromUrlInsertBadIndex()
439
    {
440
        $this->setSearchAndIndex();
441
        $this->assertFromUrl("/black+decker/q/index.html?z=i26-R", "black decker");
442
    }
443
444
    public function assertFromUrlInsertMalformedIndex()
445
    {
446
        $this->setSearchAndIndex();
447
        $this->assertFailingQuery("/black+decker/q/index.html?z=i-1-R");
448
    }
449
450
    public function assertFromUrlInsertBadIndex3()
451
    {
452
        $this->setSearchAndIndex();
453
        $this->assertFromUrl("/black+decker/q/index.html?z=i0-R", "black decker");
454
    }
455
456
    public function assertFromUrlInsertNoIndex()
457
    {
458
        $this->setSearchAndIndex();
459
        $this->assertFailingQuery("/black+decker/q/index.html?z=i-R");
460
    }
461
462
    public function assertFromUrlInsertValidEdgeIndex()
463
    {
464
        $this->setSearchAndIndex();
465
        $this->assertFromUrl("/black+decker/q/index.html?z=i13-R-6-%26", "black&deckerR");
466
    }
467
468 View Code Duplication
    public function assertFromUrlWithReplace()
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...
469
    {
470
        $this->beautifier->setSearchMapping("q")
471
            ->addRefinementMapping("d", "department")
472
            ->addRefinementMapping("c", "category")
473
            ->setAppend("/index.html");
474
        $this->assertFromUrl("/mice/wireless/dell/cdq/index.html?z=1-M-i14-123-18-D", "Dell", "Mice", "wireless123");
475
    }
476
477 View Code Duplication
    public function assertFromUrlWithReplaceFullUrl()
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...
478
    {
479
        $this->beautifier->setSearchMapping("q")
480
            ->addRefinementMapping("d", "department")
481
            ->addRefinementMapping("c", "category")
482
            ->setAppend("/index.html");
483
        $this->assertFromUrl("www.example.com/mice/wireless/dell/cdq/index.html?z=1-M-i14-123-18-D", "Dell", "Mice",
484
            "wireless123");
485
    }
486
487 View Code Duplication
    public function testSimpleToUrlOneReplace()
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...
488
    {
489
        $this->beautifier->setSearchMapping("q")
490
            ->addReplacementRule("/", "-");
491
        $searchString = "this is/a test";
492
        $this->assertEquals("/this+is-a+test/q?z=8-%2F", $this->beautifier->toUrl($searchString, null));
493
        $this->assertToAndFromUrl($searchString, null);
494
    }
495
496 View Code Duplication
    public function testSimpleToUrlMultipleReplace()
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...
497
    {
498
        $this->beautifier->setSearchMapping("q")
499
            ->addReplacementRule("/", "-")
500
            ->addReplacementRule("T", "t");
501
        $searchString = "This is/a Test";
502
        $this->assertEquals("/this+is-a+test/q?z=8-%2F-1-T-11-T", $this->beautifier->toUrl($searchString, null));
503
        $this->assertToAndFromUrl($searchString, null);
504
    }
505
506 View Code Duplication
    public function testSimpleToUrlReplaceWithEmpty()
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...
507
    {
508
        $this->beautifier->setSearchMapping("q")
509
            ->addReplacementRule("/", null);
510
        $searchString = "this is/a test";
511
        $this->assertEquals("/this+isa+test/q?z=i8-%2F", $this->beautifier->toUrl($searchString, null));
512
        $this->assertToAndFromUrl($searchString, null);
513
    }
514
515 View Code Duplication
    public function testSimpleToUrlMultipleReplaceWithEmpty()
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...
516
    {
517
        $this->beautifier->setSearchMapping("q")
518
            ->addReplacementRule("/", null)
519
            ->addReplacementRule("_", null);
520
        $searchString = "this _is/a _test";
521
        $this->assertEquals("/this+isa+test/q?z=i9-%2F-i6-_-i10-_", $this->beautifier->toUrl($searchString, null));
522
        $this->assertToAndFromUrl($searchString, null);
523
    }
524
525
    public function testSimpleToUrlMultipleReplaceOrderMatters()
526
    {
527
        $this->beautifier->setSearchMapping("q")
528
            ->addReplacementRule("a", null)
529
            ->addReplacementRule("/", "-")
530
            ->addReplacementRule("_", null);
531
        $this->assertToAndFromUrl("this _is/a _test", null);
532
    }
533
534 View Code Duplication
    public function testToUrlWithReplace()
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...
535
    {
536
        $this->setUpTestHeightAndCategoryRefinements();
537
        $this->beautifier->setSearchMapping("q")
538
            ->addReplacementRule("/", "-")
539
            ->addReplacementRule("&", null);
540
        $searchString = "test&query";
541
        $refinements = "test=value~height=20/in~category=computer accessories";
542
        $url = $this->beautifier->toUrl($searchString, $refinements);
543
        $this->assertEquals("/value/20-in/computer+accessories/testquery/thcq?z=9-%2F-i38-%26", $url);
544
        $this->assertToAndFromUrl($searchString, $refinements, "value", "20/in", "computer accessories");
545
    }
546
547 View Code Duplication
    public function testToUrlWithReplaceDash()
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...
548
    {
549
        $this->setUpTestHeightAndCategoryRefinements();
550
        $this->beautifier->setSearchMapping("q")
551
            ->addReplacementRule("-", " ")
552
            ->addReplacementRule("&", null);
553
        $searchString = "test&query";
554
        $refinements = "test=value~height=20-in~category=computer accessories";
555
        $url = $this->beautifier->toUrl($searchString, $refinements);
556
        $this->assertEquals("/value/20+in/computer+accessories/testquery/thcq?z=9---i38-%26", $url);
557
        $this->assertToAndFromUrl($searchString, $refinements, "value", "20-in", "computer accessories");
558
    }
559
560 View Code Duplication
    public function testToUrlWithReplaceWithRefinement()
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...
561
    {
562
        $this->setUpTestHeightCategoryAndSearch();
563
        $this->beautifier->addReplacementRule("/", " ")
564
            ->addReplacementRule("&", " ", Beauty::SEARCH_NAVIGATION_NAME);
565
        $refinements = "test=val&ue~height=20/in~category=computer accessories";
566
        $this->assertToAndFromUrl("test&query", $refinements, "val&ue", "20/in", "computer accessories");
567
    }
568
569
    private function setUpTestHeightCategoryAndSearch()
570
    {
571
        $this->beautifier->setSearchMapping("q");
572
        $this->setUpTestHeightAndCategoryRefinements();
573
    }
574
575
    public function testToUrlWithUnmappedRefinements()
576
    {
577
        $this->beautifier
578
            ->addRefinementMapping("h", "height")
579
            ->addRefinementMapping("c", "category")
580
            ->setSearchMapping("q")
581
            ->addReplacementRule("-", " ")
582
            ->addReplacementRule("&", null);
583
        $searchString = "test&query";
584
        $refinements = "test=value~height=20-in~category=computer accessories";
585
        $url = $this->beautifier->toUrl($searchString, $refinements);
586
        $this->assertEquals("/20+in/computer+accessories/testquery/hcq?z=3---i32-%26&refinements=%7Etest%3Dvalue",
587
            $url);
588
        $this->assertToAndFromUrl($searchString, $refinements, "20-in", "computer accessories", "value");
589
    }
590
591
    public function assertToAndFromUrlWithRefinementSpecificReplacements()
592
    {
593
        $this->setUpTestHeightCategoryAndSearch();
594
        $this->beautifier->setAppend("/index.html")
595
            ->addReplacementRule("&", " ", Beauty::SEARCH_NAVIGATION_NAME);
596
        $searchString = "test&query";
597
        $refinements = "test=val&ue~height=20-in~category=computer accessories";
598
        $url = $this->beautifier->toUrl($searchString, $refinements);
599
        $expected = "/test+query/val%2526ue";
600
        $this->assertEquals($expected, substr($url, 0, strlen($expected)));
601
        $this->assertToAndFromUrl($searchString, $refinements, "val&ue", "20-in", "computer accessories");
602
    }
603
604
    public function assertToAndFromUrlWithMultipleRefinementSpecificReplacements()
605
    {
606
        $this->setUpTestHeightCategoryAndSearch();
607
        $this->beautifier->setAppend("/index.html")
608
            ->addReplacementRule("&", " ", Beauty::SEARCH_NAVIGATION_NAME)
609
            ->addReplacementRule("i", "m", "height")
610
            ->addReplacementRule("e", "a", "category");
611
        $searchString = "test&query";
612
        $refinements = "test=val&ue~height=20-in~category=computer accessories";
613
        $url = $this->beautifier->toUrl($searchString, $refinements);
614
        $expected = "/test+query/val%2526ue/20-mn/computar+accassorias";
615
        $this->assertEquals($expected, substr($url, 0, strlen($expected)));
616
        $this->assertToAndFromUrl($searchString, $refinements, "val&ue", "20-in", "computer accessories");
617
    }
618
619 View Code Duplication
    public function assertToAndFromUrlWithReplaceWithSpecialChar()
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...
620
    {
621
        $this->setUpTestHeightCategoryAndSearch();
622
        $this->beautifier->setAppend("/index.html")
623
            ->addReplacementRule("e", "/")
624
            ->addReplacementRule("a", "\\");
625
        $refinements = "test=val&ue~height=20-in~category=computer accessories";
626
        $this->assertToAndFromUrl("test&query", $refinements, "val&ue", "20-in", "computer accessories");
627
    }
628
629 View Code Duplication
    public function assertToAndFromUrlWithReplaceWithSpecialChar2()
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...
630
    {
631
        $this->setUpTestHeightCategoryAndSearch();
632
        $this->beautifier->setAppend("/index.html")
633
            ->addReplacementRule("e", "%");
634
        $refinements = "test=val&ue~height=20-in~category=computer accessories";
635
        $this->assertToAndFromUrl("test&qu%ery", $refinements, "val&ue", "20-in", "computer accessories");
636
    }
637
638 View Code Duplication
    public function assertToAndFromUrlWithReplaceWithRegexSpecialChar()
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...
639
    {
640
        $this->setUpTestHeightCategoryAndSearch();
641
        $this->beautifier->setAppend("/index.html")
642
            ->addReplacementRule(".", "%");
643
        $refinements = "test=val&ue~height=20-in~category=computer accessories";
644
        $this->assertToAndFromUrl("test&qu%ery", $refinements, "val&ue", "20-in", "computer accessories");
645
    }
646
647 View Code Duplication
    public function assertToAndFromUrlWithReplaceWithRegexSpecialChar2()
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...
648
    {
649
        $this->setUpTestHeightCategoryAndSearch();
650
        $this->beautifier->setAppend("/index.html")
651
            ->addReplacementRule("e", ".");
652
        $refinements = "test=val&ue~height=20-in~category=computer accessories";
653
        $this->assertToAndFromUrl("test&qu%ery", $refinements, "val&ue", "20-in", "computer accessories");
654
    }
655
656 View Code Duplication
    public function assertToAndFromUrlWithReplaceWithSameChar()
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...
657
    {
658
        $this->setUpTestHeightCategoryAndSearch();
659
        $this->beautifier->setAppend("/index.html")
660
            ->addReplacementRule("e", "e");
661
        $refinements = "test=val&ue~height=20-in~category=computer accessories";
662
        $this->assertToAndFromUrl("test&qu%ery", $refinements, "val&ue", "20-in", "computer accessories");
663
    }
664
665 View Code Duplication
    public function assertToAndFromUrlWithNullSearchString()
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...
666
    {
667
        $this->setUpTestHeightCategoryAndSearch();
668
        $this->beautifier->setAppend("/index.html")
669
            ->addReplacementRule("e", "e");
670
        $refinements = "test=val&ue~height=20-in~category=computer accessories";
671
        $this->assertToAndFromUrl(null, $refinements, "val&ue", "20-in", "computer accessories");
672
    }
673
}
674