1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Elastica\Test\Processor; |
4
|
|
|
|
5
|
|
|
use Elastica\Bulk; |
6
|
|
|
use Elastica\Document; |
7
|
|
|
use Elastica\Processor\JsonProcessor; |
8
|
|
|
use Elastica\Test\BasePipeline as BasePipelineTest; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @internal |
12
|
|
|
*/ |
13
|
|
|
class JsonProcessorTest extends BasePipelineTest |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @group unit |
17
|
|
|
*/ |
18
|
|
|
public function testJson(): void |
19
|
|
|
{ |
20
|
|
|
$processor = new JsonProcessor('string_source'); |
21
|
|
|
|
22
|
|
|
$expected = [ |
23
|
|
|
'json' => [ |
24
|
|
|
'field' => 'string_source', |
25
|
|
|
], |
26
|
|
|
]; |
27
|
|
|
|
28
|
|
|
$this->assertEquals($expected, $processor->toArray()); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @group unit |
33
|
|
|
*/ |
34
|
|
|
public function testJsonWithNonDefaultOptions(): void |
35
|
|
|
{ |
36
|
|
|
$processor = new JsonProcessor('string_source'); |
37
|
|
|
$processor->setTargetField('json_target'); |
38
|
|
|
$processor->setAddToRoot(true); |
39
|
|
|
|
40
|
|
|
$expected = [ |
41
|
|
|
'json' => [ |
42
|
|
|
'field' => 'string_source', |
43
|
|
|
'target_field' => 'json_target', |
44
|
|
|
'add_to_root' => true, |
45
|
|
|
], |
46
|
|
|
]; |
47
|
|
|
|
48
|
|
|
$this->assertEquals($expected, $processor->toArray()); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @group functional |
53
|
|
|
*/ |
54
|
|
View Code Duplication |
public function testJsonField(): void |
|
|
|
|
55
|
|
|
{ |
56
|
|
|
$json = new JsonProcessor('name'); |
57
|
|
|
$json->setTargetField('realname'); |
58
|
|
|
|
59
|
|
|
$pipeline = $this->_createPipeline('my_custom_pipeline', 'pipeline for Json'); |
60
|
|
|
$pipeline->addProcessor($json)->create(); |
61
|
|
|
|
62
|
|
|
$index = $this->_createIndex(); |
63
|
|
|
$bulk = new Bulk($index->getClient()); |
64
|
|
|
$bulk->setIndex($index); |
65
|
|
|
|
66
|
|
|
$bulk->addDocument(new Document(null, ['name' => \json_encode(['foo' => 2000])])); |
67
|
|
|
$bulk->setRequestParam('pipeline', 'my_custom_pipeline'); |
68
|
|
|
|
69
|
|
|
$bulk->send(); |
70
|
|
|
$index->refresh(); |
71
|
|
|
|
72
|
|
|
$result = $index->search('*'); |
73
|
|
|
|
74
|
|
|
$this->assertCount(1, $result->getResults()); |
|
|
|
|
75
|
|
|
|
76
|
|
|
$resultExpected = [ |
77
|
|
|
'foo' => 2000, |
78
|
|
|
]; |
79
|
|
|
|
80
|
|
|
foreach ($result->getResults() as $rx) { |
81
|
|
|
$value = $rx->getData(); |
82
|
|
|
$this->assertEquals($resultExpected, $value['realname']); |
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.