-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathGeoShape.phpt
More file actions
139 lines (107 loc) · 3.69 KB
/
Copy pathGeoShape.phpt
File metadata and controls
139 lines (107 loc) · 3.69 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
<?php declare(strict_types = 1);
namespace SpameriTests\ElasticQuery\Query;
require_once __DIR__ . '/../../bootstrap.php';
class GeoShape extends \SpameriTests\ElasticQuery\AbstractElasticTestCase
{
protected const INDEX = 'spameri_test_query_geo_shape';
private const SHAPE_INDEX = 'spameri_test_query_geo_shape_lookup';
protected function mapping(): array|null
{
return ['mappings' => ['properties' => ['location' => ['type' => 'geo_shape']]]];
}
public function testToArray(): void
{
$geoShape = new \Spameri\ElasticQuery\Query\GeoShape(
field: 'location',
shape: ['type' => 'envelope', 'coordinates' => [[13.0, 53.0], [14.0, 52.0]]],
relation: 'within',
);
$array = $geoShape->toArray();
\Tester\Assert::same('envelope', $array['geo_shape']['location']['shape']['type']);
\Tester\Assert::same('within', $array['geo_shape']['location']['relation']);
}
public function testToArrayWithIndexedShape(): void
{
$geoShape = new \Spameri\ElasticQuery\Query\GeoShape(
field: 'location',
indexedShape: new \Spameri\ElasticQuery\Query\IndexedShape(
id: 'deu',
index: 'shapes',
path: 'location',
routing: 'eu',
),
boost: 2.0,
);
$array = $geoShape->toArray();
\Tester\Assert::same('deu', $array['geo_shape']['location']['indexed_shape']['id']);
\Tester\Assert::same('shapes', $array['geo_shape']['location']['indexed_shape']['index']);
\Tester\Assert::same('eu', $array['geo_shape']['location']['indexed_shape']['routing']);
\Tester\Assert::same(2.0, $array['geo_shape']['boost']);
}
public function testRelationValidated(): void
{
\Tester\Assert::exception(
static function (): void {
new \Spameri\ElasticQuery\Query\GeoShape('loc', ['type' => 'point', 'coordinates' => [0, 0]], 'nonsense');
},
\Spameri\ElasticQuery\Exception\InvalidArgumentException::class,
);
}
public function testRequiresShapeOrIndexedShape(): void
{
\Tester\Assert::exception(
static function (): void {
new \Spameri\ElasticQuery\Query\GeoShape('loc');
},
\Spameri\ElasticQuery\Exception\InvalidArgumentException::class,
);
}
public function testCreate(): void
{
$this->indexDocument(['location' => ['type' => 'point', 'coordinates' => [13.5, 52.5]]]);
$query = new \Spameri\ElasticQuery\ElasticQuery(
new \Spameri\ElasticQuery\Query\QueryCollection(
null,
new \Spameri\ElasticQuery\Query\MustCollection(
new \Spameri\ElasticQuery\Query\GeoShape(
field: 'location',
shape: ['type' => 'envelope', 'coordinates' => [[13.0, 53.0], [14.0, 52.0]]],
relation: 'within',
),
),
),
);
$result = $this->search($query);
\Tester\Assert::same(1, $result->stats()->total());
}
public function testCreateWithIndexedShape(): void
{
$this->request('PUT', self::SHAPE_INDEX, ['mappings' => ['properties' => ['location' => ['type' => 'geo_shape']]]]);
$this->request(
'PUT',
self::SHAPE_INDEX . '/_doc/deu?refresh=true',
['location' => ['type' => 'envelope', 'coordinates' => [[13.0, 53.0], [14.0, 52.0]]]],
);
$this->indexDocument(['location' => ['type' => 'point', 'coordinates' => [13.5, 52.5]]]);
$query = new \Spameri\ElasticQuery\ElasticQuery(
new \Spameri\ElasticQuery\Query\QueryCollection(
null,
new \Spameri\ElasticQuery\Query\MustCollection(
new \Spameri\ElasticQuery\Query\GeoShape(
field: 'location',
relation: 'within',
indexedShape: new \Spameri\ElasticQuery\Query\IndexedShape(
id: 'deu',
index: self::SHAPE_INDEX,
path: 'location',
),
),
),
),
);
$result = $this->search($query);
$this->request('DELETE', self::SHAPE_INDEX);
\Tester\Assert::same(1, $result->stats()->total());
}
}
(new GeoShape())->run();