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

tests/Query/PercolateTest.php (14 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\Mapping;
7
use Elastica\Query\Percolate;
8
use Elastica\Test\Base as BaseTest;
9
10
/**
11
 * @internal
12
 */
13
class PercolateTest extends BaseTest
14
{
15
    /**
16
     * @var \Elastica\Index
17
     */
18
    private $index;
19
20
    /**
21
     * @group functional
22
     */
23
    public function testPercolateQueryOnNewDocument(): void
24
    {
25
        $this->_prepareIndexForPercolate();
26
        //Register a query in the percolator:
27
        $queryDoc = new Document(1, ['query' => ['match' => ['message' => 'bonsai tree']]]);
28
        $doc = new Document(2, ['message' => 'A new bonsai tree in the office']);
29
        $this->index->addDocument($queryDoc);
30
        $this->index->refresh();
31
        //Match a document to the registered percolator queries:
32
        $percolateQuery = new Percolate();
33
        $percolateQuery->setField('query')
34
            ->setDocument($doc->getData())
0 ignored issues
show
It seems like $doc->getData() targeting Elastica\Document::getData() can also be of type string; however, Elastica\Query\Percolate::setDocument() does only seem to accept array|object<Elastica\Document>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
35
        ;
36
        $resultSet = $this->index->search($percolateQuery);
0 ignored issues
show
$percolateQuery is of type object<Elastica\Query\Percolate>, 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...
37
38
        $this->assertEquals(1, $resultSet->count());
39
40
        //Register a new query in the percolator:
41
        $queryDoc = new Document(3, ['query' => ['match' => ['message' => 'i have nice bonsai tree']]]);
42
        $this->index->addDocument($queryDoc);
43
        $this->index->refresh();
44
        $resultSet = $this->index->search($percolateQuery);
0 ignored issues
show
$percolateQuery is of type object<Elastica\Query\Percolate>, 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...
45
46
        //Match a document to the registered percolator queries:
47
        $this->assertEquals(2, $resultSet->count());
48
49
        //Check on the document without keywords from percolate stored query
50
        $doc2 = new Document(4, ['message' => 'Just a simple text for test']);
51
        $percolateQuery = new Percolate();
52
        $percolateQuery->setField('query')
53
            ->setDocument($doc2->getData())
0 ignored issues
show
It seems like $doc2->getData() targeting Elastica\Document::getData() can also be of type string; however, Elastica\Query\Percolate::setDocument() does only seem to accept array|object<Elastica\Document>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
54
        ;
55
        $resultSet = $this->index->search($percolateQuery);
0 ignored issues
show
$percolateQuery is of type object<Elastica\Query\Percolate>, 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...
56
57
        $this->assertEquals(0, $resultSet->count());
58
    }
59
60
    /**
61
     * @group functional
62
     */
63
    public function testPercolateQueryOnExistingDocument(): void
64
    {
65
        $this->_prepareIndexForPercolate();
66
        //Register a query in the percolator:
67
        $queryDoc = new Document(1, ['query' => ['match' => ['message' => 'bonsai tree']]]);
68
        $doc = new Document(2, ['message' => 'A new bonsai tree in the office']);
69
        $this->index->addDocument($doc);
70
        $this->index->addDocument($queryDoc);
71
        $this->index->refresh();
72
73
        $percolateQuery = new Percolate();
74
        $percolateQuery->setField('query')
75
            ->setDocumentIndex($this->index->getName())
76
            ->setDocumentId($doc->getId())
77
        ;
78
        $resultSet = $this->index->search($percolateQuery);
0 ignored issues
show
$percolateQuery is of type object<Elastica\Query\Percolate>, 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...
79
80
        $this->assertEquals(1, $resultSet->count());
81
82
        $queryDoc = new Document(3, ['query' => ['match' => ['message' => 'i have nice bonsai tree']]]);
83
        $this->index->addDocument($queryDoc);
84
        $this->index->refresh();
85
        $resultSet = $this->index->search($percolateQuery);
0 ignored issues
show
$percolateQuery is of type object<Elastica\Query\Percolate>, 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...
86
87
        $this->assertEquals(2, $resultSet->count());
88
89
        $doc2 = new Document(4, ['message' => 'Just a simple text for test']);
90
        $this->index->addDocument($doc2);
91
        $percolateQuery = new Percolate();
92
        $percolateQuery->setField('query')
93
            ->setDocumentIndex($this->index->getName())
94
            ->setDocumentId($doc2->getId())
95
        ;
96
        $resultSet = $this->index->search($percolateQuery);
0 ignored issues
show
$percolateQuery is of type object<Elastica\Query\Percolate>, 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(0, $resultSet->count());
99
    }
100
101
    /**
102
     * @group unit
103
     */
104 View Code Duplication
    public function testSetField(): void
0 ignored issues
show
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
        $field = 'field1';
107
        $query = new Percolate();
108
        $query->setField($field);
109
110
        $data = $query->toArray();
111
112
        $this->assertEquals($data['percolate']['field'], $field);
113
    }
114
115
    /**
116
     * @group unit
117
     */
118 View Code Duplication
    public function testSetDocument(): void
0 ignored issues
show
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...
119
    {
120
        $query = new Percolate();
121
        $doc = new Document(1, ['message' => 'A new bonsai tree in the office']);
122
        $query->setDocument($doc->getData());
0 ignored issues
show
It seems like $doc->getData() targeting Elastica\Document::getData() can also be of type string; however, Elastica\Query\Percolate::setDocument() does only seem to accept array|object<Elastica\Document>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
123
124
        $data = $query->toArray();
125
126
        $this->assertEquals($data['percolate']['document'], $doc->getData());
127
    }
128
129
    /**
130
     * @group unit
131
     */
132
    public function testSetDocumentIndex(): void
133
    {
134
        $index = $this->_createIndex('indexone');
135
        $query = new Percolate();
136
        $query->setDocumentIndex($index->getName());
137
138
        $data = $query->toArray();
139
140
        $this->assertEquals($data['percolate']['index'], $index->getName());
141
    }
142
143
    /**
144
     * @group unit
145
     */
146 View Code Duplication
    public function testSetDocumentId(): void
0 ignored issues
show
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...
147
    {
148
        $id = 3;
149
        $query = new Percolate();
150
        $query->setDocumentId($id);
151
152
        $data = $query->toArray();
153
154
        $this->assertEquals($data['percolate']['id'], $id);
155
    }
156
157
    /**
158
     * @group unit
159
     */
160 View Code Duplication
    public function testSetDocumentRouting(): void
0 ignored issues
show
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...
161
    {
162
        $routing = 'testRout';
163
        $query = new Percolate();
164
        $query->setDocumentRouting($routing);
165
166
        $data = $query->toArray();
167
168
        $this->assertEquals($data['percolate']['routing'], $routing);
169
    }
170
171
    /**
172
     * @group unit
173
     */
174
    public function testSetDocumentPreference(): void
175
    {
176
        $preference = ['pref1' => 'test', 'pref2' => 'test2'];
177
        $query = new Percolate();
178
        $query->setDocumentPreference($preference);
179
180
        $data = $query->toArray();
181
182
        $this->assertEquals($data['percolate']['preference'], $preference);
183
    }
184
185
    /**
186
     * @group unit
187
     */
188 View Code Duplication
    public function testSetDocumentVersion(): void
0 ignored issues
show
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...
189
    {
190
        $version = 10;
191
        $query = new Percolate();
192
        $query->setDocumentVersion($version);
193
194
        $data = $query->toArray();
195
196
        $this->assertEquals($data['percolate']['version'], $version);
197
    }
198
199
    private function _prepareIndexForPercolate(): void
200
    {
201
        $this->index = $this->_createIndex();
202
        $this->index->getSettings()->setNumberOfReplicas(0);
203
        //The doctype mapping is the mapping used to preprocess the document
204
        // defined in the percolator query before it gets indexed into a temporary index.
205
        //The queries mapping is the mapping used for indexing the query documents.
206
        $this->index->setMapping(new Mapping(['message' => ['type' => 'text'], 'query' => ['type' => 'percolator']]));
207
    }
208
}
209