|
| 1 | +<?php |
| 2 | + |
| 3 | +use Ensi\LaravelElasticQuery\Contracts\AggregationsBuilder; |
| 4 | +use Ensi\LaravelElasticQuerySpecification\Faceting\AllowedFacet; |
| 5 | +use Ensi\LaravelElasticQuerySpecification\Filtering\AllowedFilter; |
| 6 | +use Ensi\LaravelElasticQuerySpecification\Processors\FacetQueryProcessor; |
| 7 | +use Ensi\LaravelElasticQuerySpecification\Specification\Specification; |
| 8 | +use Ensi\LaravelElasticQuerySpecification\Tests\Unit\Processors\FluentProcessor; |
| 9 | + |
| 10 | +uses()->group('unit'); |
| 11 | + |
| 12 | +test('process root specification with current facet', function () { |
| 13 | + $facet = AllowedFacet::terms('foo'); |
| 14 | + $facet->attachFilter(AllowedFilter::exact('foo')->default(10)); |
| 15 | + $facet->enable(); |
| 16 | + |
| 17 | + $spec = Specification::new()->allowedFacets([$facet]); |
| 18 | + |
| 19 | + $query = Mockery::mock(AggregationsBuilder::class); |
| 20 | + $query->expects('terms')->once(); |
| 21 | + $query->expects('where')->never(); |
| 22 | + |
| 23 | + FluentProcessor::new(FacetQueryProcessor::class, $query, 'foo') |
| 24 | + ->visitRoot($spec) |
| 25 | + ->done(); |
| 26 | +}); |
| 27 | + |
| 28 | +test('process root specification without current facet', function () { |
| 29 | + $facet = AllowedFacet::terms('foo'); |
| 30 | + $facet->attachFilter(AllowedFilter::exact('foo')->default(10)); |
| 31 | + $facet->enable(); |
| 32 | + |
| 33 | + $spec = Specification::new()->allowedFacets([$facet]); |
| 34 | + |
| 35 | + $query = Mockery::mock(AggregationsBuilder::class); |
| 36 | + $query->expects('terms')->never(); |
| 37 | + $query->expects('where')->with('foo', 10)->once(); |
| 38 | + |
| 39 | + FluentProcessor::new(FacetQueryProcessor::class, $query, 'bar') |
| 40 | + ->visitRoot($spec) |
| 41 | + ->done(); |
| 42 | +}); |
| 43 | + |
| 44 | +test('process nested specification with current facet', function () { |
| 45 | + $filter = AllowedFilter::exact('foo')->default(10); |
| 46 | + $facet = AllowedFacet::terms('foo'); |
| 47 | + $facet->attachFilter($filter); |
| 48 | + $facet->enable(); |
| 49 | + |
| 50 | + $spec = Specification::new() |
| 51 | + ->allowedFacets([$facet]) |
| 52 | + ->allowedFilters([$filter]); |
| 53 | + |
| 54 | + $query = Mockery::mock(AggregationsBuilder::class); |
| 55 | + $query->allows('nested') |
| 56 | + ->with('field', any()) |
| 57 | + ->andReturnUsing(function ($field, callable $callback) use ($query) { |
| 58 | + $callback($query); |
| 59 | + |
| 60 | + return $query; |
| 61 | + }); |
| 62 | + |
| 63 | + $query->expects('terms')->once(); |
| 64 | + $query->expects('where')->never(); |
| 65 | + |
| 66 | + FluentProcessor::new(FacetQueryProcessor::class, $query, 'foo') |
| 67 | + ->visitNested('field', $spec) |
| 68 | + ->done(); |
| 69 | +}); |
| 70 | + |
| 71 | +test('process nested specification without current facet', function () { |
| 72 | + $filter = AllowedFilter::exact('foo')->default(10); |
| 73 | + $facet = AllowedFacet::terms('foo'); |
| 74 | + $facet->attachFilter($filter); |
| 75 | + $facet->enable(); |
| 76 | + |
| 77 | + $spec = Specification::new() |
| 78 | + ->allowedFacets([$facet]) |
| 79 | + ->allowedFilters([$filter]) |
| 80 | + ->where('bar', 50); |
| 81 | + |
| 82 | + $query = Mockery::mock(AggregationsBuilder::class); |
| 83 | + $query->allows('nested') |
| 84 | + ->with('field', any()) |
| 85 | + ->andReturnUsing(function ($field, callable $callback) use ($query) { |
| 86 | + $callback($query); |
| 87 | + |
| 88 | + return $query; |
| 89 | + }); |
| 90 | + |
| 91 | + $query->expects('terms')->never(); |
| 92 | + $query->expects('where')->with('foo', 10)->once(); |
| 93 | + $query->expects('where')->with('bar', 50)->once(); |
| 94 | + |
| 95 | + FluentProcessor::new(FacetQueryProcessor::class, $query, 'bar') |
| 96 | + ->visitNested('field', $spec) |
| 97 | + ->done(); |
| 98 | +}); |
0 commit comments