Completed
Pull Request — master (#1893)
by
unknown
02:46
created

SortProcessorTest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 8
dl 0
loc 64
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A testSort() 0 12 1
A testSortWithNonDefaultOptions() 0 14 1
A testSortField() 0 24 1
1
<?php
2
3
namespace Elastica\Test\Processor;
4
5
use Elastica\Bulk;
6
use Elastica\Document;
7
use Elastica\Processor\SortProcessor;
8
use Elastica\Test\BasePipeline as BasePipelineTest;
9
10
/**
11
 * @internal
12
 */
13
class SortProcessorTest extends BasePipelineTest
14
{
15
    /**
16
     * @group unit
17
     */
18
    public function testSort(): void
19
    {
20
        $processor = new SortProcessor('field_to_sort');
21
22
        $expected = [
23
            'sort' => [
24
                'field' => 'field_to_sort',
25
            ],
26
        ];
27
28
        $this->assertEquals($expected, $processor->toArray());
29
    }
30
31
    /**
32
     * @group unit
33
     */
34
    public function testSortWithNonDefaultOptions(): void
35
    {
36
        $processor = new SortProcessor('field_to_sort');
37
        $processor->setOrder('desc');
38
39
        $expected = [
40
            'sort' => [
41
                'field' => 'field_to_sort',
42
                'order' => 'desc',
43
            ],
44
        ];
45
46
        $this->assertEquals($expected, $processor->toArray());
47
    }
48
49
    /**
50
     * @group functional
51
     */
52
    public function testSortField(): void
53
    {
54
        $sort = new SortProcessor('name');
55
56
        $pipeline = $this->_createPipeline('my_custom_pipeline', 'pipeline for Sort');
57
        $pipeline->addProcessor($sort)->create();
58
59
        $index = $this->_createIndex();
60
        $bulk = new Bulk($index->getClient());
61
        $bulk->setIndex($index);
62
63
        $bulk->addDocument(new Document(null, ['name' => [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]]));
64
        $bulk->setRequestParam('pipeline', 'my_custom_pipeline');
65
66
        $bulk->send();
67
        $index->refresh();
68
69
        $result = $index->search('*');
70
71
        $this->assertCount(1, $result->getResults());
0 ignored issues
show
Documentation introduced by
$result->getResults() is of type array<integer,object<Elastica\Result>>, but the function expects a object<Countable>|object...nit\Framework\iterable>.

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...
72
73
        $results = $result->getResults();
74
        $this->assertSame([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], ($results[0]->getHit())['_source']['name']);
75
    }
76
}
77