ProductEntity example

$response = $this->controller->switch($ids->get('product')new Request()$this->createMock(SalesChannelContext::class));

        static::assertEquals('{"url":"","productId":"' . $ids->get('product') . '"}', $response->getContent());
    }

    public function testQuickViewMinimal(): void
    {
        $ids = new IdsCollection();

        $request = new Request(['productId' => $ids->get('productId')]);
        $this->minimalQuickViewPageLoaderMock->method('load')->with($request)->willReturn(new MinimalQuickViewPage(new ProductEntity()));

        $response = $this->controller->quickviewMinimal(
            $request,
            $this->createMock(SalesChannelContext::class)
        );

        static::assertEquals(Response::HTTP_OK, $response->getStatusCode());
        static::assertInstanceOf(MinimalQuickViewPage::class$this->controller->renderStorefrontParameters['page']);
        static::assertInstanceOf(ProductQuickViewWidgetLoadedHook::class$this->controller->calledHook);
    }

    
$this->createMock(SalesChannelContext::class)
        );

        $subscriber->salesChannelLoaded($event);

        static::assertSame($expected$entity->get('cmsPageId'));
    }

    public static function resolveCmsPageIdProviderWithLoadedEventProvider(): \Generator
    {
        yield 'It does not set cms page id if already given' => [
            (new ProductEntity())->assign(['id' => Uuid::randomHex(), 'cmsPageId' => 'own-id']),
            new StaticSystemConfigService([self::CONFIG => 'config-id']),
            'own-id',
        ];

        yield 'It does not set if no default is given' => [
            (new ProductEntity())->assign(['id' => Uuid::randomHex(), 'cmsPageId' => null]),
            new StaticSystemConfigService(),
            null,
        ];

        yield 'It sets cms page id if none is given and default is provided' => [
            (

        $route = new ProductPageSeoUrlRoute($this->createMock(ProductDefinition::class));

        static::expectException(\InvalidArgumentException::class);
        $route->getMapping(new ArrayEntity()new SalesChannelEntity());
    }

    public function testMapping(): void
    {
        $route = new ProductPageSeoUrlRoute($this->createMock(ProductDefinition::class));

        $product = new ProductEntity();
        $product->setId('test');
        $data = $route->getMapping($productnew SalesChannelEntity());

        static::assertNull($data->getError());
        static::assertSame($product$data->getEntity());
        static::assertSame(['productId' => 'test']$data->getInfoPathContext());

        $context = $data->getSeoPathInfoContext();
        static::assertIsArray($context);
        static::assertArrayHasKey('product', $context);
        static::assertSame($product->jsonSerialize()$context['product']);
    }

class RequirementsCheckCollectionTest extends TestCase
{
    public function testGetExpectedClass(): void
    {
        $collection = new RequirementsCheckCollection();

        $collection->add(new PathCheck('name', RequirementCheck::STATUS_SUCCESS));

        static::expectException(\InvalidArgumentException::class);
        $collection->add(new ProductEntity()); /** @phpstan-ignore-line */
    }

    /** * @param RequirementCheck[] $elements * @param RequirementCheck[] $expected * * @dataProvider pathCheckProvider */
    public function testGetPathChecks(array $elements, array $expected): void
    {
        $collection = new RequirementsCheckCollection($elements);

        
static::assertEquals('test', $encoded['name']);
    }

    public function testExtensionAreSupported(): void
    {
        $registry = new StaticDefinitionInstanceRegistry(
            [ExtensionDefinition::class],
            $this->createMock(ValidatorInterface::class),
            $this->createMock(EntityWriteGatewayInterface::class)
        );

        $product = new ProductEntity();
        $product->internalSetEntityData('product', new FieldVisibility([]));

        $manufacturer = (new ProductManufacturerEntity())->assign(['name' => 'foo']);
        $product->addExtension('exposedExtension', $manufacturer);
        $product->addExtension('notExposedExtension', $manufacturer);
        $product->setName('test');

        $product->addExtension('foreignKeys', new ArrayStruct(['exposedFk' => 'exposed', 'notExposedFk' => 'not_exposed'], 'product'));
        $product->addExtension('search', new ArrayEntity(['score' => 2000]));

        $serializer = new Serializer([new StructNormalizer()][new JsonEncoder()]);

        
'field' => 'customFields.notExists',
                'tokenize' => true,
                'ranking' => 100,
            ],
            [
                'field' => 'customFields',
                'tokenize' => true,
                'ranking' => 100,
            ],
        ];

        $product = new ProductEntity();
        $product->setCustomFields([
            'field1' => 'searchable',
            'field2' => 'match',
            'field3' => ['array'],
            'field4' => 10000000,
            'field5' => false,
            'field6' => 10.99999,
            'nestedField' => [
                'value' => 'nested',
                'second' => 'ignored',
            ],
            

        $builder = new ProductVariationBuilder();

        $builder->build($product);

        static::assertEquals($expected$product->get('variation'));
    }

    public static function buildingProvider(): \Generator
    {
        yield 'Test without options' => [
            new ProductEntity(),
            [],
        ];

        yield 'Test without loaded option groups' => [
            (new ProductEntity())->assign([
                '_uniqueIdentifier' => Uuid::randomHex(),
                'options' => new PropertyGroupOptionCollection([
                    (new PropertyGroupOptionEntity())->assign([
                        '_uniqueIdentifier' => Uuid::randomHex(),
                        'name' => 'red',
                    ]),
                ]),
static::assertSame($media$imageStruct->getMedia());
    }

    public function testMediaWithLocal(): void
    {
        $media = new MediaEntity();
        $media->setUniqueIdentifier('media123');

        $productMedia = new ProductMediaEntity();
        $productMedia->setMedia($media);

        $product = new ProductEntity();
        $product->setCover($productMedia);

        $resolverContext = new EntityResolverContext($this->createMock(SalesChannelContext::class)new Request()$this->createMock(ProductDefinition::class)$product);

        $mediaSearchResult = new EntitySearchResult(
            'media',
            0,
            new MediaCollection(),
            null,
            new Criteria(),
            Context::createDefaultContext()
        );
$this->textResolver->enrich($slot$resolverContext$result);

        /** @var TextStruct|null $textStruct */
        $textStruct = $slot->getData();
        static::assertInstanceOf(TextStruct::class$textStruct);
        static::assertSame('lorem', $textStruct->getContent());
    }

    public function testWithMappedContent(): void
    {
        $product = new ProductEntity();
        $product->setDescription('foobar loo');

        $resolverContext = new EntityResolverContext($this->createMock(SalesChannelContext::class)new Request()$this->createMock(ProductDefinition::class)$product);
        $result = new ElementDataCollection();

        $fieldConfig = new FieldConfigCollection();
        $fieldConfig->add(new FieldConfig('content', FieldConfig::SOURCE_MAPPED, 'product.description'));

        $slot = new CmsSlotEntity();
        $slot->setUniqueIdentifier('id');
        $slot->setType('text');
        

        return 100;
    }

    public function setWithReturnType(string $name): object
    {
        return new \stdClass();
    }

    public function setWithObjectReturnType(string $name): ProductEntity
    {
        return new ProductEntity();
    }
}
public function testGetDecoratedShouldThrowException(): void
    {
        static::expectException(DecorationPatternException::class);
        $this->searchIndexer->getDecorated();
    }

    public function testGlobalData(): void
    {
        $context = Context::createDefaultContext();
        $repository = $this->createMock(EntityRepository::class);
        $product = new ProductEntity();
        $product->setUniqueIdentifier(Uuid::randomHex());
        $repository->method('search')->willReturn(
            new EntitySearchResult(
                'product',
                1,
                new EntityCollection([$product]),
                null,
                new Criteria(),
                $context
            )
        );

        


    /** * @dataProvider analyzeCases * * @param array<string, mixed> $productData * @param array<int, array{field: string, tokenize: bool, ranking: int}> $configFields * @param array<int, string> $expected */
    public function testAnalyze(array $productData, array $configFields, array $expected): void
    {
        $product = new ProductEntity();
        $product->assign($productData);

        $tokenizer = new Tokenizer(3);
        $tokenFilter = $this->createMock(TokenFilter::class);
        $tokenFilter->method('filter')->willReturnCallback(fn (array $tokens) => $tokens);

        $analyzer = new ProductSearchKeywordAnalyzer($tokenizer$tokenFilter);
        $analyzer = $analyzer->analyze($product$this->context, $configFields);
        $analyzerResult = $analyzer->getKeys();

        sort($analyzerResult);
        
$this->controller->addLineItems($cartnew RequestDataBag($request->request->all())$request$context);
    }

    public function testAddByProductNumber(): void
    {
        $productNumber = Uuid::randomHex();
        $id = Uuid::randomHex();
        $request = new Request([]['number' => $productNumber]);
        $cart = new Cart(Uuid::randomHex());
        $context = $this->createMock(SalesChannelContext::class);
        $product = new ProductEntity();
        $product->setUniqueIdentifier($id);
        $product->setId($id);
        $item = new LineItem($id, PromotionProcessor::LINE_ITEM_TYPE);

        $cart->add($item);
        $this->productListRouteMock->expects(static::once())
            ->method('load')
            ->willReturn(
                new ProductListResponse(
                    new EntitySearchResult(
                        ProductDefinition::ENTITY_NAME,
                        
'sales_channel_definition.',
            new Container(),
            [],
            []
        );

        $registry->register(new ProductDefinition());

        static::assertInstanceOf(ProductDefinition::class$registry->get(ProductDefinition::class));
        static::assertTrue($registry->has(ProductDefinition::ENTITY_NAME));
        static::assertInstanceOf(ProductDefinition::class$registry->getByEntityName(ProductDefinition::ENTITY_NAME));
        static::assertInstanceOf(ProductDefinition::class$registry->getByEntityClass(new ProductEntity()));
    }

    public function testItThrowsExceptionWhenSalesChannelRepositoryWasNotFoundByEntityName(): void
    {
        $registry = new SalesChannelDefinitionInstanceRegistry(
            'sales_channel_definition.',
            new Container(),
            [],
            []
        );

        
$this->getContainer()->get(DefinitionInstanceRegistry::class),
            $this->getContainer()->get(SerializerRegistry::class),
            null,
            null,
            $this->getContainer()->get(PrimaryKeyResolver::class)
        );

        $sourceEntity = ProductDefinition::ENTITY_NAME;
        $config = new Config([]['sourceEntity' => $sourceEntity][]);
        $id = Uuid::randomHex();

        $product = (new ProductEntity())->assign([
            'id' => $id,
            'stock' => 101,
            'productNumber' => 'P101',
            'active' => true,
            'translations' => new ProductTranslationCollection([
                (new ProductTranslationEntity())->assign([
                    'languageId' => Defaults::LANGUAGE_SYSTEM,
                    'name' => 'test product',
                    '_uniqueIdentifier' => $id . '_' . Defaults::LANGUAGE_SYSTEM,
                ]),
            ]),
        ]);
Home | Imprint | This part of the site doesn't use cookies.