Validator example


            }

            public function validate(
                Cart $cart,
                ErrorCollection $errors,
                SalesChannelContext $context
            ): void {
                $errors->add($this->error);
            }
        };
        $validator = new Validator([$mockValidator$mockValidator2]);
        $context = $this->createMock(SalesChannelContext::class);
        $cart = new Cart('test');

        $mockValidator->expects(static::once())->method('validate')->with($cartstatic::anything()$context);

        $errors = $validator->validate($cart$context);
        static::assertCount(1, $errors);
    }
}


    public function testExtensionsAreMergedEarly(): void
    {
        $extension = new class() extends Struct {
        };

        $cart = new Cart('bar');
        $cart->addExtension('unit-test', $extension);

        $processor = new Processor(
            new Validator([]),
            $this->createMock(AmountCalculator::class),
            $this->createMock(TransactionProcessor::class),
            [
                new class() implements CartProcessorInterface {
                    public function process(CartDataCollection $data, Cart $original, Cart $toCalculate, SalesChannelContext $context, CartBehavior $behavior): void
                    {
                        TestCase::assertNotEmpty($original->getExtension('unit-test'));
                        TestCase::assertNotEmpty($toCalculate->getExtension('unit-test'));
                        TestCase::assertSame($original->getExtension('unit-test')$toCalculate->getExtension('unit-test'));
                    }
                },
            ],
$error = $result->error();
        if (!$error instanceof ValidationError) {
            return '';
        }

        return print_r((new ErrorFormatter())->format($error), true);
    }

    private function validateRequestSchema(string $body): ValidationResult
    {
        $requestData = json_decode($body, null, 512, \JSON_THROW_ON_ERROR);
        $validator = new Validator();
        /** @var SchemaResolver $resolver */
        $resolver = $validator->resolver();
        $resolver->registerFile(
            'http://api.example.com/appActionEndpointSchema.json',
            $this->schemaLocation
        );

        return $validator->validate($requestData, 'http://api.example.com/appActionEndpointSchema.json');
    }

    /** * @param array<string, mixed>|null $responseData */
Home | Imprint | This part of the site doesn't use cookies.