PrefixFilter example

use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\PrefixFilter;

/** * @internal * * @covers \Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\PrefixFilter */
class PrefixFilterTest extends TestCase
{
    public function testEncode(): void
    {
        $filter = new PrefixFilter('foo', 'bar');

        static::assertEquals(
            [
                'field' => 'foo',
                'value' => 'bar',
                'isPrimary' => false,
                'resolved' => null,
                'extensions' => [],
                '_class' => PrefixFilter::class,
            ],
            $filter->jsonSerialize()
        );
return new ContainsFilter(self::buildFieldName($definition$query['field'])$query['value']);
            case 'prefix':
                if (empty($query['field'])) {
                    throw DataAbstractionLayerException::invalidFilterQuery('Parameter "field" for prefix filter is missing.', $path . '/field');
                }

                if (!isset($query['value']) || $query['value'] === '') {
                    throw DataAbstractionLayerException::invalidFilterQuery('Parameter "value" for prefix filter is missing.', $path . '/value');
                }

                return new PrefixFilter(self::buildFieldName($definition$query['field'])$query['value']);
            case 'suffix':
                if (empty($query['field'])) {
                    throw DataAbstractionLayerException::invalidFilterQuery('Parameter "field" for suffix filter is missing.', $path . '/field');
                }

                if (!isset($query['value']) || $query['value'] === '') {
                    throw DataAbstractionLayerException::invalidFilterQuery('Parameter "value" for suffix filter is missing.', $path . '/value');
                }

                return new SuffixFilter(self::buildFieldName($definition$query['field'])$query['value']);

            
$criteria = (new Criteria())->addFilter(new ContainsFilter('link', ' \\ '));
        $foundIds = $this->manufacturerRepository->searchIds($criteria, Context::createDefaultContext());

        static::assertContains($targetId$foundIds->getIds());
        static::assertNotContains($errournousId$foundIds->getIds());
    }

    public function testPrefixFilterFindUnderscore(): void
    {
        $targetId = $this->createManufacturer(['link' => 'target_to_find']);
        $erroneousId = $this->createManufacturer(['link' => 'target to find']);
        $criteria = (new Criteria())->addFilter(new PrefixFilter('link', 'target_to'));
        $foundIds = $this->manufacturerRepository->searchIds($criteria, Context::createDefaultContext());

        static::assertContains($targetId$foundIds->getIds());
        static::assertNotContains($erroneousId$foundIds->getIds());
    }

    public function testPrefixFilterFindPercentageSign(): void
    {
        $targetId = $this->createManufacturer(['link' => 'target%find']);
        $erroneousId = $this->createManufacturer(['link' => 'target to find']);
        $criteria = (new Criteria())->addFilter(new PrefixFilter('link', 'target%fi'));
        


    /** * @depends testIndexing */
    public function testPrefixFilter(IdsCollection $data): void
    {
        try {
            $searcher = $this->createEntitySearcher();
            $criteria = new Criteria();
            $criteria->addState(Criteria::STATE_ELASTICSEARCH_AWARE);
            $criteria->addFilter(new PrefixFilter('product.name', 'Sti'));

            $products = $searcher->search($this->productDefinition, $criteria$this->context);
            static::assertCount(1, $products->getIds());
            static::assertSame(1, $products->getTotal());
            static::assertContains($data->get('product-3')$products->getIds());

            $criteria = new Criteria();
            $criteria->addState(Criteria::STATE_ELASTICSEARCH_AWARE);
            $criteria->addFilter(new PrefixFilter('product.name', 'subber'));

            $products = $searcher->search($this->productDefinition, $criteria$this->context);
            
static::assertFalse($result->has($ids->get('product-2')));
    }

    /** * @depends testIndexing */
    public function testPrefixFilter(IdsCollection $ids): void
    {
        $criteria = new Criteria($ids->prefixed('product-'));
        // "re" refers to the property "red" of "product-1" and "product-2"         $criteria->addFilter(
            new PrefixFilter('product.properties.name', 're')
        );
        // "yell" refers to the property "yellow" of only "product-1"         $criteria->addFilter(
            new PrefixFilter('product.properties.name', 'yell')
        );

        $result = $this->getContainer()->get('product.repository')
            ->searchIds($criteria, Context::createDefaultContext());

        static::assertEquals(1, $result->getTotal());
        static::assertTrue($result->has($ids->get('product-1')));
        
static::assertSame($hash$generator->getCriteriaHash($criteria));
    }

    public static function criteriaHashProvider(): \Generator
    {
        yield 'empty' => [
            new Criteria(),
            '749322be59780dc4034598e25b3cd946',
        ];

        yield 'prefix-filter' => [
            (new Criteria())->addFilter(new PrefixFilter('foo', 'bar')),
            'a3def85d7155b475e330761d1eb8b1f1',
        ];

        // this has a different hash because of a different filter type used         yield 'suffix-filter' => [
            (new Criteria())->addFilter(new SuffixFilter('foo', 'bar')),
            'fa6fcaab1e5a33f0c7fdedb61bef8d22',
        ];

        yield 'filter+sort' => [
            (new Criteria())->addFilter(new PrefixFilter('foo', 'bar'))->addSorting(new FieldSorting('foo')),
            
'wildcard' => [
                                'name.' . Defaults::LANGUAGE_SYSTEM => [
                                    'value' => '*foo*',
                                ],
                            ],
                        ],
                    ],
                ],
            ],
        ];
        yield 'PrefixFilter field' => [
            new PrefixFilter('productNumber', 'foo'),
            [
                'prefix' => [
                    'productNumber' => [
                        'value' => 'foo',
                    ],
                ],
            ],
        ];
        yield 'PrefixFilter translated field' => [
            new PrefixFilter('name', 'foo'),
            [
                
Home | Imprint | This part of the site doesn't use cookies.