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

ConvertProcessorTest::testConvertField()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 33
c 0
b 0
f 0
rs 9.392
cc 2
nc 2
nop 0
1
<?php
2
3
namespace Elastica\Test\Processor;
4
5
use Elastica\Bulk;
6
use Elastica\Document;
7
use Elastica\Processor\ConvertProcessor;
8
use Elastica\Test\BasePipeline as BasePipelineTest;
9
10
/**
11
 * @internal
12
 */
13
class ConvertProcessorTest extends BasePipelineTest
14
{
15
    /**
16
     * @group unit
17
     */
18
    public function testConvert(): void
19
    {
20
        $processor = new ConvertProcessor('foo', 'integer');
21
22
        $expected = [
23
            'convert' => [
24
                'field' => 'foo',
25
                'type' => 'integer',
26
            ],
27
        ];
28
29
        $this->assertEquals($expected, $processor->toArray());
30
    }
31
32
    /**
33
     * @group unit
34
     */
35
    public function testConvertWithNonDefaultOptions(): void
36
    {
37
        $processor = new ConvertProcessor('foo', 'integer');
38
        $processor->setIgnoreMissing(true);
39
40
        $expected = [
41
            'convert' => [
42
                'field' => 'foo',
43
                'type' => 'integer',
44
                'ignore_missing' => true,
45
            ],
46
        ];
47
48
        $this->assertEquals($expected, $processor->toArray());
49
50
        $processor->setTargetField('field2');
51
52
        $expected = [
53
            'convert' => [
54
                'field' => 'foo',
55
                'type' => 'integer',
56
                'ignore_missing' => true,
57
                'target_field' => 'field2',
58
            ],
59
        ];
60
61
        $this->assertEquals($expected, $processor->toArray());
62
    }
63
64
    /**
65
     * @group functional
66
     */
67
    public function testConvertField(): void
68
    {
69
        $append = new ConvertProcessor('foo', 'float');
70
71
        $pipeline = $this->_createPipeline('my_custom_pipeline', 'pipeline for Convert');
72
        $pipeline->addProcessor($append)->create();
73
74
        $index = $this->_createIndex();
75
        $bulk = new Bulk($index->getClient());
76
        $bulk->setIndex($index);
77
78
        $bulk->addDocuments([
79
            new Document(null, ['name' => 'ruflin', 'type' => 'elastica', 'foo' => '5.290']),
80
            new Document(null, ['name' => 'nicolas', 'type' => 'elastica', 'foo' => '6.908']),
81
        ]);
82
        $bulk->setRequestParam('pipeline', 'my_custom_pipeline');
83
84
        $bulk->send();
85
        $index->refresh();
86
87
        $result = $index->search('elastica');
88
89
        $this->assertCount(2, $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...
90
91
        $results = $result->getResults();
92
        foreach ($results as $result) {
93
            $value = $result->getData();
94
            $this->assertIsFloat($value['foo']);
95
        }
96
97
        $this->assertSame(5.290, ($results[0]->getHit())['_source']['foo']);
98
        $this->assertSame(6.908, ($results[1]->getHit())['_source']['foo']);
99
    }
100
}
101