CheckoutRuleScope example

static::assertEquals(new Type('string')$streetName[1]);
    }

    public function testUnsupportedValue(): void
    {
        try {
            $rule = new BillingStreetRule();
            $salesChannelContext = $this->createMock(SalesChannelContext::class);
            $customer = new CustomerEntity();
            $customer->setActiveBillingAddress(new CustomerAddressEntity());
            $salesChannelContext->method('getCustomer')->willReturn($customer);
            $rule->match(new CheckoutRuleScope($salesChannelContext));
            static::fail('Exception was not thrown');
        } catch (\Throwable $exception) {
            static::assertInstanceOf(UnsupportedValueException::class$exception);
        }
    }

    public function testRuleNotMatchingWithoutAddress(): void
    {
        $this->rule->assign(['streetName' => 'foo', 'operator' => Rule::OPERATOR_EQ]);
        $salesChannelContext = $this->createMock(SalesChannelContext::class);

        
'config' => [
                        'entity' => 'promotion',
                    ],
                ],
            ],
        ]$config->getData());
    }

    public function testMatchesInCheckoutRuleScope(): void
    {
        $equalsRule = new PromotionLineItemRule(Rule::OPERATOR_EQ, ['id']);
        static::assertFalse($equalsRule->match(new CheckoutRuleScope($this->createMock(SalesChannelContext::class))));

        $notEqualsRule = new PromotionLineItemRule(Rule::OPERATOR_NEQ, ['id']);
        static::assertFalse($notEqualsRule->match(new CheckoutRuleScope($this->createMock(SalesChannelContext::class))));
    }

    /** * @dataProvider lineItemScopeCases * * @param list<string>|null $ids */
    public function testMatchesInLineItemScope(?array $ids, LineItem $lineItem, bool $expected): void
    {
static::assertArrayHasKey('countryIds', $ruleConstraints, 'Constraint countryIds not found in Rule');
        $countryIds = $ruleConstraints['countryIds'];
        static::assertEquals(new NotBlank()$countryIds[0]);
        static::assertEquals(new ArrayOfUuid()$countryIds[1]);
    }

    public function testRuleNotMatchingWithoutCountry(): void
    {
        $this->rule->assign(['countryIds' => ['foo'], 'operator' => Rule::OPERATOR_EQ]);
        $salesChannelContext = $this->createMock(SalesChannelContext::class);

        static::assertFalse($this->rule->match(new CheckoutRuleScope($salesChannelContext)));

        $customer = new CustomerEntity();
        $salesChannelContext->method('getCustomer')->willReturn($customer);

        static::assertFalse($this->rule->match(new CheckoutRuleScope($salesChannelContext)));

        $customerAddress = new CustomerAddressEntity();
        $customer->setActiveBillingAddress($customerAddress);
        $salesChannelContext->method('getCustomer')->willReturn($customer);

        static::assertFalse($this->rule->match(new CheckoutRuleScope($salesChannelContext)));
    }

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

        $customer = new CustomerEntity();
        $customer->setEmail($customerEmail);

        if ($noCustomer) {
            $customer = null;
        }

        $salesChannelContext->method('getCustomer')->willReturn($customer);
        $scope = new CheckoutRuleScope($salesChannelContext);
        $this->rule->assign(['email' => $email, 'operator' => $operator]);

        $match = $this->rule->match($scope);

        static::assertSame($expected$match);
    }

    /** * @return \Traversable<string, array<string|bool>> */
    public static function getMatchValues(): \Traversable
    {
static::assertFalse($match);
    }

    public function testInvalidScope(): void
    {
        $this->rule->assign([
            'lineItemReleaseDate' => '2020-02-06 00:00:00',
            'operator' => Rule::OPERATOR_EQ,
        ]);

        $match = $this->rule->match(new CheckoutRuleScope(
            $this->createMock(SalesChannelContext::class)
        ));

        static::assertFalse($match);
    }

    /** * @dataProvider getCartRuleScopeTestData */
    public function testMultipleLineItemsInCartRuleScope(
        string $ruleReleaseDate,
        

        $this->expectException(UnsupportedValueException::class);
        $this->rule->assign([
            'operator' => Rule::OPERATOR_EQ,
            'daysPassed' => null,
        ]);

        $salesChannelContext = $this->createMock(SalesChannelContext::class);
        $customer = new CustomerEntity();
        $salesChannelContext->method('getCustomer')->willReturn($customer);

        $this->rule->match(new CheckoutRuleScope($salesChannelContext));
    }

    /** * @dataProvider getCaseTestMatchValues */
    public function testIfMatchesCorrect(
        string $operator,
        bool $isMatching,
        float $daysPassed,
        ?\DateTimeImmutable $day,
        bool $noCustomer = false
    ):
/** * @runInSeparateProcess * * @param array<string, string> $values * * @dataProvider scriptProvider */
    public function testRuleScriptExecution(string $path, array $values, bool $expectedTrue): void
    {
        $script = file_get_contents(__DIR__ . $path);
        $scope = new CheckoutRuleScope($this->createSalesChannelContext());
        $rule = new ScriptRule();

        $rule->assign([
            'values' => $values,
            'script' => $script,
            'debug' => false,
            'cacheDir' => $this->getContainer()->getParameter('kernel.cache_dir'),
        ]);

        if ($expectedTrue) {
            static::assertTrue($rule->match($scope));
        }
if (!$noCustomer) {
            $customer = new CustomerEntity();
            $customer->setActive($customerActiveValue);

            $salesChannelContext
                ->method('getCustomer')
                ->willReturn($customer);
        }

        $isActiveCustomerRule = new IsActiveRule($isActive);

        $scope = new CheckoutRuleScope($salesChannelContext);

        static::assertSame($expectedValue$isActiveCustomerRule->match($scope));
    }

    public function testConstrains(): void
    {
        $actualConstraints = $this->rule->getConstraints();

        static::assertArrayHasKey('isActive', $actualConstraints, 'Constrains not found in rule, given "isActive"');

        $isActiveConstraint = $actualConstraints['isActive'];

        
static::assertTrue($rule->match($scope));
    }

    public function testMatchWrongScopeAlwaysFalse(): void
    {
        $rule = new LineItemOfTypeRule(Rule::OPERATOR_NEQ, 'jeans');

        $lineItem = new LineItem(Uuid::randomHex(), 'shirt');

        $cart = new Cart(Uuid::randomHex());
        $cart->setLineItems(new LineItemCollection([$lineItem]));
        $scope = new CheckoutRuleScope(static::createMock(SalesChannelContext::class));

        static::assertFalse($rule->match($scope));

        $rule = new LineItemOfTypeRule(Rule::OPERATOR_NEQ, 'jeans');

        $lineItem = new LineItem(Uuid::randomHex(), 'jeans');

        $cart = new Cart(Uuid::randomHex());
        $cart->setLineItems(new LineItemCollection([$lineItem]));
        $scope = new CheckoutRuleScope(static::createMock(SalesChannelContext::class));

        


        yield 'operator lower than equals / higher shipping cost / no match' => [
            new CartShippingCostRule(Rule::OPERATOR_LTE, 5),
            new CalculatedPrice(1.0, 8.99, new CalculatedTaxCollection()new TaxRuleCollection()),
            false,
        ];
    }

    public function testMatchIfDifferentRuleScope(): void
    {
        $scope = new CheckoutRuleScope($this->createMock(SalesChannelContext::class));
        static::assertFalse($this->rule->match($scope));
    }

    public function testConstraints(): void
    {
        $constraints = (new CartShippingCostRule())->getConstraints();

        static::assertCount(2, $constraints);

        static::assertArrayHasKey('cartShippingCost', $constraints);
        static::assertEquals($constraints['cartShippingCost'][
            
$this->expectException(UnsupportedValueException::class);
        $this->rule->assign([
            'operator' => Rule::OPERATOR_EQ,
            'age' => null,
        ]);

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

        $customer = new CustomerEntity();
        $salesChannelContext->method('getCustomer')->willReturn($customer);

        $this->rule->match(new CheckoutRuleScope($salesChannelContext));
    }

    /** * @dataProvider getCaseTestMatchValues */
    public function testIfMatchesCorrect(
        ?string $birthday,
        string $operator,
        ?int $age,
        bool $expected
    ): void {
        

    public function testRuleMatchingNumeric(string $operator, bool $isMatching, string $zipCode): void
    {
        $zipCodes = ['90210', '81985'];
        $salesChannelContext = $this->createMock(SalesChannelContext::class);
        $customerAddress = new CustomerAddressEntity();
        $customerAddress->setZipcode($zipCode);

        $customer = new CustomerEntity();
        $customer->setActiveBillingAddress($customerAddress);
        $salesChannelContext->method('getCustomer')->willReturn($customer);
        $scope = new CheckoutRuleScope($salesChannelContext);
        $this->rule->assign(['zipCodes' => $zipCodes, 'operator' => $operator]);

        $match = $this->rule->match($scope);
        if ($isMatching) {
            static::assertTrue($match);
        } else {
            static::assertFalse($match);
        }
    }

    /** * @dataProvider getMatchValuesAlphanumeric */

        $this->expectException(UnsupportedValueException::class);
        $this->rule->assign([
            'operator' => Rule::OPERATOR_EQ,
            'daysPassed' => null,
        ]);

        $salesChannelContext = $this->createMock(SalesChannelContext::class);
        $customer = new CustomerEntity();
        $salesChannelContext->method('getCustomer')->willReturn($customer);

        $this->rule->match(new CheckoutRuleScope($salesChannelContext));
    }

    /** * @dataProvider getCaseTestMatchValues */
    public function testIfMatchesCorrect(
        string $operator,
        bool $isMatching,
        float $daysPassed,
        ?\DateTimeImmutable $day,
        bool $noCustomer = false
    ):
yield 'operator_empty / not match / identifier' => [Rule::OPERATOR_NEQ, false, ['kyln123', 'kyln456'], 'kyln123'];
        yield 'operator_empty / match / identifier' => [Rule::OPERATOR_EMPTY, true, ['kyln123', 'kyln456'], null];
        yield 'operator_neq / match / no customer' => [Rule::OPERATOR_NEQ, true, ['kyln123', 'kyln456'], 'kyln123', true];
        yield 'operator_empty / match / no customer' => [Rule::OPERATOR_EMPTY, true, ['kyln123', 'kyln456'], 'kyln123', true];
    }

    public function createScope(?CustomerEntity $customer): CheckoutRuleScope
    {
        $context = $this->createMock(SalesChannelContext::class);
        $context->method('getCustomer')->willReturn($customer);

        return new CheckoutRuleScope($context);
    }
}


    private function createRuleScope(string $salesChannelId): RuleScope
    {
        $salesChannelContext = $this->createMock(SalesChannelContext::class);

        $salesChannel = new SalesChannelEntity();
        $salesChannel->setId($salesChannelId);

        $salesChannelContext->method('getSalesChannel')->willReturn($salesChannel);

        return new CheckoutRuleScope(
            $salesChannelContext
        );
    }
}
Home | Imprint | This part of the site doesn't use cookies.