Skip to content

Commit 9661721

Browse files
committed
test: add unit tests for various response and mapping classes
1 parent 2730fd3 commit 9661721

File tree

96 files changed

+12184
-1
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

96 files changed

+12184
-1
lines changed
Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace SpameriTests\ElasticQuery\Aggregation;
4+
5+
require_once __DIR__ . '/../../bootstrap.php';
6+
7+
8+
class AggregationCollection extends \Tester\TestCase
9+
{
10+
11+
private const INDEX = 'spameri_test_aggregation_collection';
12+
13+
14+
public function setUp(): void
15+
{
16+
$ch = \curl_init();
17+
\curl_setopt($ch, \CURLOPT_URL, 'localhost:9200/' . self::INDEX);
18+
\curl_setopt($ch, \CURLOPT_RETURNTRANSFER, 1);
19+
\curl_setopt($ch, \CURLOPT_CUSTOMREQUEST, 'PUT');
20+
\curl_setopt($ch, \CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
21+
22+
\curl_exec($ch);
23+
\curl_close($ch);
24+
}
25+
26+
27+
public function testConstructorEmpty(): void
28+
{
29+
$collection = new \Spameri\ElasticQuery\Aggregation\AggregationCollection();
30+
31+
\Tester\Assert::same(0, $collection->count());
32+
}
33+
34+
35+
public function testConstructorWithAggregations(): void
36+
{
37+
$leafCollection1 = new \Spameri\ElasticQuery\Aggregation\LeafAggregationCollection(
38+
'avg_price',
39+
null,
40+
new \Spameri\ElasticQuery\Aggregation\Avg('price'),
41+
);
42+
$leafCollection2 = new \Spameri\ElasticQuery\Aggregation\LeafAggregationCollection(
43+
'max_price',
44+
null,
45+
new \Spameri\ElasticQuery\Aggregation\Max('price'),
46+
);
47+
48+
$collection = new \Spameri\ElasticQuery\Aggregation\AggregationCollection(null, $leafCollection1, $leafCollection2);
49+
50+
\Tester\Assert::same(2, $collection->count());
51+
}
52+
53+
54+
public function testAdd(): void
55+
{
56+
$collection = new \Spameri\ElasticQuery\Aggregation\AggregationCollection();
57+
$leafCollection = new \Spameri\ElasticQuery\Aggregation\LeafAggregationCollection(
58+
'avg_price',
59+
null,
60+
new \Spameri\ElasticQuery\Aggregation\Avg('price'),
61+
);
62+
63+
$collection->add($leafCollection);
64+
65+
\Tester\Assert::same(1, $collection->count());
66+
}
67+
68+
69+
public function testKey(): void
70+
{
71+
$collection = new \Spameri\ElasticQuery\Aggregation\AggregationCollection();
72+
73+
\Tester\Assert::same('top-aggs-collection', $collection->key());
74+
}
75+
76+
77+
public function testKeys(): void
78+
{
79+
$collection = new \Spameri\ElasticQuery\Aggregation\AggregationCollection();
80+
$collection->add(new \Spameri\ElasticQuery\Aggregation\LeafAggregationCollection(
81+
'avg_price',
82+
null,
83+
new \Spameri\ElasticQuery\Aggregation\Avg('price'),
84+
));
85+
$collection->add(new \Spameri\ElasticQuery\Aggregation\LeafAggregationCollection(
86+
'max_price',
87+
null,
88+
new \Spameri\ElasticQuery\Aggregation\Max('price'),
89+
));
90+
91+
$keys = $collection->keys();
92+
93+
\Tester\Assert::contains('avg_price', $keys);
94+
\Tester\Assert::contains('max_price', $keys);
95+
}
96+
97+
98+
public function testFilter(): void
99+
{
100+
$filterCollection = new \Spameri\ElasticQuery\Filter\FilterCollection();
101+
$filterCollection->must()->add(new \Spameri\ElasticQuery\Query\Term('status', 'active'));
102+
103+
$collection = new \Spameri\ElasticQuery\Aggregation\AggregationCollection($filterCollection);
104+
105+
\Tester\Assert::same($filterCollection, $collection->filter());
106+
}
107+
108+
109+
public function testToArray(): void
110+
{
111+
$leafCollection1 = new \Spameri\ElasticQuery\Aggregation\LeafAggregationCollection(
112+
'avg_price',
113+
null,
114+
new \Spameri\ElasticQuery\Aggregation\Avg('price'),
115+
);
116+
$leafCollection2 = new \Spameri\ElasticQuery\Aggregation\LeafAggregationCollection(
117+
'max_price',
118+
null,
119+
new \Spameri\ElasticQuery\Aggregation\Max('price'),
120+
);
121+
122+
$collection = new \Spameri\ElasticQuery\Aggregation\AggregationCollection(null, $leafCollection1, $leafCollection2);
123+
124+
$array = $collection->toArray();
125+
126+
\Tester\Assert::true(isset($array['avg_price']));
127+
\Tester\Assert::true(isset($array['max_price']));
128+
}
129+
130+
131+
public function testCreate(): void
132+
{
133+
$collection = new \Spameri\ElasticQuery\Aggregation\AggregationCollection();
134+
$collection->add(
135+
new \Spameri\ElasticQuery\Aggregation\LeafAggregationCollection(
136+
'avg_price',
137+
null,
138+
new \Spameri\ElasticQuery\Aggregation\Avg('price'),
139+
),
140+
);
141+
$collection->add(
142+
new \Spameri\ElasticQuery\Aggregation\LeafAggregationCollection(
143+
'max_price',
144+
null,
145+
new \Spameri\ElasticQuery\Aggregation\Max('price'),
146+
),
147+
);
148+
149+
$elasticQuery = new \Spameri\ElasticQuery\ElasticQuery(
150+
null,
151+
null,
152+
null,
153+
$collection,
154+
);
155+
156+
$document = new \Spameri\ElasticQuery\Document(
157+
self::INDEX,
158+
new \Spameri\ElasticQuery\Document\Body\Plain(
159+
$elasticQuery->toArray(),
160+
),
161+
);
162+
163+
$ch = \curl_init();
164+
\curl_setopt($ch, \CURLOPT_URL, 'localhost:9200/' . $document->index . '/_search');
165+
\curl_setopt($ch, \CURLOPT_RETURNTRANSFER, 1);
166+
\curl_setopt($ch, \CURLOPT_CUSTOMREQUEST, 'GET');
167+
\curl_setopt($ch, \CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
168+
\curl_setopt(
169+
$ch,
170+
\CURLOPT_POSTFIELDS,
171+
\json_encode($document->toArray()['body']),
172+
);
173+
174+
\Tester\Assert::noError(static function () use ($ch): void {
175+
$response = \curl_exec($ch);
176+
$resultMapper = new \Spameri\ElasticQuery\Response\ResultMapper();
177+
/** @var \Spameri\ElasticQuery\Response\ResultSearch $result */
178+
$result = $resultMapper->map(\json_decode($response, true));
179+
\Tester\Assert::type(\Spameri\ElasticQuery\Response\ResultSearch::class, $result);
180+
});
181+
182+
\curl_close($ch);
183+
}
184+
185+
186+
public function tearDown(): void
187+
{
188+
$ch = \curl_init();
189+
\curl_setopt($ch, \CURLOPT_URL, 'localhost:9200/' . self::INDEX);
190+
\curl_setopt($ch, \CURLOPT_RETURNTRANSFER, 1);
191+
\curl_setopt($ch, \CURLOPT_CUSTOMREQUEST, 'DELETE');
192+
\curl_setopt($ch, \CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
193+
194+
\curl_exec($ch);
195+
\curl_close($ch);
196+
}
197+
198+
}
199+
200+
(new AggregationCollection())->run();
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace SpameriTests\ElasticQuery\Aggregation;
4+
5+
require_once __DIR__ . '/../../bootstrap.php';
6+
7+
8+
class Avg extends \Tester\TestCase
9+
{
10+
11+
private const INDEX = 'spameri_test_aggregation_avg';
12+
13+
14+
public function setUp(): void
15+
{
16+
$ch = \curl_init();
17+
\curl_setopt($ch, \CURLOPT_URL, 'localhost:9200/' . self::INDEX);
18+
\curl_setopt($ch, \CURLOPT_RETURNTRANSFER, 1);
19+
\curl_setopt($ch, \CURLOPT_CUSTOMREQUEST, 'PUT');
20+
\curl_setopt($ch, \CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
21+
22+
\curl_exec($ch);
23+
\curl_close($ch);
24+
}
25+
26+
27+
public function testToArray(): void
28+
{
29+
$avg = new \Spameri\ElasticQuery\Aggregation\Avg('price');
30+
31+
$array = $avg->toArray();
32+
33+
\Tester\Assert::true(isset($array['avg']['field']));
34+
\Tester\Assert::same('price', $array['avg']['field']);
35+
}
36+
37+
38+
public function testKey(): void
39+
{
40+
$avg = new \Spameri\ElasticQuery\Aggregation\Avg('price');
41+
42+
\Tester\Assert::same('avg_price', $avg->key());
43+
}
44+
45+
46+
public function testCreate(): void
47+
{
48+
$avg = new \Spameri\ElasticQuery\Aggregation\Avg('price');
49+
50+
$elasticQuery = new \Spameri\ElasticQuery\ElasticQuery();
51+
$elasticQuery->aggregation()->add(
52+
new \Spameri\ElasticQuery\Aggregation\LeafAggregationCollection(
53+
'price_avg',
54+
null,
55+
$avg,
56+
),
57+
);
58+
59+
$document = new \Spameri\ElasticQuery\Document(
60+
self::INDEX,
61+
new \Spameri\ElasticQuery\Document\Body\Plain(
62+
$elasticQuery->toArray(),
63+
),
64+
);
65+
66+
$ch = \curl_init();
67+
\curl_setopt($ch, \CURLOPT_URL, 'localhost:9200/' . $document->index . '/_search');
68+
\curl_setopt($ch, \CURLOPT_RETURNTRANSFER, 1);
69+
\curl_setopt($ch, \CURLOPT_CUSTOMREQUEST, 'GET');
70+
\curl_setopt($ch, \CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
71+
\curl_setopt(
72+
$ch,
73+
\CURLOPT_POSTFIELDS,
74+
\json_encode($document->toArray()['body']),
75+
);
76+
77+
\Tester\Assert::noError(static function () use ($ch): void {
78+
$response = \curl_exec($ch);
79+
$resultMapper = new \Spameri\ElasticQuery\Response\ResultMapper();
80+
/** @var \Spameri\ElasticQuery\Response\ResultSearch $result */
81+
$result = $resultMapper->map(\json_decode($response, true));
82+
\Tester\Assert::type(\Spameri\ElasticQuery\Response\ResultSearch::class, $result);
83+
});
84+
85+
\curl_close($ch);
86+
}
87+
88+
89+
public function tearDown(): void
90+
{
91+
$ch = \curl_init();
92+
\curl_setopt($ch, \CURLOPT_URL, 'localhost:9200/' . self::INDEX);
93+
\curl_setopt($ch, \CURLOPT_RETURNTRANSFER, 1);
94+
\curl_setopt($ch, \CURLOPT_CUSTOMREQUEST, 'DELETE');
95+
\curl_setopt($ch, \CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
96+
97+
\curl_exec($ch);
98+
\curl_close($ch);
99+
}
100+
101+
}
102+
103+
(new Avg())->run();

0 commit comments

Comments
 (0)