|
| 1 | +<?php |
| 2 | + |
| 3 | +use Ensi\LaravelElasticQuerySpecification\Faceting\AllowedFacet; |
| 4 | +use Ensi\LaravelElasticQuerySpecification\Filtering\AllowedFilter; |
| 5 | +use Ensi\LaravelElasticQuerySpecification\Specification\CompositeSpecification; |
| 6 | +use Ensi\LaravelElasticQuerySpecification\Specification\Specification; |
| 7 | + |
| 8 | +uses()->group('integration'); |
| 9 | + |
| 10 | +test('facets in root specification', function () { |
| 11 | + $spec = CompositeSpecification::new() |
| 12 | + ->allowedFacets([ |
| 13 | + AllowedFacet::minmax('rating'), |
| 14 | + AllowedFacet::terms('tags'), |
| 15 | + ]) |
| 16 | + ->allowedFilters([ |
| 17 | + AllowedFilter::exact('tags'), |
| 18 | + AllowedFilter::exact('rating'), |
| 19 | + ]); |
| 20 | + |
| 21 | + $request = [ |
| 22 | + 'facet' => ['tags', 'rating'], |
| 23 | + 'filter' => ['tags' => 'water'], |
| 24 | + ]; |
| 25 | + |
| 26 | + facetQuery($spec, $request) |
| 27 | + ->assertBucketKeys('tags', ['water', 'drinks', 'clothes', 'gloves', 'video']) |
| 28 | + ->assertMinMax('rating', 8, 10); |
| 29 | +}); |
| 30 | + |
| 31 | +test('facets in nested specification', function () { |
| 32 | + $spec = CompositeSpecification::new() |
| 33 | + ->nested('offers', function (Specification $spec) { |
| 34 | + $spec |
| 35 | + ->allowedFilters([ |
| 36 | + AllowedFilter::exact('price'), |
| 37 | + AllowedFilter::exact('seller_id'), |
| 38 | + ]) |
| 39 | + ->allowedFacets([ |
| 40 | + AllowedFacet::minmax('price'), |
| 41 | + AllowedFacet::terms('seller_id'), |
| 42 | + ]); |
| 43 | + }); |
| 44 | + |
| 45 | + $request = [ |
| 46 | + 'facet' => ['price', 'seller_id'], |
| 47 | + 'filter' => ['seller_id' => 20], |
| 48 | + ]; |
| 49 | + |
| 50 | + facetQuery($spec, $request) |
| 51 | + ->assertBucketKeys('seller_id', [10, 15, 20, 90]) |
| 52 | + ->assertMinMax('price', 200, 28990); |
| 53 | +}); |
| 54 | + |
| 55 | +test('facets in different nested specification', function () { |
| 56 | + $spec = CompositeSpecification::new() |
| 57 | + ->nested('offers', function (Specification $spec) { |
| 58 | + $spec->allowedFilters([AllowedFilter::exact('price')]) |
| 59 | + ->allowedFacets([AllowedFacet::minmax('price')]); |
| 60 | + }) |
| 61 | + ->nested('offers', function (Specification $spec) { |
| 62 | + $spec->allowedFilters([AllowedFilter::exact('seller_id')]) |
| 63 | + ->allowedFacets([AllowedFacet::terms('seller_id')]); |
| 64 | + }); |
| 65 | + |
| 66 | + $request = [ |
| 67 | + 'facet' => ['price', 'seller_id'], |
| 68 | + 'filter' => ['seller_id' => 90], |
| 69 | + ]; |
| 70 | + |
| 71 | + facetQuery($spec, $request) |
| 72 | + ->assertBucketKeys('seller_id', [10, 15, 20, 90]) |
| 73 | + ->assertMinMax('price', 980, 41999); |
| 74 | +}); |
| 75 | + |
| 76 | +test('facets in root and nested specifications', function () { |
| 77 | + $spec = CompositeSpecification::new() |
| 78 | + ->nested('offers', function (Specification $spec) { |
| 79 | + $spec->allowedFilters([AllowedFilter::exact('seller_id')]) |
| 80 | + ->allowedFacets([AllowedFacet::terms('seller_id')]); |
| 81 | + }) |
| 82 | + ->allowedFacets([AllowedFacet::terms('tags')]) |
| 83 | + ->allowedFilters([AllowedFilter::exact('tags')]); |
| 84 | + |
| 85 | + $request = [ |
| 86 | + 'facet' => ['tags', 'seller_id'], |
| 87 | + 'filter' => ['tags' => 'water', 'seller_id' => 20], |
| 88 | + ]; |
| 89 | + |
| 90 | + facetQuery($spec, $request) |
| 91 | + ->assertBucketKeys('tags', ['water', 'drinks', 'video']) |
| 92 | + ->assertBucketKeys('seller_id', [10, 15, 20]); |
| 93 | +}); |
0 commit comments