Completed
Push — master ( c22a1e...f63d17 )
by François-Xavier
02:39
created

testPhraseSuggestWithBackwardsCompatibility()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21

Duplication

Lines 21
Ratio 100 %

Importance

Changes 0
Metric Value
dl 21
loc 21
rs 9.584
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
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
12
13
/**
14
 * @internal
15
 */
16
class PhraseTest extends BaseTest
17
{
18
    use ExpectDeprecationTrait;
19
20
    /**
21
     * @group unit
22
     */
23
    public function testToArray(): void
24
    {
25
        $suggest = new Suggest();
26
        $phraseSuggest = new Phrase('suggest1', 'text');
27
        $phraseSuggest->setText('elasticsearch is bansai coor');
28
        $phraseSuggest->setAnalyzer('simple');
29
        $suggest->addSuggestion($phraseSuggest);
30
        $suggest->setGlobalText('global!');
31
32
        $expected = [
33
            'suggest' => [
34
                'text' => 'global!',
35
                'suggest1' => [
36
                    'text' => 'elasticsearch is bansai coor',
37
                    'phrase' => [
38
                        'field' => 'text',
39
                        'analyzer' => 'simple',
40
                    ],
41
                ],
42
            ],
43
        ];
44
45
        $this->assertEquals($expected, $suggest->toArray());
46
    }
47
48
    /**
49
     * @group functional
50
     */
51 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...
52
    {
53
        $suggest = new Suggest();
54
        $phraseSuggest = new Phrase('suggest1', 'text');
55
        $phraseSuggest->setText('elasticsearch is bansai coor');
56
        $phraseSuggest->setAnalyzer('simple')->setHighlight('<suggest>', '</suggest>')->setStupidBackoffSmoothing(0.4);
57
        $phraseSuggest->addDirectGenerator(new DirectGenerator('text'));
58
        $suggest->addSuggestion($phraseSuggest);
59
60
        $index = $this->_getIndexForTest();
61
        $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...
62
        $suggests = $result->getSuggests();
63
64
        // 3 suggestions should be returned: One in which both misspellings are corrected, and two in which only one misspelling is corrected.
65
        $this->assertCount(3, $suggests['suggest1'][0]['options']);
66
67
        $this->assertEquals('elasticsearch is <suggest>bonsai cool</suggest>', $suggests['suggest1'][0]['options'][0]['highlighted']);
68
        $this->assertEquals('elasticsearch is bonsai cool', $suggests['suggest1'][0]['options'][0]['text']);
69
    }
70
71
    /**
72
     * @group functional
73
     *
74
     * @group legacy
75
     */
76 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...
77
    {
78
        $suggest = new Suggest();
79
        $phraseSuggest = new Phrase('suggest1', 'text');
80
        $phraseSuggest->setText('elasticsearch is bansai coor');
81
        $phraseSuggest->setAnalyzer('simple')->setHighlight('<suggest>', '</suggest>')->setStupidBackoffSmoothing(0.4);
82
83
        $this->expectDeprecation('Since ruflin/elastica 7.2.0: The "Elastica\Suggest\Phrase::addCandidateGenerator()" method is deprecated, use the "addDirectGenerator()" method instead. It will be removed in 8.0.');
84
        $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...
85
        $suggest->addSuggestion($phraseSuggest);
86
87
        $index = $this->_getIndexForTest();
88
        $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...
89
        $suggests = $result->getSuggests();
90
91
        // 3 suggestions should be returned: One in which both misspellings are corrected, and two in which only one misspelling is corrected.
92
        $this->assertCount(3, $suggests['suggest1'][0]['options']);
93
94
        $this->assertEquals('elasticsearch is <suggest>bonsai cool</suggest>', $suggests['suggest1'][0]['options'][0]['highlighted']);
95
        $this->assertEquals('elasticsearch is bonsai cool', $suggests['suggest1'][0]['options'][0]['text']);
96
    }
97
98
    protected function _getIndexForTest(): Index
99
    {
100
        $index = $this->_createIndex();
101
        $index->addDocuments([
102
            new Document(1, ['text' => 'Github is pretty cool']),
103
            new Document(2, ['text' => 'Elasticsearch is bonsai cool']),
104
            new Document(3, ['text' => 'This is a test phrase']),
105
            new Document(4, ['text' => 'Another sentence for testing']),
106
            new Document(5, ['text' => 'Some more words here']),
107
        ]);
108
        $index->refresh();
109
110
        return $index;
111
    }
112
}
113