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