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