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.

SearchQueryTest   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 232
Duplicated Lines 12.07 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 14
c 0
b 0
f 0
lcom 1
cbo 2
dl 28
loc 232
rs 10

14 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 10 1
A it_can_handle_an_empty_query() 0 4 1
A it_can_handle_a_query_string() 0 7 1
A it_can_handle_a_location_aware_query() 0 14 1
A it_can_handle_a_location_aware_query_around_a_radius() 0 15 1
A it_can_handle_date_restrictions() 0 14 1
A it_can_handle_multiple_date_restrictions() 0 19 1
A it_can_handle_numeric_filters() 14 14 1
A it_can_handle_numeric_filters_with_an_and_relation() 14 14 1
A it_can_handle_a_search_facet() 0 14 1
A it_allows_hits_per_page_to_be_set() 0 8 1
A it_allows_hits_page_to_be_set() 0 10 1
A it_allows_method_chaining_for_multiple_filters() 0 23 1
A expectedResult() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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