-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathHighlight.phpt
More file actions
210 lines (154 loc) · 5.04 KB
/
Highlight.phpt
File metadata and controls
210 lines (154 loc) · 5.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<?php declare(strict_types = 1);
namespace SpameriTests\ElasticQuery;
require_once __DIR__ . '/../bootstrap.php';
class Highlight extends \Tester\TestCase
{
public function testToArrayBasic(): void
{
$highlight = new \Spameri\ElasticQuery\Highlight(
preTags: ['<em>'],
postTags: ['</em>'],
fields: ['title'],
);
$array = $highlight->toArray();
\Tester\Assert::same(['<em>'], $array['pre_tags']);
\Tester\Assert::same(['</em>'], $array['post_tags']);
\Tester\Assert::true(isset($array['fields']['title']));
}
public function testToArrayWithMultipleFields(): void
{
$highlight = new \Spameri\ElasticQuery\Highlight(
preTags: ['<mark>'],
postTags: ['</mark>'],
fields: ['title', 'content', 'description'],
);
$array = $highlight->toArray();
\Tester\Assert::true(isset($array['fields']['title']));
\Tester\Assert::true(isset($array['fields']['content']));
\Tester\Assert::true(isset($array['fields']['description']));
}
public function testToArrayWithMultipleTags(): void
{
$highlight = new \Spameri\ElasticQuery\Highlight(
preTags: ['<strong>', '<em>'],
postTags: ['</strong>', '</em>'],
fields: ['body'],
);
$array = $highlight->toArray();
\Tester\Assert::same(['<strong>', '<em>'], $array['pre_tags']);
\Tester\Assert::same(['</strong>', '</em>'], $array['post_tags']);
}
public function testToArrayFieldsHaveNumberOfFragments(): void
{
$highlight = new \Spameri\ElasticQuery\Highlight(
preTags: ['<b>'],
postTags: ['</b>'],
fields: ['summary'],
);
$array = $highlight->toArray();
\Tester\Assert::same(0, $array['fields']['summary']['number_of_fragments']);
}
public function testToArrayAllFieldsHaveNumberOfFragments(): void
{
$highlight = new \Spameri\ElasticQuery\Highlight(
preTags: ['<span class="highlight">'],
postTags: ['</span>'],
fields: ['field1', 'field2', 'field3'],
);
$array = $highlight->toArray();
foreach (['field1', 'field2', 'field3'] as $field) {
\Tester\Assert::same(0, $array['fields'][$field]['number_of_fragments']);
}
}
public function testToArrayWithEmptyFields(): void
{
$highlight = new \Spameri\ElasticQuery\Highlight(
preTags: ['<em>'],
postTags: ['</em>'],
fields: [],
);
$array = $highlight->toArray();
\Tester\Assert::same(['<em>'], $array['pre_tags']);
\Tester\Assert::same(['</em>'], $array['post_tags']);
\Tester\Assert::false(isset($array['fields']));
}
public function testToArrayWithEmptyTags(): void
{
$highlight = new \Spameri\ElasticQuery\Highlight(
preTags: [],
postTags: [],
fields: ['content'],
);
$array = $highlight->toArray();
\Tester\Assert::same([], $array['pre_tags']);
\Tester\Assert::same([], $array['post_tags']);
}
public function testToArrayWithHtmlTags(): void
{
$highlight = new \Spameri\ElasticQuery\Highlight(
preTags: ['<span class="search-highlight" style="background: yellow;">'],
postTags: ['</span>'],
fields: ['text'],
);
$array = $highlight->toArray();
\Tester\Assert::same(['<span class="search-highlight" style="background: yellow;">'], $array['pre_tags']);
}
public function testToArrayStructure(): void
{
$highlight = new \Spameri\ElasticQuery\Highlight(
preTags: ['<em>'],
postTags: ['</em>'],
fields: ['title', 'body'],
);
$array = $highlight->toArray();
// Verify structure matches Elasticsearch highlight format
\Tester\Assert::true(\array_key_exists('pre_tags', $array));
\Tester\Assert::true(\array_key_exists('post_tags', $array));
\Tester\Assert::true(\array_key_exists('fields', $array));
\Tester\Assert::true(\is_array($array['fields']));
}
public function testImplementsArrayInterface(): void
{
$highlight = new \Spameri\ElasticQuery\Highlight(
preTags: ['<em>'],
postTags: ['</em>'],
fields: ['content'],
);
\Tester\Assert::type(\Spameri\ElasticQuery\Entity\ArrayInterface::class, $highlight);
}
public function testToArrayWithSpecialCharactersInTags(): void
{
$highlight = new \Spameri\ElasticQuery\Highlight(
preTags: ['[[HIGHLIGHT]]'],
postTags: ['[[/HIGHLIGHT]]'],
fields: ['data'],
);
$array = $highlight->toArray();
\Tester\Assert::same(['[[HIGHLIGHT]]'], $array['pre_tags']);
\Tester\Assert::same(['[[/HIGHLIGHT]]'], $array['post_tags']);
}
public function testToArrayWithNestedFieldNames(): void
{
$highlight = new \Spameri\ElasticQuery\Highlight(
preTags: ['<em>'],
postTags: ['</em>'],
fields: ['user.name', 'address.city', 'metadata.tags'],
);
$array = $highlight->toArray();
\Tester\Assert::true(isset($array['fields']['user.name']));
\Tester\Assert::true(isset($array['fields']['address.city']));
\Tester\Assert::true(isset($array['fields']['metadata.tags']));
}
public function testToArrayWithWildcardField(): void
{
$highlight = new \Spameri\ElasticQuery\Highlight(
preTags: ['<mark>'],
postTags: ['</mark>'],
fields: ['*'],
);
$array = $highlight->toArray();
\Tester\Assert::true(isset($array['fields']['*']));
\Tester\Assert::same(0, $array['fields']['*']['number_of_fragments']);
}
}
(new Highlight())->run();