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

ForeachProcessorTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 123
Duplicated Lines 56.1 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

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

4 Methods

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