|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Spatie\ElasticsearchQueryBuilder\Tests\Aggregations; |
| 4 | + |
| 5 | +use PHPUnit\Framework\TestCase; |
| 6 | +use Spatie\ElasticsearchQueryBuilder\Aggregations\DateHistogramAggregation; |
| 7 | +use Spatie\ElasticsearchQueryBuilder\Aggregations\TermsAggregation; |
| 8 | + |
| 9 | +class DateHistogramAggregationTest extends TestCase |
| 10 | +{ |
| 11 | + public function testCreateReturnsNewInstance(): void |
| 12 | + { |
| 13 | + $aggregation = DateHistogramAggregation::create('test_name', 'test_field', '1h'); |
| 14 | + |
| 15 | + self::assertInstanceOf(DateHistogramAggregation::class, $aggregation); |
| 16 | + } |
| 17 | + |
| 18 | + public function testDefaultSetup(): void |
| 19 | + { |
| 20 | + $aggregation = new DateHistogramAggregation('test_name', 'test_field', '1h'); |
| 21 | + |
| 22 | + self::assertEquals([ |
| 23 | + 'date_histogram' => [ |
| 24 | + 'field' => 'test_field', |
| 25 | + 'calendar_interval' => '1h', |
| 26 | + ], |
| 27 | + ], $aggregation->toArray()); |
| 28 | + } |
| 29 | + |
| 30 | + public function testDefaultSetupWithAdditionalAggregation(): void |
| 31 | + { |
| 32 | + $aggregation = (new DateHistogramAggregation('test_name', 'test_field', '1h'))->aggregation(new TermsAggregation('test_agg_name_1', 'test_agg_field_1')); |
| 33 | + |
| 34 | + self::assertEquals([ |
| 35 | + 'date_histogram' => [ |
| 36 | + 'field' => 'test_field', |
| 37 | + 'calendar_interval' => '1h', |
| 38 | + ], |
| 39 | + 'aggs' => [ |
| 40 | + 'test_agg_name_1' => [ |
| 41 | + 'terms' => [ |
| 42 | + 'field' => 'test_agg_field_1', |
| 43 | + ], |
| 44 | + ], |
| 45 | + ], |
| 46 | + ], $aggregation->toArray()); |
| 47 | + } |
| 48 | + |
| 49 | + public function testFullSetup(): void |
| 50 | + { |
| 51 | + $aggregation = new DateHistogramAggregation( |
| 52 | + 'test_name', |
| 53 | + 'test_field', |
| 54 | + '1h', |
| 55 | + new TermsAggregation('test_agg_name_1', 'test_agg_field_1'), |
| 56 | + new TermsAggregation('test_agg_name_2', 'test_agg_field_2') |
| 57 | + ); |
| 58 | + |
| 59 | + self::assertEquals([ |
| 60 | + 'date_histogram' => [ |
| 61 | + 'field' => 'test_field', |
| 62 | + 'calendar_interval' => '1h', |
| 63 | + ], |
| 64 | + 'aggs' => [ |
| 65 | + 'test_agg_name_1' => [ |
| 66 | + 'terms' => [ |
| 67 | + 'field' => 'test_agg_field_1', |
| 68 | + ], |
| 69 | + ], |
| 70 | + 'test_agg_name_2' => [ |
| 71 | + 'terms' => [ |
| 72 | + 'field' => 'test_agg_field_2', |
| 73 | + ], |
| 74 | + ], |
| 75 | + ], |
| 76 | + ], $aggregation->toArray()); |
| 77 | + } |
| 78 | + |
| 79 | + public function testDefaultSetupWithStaticCreateFunction(): void |
| 80 | + { |
| 81 | + $aggregation = DateHistogramAggregation::create('test_name', 'test_field', '1h'); |
| 82 | + |
| 83 | + self::assertEquals([ |
| 84 | + 'date_histogram' => [ |
| 85 | + 'field' => 'test_field', |
| 86 | + 'calendar_interval' => '1h', |
| 87 | + ], |
| 88 | + ], $aggregation->toArray()); |
| 89 | + } |
| 90 | + |
| 91 | + public function testDefaultSetupWithAdditionalAggregationWithStaticCreateFunction(): void |
| 92 | + { |
| 93 | + $aggregation = DateHistogramAggregation::create('test_name', 'test_field', '1h')->aggregation(TermsAggregation::create('test_agg_name_1', 'test_agg_field_1')); |
| 94 | + |
| 95 | + self::assertEquals([ |
| 96 | + 'date_histogram' => [ |
| 97 | + 'field' => 'test_field', |
| 98 | + 'calendar_interval' => '1h', |
| 99 | + ], |
| 100 | + 'aggs' => [ |
| 101 | + 'test_agg_name_1' => [ |
| 102 | + 'terms' => [ |
| 103 | + 'field' => 'test_agg_field_1', |
| 104 | + ], |
| 105 | + ], |
| 106 | + ], |
| 107 | + ], $aggregation->toArray()); |
| 108 | + } |
| 109 | + |
| 110 | + public function testFullSetupWithStaticCreateFunction(): void |
| 111 | + { |
| 112 | + $aggregation = DateHistogramAggregation::create( |
| 113 | + 'test_name', |
| 114 | + 'test_field', |
| 115 | + '1h', |
| 116 | + TermsAggregation::create('test_agg_name_1', 'test_agg_field_1'), |
| 117 | + TermsAggregation::create('test_agg_name_2', 'test_agg_field_2') |
| 118 | + ); |
| 119 | + |
| 120 | + self::assertEquals([ |
| 121 | + 'date_histogram' => [ |
| 122 | + 'field' => 'test_field', |
| 123 | + 'calendar_interval' => '1h', |
| 124 | + ], |
| 125 | + 'aggs' => [ |
| 126 | + 'test_agg_name_1' => [ |
| 127 | + 'terms' => [ |
| 128 | + 'field' => 'test_agg_field_1', |
| 129 | + ], |
| 130 | + ], |
| 131 | + 'test_agg_name_2' => [ |
| 132 | + 'terms' => [ |
| 133 | + 'field' => 'test_agg_field_2', |
| 134 | + ], |
| 135 | + ], |
| 136 | + ], |
| 137 | + ], $aggregation->toArray()); |
| 138 | + } |
| 139 | +} |
0 commit comments