CategoryCollection example

$definition = $this->getContainer()->get(CategoryDefinition::class);
        $request = Request::create($path, 'GET', [][][]['HTTP_ACCEPT' => $accept]);
        $this->setOrigin($request$context);

        return $this->getFactory($request)->createDetailResponse(new Criteria()$category$definition$request$context$setLocationHeader);
    }

    private function getListResponse(Context $context, string $id, string $path, string $accept): Response
    {
        $category = $this->getTestCategory($id);

        $col = new CategoryCollection([$category]);
        $criteria = new Criteria();
        $searchResult = new EntitySearchResult('product', 1, $col, null, $criteria$context);

        $definition = $this->getContainer()->get(CategoryDefinition::class);
        $request = Request::create($path, 'GET', [][][]['HTTP_ACCEPT' => $accept]);
        $this->setOrigin($request$context);

        return $this->getFactory($request)->createListingResponse($criteria$searchResult$definition$request$context);
    }

    private function getTestCategory(string $id): CategoryEntity
    {
        $this->validate($activeId$active['path']$context);

        $isChild = $this->isChildCategory($activeId$active['path']$rootId);

        // If the provided activeId is not part of the rootId, a fallback to the rootId must be made here.         // The passed activeId is therefore part of another navigation and must therefore not be loaded.         // The availability validation has already been done in the `validate` function.         if (!$isChild) {
            $activeId = $rootId;
        }

        $categories = new CategoryCollection();
        if ($depth > 0) {
            // Load the first two levels without using the activeId in the query             $categories = $this->loadLevels($rootId(int) $root['level']$contextclone $criteria$depth);
        }

        // If the active category is part of the provided root id, we have to load the children and the parents of the active id         $categories = $this->loadChildren($activeId$context$rootId$metaInfo$categoriesclone $criteria);

        return new NavigationRouteResponse($categories);
    }

    
public function testTreeBuilderWithSimpleTree(): void
    {
        $loader = new NavigationLoader(
            $this->createMock(EventDispatcher::class),
            $this->createMock(NavigationRoute::class)
        );

        $categories = $this->createSimpleTree();

        /** @var Tree $tree */
        $tree = ReflectionHelper::getMethod(NavigationLoader::class, 'getTree')->invoke($loader, '1', new CategoryCollection($categories), \array_shift($categories));

        $treeItems = $tree->getTree();

        static::assertCount(3, $treeItems);
        static::assertCount(2, $treeItems['1.1']->getChildren());
        static::assertCount(0, $treeItems['1.1']->getChildren()['1.1.1']->getChildren());
        static::assertCount(0, $treeItems['1.1']->getChildren()['1.1.2']->getChildren());
        static::assertCount(2, $treeItems['1.2']->getChildren());
        static::assertCount(1, $treeItems['1.2']->getChildren()['1.2.1']->getChildren());
        static::assertCount(1, $treeItems['1.2']->getChildren()['1.2.2']->getChildren());
        static::assertCount(0, $treeItems['1.3']->getChildren());
    }
$categories = $this->buildTree($rootId$response->getCategories()->getElements());

        return new NavigationRouteResponse($categories);
    }

    /** * @param CategoryEntity[] $categories */
    private function buildTree(?string $parentId, array $categories): CategoryCollection
    {
        $children = new CategoryCollection();
        foreach ($categories as $key => $category) {
            if ($category->getParentId() !== $parentId) {
                continue;
            }

            unset($categories[$key]);

            $children->add($category);
        }

        $children->sortByPosition();

        
static::assertNotNull($categoryEntity);
    }

    /** * @param array<CategoryEntity> $categoryEntityCollection1 * @param array<CategoryEntity> $categoryEntityCollection2 */
    private function getCategoryRepositoryMock(array $categoryEntityCollection1, array $categoryEntityCollection2): EntityRepository
    {
        $categoryRepositoryMock = $this->createMock(EntityRepository::class);
        $categoryRepositoryMock->method('search')->willReturnOnConsecutiveCalls(
            new EntitySearchResult('category', 1, new CategoryCollection($categoryEntityCollection1), null, new Criteria()$this->context->getContext()),
            new EntitySearchResult('category', 1, new CategoryCollection($categoryEntityCollection2), null, new Criteria()$this->context->getContext()),
        );

        return $categoryRepositoryMock;
    }

    /** * @param array<string> $streamIds * @param array<string>|null $categoryIds */
    private function getProductEntity(?array $streamIds, ?array $categoryIds): ProductEntity
    {
new CurrencyEntity(),
            new CustomerGroupEntity(),
            new TaxCollection(),
            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 NavigationRouteResponse(
            new CategoryCollection()
        );

        $this->cachedRoute = new CachedNavigationRoute(
            $this->decorated,
            $this->cache,
            $this->createMock(EntityCacheKeyGenerator::class),
            $this->createMock(AbstractCacheTracer::class),
            $this->eventDispatcher,
            []
        );
    }

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

        return $page;
    }

    private function getServiceMenu(SalesChannelContext $context): CategoryCollection
    {
        $serviceId = $context->getSalesChannel()->getServiceCategoryId();

        if ($serviceId === null) {
            return new CategoryCollection();
        }

        $navigation = $this->navigationLoader->load($serviceId$context$serviceId, 1);

        return new CategoryCollection(array_map(static fn (TreeItem $treeItem) => $treeItem->getCategory()$navigation->getTree()));
    }

    private function getLanguages(SalesChannelContext $context, Request $request): LanguageCollection
    {
        $criteria = new Criteria();
        $criteria->setTitle('header::languages');

        
Home | Imprint | This part of the site doesn't use cookies.