Completed
Pull Request — master (#1893)
by
unknown
06:15 queued 02:30
created

AttachmentProcessorTest::testExcludeFileSource()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 41
c 0
b 0
f 0
rs 9.264
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Elastica\Test\Processor;
4
5
use Elastica\Bulk;
6
use Elastica\Document;
7
use Elastica\Mapping;
8
use Elastica\Processor\AttachmentProcessor;
9
use Elastica\Test\BasePipeline as BasePipelineTest;
10
11
/**
12
 * @internal
13
 */
14
class AttachmentProcessorTest extends BasePipelineTest
15
{
16
    /**
17
     * @group unit
18
     */
19
    public function testAttachment(): void
20
    {
21
        $processor = new AttachmentProcessor('data');
22
23
        $expected = [
24
            'attachment' => [
25
                'field' => 'data',
26
            ],
27
        ];
28
29
        $this->assertEquals($expected, $processor->toArray());
30
    }
31
32
    /**
33
     * @group unit
34
     */
35
    public function testAttachmentWithNonDefaultOptions(): void
36
    {
37
        $processor = new AttachmentProcessor('data');
38
        $processor->setIndexedChars(1000);
39
        $processor->setProperties(['content', 'title', 'language']);
40
        $processor->setTargetField('attachment-new-name');
41
        $processor->setIgnoreMissing(true);
42
43
        $expected = [
44
            'attachment' => [
45
                'field' => 'data',
46
                'indexed_chars' => 1000,
47
                'properties' => ['content', 'title', 'language'],
48
                'target_field' => 'attachment-new-name',
49
                'ignore_missing' => true,
50
            ],
51
        ];
52
53
        $this->assertEquals($expected, $processor->toArray());
54
    }
55
56
    /**
57
     * @group functional
58
     */
59
    public function testAttachmentAddPdf(): void
60
    {
61
        $attachment = new AttachmentProcessor('data');
62
        $pipeline = $this->_createPipeline('my_custom_pipeline_attachment', 'pipeline for Attachment');
63
        $pipeline->addProcessor($attachment);
64
        $pipeline->create();
65
66
        $index = $this->_createIndex();
67
68
        $bulk = new Bulk($index->getClient());
69
        $bulk->setIndex($index);
70
71
        $doc1 = new Document(null);
72
        $doc1->addFile('data', __DIR__.'/../data/test.pdf');
73
        $doc2 = new Document(2, ['data' => '', 'text' => 'test running in basel']);
74
75
        $bulk->addDocuments([
76
            $doc1, $doc2,
77
        ]);
78
        $bulk->setRequestParam('pipeline', 'my_custom_pipeline_attachment');
79
80
        $bulk->send();
81
        $index->refresh();
82
83
        $resultSet = $index->search('xodoa');
84
        $this->assertEquals(1, $resultSet->count());
85
86
        $resultSet = $index->search('test');
87
        $this->assertEquals(2, $resultSet->count());
88
89
        // Author is ruflin
90
        $resultSet = $index->search('ruflin');
91
        $this->assertEquals(1, $resultSet->count());
92
93
        // String does not exist in file
94
        $resultSet = $index->search('guschti');
95
        $this->assertEquals(0, $resultSet->count());
96
    }
97
98
    /**
99
     * @group functional
100
     */
101 View Code Duplication
    public function testAttachmentAddPdfFileContent(): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
102
    {
103
        $attachment = new AttachmentProcessor('data');
104
        $pipeline = $this->_createPipeline('my_custom_pipeline_attachment', 'pipeline for Attachment');
105
        $pipeline->addProcessor($attachment);
106
        $pipeline->create();
107
108
        $index = $this->_createIndex();
109
110
        $bulk = new Bulk($index->getClient());
111
        $bulk->setIndex($index);
112
113
        $doc1 = new Document(null);
114
        $doc1->addFile('data', __DIR__.'/../data/test.pdf');
115
        $doc1->set('text', 'basel world');
116
117
        $doc2 = new Document(2, ['data' => '', 'text' => 'test running in basel']);
118
        $doc2->set('text', 'running in basel');
119
120
        $bulk->addDocuments([
121
            $doc1, $doc2,
122
        ]);
123
        $bulk->setRequestParam('pipeline', 'my_custom_pipeline_attachment');
124
125
        $bulk->send();
126
        $index->refresh();
127
128
        $resultSet = $index->search('xodoa');
129
        $this->assertEquals(1, $resultSet->count());
130
131
        $resultSet = $index->search('basel');
132
        $this->assertEquals(2, $resultSet->count());
133
134
        // Author is ruflin
135
        $resultSet = $index->search('ruflin');
136
        $this->assertEquals(1, $resultSet->count());
137
138
        // String does not exist in file
139
        $resultSet = $index->search('guschti');
140
        $this->assertEquals(0, $resultSet->count());
141
    }
142
143
    /**
144
     * @group functional
145
     */
146 View Code Duplication
    public function testAddWordxFile(): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
147
    {
148
        $attachment = new AttachmentProcessor('data');
149
        $pipeline = $this->_createPipeline('my_custom_pipeline_attachment', 'pipeline for Attachment');
150
        $pipeline->addProcessor($attachment);
151
        $pipeline->create();
152
153
        $index = $this->_createIndex();
154
155
        $bulk = new Bulk($index->getClient());
156
        $bulk->setIndex($index);
157
158
        $doc1 = new Document(null);
159
        $doc1->addFile('data', __DIR__.'/../data/test.docx');
160
        $doc1->set('text', 'basel world');
161
162
        $doc2 = new Document(2, ['data' => '', 'text' => 'test running in basel']);
163
164
        $bulk->addDocuments([
165
            $doc1, $doc2,
166
        ]);
167
        $bulk->setRequestParam('pipeline', 'my_custom_pipeline_attachment');
168
169
        $bulk->send();
170
        $index->refresh();
171
172
        $resultSet = $index->search('basel');
173
        $this->assertEquals(2, $resultSet->count());
174
175
        $resultSet = $index->search('ruflin');
176
        $this->assertEquals(0, $resultSet->count());
177
178
        $resultSet = $index->search('Xodoa');
179
        $this->assertEquals(1, $resultSet->count());
180
181
        // String does not exist in file
182
        $resultSet = $index->search('guschti');
183
        $this->assertEquals(0, $resultSet->count());
184
    }
185
186
    /**
187
     * @group functional
188
     */
189
    public function testExcludeFileSource(): void
190
    {
191
        $attachment = new AttachmentProcessor('data');
192
        $pipeline = $this->_createPipeline('my_custom_pipeline_attachment', 'pipeline for Attachment');
193
        $pipeline->addProcessor($attachment);
194
        $pipeline->create();
195
        $index = $this->_createIndex();
196
197
        $mapping = new Mapping([
198
            'data' => ['type' => 'text'],
199
            'text' => ['type' => 'text', 'store' => true],
200
            'title' => ['type' => 'text', 'store' => true],
201
        ]);
202
        $mapping->setSource(['excludes' => ['data']]);
203
204
        $index->setMapping($mapping);
205
206
        $docId = 1;
207
        $text = 'Basel World';
208
        $title = 'No Title';
209
210
        $doc1 = new Document($docId);
211
        $doc1->set('text', $text);
212
        $doc1->set('title', $title);
213
        $doc1->addFile('data', __DIR__.'/../data//test.docx');
214
215
        $bulk = new Bulk($index->getClient());
216
        $bulk->setIndex($index);
217
218
        $bulk->addDocuments([$doc1]);
219
        $bulk->setRequestParam('pipeline', 'my_custom_pipeline_attachment');
220
221
        // Optimization necessary, as otherwise source still in realtime get
222
        $bulk->send();
223
        $index->forcemerge();
224
225
        $data = $index->getDocument($docId)->getData();
226
        $this->assertEquals($data['title'], $title);
227
        $this->assertEquals($data['text'], $text);
228
        $this->assertArrayNotHasKey('file', $data);
0 ignored issues
show
Bug introduced by
It seems like $data defined by $index->getDocument($docId)->getData() on line 225 can also be of type string; however, PHPUnit\Framework\Assert::assertArrayNotHasKey() does only seem to accept array|object<ArrayAccess>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
229
    }
230
}
231