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

SetProcessorTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 71
Duplicated Lines 18.31 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 8
dl 13
loc 71
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A testSet() 13 13 1
A testSetWithNonDefaultOptions() 0 15 1
A testSetField() 0 29 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\SetProcessor;
8
use Elastica\Test\BasePipeline as BasePipelineTest;
9
10
/**
11
 * @internal
12
 */
13
class SetProcessorTest extends BasePipelineTest
14
{
15
    /**
16
     * @group unit
17
     */
18 View Code Duplication
    public function testSet(): 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...
19
    {
20
        $processor = new SetProcessor('field1', 582.1);
21
22
        $expected = [
23
            'set' => [
24
                'field' => 'field1',
25
                'value' => 582.1,
26
            ],
27
        ];
28
29
        $this->assertEquals($expected, $processor->toArray());
30
    }
31
32
    /**
33
     * @group unit
34
     */
35
    public function testSetWithNonDefaultOptions(): void
36
    {
37
        $processor = new SetProcessor('field1', 582.1);
38
        $processor->setOverride(false);
39
40
        $expected = [
41
            'set' => [
42
                'field' => 'field1',
43
                'value' => 582.1,
44
                'override' => false,
45
            ],
46
        ];
47
48
        $this->assertEquals($expected, $processor->toArray());
49
    }
50
51
    /**
52
     * @group functional
53
     */
54
    public function testSetField(): void
55
    {
56
        $set = new SetProcessor('package', 'Elastica');
57
58
        $pipeline = $this->_createPipeline('my_custom_pipeline', 'pipeline for Set');
59
        $pipeline->addProcessor($set)->create();
60
61
        $index = $this->_createIndex();
62
        $bulk = new Bulk($index->getClient());
63
        $bulk->setIndex($index);
64
65
        $bulk->addDocuments([
66
            new Document(null, ['name' => 'nicolas', 'package' => 'Elastico']),
67
            new Document(null, ['name' => 'ruflin']),
68
        ]);
69
        $bulk->setRequestParam('pipeline', 'my_custom_pipeline');
70
71
        $bulk->send();
72
        $index->refresh();
73
74
        $result = $index->search('*');
75
76
        $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...
77
78
        foreach ($result->getResults() as $rx) {
79
            $value = $rx->getData();
80
            $this->assertSame('Elastica', $value['package']);
81
        }
82
    }
83
}
84