Completed
Pull Request — master (#1872)
by
unknown
02:26
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 testVariousDataTypesViaAddTerm(): void
132
    {
133
        $index = $this->_createIndex();
134
135
        $index->addDocuments([
136
            new Document(1, ['some_numeric_field' => 9876]),
137
        ]);
138
        $index->refresh();
139
140
        // string
141
        $query = new Terms('some_numeric_field');
142
        $query->addTerm('9876');
143
        $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...
144
        $this->assertEquals(1, $resultSet->count());
145
146
        // int
147
        $query = new Terms('some_numeric_field');
148
        $query->addTerm(9876);
149
        $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...
150
        $this->assertEquals(1, $resultSet->count());
151
152
        // float
153
        $query = new Terms('some_numeric_field');
154
        $query->addTerm(9876.0);
155
        $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...
156
        $this->assertEquals(1, $resultSet->count());
157
    }
158
}
159