-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathTerm.phpt
More file actions
89 lines (69 loc) · 2.3 KB
/
Term.phpt
File metadata and controls
89 lines (69 loc) · 2.3 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
<?php declare(strict_types = 1);
namespace SpameriTests\ElasticQuery\Query;
require_once __DIR__ . '/../../bootstrap.php';
class Term extends \Tester\TestCase
{
private const INDEX = 'spameri_test_video_term';
public function setUp() : void
{
$ch = \curl_init();
\curl_setopt($ch, CURLOPT_URL, \ELASTICSEARCH_HOST . '/' . self::INDEX);
\curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
\curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
\curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
\curl_exec($ch);
}
public function testCreate() : void
{
$term = new \Spameri\ElasticQuery\Query\Term(
'name',
'Avengers',
1.0
);
$array = $term->toArray();
\Tester\Assert::true(isset($array['term']['name']['value']));
\Tester\Assert::same('Avengers', $array['term']['name']['value']);
\Tester\Assert::same(1.0, $array['term']['name']['boost']);
$document = new \Spameri\ElasticQuery\Document(
self::INDEX,
new \Spameri\ElasticQuery\Document\Body\Plain(
(
new \Spameri\ElasticQuery\ElasticQuery(
new \Spameri\ElasticQuery\Query\QueryCollection(
NULL,
new \Spameri\ElasticQuery\Query\MustCollection(
$term
)
)
)
)->toArray()
)
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, \ELASTICSEARCH_HOST . '/' . $document->index . '/_search');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
curl_setopt(
$ch, CURLOPT_POSTFIELDS,
\json_encode($document->toArray()['body'])
);
\Tester\Assert::noError(static function () use ($ch) {
$response = curl_exec($ch);
$resultMapper = new \Spameri\ElasticQuery\Response\ResultMapper();
/** @var \Spameri\ElasticQuery\Response\ResultSearch $result */
$result = $resultMapper->map(\json_decode($response, TRUE));
\Tester\Assert::type('int', $result->stats()->total());
});
}
public function tearDown() : void
{
$ch = \curl_init();
\curl_setopt($ch, CURLOPT_URL, \ELASTICSEARCH_HOST . '/' . self::INDEX);
\curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
\curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
\curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
\curl_exec($ch);
}
}
(new Term())->run();