1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
require_once __DIR__ . '/Expectation/Object.php'; |
4
|
|
|
|
5
|
|
|
use GroupByInc\API\Query; |
6
|
|
|
use GroupByInc\API\Model\Sort; |
7
|
|
|
use Httpful\Response; |
8
|
|
|
|
9
|
|
|
class QueryTest extends PHPUnit_Framework_TestCase |
|
|
|
|
10
|
|
|
{ |
11
|
|
|
private static $QUERY; |
12
|
|
|
private static $REFINEMENTS_QUERY; |
13
|
|
|
private static $SORT; |
14
|
|
|
|
15
|
|
|
public static function init() |
16
|
|
|
{ |
17
|
|
|
self::$QUERY = '{"clientKey":"XXXX-XXXX-XXXX-XXXX","collection":"testproducts","area":"Production",' . |
18
|
|
|
'"biasingProfile":"testProfile","language":"en","query":"the","sort":[{"field":"price","order":"Descending"}],' . |
19
|
|
|
'"fields":["brand","category","height"],"orFields":["price","color"],"refinements":[{"navigationName":"green",' . |
20
|
|
|
'"exclude":true,"high":"delicious","low":"atrocious","type":"Range"},{"navigationName":"green","exclude":false,' . |
21
|
|
|
'"value":"malaise","type":"Value"}],"customUrlParams":[{"key":"guava","value":"mango"}],"skip":20,"pageSize":14,' . |
22
|
|
|
'"disableAutocorrection":true,"pruneRefinements":false,"wildcardSearchEnabled":true}'; |
23
|
|
|
|
24
|
|
|
self::$REFINEMENTS_QUERY = '{"originalQuery":' . self::$QUERY . ',"navigationName":"height"}'; |
25
|
|
|
|
26
|
|
|
self::$SORT = new Sort(); |
27
|
|
|
self::$SORT->setOrder(Sort\Order::Descending) |
28
|
|
|
->setField("price"); |
29
|
|
|
|
30
|
|
|
} |
31
|
|
|
|
32
|
|
View Code Duplication |
public function testSerializeQuery() |
|
|
|
|
33
|
|
|
{ |
34
|
|
|
$query = new Query(); |
35
|
|
|
$query->setQuery('the'); |
36
|
|
|
$query->setSort(array(self::$SORT)); |
37
|
|
|
$query->setSkip(20); |
38
|
|
|
$query->setPageSize(14); |
39
|
|
|
$query->setCollection('testproducts'); |
40
|
|
|
$query->setArea('Production'); |
41
|
|
|
$query->setBiasingProfile('testProfile'); |
42
|
|
|
$query->setLanguage('en'); |
43
|
|
|
$query->setPruneRefinements(false); |
44
|
|
|
$query->setDisableAutocorrection(true); |
45
|
|
|
$query->setCustomUrlParams(array(Object::$CUSTOM_URL_PARAM)); |
46
|
|
|
$query->setNavigations(array(Object::$NAVIGATION)); |
47
|
|
|
$query->addFields(array("brand", "category", "height")); |
48
|
|
|
$query->addOrFields(array("price", "color")); |
49
|
|
|
$query->setWildcardSearchEnabled(true); |
50
|
|
|
|
51
|
|
|
$json = $query->getBridgeJson('XXXX-XXXX-XXXX-XXXX'); |
52
|
|
|
$this->assertEquals(self::$QUERY, $json); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
View Code Duplication |
public function testSerializeRefinementsQuery() |
|
|
|
|
56
|
|
|
{ |
57
|
|
|
$query = new Query(); |
58
|
|
|
$query->setQuery('the'); |
59
|
|
|
$query->setSort(array(self::$SORT)); |
60
|
|
|
$query->setSkip(20); |
61
|
|
|
$query->setPageSize(14); |
62
|
|
|
$query->setCollection('testproducts'); |
63
|
|
|
$query->setArea('Production'); |
64
|
|
|
$query->setBiasingProfile('testProfile'); |
65
|
|
|
$query->setLanguage('en'); |
66
|
|
|
$query->setPruneRefinements(false); |
67
|
|
|
$query->setDisableAutocorrection(true); |
68
|
|
|
$query->setCustomUrlParams(array(Object::$CUSTOM_URL_PARAM)); |
69
|
|
|
$query->setNavigations(array(Object::$NAVIGATION)); |
70
|
|
|
$query->addFields(array("brand", "category", "height")); |
71
|
|
|
$query->addOrFields(array("price", "color")); |
72
|
|
|
$query->setWildcardSearchEnabled(true); |
73
|
|
|
|
74
|
|
|
$json = $query->getBridgeRefinementsJson('XXXX-XXXX-XXXX-XXXX', 'height'); |
75
|
|
|
$this->assertEquals(self::$REFINEMENTS_QUERY, $json); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
|
79
|
|
|
public function testSplitRange() |
80
|
|
|
{ |
81
|
|
|
$query = new Query(); |
82
|
|
|
$split = $query->splitRefinements("test=bob~price:10..20"); |
83
|
|
|
$this->assertEquals(["test=bob", "price:10..20"], $split); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function testSplitNoCategory() |
87
|
|
|
{ |
88
|
|
|
$query = new Query(); |
89
|
|
|
$split = $query->splitRefinements("~gender=Women~simpleColorDesc=Pink~product=Clothing"); |
90
|
|
|
$this->assertEquals(["gender=Women", "simpleColorDesc=Pink", "product=Clothing"], $split); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function testSplitCategory() |
94
|
|
|
{ |
95
|
|
|
$query = new Query(); |
96
|
|
|
$split = $query->splitRefinements("~category_leaf_expanded=Category Root~Athletics~Men's~Sneakers"); |
97
|
|
|
$this->assertEquals(["category_leaf_expanded=Category Root~Athletics~Men's~Sneakers"], $split); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function testSplitMultipleCategory() |
101
|
|
|
{ |
102
|
|
|
$query = new Query(); |
103
|
|
|
$split = $query->splitRefinements("~category_leaf_expanded=Category Root~Athletics~Men's~Sneakers~category_leaf_id=580003"); |
104
|
|
|
$this->assertEquals(["category_leaf_expanded=Category Root~Athletics~Men's~Sneakers", "category_leaf_id=580003"], $split); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
View Code Duplication |
public function testSplitRangeAndMultipleCategory() |
|
|
|
|
108
|
|
|
{ |
109
|
|
|
$query = new Query(); |
110
|
|
|
$split = $query->splitRefinements("test=bob~price:10..20~category_leaf_expanded=Category Root~Athletics~Men's" . |
111
|
|
|
"~Sneakers~category_leaf_id=580003~color=BLUE~color=YELLOW~color=GREY"); |
112
|
|
|
$this->assertEquals(["test=bob", "price:10..20", |
113
|
|
|
"category_leaf_expanded=Category Root~Athletics~Men's~Sneakers", "category_leaf_id=580003", |
114
|
|
|
"color=BLUE", "color=YELLOW", "color=GREY"], $split); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
View Code Duplication |
public function testSplitCategoryLong() |
|
|
|
|
118
|
|
|
{ |
119
|
|
|
$query = new Query(); |
120
|
|
|
$split = $query->splitRefinements("~category_leaf_expanded=Category Root~Athletics~Men's~Sneakers~category_leaf_id=580003~" . |
121
|
|
|
"color=BLUE~color=YELLOW~color=GREY~feature=Lace Up~feature=Light Weight~brand=Nike"); |
122
|
|
|
$this->assertEquals(["category_leaf_expanded=Category Root~Athletics~Men's~Sneakers", "category_leaf_id=580003", |
123
|
|
|
"color=BLUE", "color=YELLOW", "color=GREY", "feature=Lace Up", "feature=Light Weight", |
124
|
|
|
"brand=Nike"], $split); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function testNull() |
128
|
|
|
{ |
129
|
|
|
$query = new Query(); |
130
|
|
|
$split = $query->splitRefinements(null); |
131
|
|
|
$this->assertEquals([], $split); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function testEmpty() |
135
|
|
|
{ |
136
|
|
|
$query = new Query(); |
137
|
|
|
$split = $query->splitRefinements(""); |
138
|
|
|
$this->assertEquals([], $split); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public function testUtf8() |
142
|
|
|
{ |
143
|
|
|
$query = new Query(); |
144
|
|
|
$split = $query->splitRefinements("tëst=bäb~price:10..20"); |
145
|
|
|
$this->assertEquals(["tëst=bäb", "price:10..20"], $split); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
QueryTest::init(); |
150
|
|
|
|
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.