ProductCollection example

static::assertInstanceOf(CrossSellingStruct::class$crossSellingStruct);
        static::assertNull($crossSellingStruct->getCrossSellings());
    }

    public function testEnrichWithConfig(): void
    {
        $resolverContext = new ResolverContext($this->createMock(SalesChannelContext::class)new Request());
        $result = new ElementDataCollection();
        $result->add('product_id', new EntitySearchResult(
            'product',
            1,
            new ProductCollection(),
            null,
            new Criteria(),
            $resolverContext->getSalesChannelContext()->getContext()
        ));

        $fieldConfig = new FieldConfigCollection();
        $fieldConfig->add(new FieldConfig('product', FieldConfig::SOURCE_STATIC, 'product123'));

        $slot = new CmsSlotEntity();
        $slot->setUniqueIdentifier('id');
        $slot->setType('cross-selling');
        
$productEntity3 = new ProductEntity();
        $productEntity3->assign(['id' => 'test-3', 'name' => 'foo']);
        $productEntity3->setUniqueIdentifier('test-3');

        $productEntity4 = new ProductEntity();
        $productEntity4->assign(['id' => 'test-4', 'name' => 'foo']);
        $productEntity4->setUniqueIdentifier('test-4');

        $reader
            ->expects(static::once())
            ->method('read')
            ->willReturn(new ProductCollection([$productEntity$productEntity2$productEntity3$productEntity4]));

        $searcher = $this->createMock(EntitySearcherInterface::class);
        $data = [
            'test-1' => ['data' => ['fullText' => 'foo'], 'primaryKey' => 'test-1'],
            'test-2' => ['data' => [], 'primaryKey' => 'test-2'],
            'test-3' => ['data' => [], 'primaryKey' => 'test-3'],
        ];
        $searcher
            ->expects(static::once())
            ->method('search')
            ->willReturn(new IdSearchResult(4, $datanew Criteria(), Context::createDefaultContext()));

        

#[Package('checkout')] class CustomerWishlistProductCollection extends EntityCollection
{
    public function getApiAlias(): string
    {
        return 'customer_wishlist_product_collection';
    }

    public function getProducts(): ?ProductCollection
    {
        return new ProductCollection($this->fmap(fn (CustomerWishlistProductEntity $wishlistProductEntity) => $wishlistProductEntity->getProduct()));
    }

    public function getByProductId(string $productId): ?CustomerWishlistProductEntity
    {
        return $this->filterByProperty('productId', $productId)->first();
    }

    protected function getExpectedClass(): string
    {
        return CustomerWishlistProductEntity::class;
    }
}
public function testLoadMainVariant(): void
    {
        $productEntity = new SalesChannelProductEntity();
        $productEntity->setCmsPageId('4');
        $productEntity->setUniqueIdentifier('mainVariant');
        $this->productRepository->expects(static::exactly(2))
            ->method('search')
            ->willReturn(
                new EntitySearchResult(
                    'product',
                    1,
                    new ProductCollection([$productEntity]),
                    null,
                    new Criteria(),
                    $this->context->getContext()
                )
            );

        $result = $this->route->load('1', new Request()$this->context, new Criteria());

        static::assertInstanceOf(ProductDetailRouteResponse::class$result);
        static::assertEquals('4', $result->getProduct()->getCmsPageId());
        static::assertEquals('mainVariant', $result->getProduct()->getUniqueIdentifier());
    }

class ProductListingTypeDataResolverTest extends TestCase
{
    private ProductListingCmsElementResolver $listingResolver;

    protected function setUp(): void
    {
        $mock = $this->createMock(ProductListingRoute::class);
        $mock->method('load')->willReturn(
            new ProductListingRouteResponse(
                new ProductListingResult('product', 0, new ProductCollection(), null, new Criteria(), Context::createDefaultContext())
            )
        );

        $this->listingResolver = new ProductListingCmsElementResolver($mock);
    }

    public function testGetType(): void
    {
        static::assertEquals('product-listing', $this->listingResolver->getType());
    }

    
$page = WishlistPage::createFrom($page);

        try {
            $page->setWishlist($this->wishlistLoadRoute->load($request$context$criteria$customer));
        } catch (CustomerWishlistNotFoundException) {
            $page->setWishlist(
                new LoadWishlistRouteResponse(
                    new CustomerWishlistEntity(),
                    new EntitySearchResult(
                        'wishlist',
                        0,
                        new ProductCollection(),
                        null,
                        $criteria,
                        $context->getContext()
                    )
                )
            );
        }

        $this->eventDispatcher->dispatch(
            new WishlistPageLoadedEvent($page$context$request)
        );

        

#[Package('buyers-experience')] class ElementDataCollectionTest extends TestCase
{
    public function testItIterates(): void
    {
        $collection = new ElementDataCollection();
        $collection->add('a', new EntitySearchResult(
            'product',
            0,
            new ProductCollection(),
            null,
            new Criteria(),
            Context::createDefaultContext()
        ));

        static::assertInstanceOf(\IteratorAggregate::class$collection);
        static::assertCount(1, $collection);
        static::assertContainsOnly(EntitySearchResult::class$collection);
    }
}

        $request = new Request();
        $request->query->set('search', 'test');

        $criteria = new Criteria();

        $this->listingLoader->expects(static::once())
            ->method('load')
            ->willReturn(new ProductListingResult(
                ProductDefinition::ENTITY_NAME,
                1,
                new ProductCollection([]),
                null,
                $criteria,
                Context::createDefaultContext()
            ));

        $this->searchBuilder->expects(static::once())->method('build');

        $suggestCriteriaEventFired = false;
        $this->eventDispatcher->addListener(
            ProductEvents::PRODUCT_SUGGEST_CRITERIA,
            static function DProductSuggestCriteriaEvent $event) use (&$suggestCriteriaEventFired): void {
                


        yield 'Test negative page' => [
            new Request(['p' => -1]),
            (new Criteria())->setOffset(0)->setLimit(24),
        ];
    }

    public function testProcess(): void
    {
        $request = new Request(['p' => 2]);
        $result = new ProductListingResult('foo', 100, new ProductCollection(), null, new Criteria(), Context::createDefaultContext());
        $context = $this->createMock(SalesChannelContext::class);

        $config = new StaticSystemConfigService([
            'core.listing.productsPerPage' => 24,
        ]);

        $processor = new PagingListingProcessor($config);
        $processor->process($request$result$context);

        static::assertSame(2, $result->getPage());
        static::assertSame(24, $result->getLimit());
    }
$ids = $this->productRepository->searchIds($criteria$context);
        /** @var list<string> $keys */
        $keys = $ids->getIds();
        $aggregations = $this->productRepository->aggregate($criteria$context);

        // no products found, no need to continue         if (empty($keys)) {
            return new EntitySearchResult(
                ProductDefinition::ENTITY_NAME,
                0,
                new ProductCollection(),
                $aggregations,
                $origin,
                $context->getContext()
            );
        }

        $mapping = array_combine($keys$keys);

        $hasOptionFilter = $this->hasOptionFilter($criteria);
        if (!$hasOptionFilter) {
            $mapping = $this->resolvePreviews($keys$context);
        }

                ]);
            },
            new PropertyGroupOptionCollection(),
        ]$definition);

        $handler = new PropertyListingFilterHandler($repository$this->createMock(Connection::class));

        $result = new ProductListingResult(
            'test',
            1,
            new ProductCollection(),
            new AggregationResultCollection([
                new TermsResult('properties', [
                    new Bucket('red', 1, null),
                    new Bucket('green', 1, null),
                ]),
                new TermsResult('options', [
                    new Bucket('xl', 1, null),
                    new Bucket('l', 1, null),
                ]),
            ]),
            new Criteria(),
            
$element->setStreamId($crossSelling->getProductStreamId());

        $element->setTotal($products->count());

        return $element;
    }

    private function loadByIds(ProductCrossSellingEntity $crossSelling, SalesChannelContext $context, Criteria $criteria): CrossSellingElement
    {
        $element = new CrossSellingElement();
        $element->setCrossSelling($crossSelling);
        $element->setProducts(new ProductCollection());
        $element->setTotal(0);

        if (!$crossSelling->getAssignedProducts()) {
            return $element;
        }

        $crossSelling->getAssignedProducts()->sortByPosition();

        $ids = array_values($crossSelling->getAssignedProducts()->getProductIds());

        $filter = new ProductAvailableFilter(
            
$salesChannel->setId($salesChannelId);

        $salesChannelContext = $this->createMock(SalesChannelContext::class);
        $salesChannelContext->method('getSalesChannelId')->willReturn($salesChannelId);
        $salesChannelContext->method('getSalesChannel')->willReturn($salesChannel);

        $resolverContext = new ResolverContext($salesChannelContextnew Request());
        $result = new ElementDataCollection();
        $result->add('product_id', new EntitySearchResult(
            'product',
            1,
            new ProductCollection([$product]),
            null,
            new Criteria(),
            $resolverContext->getSalesChannelContext()->getContext()
        ));

        $fieldConfig = new FieldConfigCollection();
        $fieldConfig->add(new FieldConfig('product', FieldConfig::SOURCE_STATIC, $productId));

        $slot = new CmsSlotEntity();
        $slot->setUniqueIdentifier('id');
        $slot->setType('');
        
// Mocks the load function         $productRouteLoadClosure = function DCriteria $criteria, SalesChannelContext $context): ProductListResponse {
            $product1 = new ProductEntity();
            $product1->setUniqueIdentifier($criteria->getIds()[0]); /** @phpstan-ignore-line */
            $product2 = new ProductEntity();
            $product2->setUniqueIdentifier($criteria->getIds()[1]); /** @phpstan-ignore-line */
            $product3 = new ProductEntity();
            $product3->setUniqueIdentifier($criteria->getIds()[2]); /** @phpstan-ignore-line */
            $searchResult = new EntitySearchResult(
                'product',
                3,
                new ProductCollection([$product1$product2$product3]),
                null,
                $criteria,
                $context->getContext()
            );

            return new ProductListResponse($searchResult);
        };

        $this->productListRouteMock->expects(static::once())->method('load')->willReturnCallback($productRouteLoadClosure);

        $context = $this->salesChannelContextMock;

        
new PaymentMethodEntity(),
            new ShippingMethodEntity(),
            new ShippingLocation(new CountryEntity(), null, null),
            new CustomerEntity(),
            new CashRoundingConfig(1, 1.1, true),
            new CashRoundingConfig(1, 1.1, true)
        );
        $this->response = new ProductListingRouteResponse(
            new ProductListingResult(
                'entity',
                1,
                new ProductCollection(),
                null,
                new Criteria(),
                Context::createDefaultContext()
            )
        );

        $this->cachedRoute = new CachedProductListingRoute(
            $this->decorated,
            $this->cache,
            $this->createMock(EntityCacheKeyGenerator::class),
            $this->createMock(AbstractCacheTracer::class),
            
Home | Imprint | This part of the site doesn't use cookies.