1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Foolz\SphinxQL\Facet; |
4
|
|
|
use Foolz\SphinxQL\Tests\TestUtil; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* @package Foolz\SphinxQL |
8
|
|
|
* @author Vicent Valls |
9
|
|
|
*/ |
10
|
|
|
class FacetTest extends \PHPUnit\Framework\TestCase |
|
|
|
|
11
|
|
|
{ |
12
|
|
|
public static $conn = null; |
13
|
|
|
|
14
|
|
|
public static $data = array ( |
15
|
|
|
0 => array('id' => '10', 'gid' => '9003', |
16
|
|
|
'title' => 'modifying the same line again', 'content' => 'because i am that lazy'), |
17
|
|
|
1 => array('id' => '11', 'gid' => '201', |
18
|
|
|
'title' => 'replacing value by value', 'content' => 'i have no idea who would use this directly'), |
19
|
|
|
2 => array('id' => '12', 'gid' => '200', |
20
|
|
|
'title' => 'simple logic', 'content' => 'inside the box there was the content'), |
21
|
|
|
3 => array('id' => '13', 'gid' => '304', |
22
|
|
|
'title' => 'i am getting bored', 'content' => 'with all this CONTENT'), |
23
|
|
|
4 => array('id' => '14', 'gid' => '304', |
24
|
|
|
'title' => 'i want a vacation', 'content' => 'the code is going to break sometime'), |
25
|
|
|
5 => array('id' => '15', 'gid' => '304', |
26
|
|
|
'title' => 'there\'s no hope in this class', 'content' => 'just give up'), |
27
|
|
|
6 => array('id' => '16', 'gid' => '500', |
28
|
|
|
'title' => 'we need to test', 'content' => 'selecting the best result in groups'), |
29
|
|
|
7 => array('id' => '17', 'gid' => '500', |
30
|
|
|
'title' => 'what is there to do', 'content' => 'we need to create dummy data for tests'), |
31
|
|
|
); |
32
|
|
|
|
33
|
|
|
public static function setUpBeforeClass() |
34
|
|
|
{ |
35
|
|
|
$conn = TestUtil::getConnectionDriver(); |
36
|
|
|
$conn->setParam('port', 9307); |
37
|
|
|
self::$conn = $conn; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @return Facet |
42
|
|
|
*/ |
43
|
|
|
protected function createFacet() |
44
|
|
|
{ |
45
|
|
|
return new Facet(self::$conn); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function testFacet() |
49
|
|
|
{ |
50
|
|
|
$facet = $this->createFacet() |
51
|
|
|
->facet(array('gid')) |
52
|
|
|
->getFacet(); |
53
|
|
|
|
54
|
|
|
$this->assertEquals('FACET gid', $facet); |
55
|
|
|
|
56
|
|
|
$facet = $this->createFacet() |
57
|
|
|
->facet(array('gid', 'title', 'content')) |
58
|
|
|
->getFacet(); |
59
|
|
|
|
60
|
|
|
$this->assertEquals('FACET gid, title, content', $facet); |
61
|
|
|
|
62
|
|
|
$facet = $this->createFacet() |
63
|
|
|
->facet('gid', 'title', 'content') |
64
|
|
|
->getFacet(); |
65
|
|
|
|
66
|
|
|
$this->assertEquals('FACET gid, title, content', $facet); |
67
|
|
|
|
68
|
|
|
$facet = $this->createFacet() |
69
|
|
|
->facet(array('aliAS' => 'gid')) |
70
|
|
|
->getFacet(); |
71
|
|
|
|
72
|
|
|
$this->assertEquals('FACET gid AS aliAS', $facet); |
73
|
|
|
|
74
|
|
|
$facet = $this->createFacet() |
75
|
|
|
->facet(array('gid', 'name' => 'title', 'content')) |
76
|
|
|
->getFacet(); |
77
|
|
|
|
78
|
|
|
$this->assertEquals('FACET gid, title AS name, content', $facet); |
79
|
|
|
|
80
|
|
|
$facet = new Facet(); |
81
|
|
|
$facet = $facet |
82
|
|
|
->setConnection(self::$conn) |
83
|
|
|
->facet('gid', array('name' => 'title'), 'content') |
84
|
|
|
->getFacet(); |
85
|
|
|
|
86
|
|
|
$this->assertEquals('FACET gid, title AS name, content', $facet); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function testFacetFunction() |
90
|
|
|
{ |
91
|
|
|
$facet = $this->createFacet() |
92
|
|
|
->facetFunction('INTERVAL', array('price', 200, 400, 600, 800)) |
93
|
|
|
->getFacet(); |
94
|
|
|
|
95
|
|
|
$this->assertEquals('FACET INTERVAL(price,200,400,600,800)', $facet); |
96
|
|
|
|
97
|
|
|
$facet = $this->createFacet() |
98
|
|
|
->facetFunction('COUNT', 'gid') |
99
|
|
|
->getFacet(); |
100
|
|
|
|
101
|
|
|
$this->assertEquals('FACET COUNT(gid)', $facet); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function testBy() |
105
|
|
|
{ |
106
|
|
|
$facet = $this->createFacet() |
107
|
|
|
->facet(array('gid', 'title', 'content')) |
108
|
|
|
->by('gid') |
109
|
|
|
->getFacet(); |
110
|
|
|
|
111
|
|
|
$this->assertEquals('FACET gid, title, content BY gid', $facet); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function testOrderBy() |
115
|
|
|
{ |
116
|
|
|
$facet = $this->createFacet() |
117
|
|
|
->facet(array('gid', 'title')) |
118
|
|
|
->orderBy('gid', 'DESC') |
119
|
|
|
->getFacet(); |
120
|
|
|
|
121
|
|
|
$this->assertEquals('FACET gid, title ORDER BY gid DESC', $facet); |
122
|
|
|
|
123
|
|
|
$facet = $this->createFacet() |
124
|
|
|
->facet(array('gid', 'content')) |
125
|
|
|
->orderBy('gid', 'ASC') |
126
|
|
|
->orderBy('content', 'DESC') |
127
|
|
|
->getFacet(); |
128
|
|
|
|
129
|
|
|
$this->assertEquals('FACET gid, content ORDER BY gid ASC, content DESC', $facet); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function testOrderByFunction() |
133
|
|
|
{ |
134
|
|
|
$facet = $this->createFacet() |
135
|
|
|
->facet(array('gid', 'title')) |
136
|
|
|
->orderByFunction('COUNT','*', 'DESC') |
|
|
|
|
137
|
|
|
->getFacet(); |
138
|
|
|
|
139
|
|
|
$this->assertEquals('FACET gid, title ORDER BY COUNT(*) DESC', $facet); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function testLimit() |
143
|
|
|
{ |
144
|
|
|
$facet = $this->createFacet() |
145
|
|
|
->facet(array('gid', 'title')) |
146
|
|
|
->orderByFunction('COUNT', '*', 'DESC') |
|
|
|
|
147
|
|
|
->limit(5, 5) |
148
|
|
|
->getFacet(); |
149
|
|
|
|
150
|
|
|
$this->assertEquals('FACET gid, title ORDER BY COUNT(*) DESC LIMIT 5, 5', $facet); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.