|
1
|
|
|
<?php |
|
2
|
|
|
namespace Foolz\SphinxQL\Tests; |
|
3
|
|
|
|
|
4
|
|
|
use Foolz\SphinxQL\Exception\SphinxQLException; |
|
5
|
|
|
use Foolz\SphinxQL\Facet; |
|
6
|
|
|
|
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
|
8
|
|
|
|
|
9
|
|
|
class FacetTest extends TestCase |
|
10
|
|
|
{ |
|
11
|
|
|
|
|
12
|
|
|
// public static $DATA = [ |
|
13
|
|
|
// 0 => [ |
|
14
|
|
|
// 'id' => '10', |
|
15
|
|
|
// 'gid' => '9003', |
|
16
|
|
|
// 'title' => 'modifying the same line again', |
|
17
|
|
|
// 'content' => 'because i am that lazy', |
|
18
|
|
|
// ], |
|
19
|
|
|
// 1 => [ |
|
20
|
|
|
// 'id' => '11', |
|
21
|
|
|
// 'gid' => '201', |
|
22
|
|
|
// 'title' => 'replacing value by value', |
|
23
|
|
|
// 'content' => 'i have no idea who would use this directly', |
|
24
|
|
|
// ], |
|
25
|
|
|
// 2 => [ |
|
26
|
|
|
// 'id' => '12', |
|
27
|
|
|
// 'gid' => '200', |
|
28
|
|
|
// 'title' => 'simple logic', |
|
29
|
|
|
// 'content' => 'inside the box there was the content', |
|
30
|
|
|
// ], |
|
31
|
|
|
// 3 => [ |
|
32
|
|
|
// 'id' => '13', |
|
33
|
|
|
// 'gid' => '304', |
|
34
|
|
|
// 'title' => 'i am getting bored', |
|
35
|
|
|
// 'content' => 'with all this CONTENT', |
|
36
|
|
|
// ], |
|
37
|
|
|
// 4 => [ |
|
38
|
|
|
// 'id' => '14', |
|
39
|
|
|
// 'gid' => '304', |
|
40
|
|
|
// 'title' => 'i want a vacation', |
|
41
|
|
|
// 'content' => 'the code is going to break sometime', |
|
42
|
|
|
// ], |
|
43
|
|
|
// 5 => [ |
|
44
|
|
|
// 'id' => '15', |
|
45
|
|
|
// 'gid' => '304', |
|
46
|
|
|
// 'title' => 'there\'s no hope in this class', |
|
47
|
|
|
// 'content' => 'just give up', |
|
48
|
|
|
// ], |
|
49
|
|
|
// 6 => [ |
|
50
|
|
|
// 'id' => '16', |
|
51
|
|
|
// 'gid' => '500', |
|
52
|
|
|
// 'title' => 'we need to test', |
|
53
|
|
|
// 'content' => 'selecting the best result in groups', |
|
54
|
|
|
// ], |
|
55
|
|
|
// 7 => [ |
|
56
|
|
|
// 'id' => '17', |
|
57
|
|
|
// 'gid' => '500', |
|
58
|
|
|
// 'title' => 'what is there to do', |
|
59
|
|
|
// 'content' => 'we need to create dummy data for tests', |
|
60
|
|
|
// ], |
|
61
|
|
|
// ]; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @return Facet |
|
65
|
|
|
*/ |
|
66
|
|
|
protected function createFacet(): Facet |
|
67
|
|
|
{ |
|
68
|
|
|
return new Facet(null); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @throws SphinxQLException |
|
73
|
|
|
*/ |
|
74
|
|
|
public function testFacet(): void |
|
75
|
|
|
{ |
|
76
|
|
|
$facet = $this->createFacet() |
|
77
|
|
|
->facet(['gid']) |
|
78
|
|
|
->getFacet(); |
|
79
|
|
|
|
|
80
|
|
|
$this->assertEquals('FACET gid', $facet); |
|
81
|
|
|
|
|
82
|
|
|
$facet = $this->createFacet() |
|
83
|
|
|
->facet(['gid', 'title', 'content']) |
|
84
|
|
|
->getFacet(); |
|
85
|
|
|
|
|
86
|
|
|
$this->assertEquals('FACET gid, title, content', $facet); |
|
87
|
|
|
|
|
88
|
|
|
$facet = $this->createFacet() |
|
89
|
|
|
->facet('gid', 'title', 'content') |
|
90
|
|
|
->getFacet(); |
|
91
|
|
|
|
|
92
|
|
|
$this->assertEquals('FACET gid, title, content', $facet); |
|
93
|
|
|
|
|
94
|
|
|
$facet = $this->createFacet() |
|
95
|
|
|
->facet(['aliAS' => 'gid']) |
|
96
|
|
|
->getFacet(); |
|
97
|
|
|
|
|
98
|
|
|
$this->assertEquals('FACET gid AS aliAS', $facet); |
|
99
|
|
|
|
|
100
|
|
|
$facet = $this->createFacet() |
|
101
|
|
|
->facet(['gid', 'name' => 'title', 'content']) |
|
102
|
|
|
->getFacet(); |
|
103
|
|
|
|
|
104
|
|
|
$this->assertEquals('FACET gid, title AS name, content', $facet); |
|
105
|
|
|
|
|
106
|
|
|
$facet = new Facet(); |
|
107
|
|
|
$facet = $facet |
|
108
|
|
|
->facet('gid', ['name' => 'title'], 'content') |
|
109
|
|
|
->getFacet(); |
|
110
|
|
|
|
|
111
|
|
|
$this->assertEquals('FACET gid, title AS name, content', $facet); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @throws SphinxQLException |
|
116
|
|
|
*/ |
|
117
|
|
|
public function testFacetFunction(): void |
|
118
|
|
|
{ |
|
119
|
|
|
$facet = $this->createFacet() |
|
120
|
|
|
->facetFunction('INTERVAL', ['price', 200, 400, 600, 800]) |
|
121
|
|
|
->getFacet(); |
|
122
|
|
|
|
|
123
|
|
|
$this->assertEquals('FACET INTERVAL(price,200,400,600,800)', $facet); |
|
124
|
|
|
|
|
125
|
|
|
$facet = $this->createFacet() |
|
126
|
|
|
->facetFunction('COUNT', 'gid') |
|
127
|
|
|
->getFacet(); |
|
128
|
|
|
|
|
129
|
|
|
$this->assertEquals('FACET COUNT(gid)', $facet); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @throws SphinxQLException |
|
134
|
|
|
*/ |
|
135
|
|
|
public function testBy(): void |
|
136
|
|
|
{ |
|
137
|
|
|
$facet = $this->createFacet() |
|
138
|
|
|
->facet(['gid', 'title', 'content']) |
|
139
|
|
|
->by('gid') |
|
140
|
|
|
->getFacet(); |
|
141
|
|
|
|
|
142
|
|
|
$this->assertEquals('FACET gid, title, content BY gid', $facet); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* @throws SphinxQLException |
|
147
|
|
|
*/ |
|
148
|
|
|
public function testOrderBy(): void |
|
149
|
|
|
{ |
|
150
|
|
|
$facet = $this->createFacet() |
|
151
|
|
|
->facet(['gid', 'title']) |
|
152
|
|
|
->orderBy('gid', 'DESC') |
|
153
|
|
|
->getFacet(); |
|
154
|
|
|
|
|
155
|
|
|
$this->assertEquals('FACET gid, title ORDER BY gid DESC', $facet); |
|
156
|
|
|
|
|
157
|
|
|
$facet = $this->createFacet() |
|
158
|
|
|
->facet(['gid', 'content']) |
|
159
|
|
|
->orderBy('gid', 'ASC') |
|
160
|
|
|
->orderBy('content', 'DESC') |
|
161
|
|
|
->getFacet(); |
|
162
|
|
|
|
|
163
|
|
|
$this->assertEquals('FACET gid, content ORDER BY gid ASC, content DESC', $facet); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* @throws SphinxQLException |
|
168
|
|
|
*/ |
|
169
|
|
|
public function testOrderByFunction(): void |
|
170
|
|
|
{ |
|
171
|
|
|
$facet = $this->createFacet() |
|
172
|
|
|
->facet(['gid', 'title']) |
|
173
|
|
|
->orderByFunction('COUNT', '*', 'DESC') |
|
174
|
|
|
->getFacet(); |
|
175
|
|
|
|
|
176
|
|
|
$this->assertEquals('FACET gid, title ORDER BY COUNT(*) DESC', $facet); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* @throws SphinxQLException |
|
181
|
|
|
*/ |
|
182
|
|
|
public function testLimit(): void |
|
183
|
|
|
{ |
|
184
|
|
|
$facet = $this->createFacet() |
|
185
|
|
|
->facet(['gid', 'title']) |
|
186
|
|
|
->orderByFunction('COUNT', '*', 'DESC') |
|
187
|
|
|
->limit(5, 5) |
|
188
|
|
|
->getFacet(); |
|
189
|
|
|
|
|
190
|
|
|
$this->assertEquals('FACET gid, title ORDER BY COUNT(*) DESC LIMIT 5, 5', $facet); |
|
191
|
|
|
} |
|
192
|
|
|
} |
|
193
|
|
|
|