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

ForeachProcessorTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 117
Duplicated Lines 16.24 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testForeachProcessorDefault() 19 19 1
A testForeachRawProcessorDefault() 0 25 1
A testForeachProcessorIgnoreMissing() 0 21 1
A testForeachProcessor() 0 34 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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