GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( caf19e...cd31e6 )
by Freek
03:19
created

SearchQueryTest::it_allows_hits_page_to_be_set()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 10
rs 9.4285
cc 1
eloc 6
nc 1
nop 0
1
<?php
2
3
namespace Spatie\SearchIndex\Test\Query\Algolia;
4
5
use DateTime;
6
use Spatie\SearchIndex\Query\Algolia\SearchQuery;
7
8
class SearchQueryTest extends \PHPUnit_Framework_TestCase
9
{
10
    /**
11
     * @var SearchQuery
12
     */
13
    protected $query;
14
15
    /**
16
     * @var array
17
     */
18
    protected $defaultResult;
19
20
    public function setUp()
21
    {
22
        $this->query = new SearchQuery();
23
        $this->defaultResult = [
24
            'numericFilters' => '',
25
            'facetFilters' => '',
26
            'hitsPerPage' => 10000,
27
            'page' => 0,
28
        ];
29
    }
30
31
    /**
32
     * @test
33
     */
34
    public function it_can_handle_an_empty_query()
35
    {
36
        $this->assertEquals($this->defaultResult, $this->query->toArray());
37
    }
38
39
    /**
40
     * @test
41
     */
42
    public function it_can_handle_a_query_string()
43
    {
44
        $this->query
45
            ->searchFor('hello');
46
47
        $this->assertEquals($this->expectedResult(['query' => 'hello']), $this->query->toArray());
48
    }
49
50
    /**
51
     * @test
52
     */
53
    public function it_can_handle_a_location_aware_query()
54
    {
55
        $lat = 1.234567890;
56
        $lng = 7.891012345;
57
58
        $this->query->aroundLocation($lat, $lng);
59
60
        $this->assertEquals($this->expectedResult(
61
            [
62
                'aroundLatLng' => $lat.','.$lng,
63
                'aroundRadius' => 30000,
64
            ]
65
        ), $this->query->toArray());
66
    }
67
68
    /**
69
     * @test
70
     */
71
    public function it_can_handle_a_location_aware_query_around_a_radius()
72
    {
73
        $lat = 1.234567890;
74
        $lng = 7.891012345;
75
        $radius = 12345;
76
77
        $this->query->aroundLocation($lat, $lng, $radius);
78
79
        $this->assertEquals($this->expectedResult(
80
            [
81
                'aroundLatLng' => $lat.','.$lng,
82
                'aroundRadius' => $radius,
83
            ]
84
        ), $this->query->toArray());
85
    }
86
87
    /**
88
     * @test
89
     */
90
    public function it_can_handle_date_restrictions()
91
    {
92
        $dateFieldName = 'myDate';
93
        $operation = '>';
94
        $date = new DateTime();
95
96
        $this->query->withDateRestriction($dateFieldName, $operation, $date);
97
98
        $this->assertEquals($this->expectedResult(
99
            [
100
                'numericFilters' => ",{$dateFieldName}{$operation}{$date->getTimestamp()}",
101
            ]
102
        ), $this->query->toArray());
103
    }
104
105
    /**
106
     * @test
107
     */
108
    public function it_can_handle_multiple_date_restrictions()
109
    {
110
        $dateFieldName = 'myDate';
111
        $operation = '>';
112
        $date = new DateTime();
113
114
        $otherDateFieldName = 'otherDate';
115
        $otherOperation = '<';
116
        $otherDate = new DateTime();
117
118
        $this->query->withDateRestriction($dateFieldName, $operation, $date);
119
        $this->query->withDateRestriction($otherDateFieldName, $otherOperation, $otherDate);
120
121
        $this->assertEquals($this->expectedResult(
122
            [
123
                'numericFilters' => ",{$dateFieldName}{$operation}{$date->getTimestamp()},{$otherDateFieldName}{$otherOperation}{$otherDate->getTimestamp()}",
124
            ]
125
        ), $this->query->toArray());
126
    }
127
128
    /**
129
     * @test
130
     */
131 View Code Duplication
    public function it_can_handle_numeric_filters()
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...
132
    {
133
        $name = 'myFilter';
134
        $myValues = [1, 2, 3];
135
        $logicalOperator = SearchQuery::LOGICAL_OPERATOR_OR;
136
137
        $this->query->withNumericFilter($name, $myValues, $logicalOperator);
138
139
        $this->assertEquals($this->expectedResult(
140
            [
141
                'numericFilters' => ",({$name}={$myValues[0]},{$name}={$myValues[1]},{$name}={$myValues[2]})",
142
            ]
143
        ), $this->query->toArray());
144
    }
145
146
    /**
147
     * @test
148
     */
149 View Code Duplication
    public function it_can_handle_numeric_filters_with_an_and_relation()
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...
150
    {
151
        $name = 'myFilter';
152
        $myValues = [1, 2, 3];
153
        $logicalOperator = SearchQuery::LOGICAL_OPERATOR_AND;
154
155
        $this->query->withNumericFilter($name, $myValues, $logicalOperator);
156
157
        $this->assertEquals($this->expectedResult(
158
            [
159
                'numericFilters' => ",{$name}={$myValues[0]},{$name}={$myValues[1]},{$name}={$myValues[2]}",
160
            ]
161
        ), $this->query->toArray());
162
    }
163
164
    /**
165
     * @test
166
     */
167
    public function it_can_handle_a_search_facet()
168
    {
169
        $names = ['facet1', 'facet2'];
170
        $values = ['value1', 'value2'];
171
172
        $this->query->withFacet($names[0], $values[0]);
173
        $this->query->withFacet($names[1], $values[1]);
174
175
        $this->assertEquals($this->expectedResult(
176
            [
177
                'facetFilters' => ",{$names[0]}:{$values[0]},{$names[1]}:{$values[1]}",
178
            ]
179
        ), $this->query->toArray());
180
    }
181
182
    /**
183
     * @test
184
     */
185
    public function it_allows_hits_per_page_to_be_set()
186
    {
187
        $hitsPerPage = 12345;
188
189
        $this->query->setHitsPerPage($hitsPerPage);
190
191
        $this->assertEquals($this->expectedResult(['hitsPerPage' => $hitsPerPage]), $this->query->toArray());
192
    }
193
194
    /**
195
     * @test
196
     */
197
    public function it_allows_hits_page_to_be_set()
198
    {
199
        $hitsPerPage = 8;
200
        $page = 2;
201
202
        $this->query->setHitsPerPage($hitsPerPage);
203
        $this->query->setPage($page);
204
205
        $this->assertEquals($this->expectedResult(['hitsPerPage' => $hitsPerPage, 'page' => $page]), $this->query->toArray());
206
    }
207
208
    /**
209
     * @test
210
     */
211
    public function it_allows_method_chaining_for_multiple_filters()
212
    {
213
        $facetFilter = ['name', 'value'];
214
215
        $numericFilterName = 'myFilter';
216
        $numericFilterValues = [1, 2, 3];
217
        $logicalOperator = SearchQuery::LOGICAL_OPERATOR_OR;
218
219
        $dateFieldName = 'myDate';
220
        $operation = '>';
221
        $date = new DateTime();
222
223
        $this->query->withFacet('name', 'value')
224
                    ->withDateRestriction($dateFieldName, $operation, $date)
225
                    ->withNumericFilter($numericFilterName, $numericFilterValues, $logicalOperator);
226
227
        $this->assertEquals($this->expectedResult(
228
            [
229
                'facetFilters' => ",{$facetFilter[0]}:{$facetFilter[1]}",
230
                'numericFilters' => ",{$dateFieldName}{$operation}{$date->getTimestamp()},({$numericFilterName}={$numericFilterValues[0]},{$numericFilterName}={$numericFilterValues[1]},{$numericFilterName}={$numericFilterValues[2]})",
231
            ]
232
        ), $this->query->toArray());
233
    }
234
235
    protected function expectedResult(array $expectedResult)
236
    {
237
        return array_merge($this->defaultResult, $expectedResult);
238
    }
239
}
240