<?php

declare(strict_types=1);

namespace CpCreation\VitiCore\Tests\Cart;

use CpCreation\VitiCore\Cart\Model\Promo;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
use Symfony\Component\Validator\Violation\ConstraintViolationBuilderInterface;

final class PromoTest extends TestCase
{
    public function testMaximumSubtotalCannotBeLowerThanMinimumSubtotal(): void
    {
        $promo = new Promo();
        $promo->setPercent(0.1);
        $promo->setMinSubtotalTTC(10000);
        $promo->setMaxSubtotalTTC(9999);

        $violation = $this->createMock(ConstraintViolationBuilderInterface::class);
        $violation
            ->expects(self::once())
            ->method('atPath')
            ->with('maxSubtotalTTC')
            ->willReturnSelf();
        $violation
            ->expects(self::once())
            ->method('addViolation');

        $context = $this->createMock(ExecutionContextInterface::class);
        $context
            ->expects(self::once())
            ->method('buildViolation')
            ->with('promo.max_subtotal_ttc_lower_than_minimum')
            ->willReturn($violation);

        $promo->validate($context);
    }

    public function testMaximumSubtotalIsNormalizedToZero(): void
    {
        $promo = new Promo();
        $promo->setMaxSubtotalTTC(-1);

        self::assertSame(0, $promo->getMaxSubtotalTTC());
    }
}
