|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace SpameriTests\ElasticQuery\Aggregation; |
| 4 | + |
| 5 | +require_once __DIR__ . '/../../bootstrap.php'; |
| 6 | + |
| 7 | + |
| 8 | +class Missing extends \Tester\TestCase |
| 9 | +{ |
| 10 | + |
| 11 | + private const INDEX = 'spameri_test_aggregation_missing'; |
| 12 | + |
| 13 | + |
| 14 | + public function setUp(): void |
| 15 | + { |
| 16 | + $ch = \curl_init(); |
| 17 | + \curl_setopt($ch, \CURLOPT_URL, \ELASTICSEARCH_HOST . '/' . 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 | + } |
| 24 | + |
| 25 | + |
| 26 | + public function testToArray(): void |
| 27 | + { |
| 28 | + $missing = new \Spameri\ElasticQuery\Aggregation\Missing('price'); |
| 29 | + |
| 30 | + $array = $missing->toArray(); |
| 31 | + |
| 32 | + \Tester\Assert::same('price', $array['missing']['field']); |
| 33 | + } |
| 34 | + |
| 35 | + |
| 36 | + public function testKey(): void |
| 37 | + { |
| 38 | + $missing = new \Spameri\ElasticQuery\Aggregation\Missing('price'); |
| 39 | + |
| 40 | + \Tester\Assert::same('missing_price', $missing->key()); |
| 41 | + } |
| 42 | + |
| 43 | + |
| 44 | + public function testCreate(): void |
| 45 | + { |
| 46 | + $missing = new \Spameri\ElasticQuery\Aggregation\Missing('price'); |
| 47 | + |
| 48 | + $elasticQuery = new \Spameri\ElasticQuery\ElasticQuery(); |
| 49 | + $elasticQuery->aggregation()->add( |
| 50 | + new \Spameri\ElasticQuery\Aggregation\LeafAggregationCollection( |
| 51 | + 'no_price', |
| 52 | + null, |
| 53 | + $missing, |
| 54 | + ), |
| 55 | + ); |
| 56 | + |
| 57 | + $document = new \Spameri\ElasticQuery\Document( |
| 58 | + self::INDEX, |
| 59 | + new \Spameri\ElasticQuery\Document\Body\Plain( |
| 60 | + $elasticQuery->toArray(), |
| 61 | + ), |
| 62 | + ); |
| 63 | + |
| 64 | + $ch = \curl_init(); |
| 65 | + \curl_setopt($ch, \CURLOPT_URL, \ELASTICSEARCH_HOST . '/' . $document->index . '/_search'); |
| 66 | + \curl_setopt($ch, \CURLOPT_RETURNTRANSFER, 1); |
| 67 | + \curl_setopt($ch, \CURLOPT_CUSTOMREQUEST, 'GET'); |
| 68 | + \curl_setopt($ch, \CURLOPT_HTTPHEADER, ['Content-Type: application/json']); |
| 69 | + \curl_setopt( |
| 70 | + $ch, |
| 71 | + \CURLOPT_POSTFIELDS, |
| 72 | + \json_encode($document->toArray()['body']), |
| 73 | + ); |
| 74 | + |
| 75 | + \Tester\Assert::noError(static function () use ($ch): void { |
| 76 | + $response = \curl_exec($ch); |
| 77 | + $resultMapper = new \Spameri\ElasticQuery\Response\ResultMapper(); |
| 78 | + /** @var \Spameri\ElasticQuery\Response\ResultSearch $result */ |
| 79 | + $result = $resultMapper->map(\json_decode($response, true)); |
| 80 | + \Tester\Assert::type(\Spameri\ElasticQuery\Response\ResultSearch::class, $result); |
| 81 | + }); |
| 82 | + } |
| 83 | + |
| 84 | + |
| 85 | + public function tearDown(): void |
| 86 | + { |
| 87 | + $ch = \curl_init(); |
| 88 | + \curl_setopt($ch, \CURLOPT_URL, \ELASTICSEARCH_HOST . '/' . self::INDEX); |
| 89 | + \curl_setopt($ch, \CURLOPT_RETURNTRANSFER, 1); |
| 90 | + \curl_setopt($ch, \CURLOPT_CUSTOMREQUEST, 'DELETE'); |
| 91 | + \curl_setopt($ch, \CURLOPT_HTTPHEADER, ['Content-Type: application/json']); |
| 92 | + |
| 93 | + \curl_exec($ch); |
| 94 | + } |
| 95 | + |
| 96 | +} |
| 97 | + |
| 98 | +(new Missing())->run(); |
0 commit comments