|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Elastica\Test\Processor; |
|
4
|
|
|
|
|
5
|
|
|
use Elastica\Bulk; |
|
6
|
|
|
use Elastica\Document; |
|
7
|
|
|
use Elastica\Processor\SplitProcessor; |
|
8
|
|
|
use Elastica\Test\BasePipeline as BasePipelineTest; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @internal |
|
12
|
|
|
*/ |
|
13
|
|
View Code Duplication |
class SplitProcessorTest extends BasePipelineTest |
|
|
|
|
|
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @group unit |
|
17
|
|
|
*/ |
|
18
|
|
|
public function testSplit(): void |
|
19
|
|
|
{ |
|
20
|
|
|
$processor = new SplitProcessor('joined_array_field', '-'); |
|
21
|
|
|
|
|
22
|
|
|
$expected = [ |
|
23
|
|
|
'split' => [ |
|
24
|
|
|
'field' => 'joined_array_field', |
|
25
|
|
|
'separator' => '-', |
|
26
|
|
|
], |
|
27
|
|
|
]; |
|
28
|
|
|
|
|
29
|
|
|
$this->assertEquals($expected, $processor->toArray()); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @group unit |
|
34
|
|
|
*/ |
|
35
|
|
|
public function testSplitWithNonDefaultOptions(): void |
|
36
|
|
|
{ |
|
37
|
|
|
$processor = new SplitProcessor('joined_array_field', '-'); |
|
38
|
|
|
$processor->setIgnoreMissing(true); |
|
39
|
|
|
|
|
40
|
|
|
$expected = [ |
|
41
|
|
|
'split' => [ |
|
42
|
|
|
'field' => 'joined_array_field', |
|
43
|
|
|
'separator' => '-', |
|
44
|
|
|
'ignore_missing' => true, |
|
45
|
|
|
], |
|
46
|
|
|
]; |
|
47
|
|
|
|
|
48
|
|
|
$this->assertEquals($expected, $processor->toArray()); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @group functional |
|
53
|
|
|
*/ |
|
54
|
|
|
public function testSplitField(): void |
|
55
|
|
|
{ |
|
56
|
|
|
$split = new SplitProcessor('name', '&'); |
|
57
|
|
|
|
|
58
|
|
|
$pipeline = $this->_createPipeline('my_custom_pipeline', 'pipeline for Split'); |
|
59
|
|
|
$pipeline->addProcessor($split)->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&ruflin']), |
|
67
|
|
|
]); |
|
68
|
|
|
$bulk->setRequestParam('pipeline', 'my_custom_pipeline'); |
|
69
|
|
|
|
|
70
|
|
|
$bulk->send(); |
|
71
|
|
|
$index->refresh(); |
|
72
|
|
|
|
|
73
|
|
|
$result = $index->search('*'); |
|
74
|
|
|
|
|
75
|
|
|
$this->assertCount(1, $result->getResults()); |
|
|
|
|
|
|
76
|
|
|
|
|
77
|
|
|
$results = $result->getResults(); |
|
78
|
|
|
$this->assertSame(['nicolas', 'ruflin'], ($results[0]->getHit())['_source']['name']); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
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.