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

ForeachProcessorTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 9
dl 0
loc 111
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testForeachProcessorDefault() 0 18 1
A testForeachRawProcessorDefault() 0 22 1
A testForeachProcessorIgnoreMissing() 0 20 1
A testForeachProcessor() 0 33 2
1
<?php
2
3
namespace Elastica\Test\Processor;
4
5
use Elastica\Bulk;
6
use Elastica\Document;
7
use Elastica\Processor\ForeachProcessor;
8
use Elastica\Processor\Uppercase;
9
use Elastica\ResultSet;
10
use Elastica\Test\BasePipeline as BasePipelineTest;
11
12
/**
13
 * @internal
14
 */
15
class ForeachProcessorTest extends BasePipelineTest
16
{
17
    /**
18
     * @group unit
19
     */
20
    public function testForeachProcessorDefault(): void
21
    {
22
        $subprocessor = new Uppercase('field2');
23
        $processor = new ForeachProcessor('field1', $subprocessor);
24
25
        $expected = [
26
            'foreach' => [
27
                'field' => 'field1',
28
                'processor' => [
29
                    'uppercase' => [
30
                        'field' => 'field2',
31
                    ],
32
                ],
33
            ],
34
        ];
35
36
        $this->assertEquals($expected, $processor->toArray());
37
    }
38
39
    /**
40
     * @group unit
41
     */
42
    public function testForeachRawProcessorDefault(): void
43
    {
44
        $subprocessor = [
45
            'uppercase' => [
46
                'field' => 'field2',
47
            ],
48
        ];
49
        $processor = new ForeachProcessor('field1', $subprocessor);
50
51
        $expected = [
52
            'foreach' => [
53
                'field' => 'field1',
54
                'processor' => [
55
                    'uppercase' => [
56
                        'field' => 'field2',
57
                    ],
58
                ],
59
            ],
60
        ];
61
62
        $this->assertEquals($expected, $processor->toArray());
63
    }
64
65
    /**
66
     * @group unit
67
     */
68
    public function testForeachProcessorIgnoreMissing(): void
69
    {
70
        $subprocessor = new Uppercase('field2');
71
        $processor = new ForeachProcessor('field1', $subprocessor);
72
        $processor->setIgnoreMissing(true);
73
74
        $expected = [
75
            'foreach' => [
76
                'field' => 'field1',
77
                'processor' => [
78
                    'uppercase' => [
79
                        'field' => 'field2',
80
                    ],
81
                ],
82
                'ignore_missing' => true,
83
            ],
84
        ];
85
86
        $this->assertEquals($expected, $processor->toArray());
87
    }
88
89
    /**
90
     * @group functional
91
     */
92
    public function testForeachProcessor(): void
93
    {
94
        $subprocessor = new Uppercase('_ingest._value');
95
        $foreach = new ForeachProcessor('values', $subprocessor);
96
97
        $pipeline = $this->_createPipeline('my_custom_pipeline', 'pipeline for Foreach');
98
        $pipeline->addProcessor($foreach)->create();
99
100
        $index = $this->_createIndex();
101
        $bulk = new Bulk($index->getClient());
102
        $bulk->setIndex($index);
103
104
        $bulk->addDocuments([
105
            new Document(null, ['name' => 'ruflin', 'type' => 'elastica', 'values' => ['foo', 'bar', 'baz']]),
106
        ]);
107
        $bulk->setRequestParam('pipeline', 'my_custom_pipeline');
108
109
        $bulk->send();
110
        $index->refresh();
111
112
        /** @var ResultSet $result */
113
        $result = $index->search('*');
114
115
        $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...
116
117
        foreach ($result->getResults() as $rx) {
118
            $value = $rx->getData();
119
            $this->assertCount(3, $value['values']);
120
            $this->assertEquals('FOO', $value['values'][0]);
121
            $this->assertEquals('BAR', $value['values'][1]);
122
            $this->assertEquals('BAZ', $value['values'][2]);
123
        }
124
    }
125
}
126