StructNormalizer example

$cart = new Cart($this->ids->get('cart'));
        $source = new Source('https://shopware.com', $this->ids->get('shop-id'), '1.0.0');
        $payload = new TaxProviderPayload($cart$salesChannelContext);
        $payload->setSource($source);

        $definitionInstanceRegistry = static::createMock(DefinitionInstanceRegistry::class);
        $definitionInstanceRegistry
            ->method('getByEntityClass')
            ->willReturn(new TaxProviderDefinition());

        $entityEncoder = new JsonEntityEncoder(
            new Serializer([new StructNormalizer()][new JsonEncoder()])
        );

        $appPayloadServiceHelper = new AppPayloadServiceHelper(
            $definitionInstanceRegistry,
            $entityEncoder,
            static::createMock(ShopIdProvider::class)
        );

        $array = $appPayloadServiceHelper->encode($payload);

        static::assertSame(['source' => $source, 'cart' => $cart, 'context' => []]$array);
    }
$versionContext = $context->createWithVersionId(Uuid::randomHex());

        static::assertNotNull($versionContext->getExtension('foo'));
    }

    public function testExtensionsAreStripped(): void
    {
        $classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
        $discriminator = new ClassDiscriminatorFromClassMetadata($classMetadataFactory);

        $normalizers = [new StructNormalizer()new ObjectNormalizer($classMetadataFactory, null, null, null, $discriminator)new ArrayDenormalizer()];
        $serializer = new Serializer($normalizers[new JsonEncoder()]);

        $context = Context::createDefaultContext();

        $context->addExtension('foo', new ArrayEntity());

        $serialized = $serializer->serialize($context, 'json');
        $deserialized = $serializer->deserialize($serialized, Context::class, 'json');

        static::assertInstanceOf(Context::class$deserialized);

        
$definitionInstanceRegistry = static::createMock(DefinitionInstanceRegistry::class);
        $definitionInstanceRegistry
            ->method('getByEntityClass')
            ->willReturn(new TaxProviderDefinition());

        $shopIdProvider = static::createMock(ShopIdProvider::class);
        $shopIdProvider
            ->method('getShopId')
            ->willReturn($this->ids->get('shop-id'));

        $entityEncoder = new JsonEntityEncoder(
            new Serializer([new StructNormalizer()][new JsonEncoder()])
        );

        $appPayloadServiceHelper = new AppPayloadServiceHelper(
            $definitionInstanceRegistry,
            $entityEncoder,
            $shopIdProvider
        );

        $url = 'https://example.com/provide-tax';
        $context = new Context(new SystemSource());
        $responseContent = \json_encode([
            
static::assertInstanceOf(MailTemplateEntity::class$mailTemplate);

        $criteria = new Criteria([TestDefaults::SALES_CHANNEL]);
        $criteria->setLimit(1);
        $salesChannel = $this->getContainer()
            ->get('sales_channel.repository')
            ->search($criteria$context)
            ->first();
        static::assertInstanceOf(SalesChannelEntity::class$salesChannel);

        $entityEncoder = new JsonEntityEncoder(
            new Serializer([new StructNormalizer()][new JsonEncoder()])
        );
        $orderDefinition = $this->getContainer()->get(OrderDefinition::class);
        $orderDecode = $entityEncoder->encode(new Criteria()$orderDefinition$order, '/api');
        array_walk_recursive($orderDecodestatic function D&$value): void {
            if ($value instanceof \stdClass) {
                $value = json_decode((string) json_encode($value), true, 512, \JSON_THROW_ON_ERROR);
            }
        });

        $salesChannelDefinition = $this->getContainer()->get(SalesChannelDefinition::class);
        $salesChannelDecode = $entityEncoder->encode(new Criteria()$salesChannelDefinition$salesChannel, '/api');
        
'reference' => 1.0,
                'price' => new PriceCollection(),
            ])
        );

        $registry = new StaticDefinitionInstanceRegistry(
            [SalesChannelProductDefinition::class],
            $this->createMock(ValidatorInterface::class),
            $this->createMock(EntityWriteGatewayInterface::class)
        );

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

        $encoded = (new StructEncoder($this->getChainRegistry($registry)$serializer))->encode($productnew ResponseFields(null));

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

    public function testNoneMappedFieldsAreNotExposed(): void
    {
        $product = new ExtendedProductEntity();
        
use Symfony\Component\Serializer\Exception\InvalidArgumentException;

/** * @internal */
class StructNormalizerTest extends TestCase
{
    private StructNormalizer $normalizer;

    protected function setUp(): void
    {
        $this->normalizer = new StructNormalizer();
    }

    public function testSupportFormat(): void
    {
        static::assertTrue($this->normalizer->supportsNormalization(new TestStruct()));
        static::assertFalse($this->normalizer->supportsNormalization('string'));
        static::assertFalse($this->normalizer->supportsNormalization(1));
        static::assertFalse($this->normalizer->supportsNormalization(null));
        static::assertFalse($this->normalizer->supportsNormalization(false));
        static::assertFalse($this->normalizer->supportsNormalization(['array']));
        static::assertFalse($this->normalizer->supportsNormalization(new \stdClass()));
    }
Home | Imprint | This part of the site doesn't use cookies.