<commit_before><?php

/*
 * This file is part of the Sylius package.
 *
 * (c) <NAME>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

declare(strict_types=1);

namespace spec\Sylius\Bundle\ApiBundle\Validator\Constraints;

use PhpSpec\ObjectBehavior;
use Sylius\Bundle\ApiBundle\Command\Cart\AddItemToCart;
use Sylius\Bundle\ApiBundle\Command\Checkout\CompleteOrder;
use Sylius\Bundle\ApiBundle\Validator\Constraints\ProductAvailableInChannel;
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Model\ProductInterface;
use Sylius\Component\Core\Model\ProductVariantInterface;
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
use Sylius\Component\Core\Repository\ProductVariantRepositoryInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidatorInterface;
use Symfony\Component\Validator\Context\ExecutionContextInterface;

final class ProductAvailableInChannelValidatorSpec extends ObjectBehavior
{
    function let(
        ProductVariantRepositoryInterface $productVariantRepository,
        OrderRepositoryInterface $orderRepository
    ): void {
        $this->beConstructedWith($productVariantRepository, $orderRepository);
    }

    function it_is_a_constraint_validator(): void
    {
        $this->shouldImplement(ConstraintValidatorInterface::class);
    }

    function it_throws_an_exception_if_value_is_not_an_instance_of_add_item_to_cart_command(): void
    {
        $this
            ->shouldThrow(\InvalidArgumentException::class)
            ->during('validate', [new CompleteOrder(), new ProductAvailableInChannel()]);
    }

    function it_throws_an_exception_if_constraint_is_not_an_instance_of_product_available_in_channel(): void
    {
        $this
            ->shouldThrow(\InvalidArgumentException::class)
            ->during('validate', [new AddItemToCart('productCode', 'productVariantCode', 1), new class() extends Constraint {
            }])
        ;
    }

    function it_adds_violation_if_product_is_not_available_in_channel(
        ProductVariantRepositoryInterface $productVariantRepository,
        OrderRepositoryInterface $orderRepository,
        ChannelInterface $channel,
        ProductVariantInterface $productVariant,
        ProductInterface $product,
        OrderInterface $cart,
        ExecutionContextInterface $executionContext
    ): void {
        $this->initialize($executionContext);

        $value = new AddItemToCart('productCode', 'productVariantCode', 1);
        $value->setOrderTokenValue('TOKEN');
        $constraint = new ProductAvailableInChannel();
        $constraint->message = 'message';

        $orderRepository->findCartByTokenValue('TOKEN')->willReturn($cart);
        $productVariantRepository->findOneBy(['code' => 'productVariantCode'])->willReturn($productVariant);
        $cart->getChannel()->willReturn($channel);

        $productVariant->getProduct()->willReturn($product);

        $product->hasChannel($channel)->willReturn(false);
        $product->getName()->willReturn('PRODUCTNAME');
        $executionContext
            ->addViolation('message', ['%productName%' => 'PRODUCTNAME'])
            ->shouldBeCalled()
        ;

        $this->validate($value, $constraint);
    }

    function it_does_nothing_if_product_is_available_in_channel(
        ProductVariantRepositoryInterface $productVariantRepository,
        OrderRepositoryInterface $orderRepository,
        ChannelInterface $channel,
        ProductVariantInterface $productVariant,
        ProductInterface $product,
        OrderInterface $cart,
        ExecutionContextInterface $executionContext
    ): void {
        $this->initialize($executionContext);

        $value = new AddItemToCart('productCode', 'productVariantCode', 1);
        $value->setOrderTokenValue('TOKEN');
        $constraint = new ProductAvailableInChannel();
        $constraint->message = 'message';

        $orderRepository->findCartByTokenValue('TOKEN')->willReturn($cart);
        $productVariantRepository->findOneBy(['code' => 'productVariantCode'])->willReturn($productVariant);
        $cart->getChannel()->willReturn($channel);

        $productVariant->getProduct()->willReturn($product);

        $product->hasChannel($channel)->willReturn(true);
        $product->getName()->willReturn('PRODUCTNAME');
        $executionContext
            ->addViolation('message', ['%productName%' => 'PRODUCTNAME'])
            ->shouldNotBeCalled()
        ;

        $this->validate($value, $constraint);
    }

}
<commit_msg>bug #12617 [API][PHPSpec] Fix validator after upmerge (GSadee)<commit_after><?php

/*
 * This file is part of the Sylius package.
 *
 * (c) <NAME>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

declare(strict_types=1);

namespace spec\Sylius\Bundle\ApiBundle\Validator\Constraints;

use PhpSpec\ObjectBehavior;
use Sylius\Bundle\ApiBundle\Command\Cart\AddItemToCart;
use Sylius\Bundle\ApiBundle\Command\Checkout\CompleteOrder;
use Sylius\Bundle\ApiBundle\Validator\Constraints\ProductAvailableInChannel;
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Model\ProductInterface;
use Sylius\Component\Core\Model\ProductVariantInterface;
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
use Sylius\Component\Core\Repository\ProductVariantRepositoryInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidatorInterface;
use Symfony\Component\Validator\Context\ExecutionContextInterface;

final class ProductAvailableInChannelValidatorSpec extends ObjectBehavior
{
    function let(
        ProductVariantRepositoryInterface $productVariantRepository,
        OrderRepositoryInterface $orderRepository
    ): void {
        $this->beConstructedWith($productVariantRepository, $orderRepository);
    }

    function it_is_a_constraint_validator(): void
    {
        $this->shouldImplement(ConstraintValidatorInterface::class);
    }

    function it_throws_an_exception_if_value_is_not_an_instance_of_add_item_to_cart_command(): void
    {
        $this
            ->shouldThrow(\InvalidArgumentException::class)
            ->during('validate', [new CompleteOrder(), new ProductAvailableInChannel()]);
    }

    function it_throws_an_exception_if_constraint_is_not_an_instance_of_product_available_in_channel(): void
    {
        $this
            ->shouldThrow(\InvalidArgumentException::class)
            ->during('validate', [new AddItemToCart('productVariantCode', 1), new class() extends Constraint {}])
        ;
    }

    function it_adds_violation_if_product_is_not_available_in_channel(
        ProductVariantRepositoryInterface $productVariantRepository,
        OrderRepositoryInterface $orderRepository,
        ChannelInterface $channel,
        ProductVariantInterface $productVariant,
        ProductInterface $product,
        OrderInterface $cart,
        ExecutionContextInterface $executionContext
    ): void {
        $this->initialize($executionContext);

        $value = new AddItemToCart('productVariantCode', 1);
        $value->setOrderTokenValue('TOKEN');
        $constraint = new ProductAvailableInChannel();
        $constraint->message = 'message';

        $orderRepository->findCartByTokenValue('TOKEN')->willReturn($cart);
        $productVariantRepository->findOneBy(['code' => 'productVariantCode'])->willReturn($productVariant);
        $cart->getChannel()->willReturn($channel);

        $productVariant->getProduct()->willReturn($product);

        $product->hasChannel($channel)->willReturn(false);
        $product->getName()->willReturn('PRODUCTNAME');
        $executionContext
            ->addViolation('message', ['%productName%' => 'PRODUCTNAME'])
            ->shouldBeCalled()
        ;

        $this->validate($value, $constraint);
    }

    function it_does_nothing_if_product_is_available_in_channel(
        ProductVariantRepositoryInterface $productVariantRepository,
        OrderRepositoryInterface $orderRepository,
        ChannelInterface $channel,
        ProductVariantInterface $productVariant,
        ProductInterface $product,
        OrderInterface $cart,
        ExecutionContextInterface $executionContext
    ): void {
        $this->initialize($executionContext);

        $value = new AddItemToCart('productVariantCode', 1);
        $value->setOrderTokenValue('TOKEN');
        $constraint = new ProductAvailableInChannel();
        $constraint->message = 'message';

        $orderRepository->findCartByTokenValue('TOKEN')->willReturn($cart);
        $productVariantRepository->findOneBy(['code' => 'productVariantCode'])->willReturn($productVariant);
        $cart->getChannel()->willReturn($channel);

        $productVariant->getProduct()->willReturn($product);

        $product->hasChannel($channel)->willReturn(true);
        $product->getName()->willReturn('PRODUCTNAME');
        $executionContext
            ->addViolation('message', ['%productName%' => 'PRODUCTNAME'])
            ->shouldNotBeCalled()
        ;

        $this->validate($value, $constraint);
    }

}

{'fraction_non_alphanumeric': 0.11105178857447945, 'fraction_numerical': 0.001388147357180993, 'mean_word_length': 4.057235421166307, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 97, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3756432', 'n_tokens_mistral': 2413, 'n_tokens_neox': 2369, 'n_words': 378}