Completed
Push — master ( 62d180...80157d )
by Ema
02:38
created

TermsTest::testEmptyField()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
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 = Terms::buildTermsLookup('name', 'index_name', '1', 'terms');
25
26
        $data = $query->toArray();
27
        $this->assertEquals($terms, $data['terms']['name']);
28
    }
29
30
    public function testInvalidParams(): void
31
    {
32
        $query = new Terms('field', ['aaa', 'bbb']);
33
        $query->setTermsLookup('index', '1', 'path');
34
35
        $this->expectException(InvalidException::class);
36
        $query->toArray();
37
    }
38
39
    public function testEmptyField(): void
40
    {
41
        $this->expectException(InvalidException::class);
42
        new Terms('');
43
    }
44
45
    /**
46
     * @group functional
47
     */
48
    public function testFilteredSearch(): void
49
    {
50
        $index = $this->_createIndex();
51
52
        $index->addDocuments([
53
            new Document(1, ['name' => 'hello world']),
54
            new Document(2, ['name' => 'nicolas ruflin']),
55
            new Document(3, ['name' => 'ruflin']),
56
        ]);
57
58
        $query = new Terms('name', ['nicolas', 'hello']);
59
60
        $index->refresh();
61
        $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...
62
63
        $this->assertEquals(2, $resultSet->count());
64
65
        $query->addTerm('ruflin');
66
        $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...
67
68
        $this->assertEquals(3, $resultSet->count());
69
    }
70
71
    /**
72
     * @group functional
73
     */
74
    public function testFilteredSearchWithLookup(): void
75
    {
76
        $index = $this->_createIndex();
77
78
        $lookupIndex = $this->_createIndex('lookup_index');
79
        $lookupIndex->addDocuments([
80
            new Document(1, ['terms' => ['ruflin', 'nicolas']]),
81
        ]);
82
83
        $index->addDocuments([
84
            new Document(1, ['name' => 'hello world']),
85
            new Document(2, ['name' => 'nicolas ruflin']),
86
            new Document(3, ['name' => 'ruflin']),
87
        ]);
88
89
        $query = Terms::buildTermsLookup('name', $lookupIndex->getName(), '1', 'terms');
90
        $index->refresh();
91
        $lookupIndex->refresh();
92
93
        $resultSet = $index->search($query);
94
95
        $this->assertEquals(2, $resultSet->count());
96
    }
97
}
98