|
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 |
|
|
|
|
|
|
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()); |
|
|
|
|
|
|
77
|
|
|
|
|
78
|
|
|
foreach ($result->getResults() as $rx) { |
|
79
|
|
|
$value = $rx->getData(); |
|
80
|
|
|
$this->assertSame('Elastica', $value['package']); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
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.