|
| 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(); |
0 commit comments