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

AppendProcessorTest::testAppend()   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\AppendProcessor;
8
use Elastica\Test\BasePipeline as BasePipelineTest;
9
10
/**
11
 * @internal
12
 */
13
class AppendProcessorTest extends BasePipelineTest
14
{
15
    /**
16
     * @group unit
17
     */
18 View Code Duplication
    public function testAppendSingleValue(): 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 AppendProcessor('field1', 'item2');
21
22
        $expected = [
23
            'append' => [
24
                'field' => 'field1',
25
                'value' => 'item2',
26
            ],
27
        ];
28
29
        $this->assertEquals($expected, $processor->toArray());
30
    }
31
32
    /**
33
     * @group unit
34
     */
35
    public function testAppendArray(): void
36
    {
37
        $processor = new AppendProcessor('field1', ['item2', 'item3', 'item4']);
38
39
        $expected = [
40
            'append' => [
41
                'field' => 'field1',
42
                'value' => ['item2', 'item3', 'item4'],
43
            ],
44
        ];
45
46
        $this->assertEquals($expected, $processor->toArray());
47
    }
48
49
    /**
50
     * @group functional
51
     */
52
    public function testAppend(): void
53
    {
54
        $append = new AppendProcessor('foo', ['item2', 'item3', 'item4']);
55
56
        $pipeline = $this->_createPipeline('my_custom_pipeline', 'pipeline for Append');
57
        $pipeline->addProcessor($append)->create();
58
59
        $index = $this->_createIndex();
60
        $bulk = new Bulk($index->getClient());
61
        $bulk->setIndex($index);
62
63
        $bulk->addDocuments([
64
            new Document(null, ['name' => 'ruflin', 'type' => 'elastica', 'foo' => null]),
65
            new Document(null, ['name' => 'nicolas', 'type' => 'elastica', 'foo' => null]),
66
        ]);
67
        $bulk->setRequestParam('pipeline', 'my_custom_pipeline');
68
69
        $bulk->send();
70
        $index->refresh();
71
72
        $result = $index->search('*');
73
74
        $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...
75
76
        foreach ($result->getResults() as $rx) {
77
            $value = $rx->getData();
78
            $this->assertCount(4, $value['foo']);
79
            $this->assertNull($value['foo'][0]);
80
            $this->assertEquals('item2', $value['foo'][1]);
81
            $this->assertEquals('item3', $value['foo'][2]);
82
            $this->assertEquals('item4', $value['foo'][3]);
83
        }
84
    }
85
}
86