Completed
Push — master ( 9146c8...39ecb4 )
by Nicolas
04:52
created

CompletionTest::testToArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
c 0
b 0
f 0
rs 9.7998
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Elastica\Test\Suggest;
4
5
use Elastica\Document;
6
use Elastica\Index;
7
use Elastica\Mapping;
8
use Elastica\Query;
9
use Elastica\Suggest;
10
use Elastica\Suggest\Completion;
11
use Elastica\Test\Base as BaseTest;
12
13
/**
14
 * @internal
15
 */
16
class CompletionTest extends BaseTest
17
{
18
    /**
19
     * @group unit
20
     */
21
    public function testToArray(): void
22
    {
23
        $suggest = new Completion('suggestName', 'fieldName');
24
        $suggest->setPrefix('foo');
25
        $suggest->setSize(10);
26
        $expected = [
27
            'prefix' => 'foo',
28
            'completion' => [
29
                'size' => 10,
30
                'field' => 'fieldName',
31
            ],
32
        ];
33
        $this->assertEquals($expected, $suggest->toArray());
34
    }
35
36
    /**
37
     * @group functional
38
     */
39
    public function testSuggestWorks(): void
40
    {
41
        $suggest = new Completion('suggestName', 'fieldName');
42
        $suggest->setPrefix('Never');
43
44
        $index = $this->_getIndexForTest();
45
        $resultSet = $index->search(Query::create($suggest));
46
47
        $this->assertTrue($resultSet->hasSuggests());
48
49
        $suggests = $resultSet->getSuggests();
50
        $options = $suggests['suggestName'][0]['options'];
51
52
        $this->assertCount(1, $options);
53
        $this->assertEquals('Nevermind', $options[0]['text']);
54
    }
55
56
    /**
57
     * @group functional
58
     */
59 View Code Duplication
    public function testFuzzySuggestWorks(): void
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...
60
    {
61
        $suggest = new Completion('suggestName', 'fieldName');
62
        $suggest->setFuzzy(['fuzziness' => 2]);
63
        $suggest->setPrefix('Neavermint');
64
65
        $index = $this->_getIndexForTest();
66
        $resultSet = $index->search(Query::create($suggest));
67
68
        $this->assertTrue($resultSet->hasSuggests());
69
70
        $suggests = $resultSet->getSuggests();
71
        $options = $suggests['suggestName'][0]['options'];
72
73
        $this->assertCount(1, $options);
74
        $this->assertEquals('Nevermind', $options[0]['text']);
75
    }
76
77
    /**
78
     * @group functional
79
     */
80
    public function testCompletion(): void
81
    {
82
        $suggest = new Completion('suggestName1', 'fieldName');
83
        $suggest->setPrefix('Neavermint');
84
85
        $suggest2 = new Completion('suggestName2', 'fieldName2');
86
        $suggest2->setPrefix('Neverdint');
87
88
        $sug = new Suggest();
89
        $sug->addSuggestion($suggest);
90
        $sug->addSuggestion($suggest2);
91
        $index = $this->_getIndexForTest();
92
        $query = Query::create($sug);
93
94
        $expectedSuggestions = [
95
            'suggestName1' => [
96
                0 => [
97
                    'text' => 'Neavermint',
98
                    'offset' => 0,
99
                    'length' => 10,
100
                    'options' => [],
101
                ],
102
            ],
103
            'suggestName2' => [
104
                0 => [
105
                    'text' => 'Neverdint',
106
                    'offset' => 0,
107
                    'length' => 9,
108
                    'options' => [],
109
                ],
110
            ],
111
        ];
112
113
        $resultSet = $index->search($query);
114
115
        $this->assertTrue($resultSet->hasSuggests());
116
        $this->assertEquals($expectedSuggestions, $resultSet->getSuggests());
117
    }
118
119
    /**
120
     * @group unit
121
     */
122
    public function testSetFuzzy(): void
123
    {
124
        $suggest = new Completion('suggestName', 'fieldName');
125
126
        $fuzzy = [
127
            'unicode_aware' => true,
128
            'fuzziness' => 3,
129
        ];
130
131
        $suggest->setFuzzy($fuzzy);
132
133
        $this->assertEquals($fuzzy, $suggest->getParam('fuzzy'));
134
135
        $this->assertInstanceOf(Completion::class, $suggest->setFuzzy($fuzzy));
136
    }
137
138
    /**
139
     * @group functional
140
     */
141 View Code Duplication
    public function testRegexSuggestWorks(): void
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...
142
    {
143
        $suggest = new Completion('suggestName', 'fieldName');
144
        $suggest->setRegex('n[ever|i]r');
145
        $suggest->setRegexOptions(['flags' => 'ANYSTRING', 'max_determinized_states' => 20000]);
146
        $index = $this->_getIndexForTest();
147
        $resultSet = $index->search(Query::create($suggest));
148
        $this->assertTrue($resultSet->hasSuggests());
149
        $suggests = $resultSet->getSuggests();
150
        $options = $suggests['suggestName'][0]['options'];
151
        $this->assertCount(3, $options);
152
    }
153
154
    /**
155
     * @return Index
156
     */
157
    protected function _getIndexForTest()
158
    {
159
        $index = $this->_createIndex();
160
        $index->setMapping(new Mapping([
161
            'fieldName' => [
162
                'type' => 'completion',
163
            ],
164
            'fieldName2' => [
165
                'type' => 'completion',
166
            ],
167
        ]));
168
169
        $index->addDocuments([
170
            new Document(1, [
171
                'fieldName' => [
172
                    'input' => ['Nevermind', 'Nirvana'],
173
                    'weight' => 5,
174
                ],
175
            ]),
176
            new Document(2, [
177
                'fieldName' => [
178
                    'input' => ['Bleach', 'Nirvana'],
179
                    'weight' => 2,
180
                ],
181
            ]),
182
            new Document(3, [
183
                'fieldName' => [
184
                    'input' => ['Incesticide', 'Nirvana'],
185
                    'weight' => 7,
186
                ],
187
            ]),
188
            new Document(4, [
189
                'fieldName2' => [
190
                    'input' => ['Bleach', 'Nirvana'],
191
                    'weight' => 3,
192
                ],
193
            ]),
194
            new Document(5, [
195
                'fieldName2' => [
196
                    'input' => ['Incesticide', 'Nirvana'],
197
                    'weight' => 3,
198
                ],
199
            ]),
200
        ]);
201
202
        $index->refresh();
203
204
        return $index;
205
    }
206
}
207