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 |
|
|
|
|
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()); |
|
|
|
|
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
|
|
|
|
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.