Completed
Pull Request — master (#1964)
by Sergiu
03:24 queued 01:02
created

testPhraseSuggestWithBackwardsCompatibility()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19

Duplication

Lines 19
Ratio 100 %

Importance

Changes 0
Metric Value
dl 19
loc 19
rs 9.6333
c 0
b 0
f 0
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\Suggest;
8
use Elastica\Suggest\CandidateGenerator\DirectGenerator;
9
use Elastica\Suggest\Phrase;
10
use Elastica\Test\Base as BaseTest;
11
12
/**
13
 * @internal
14
 */
15
class PhraseTest extends BaseTest
16
{
17
    /**
18
     * @group unit
19
     */
20
    public function testToArray(): void
21
    {
22
        $suggest = new Suggest();
23
        $phraseSuggest = new Phrase('suggest1', 'text');
24
        $phraseSuggest->setText('elasticsearch is bansai coor');
25
        $phraseSuggest->setAnalyzer('simple');
26
        $suggest->addSuggestion($phraseSuggest);
27
        $suggest->setGlobalText('global!');
28
29
        $expected = [
30
            'suggest' => [
31
                'text' => 'global!',
32
                'suggest1' => [
33
                    'text' => 'elasticsearch is bansai coor',
34
                    'phrase' => [
35
                        'field' => 'text',
36
                        'analyzer' => 'simple',
37
                    ],
38
                ],
39
            ],
40
        ];
41
42
        $this->assertEquals($expected, $suggest->toArray());
43
    }
44
45
    /**
46
     * @group functional
47
     */
48 View Code Duplication
    public function testPhraseSuggest(): 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...
49
    {
50
        $suggest = new Suggest();
51
        $phraseSuggest = new Phrase('suggest1', 'text');
52
        $phraseSuggest->setText('elasticsearch is bansai coor');
53
        $phraseSuggest->setAnalyzer('simple')->setHighlight('<suggest>', '</suggest>')->setStupidBackoffSmoothing(0.4);
54
        $phraseSuggest->addDirectGenerator(new DirectGenerator('text'));
55
        $suggest->addSuggestion($phraseSuggest);
56
57
        $index = $this->_getIndexForTest();
58
        $result = $index->search($suggest);
0 ignored issues
show
Documentation introduced by
$suggest is of type object<Elastica\Suggest>, but the function expects a string|array|object<Elastica\Query>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
59
        $suggests = $result->getSuggests();
60
61
        // 3 suggestions should be returned: One in which both misspellings are corrected, and two in which only one misspelling is corrected.
62
        $this->assertCount(3, $suggests['suggest1'][0]['options']);
63
64
        $this->assertEquals('elasticsearch is <suggest>bonsai cool</suggest>', $suggests['suggest1'][0]['options'][0]['highlighted']);
65
        $this->assertEquals('elasticsearch is bonsai cool', $suggests['suggest1'][0]['options'][0]['text']);
66
    }
67
68
    /**
69
     * @group functional
70
     */
71 View Code Duplication
    public function testPhraseSuggestWithBackwardsCompatibility(): 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...
72
    {
73
        $suggest = new Suggest();
74
        $phraseSuggest = new Phrase('suggest1', 'text');
75
        $phraseSuggest->setText('elasticsearch is bansai coor');
76
        $phraseSuggest->setAnalyzer('simple')->setHighlight('<suggest>', '</suggest>')->setStupidBackoffSmoothing(0.4);
77
        $phraseSuggest->addCandidateGenerator(new DirectGenerator('text'));
0 ignored issues
show
Deprecated Code introduced by
The method Elastica\Suggest\Phrase::addCandidateGenerator() has been deprecated with message: since version 7.2.0, use the "addDirectGenerator()" method instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
78
        $suggest->addSuggestion($phraseSuggest);
79
80
        $index = $this->_getIndexForTest();
81
        $result = $index->search($suggest);
0 ignored issues
show
Documentation introduced by
$suggest is of type object<Elastica\Suggest>, but the function expects a string|array|object<Elastica\Query>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
82
        $suggests = $result->getSuggests();
83
84
        // 3 suggestions should be returned: One in which both misspellings are corrected, and two in which only one misspelling is corrected.
85
        $this->assertCount(3, $suggests['suggest1'][0]['options']);
86
87
        $this->assertEquals('elasticsearch is <suggest>bonsai cool</suggest>', $suggests['suggest1'][0]['options'][0]['highlighted']);
88
        $this->assertEquals('elasticsearch is bonsai cool', $suggests['suggest1'][0]['options'][0]['text']);
89
    }
90
91
    protected function _getIndexForTest(): Index
92
    {
93
        $index = $this->_createIndex();
94
        $index->addDocuments([
95
            new Document(1, ['text' => 'Github is pretty cool']),
96
            new Document(2, ['text' => 'Elasticsearch is bonsai cool']),
97
            new Document(3, ['text' => 'This is a test phrase']),
98
            new Document(4, ['text' => 'Another sentence for testing']),
99
            new Document(5, ['text' => 'Some more words here']),
100
        ]);
101
        $index->refresh();
102
103
        return $index;
104
    }
105
}
106