Completed
Push — master ( 0d45ed...ea3502 )
by Nicolas
02:38
created

tests/Query/DisMaxTest.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Elastica\Test\Query;
4
5
use Elastica\Document;
6
use Elastica\Query\DisMax;
7
use Elastica\Query\Ids;
8
use Elastica\Query\QueryString;
9
use Elastica\Test\Base as BaseTest;
10
11
/**
12
 * @internal
13
 */
14
class DisMaxTest extends BaseTest
15
{
16
    /**
17
     * @group unit
18
     */
19
    public function testToArray(): void
20
    {
21
        $query = new DisMax();
22
23
        $idsQuery1 = new Ids();
24
        $idsQuery1->setIds(1);
25
26
        $idsQuery2 = new Ids();
27
        $idsQuery2->setIds(2);
28
29
        $idsQuery3 = new Ids();
30
        $idsQuery3->setIds(3);
31
32
        $boost = 1.2;
33
        $tieBreaker = 0.7;
34
35
        $query->setBoost($boost);
36
        $query->setTieBreaker($tieBreaker);
37
        $query->addQuery($idsQuery1);
38
        $query->addQuery($idsQuery2);
39
        $query->addQuery($idsQuery3->toArray());
40
41
        $expectedArray = [
42
            'dis_max' => [
43
                'tie_breaker' => $tieBreaker,
44
                'boost' => $boost,
45
                'queries' => [
46
                    $idsQuery1->toArray(),
47
                    $idsQuery2->toArray(),
48
                    $idsQuery3->toArray(),
49
                ],
50
            ],
51
        ];
52
53
        $this->assertEquals($expectedArray, $query->toArray());
54
    }
55
56
    /**
57
     * @group functional
58
     */
59
    public function testQuery(): void
60
    {
61
        $index = $this->_createIndex();
62
63
        $index->addDocuments([
64
            new Document(1, ['name' => 'Basel-Stadt']),
65
            new Document(2, ['name' => 'New York']),
66
            new Document(3, ['name' => 'Baden']),
67
            new Document(4, ['name' => 'Baden Baden']),
68
        ]);
69
70
        $index->refresh();
71
72
        $queryString1 = new QueryString('Bade*');
73
        $queryString2 = new QueryString('Base*');
74
75
        $boost = 1.2;
76
        $tieBreaker = 0.5;
77
78
        $query = new DisMax();
79
        $query->setBoost($boost);
80
        $query->setTieBreaker($tieBreaker);
81
        $query->addQuery($queryString1);
82
        $query->addQuery($queryString2);
83
        $resultSet = $index->search($query);
0 ignored issues
show
$query is of type object<Elastica\Query\DisMax>, 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...
84
85
        $this->assertEquals(3, $resultSet->count());
86
    }
87
88
    /**
89
     * @group functional
90
     */
91
    public function testQueryArray(): void
92
    {
93
        $index = $this->_createIndex();
94
95
        $index->addDocuments([
96
            new Document(1, ['name' => 'Basel-Stadt']),
97
            new Document(2, ['name' => 'New York']),
98
            new Document(3, ['name' => 'Baden']),
99
            new Document(4, ['name' => 'Baden Baden']),
100
        ]);
101
102
        $index->refresh();
103
104
        $queryString1 = ['query_string' => [
105
            'query' => 'Bade*',
106
        ],
107
        ];
108
109
        $queryString2 = ['query_string' => [
110
            'query' => 'Base*',
111
        ],
112
        ];
113
114
        $boost = 1.2;
115
        $tieBreaker = 0.5;
116
117
        $query = new DisMax();
118
        $query->setBoost($boost);
119
        $query->setTieBreaker($tieBreaker);
120
        $query->addQuery($queryString1);
121
        $query->addQuery($queryString2);
122
        $resultSet = $index->search($query);
0 ignored issues
show
$query is of type object<Elastica\Query\DisMax>, 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...
123
124
        $this->assertEquals(3, $resultSet->count());
125
    }
126
}
127