RangeAggregation example

yield 'empty from and to' => [null, 10, '*-10'];
        yield 'from and empty to' => [10, null, '10-*'];
    }

    /** * @dataProvider buildRangeKeyDataProvider */
    public function testBuildRangeKey(?float $from, ?float $to, string $expectedKey): void
    {
        $method = ReflectionHelper::getMethod(RangeAggregation::class, 'buildRangeKey');

        $aggregation = new RangeAggregation('test', 'test', []);

        static::assertEquals($expectedKey$method->invoke($aggregation$from$to));
    }

    /** * @return array<string, array{rangesDefinition: mixed, rangesExpectedResult: mixed}> */
    public static function rangeAggregationDataProvider(): iterable
    {
        yield 'default ranges test cases' => [
            'rangesDefinition' => [
                [],
case 'sum':
                return new SumAggregation($name$field);
            case 'count':
                return new CountAggregation($name$field);
            case 'range':
                if (!isset($aggregation['ranges'])) {
                    $exceptions->add(new InvalidAggregationQueryException('The aggregation should contain "ranges".'), '/aggregations/' . $index . '/' . $type . '/field');

                    return null;
                }

                return new RangeAggregation($name(string) $field$aggregation['ranges']);
            case 'entity':
                if (!isset($aggregation['definition'])) {
                    $exceptions->add(new InvalidAggregationQueryException('The aggregation should contain a "definition".'), '/aggregations/' . $index . '/' . $type . '/field');

                    return null;
                }

                return new EntityAggregation($name$field$aggregation['definition']);

            case 'filter':
                if (empty($aggregation['filter'])) {
                    

    public function testRangeAggregation(array $rangesDefinition, array $rangesExpectedResult, IdsCollection $data): void
    {
        $aggregator = $this->createEntityAggregator();
        $criteria = new Criteria($data->prefixed('product-'));
        $criteria->addState(Criteria::STATE_ELASTICSEARCH_AWARE);
        $criteria->addAggregation(new RangeAggregation('test-range-aggregation', 'product.stock', $rangesDefinition));

        $aggregations = $aggregator->aggregate($this->productDefinition, $criteria$this->context);

        static::assertTrue($aggregations->has('test-range-aggregation'));
        static::assertInstanceOf(RangeResult::class$aggregations->get('test-range-aggregation'));

        $rangesResult = $aggregations->get('test-range-aggregation')->getRanges();

        static::assertCount(\count($rangesDefinition)$rangesResult);
        foreach ($rangesResult as $key => $count) {
            static::assertArrayHasKey($key$rangesExpectedResult);
            
use Shopware\Core\Framework\DataAbstractionLayer\Search\Aggregation\Metric\RangeAggregation;

/** * @internal * * @covers \Shopware\Core\Framework\DataAbstractionLayer\Search\Aggregation\Metric\RangeAggregation */
class RangeAggregationTest extends TestCase
{
    public function testEncode(): void
    {
        $aggregation = new RangeAggregation('foo', 'bar', [['to' => 100]]);

        static::assertEquals([
            'name' => 'foo',
            'extensions' => [],
            'field' => 'bar',
            'ranges' => [
                [
                    'to' => 100.0,
                    'from' => null,
                    'key' => '*-100',
                ],
            ],
if ($nested = $aggregation->getAggregation()) {
            $composite->addAggregation(
                $this->parseNestedAggregation($nested$definition$context)
            );
        }

        return $composite;
    }

    protected function parseRangeAggregation(RangeAggregation $aggregation, string $fieldName): Bucketing\RangeAggregation
    {
        return new Bucketing\RangeAggregation(
            $aggregation->getName(),
            $fieldName,
            $aggregation->getRanges()
        );
    }

    /** * @return array<string, mixed> */
    private function getCheapestPriceParameters(Context $context): array
    {
        
Home | Imprint | This part of the site doesn't use cookies.