Completed
Push — master ( 59583f...a32345 )
by Nicolas
02:30
created

TermsTest::testVariousDataTypesViaAddTerm()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 9.488
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
10
/**
11
 * @internal
12
 * @group unit
13
 */
14
class TermsTest extends BaseTest
15
{
16
    public function testSetTermsLookup(): void
17
    {
18
        $terms = [
19
            'index' => 'index_name',
20
            'id' => '1',
21
            'path' => 'terms',
22
        ];
23
24
        $query = new Terms('name');
25
        $query->setTermsLookup('index_name', '1', 'terms');
26
27
        $data = $query->toArray();
28
        $this->assertEquals($terms, $data['terms']['name']);
29
    }
30
31
    public function testInvalidParams(): void
32
    {
33
        $query = new Terms('field', ['aaa', 'bbb']);
34
        $query->setTermsLookup('index', '1', 'path');
35
36
        $this->expectException(InvalidException::class);
37
        $query->toArray();
38
    }
39
40
    public function testEmptyField(): void
41
    {
42
        $this->expectException(InvalidException::class);
43
        new Terms('');
44
    }
45
46
    /**
47
     * @group functional
48
     */
49
    public function testFilteredSearch(): void
50
    {
51
        $index = $this->_createIndex();
52
53
        $index->addDocuments([
54
            new Document(1, ['name' => 'hello world']),
55
            new Document(2, ['name' => 'nicolas ruflin']),
56
            new Document(3, ['name' => 'ruflin']),
57
        ]);
58
59
        $query = new Terms('name', ['nicolas', 'hello']);
60
61
        $index->refresh();
62
        $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...
63
64
        $this->assertEquals(2, $resultSet->count());
65
66
        $query->addTerm('ruflin');
67
        $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...
68
69
        $this->assertEquals(3, $resultSet->count());
70
    }
71
72
    /**
73
     * @group functional
74
     */
75
    public function testFilteredSearchWithLookup(): void
76
    {
77
        $index = $this->_createIndex();
78
79
        $lookupIndex = $this->_createIndex('lookup_index');
80
        $lookupIndex->addDocuments([
81
            new Document(1, ['terms' => ['ruflin', 'nicolas']]),
82
        ]);
83
84
        $index->addDocuments([
85
            new Document(1, ['name' => 'hello world']),
86
            new Document(2, ['name' => 'nicolas ruflin']),
87
            new Document(3, ['name' => 'ruflin']),
88
        ]);
89
90
        $query = new Terms('name');
91
        $query->setTermsLookup($lookupIndex->getName(), '1', 'terms');
92
        $index->refresh();
93
        $lookupIndex->refresh();
94
95
        $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...
96
97
        $this->assertEquals(2, $resultSet->count());
98
    }
99
100
    /**
101
     * @group functional
102
     */
103 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...
104
    {
105
        $index = $this->_createIndex();
106
107
        $index->addDocuments([
108
            new Document(1, ['some_numeric_field' => 9876]),
109
        ]);
110
        $index->refresh();
111
112
        // string
113
        $query = new Terms('some_numeric_field', ['9876']);
114
        $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...
115
        $this->assertEquals(1, $resultSet->count());
116
117
        // int
118
        $query = new Terms('some_numeric_field', [9876]);
119
        $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...
120
        $this->assertEquals(1, $resultSet->count());
121
122
        // float
123
        $query = new Terms('some_numeric_field', [9876.0]);
124
        $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...
125
        $this->assertEquals(1, $resultSet->count());
126
    }
127
128
    /**
129
     * @group functional
130
     */
131
    public function testVariousMixedDataTypesViaConstructor(): void
132
    {
133
        $index = $this->_createIndex();
134
135
        $index->addDocuments([
136
            new Document(1, ['some_numeric_field' => 9876]),
137
            new Document(2, ['some_numeric_field' => 5678]),
138
            new Document(3, ['some_numeric_field' => 1234]),
139
            new Document(4, ['some_numeric_field' => 8899]),
140
        ]);
141
        $index->refresh();
142
143
        $query = new Terms('some_numeric_field', ['9876', 1234, 5678.0]);
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(3, $resultSet->count());
146
    }
147
148
    /**
149
     * @group functional
150
     */
151
    public function testVariousDataTypesViaAddTerm(): void
152
    {
153
        $index = $this->_createIndex();
154
155
        $index->addDocuments([
156
            new Document(1, ['some_numeric_field' => 9876]),
157
        ]);
158
        $index->refresh();
159
160
        // string
161
        $query = new Terms('some_numeric_field');
162
        $query->addTerm('9876');
163
        $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...
164
        $this->assertEquals(1, $resultSet->count());
165
166
        // int
167
        $query = new Terms('some_numeric_field');
168
        $query->addTerm(9876);
169
        $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...
170
        $this->assertEquals(1, $resultSet->count());
171
172
        // float
173
        $query = new Terms('some_numeric_field');
174
        $query->addTerm(9876.0);
175
        $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...
176
        $this->assertEquals(1, $resultSet->count());
177
    }
178
179
    public function testAddTermTypeError(): void
180
    {
181
        $this->expectException(\TypeError::class);
182
        $this->expectExceptionMessage('Argument 1 passed to "Elastica\Query\Terms::addTerm()" must be of type float|int|string, stdClass given.');
183
184
        $query = new Terms('some_numeric_field');
185
        $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...
186
    }
187
}
188