Completed
Pull Request — master (#1872)
by
unknown
02:35
created

TermsTest::testSetTermsLookup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Elastica\Test\Query;
4
5
use Elastica\Document;
6
use Elastica\Exception\InvalidException;
7
use Elastica\Query\Terms;
8
use Elastica\Test\Base as BaseTest;
9
use TypeError;
10
11
/**
12
 * @internal
13
 * @group unit
14
 */
15
class TermsTest extends BaseTest
16
{
17
    public function testSetTermsLookup(): void
18
    {
19
        $terms = [
20
            'index' => 'index_name',
21
            'id' => '1',
22
            'path' => 'terms',
23
        ];
24
25
        $query = new Terms('name');
26
        $query->setTermsLookup('index_name', '1', 'terms');
27
28
        $data = $query->toArray();
29
        $this->assertEquals($terms, $data['terms']['name']);
30
    }
31
32
    public function testInvalidParams(): void
33
    {
34
        $query = new Terms('field', ['aaa', 'bbb']);
35
        $query->setTermsLookup('index', '1', 'path');
36
37
        $this->expectException(InvalidException::class);
38
        $query->toArray();
39
    }
40
41
    public function testEmptyField(): void
42
    {
43
        $this->expectException(InvalidException::class);
44
        new Terms('');
45
    }
46
47
    /**
48
     * @group functional
49
     */
50
    public function testFilteredSearch(): void
51
    {
52
        $index = $this->_createIndex();
53
54
        $index->addDocuments([
55
            new Document(1, ['name' => 'hello world']),
56
            new Document(2, ['name' => 'nicolas ruflin']),
57
            new Document(3, ['name' => 'ruflin']),
58
        ]);
59
60
        $query = new Terms('name', ['nicolas', 'hello']);
61
62
        $index->refresh();
63
        $resultSet = $index->search($query);
0 ignored issues
show
Documentation introduced by
$query is of type object<Elastica\Query\Terms>, 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...
64
65
        $this->assertEquals(2, $resultSet->count());
66
67
        $query->addTerm('ruflin');
68
        $resultSet = $index->search($query);
0 ignored issues
show
Documentation introduced by
$query is of type object<Elastica\Query\Terms>, 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...
69
70
        $this->assertEquals(3, $resultSet->count());
71
    }
72
73
    /**
74
     * @group functional
75
     */
76
    public function testFilteredSearchWithLookup(): void
77
    {
78
        $index = $this->_createIndex();
79
80
        $lookupIndex = $this->_createIndex('lookup_index');
81
        $lookupIndex->addDocuments([
82
            new Document(1, ['terms' => ['ruflin', 'nicolas']]),
83
        ]);
84
85
        $index->addDocuments([
86
            new Document(1, ['name' => 'hello world']),
87
            new Document(2, ['name' => 'nicolas ruflin']),
88
            new Document(3, ['name' => 'ruflin']),
89
        ]);
90
91
        $query = new Terms('name');
92
        $query->setTermsLookup($lookupIndex->getName(), '1', 'terms');
93
        $index->refresh();
94
        $lookupIndex->refresh();
95
96
        $resultSet = $index->search($query);
0 ignored issues
show
Documentation introduced by
$query is of type object<Elastica\Query\Terms>, 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...
97
98
        $this->assertEquals(2, $resultSet->count());
99
    }
100
101
    /**
102
     * @group functional
103
     */
104 View Code Duplication
    public function testVariousDataTypesViaConstructor(): 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...
105
    {
106
        $index = $this->_createIndex();
107
108
        $index->addDocuments([
109
            new Document(1, ['some_numeric_field' => 9876]),
110
        ]);
111
        $index->refresh();
112
113
        // string
114
        $query = new Terms('some_numeric_field', ['9876']);
115
        $resultSet = $index->search($query);
0 ignored issues
show
Documentation introduced by
$query is of type object<Elastica\Query\Terms>, 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...
116
        $this->assertEquals(1, $resultSet->count());
117
118
        // int
119
        $query = new Terms('some_numeric_field', [9876]);
120
        $resultSet = $index->search($query);
0 ignored issues
show
Documentation introduced by
$query is of type object<Elastica\Query\Terms>, 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...
121
        $this->assertEquals(1, $resultSet->count());
122
123
        // float
124
        $query = new Terms('some_numeric_field', [9876.0]);
125
        $resultSet = $index->search($query);
0 ignored issues
show
Documentation introduced by
$query is of type object<Elastica\Query\Terms>, 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...
126
        $this->assertEquals(1, $resultSet->count());
127
    }
128
129
    /**
130
     * @group functional
131
     */
132
    public function testVariousDataTypesViaAddTerm(): void
133
    {
134
        $index = $this->_createIndex();
135
136
        $index->addDocuments([
137
            new Document(1, ['some_numeric_field' => 9876]),
138
        ]);
139
        $index->refresh();
140
141
        // string
142
        $query = new Terms('some_numeric_field');
143
        $query->addTerm('9876');
144
        $resultSet = $index->search($query);
0 ignored issues
show
Documentation introduced by
$query is of type object<Elastica\Query\Terms>, 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...
145
        $this->assertEquals(1, $resultSet->count());
146
147
        // int
148
        $query = new Terms('some_numeric_field');
149
        $query->addTerm(9876);
150
        $resultSet = $index->search($query);
0 ignored issues
show
Documentation introduced by
$query is of type object<Elastica\Query\Terms>, 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...
151
        $this->assertEquals(1, $resultSet->count());
152
153
        // float
154
        $query = new Terms('some_numeric_field');
155
        $query->addTerm(9876.0);
156
        $resultSet = $index->search($query);
0 ignored issues
show
Documentation introduced by
$query is of type object<Elastica\Query\Terms>, 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...
157
        $this->assertEquals(1, $resultSet->count());
158
    }
159
160
    public function testAddTermTypeError(): void
161
    {
162
        $this->expectException(TypeError::class);
163
        $this->expectExceptionMessage('Argument 1 passed to "Elastica\Query\Terms::addTerm()" must be of type float|int|string, stdClass given.');
164
165
        $query = new Terms('some_numeric_field');
166
        $query->addTerm(new \stdClass());
0 ignored issues
show
Documentation introduced by
new \stdClass() is of type object<stdClass>, but the function expects a double|integer|string.

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...
167
    }
168
}
169