MinQtyLessThanOrEqualMaxQtyConstraintValidatorTest.php in Commerce Product Bundle 8
File
tests/src/Unit/Plugin/Validation/Constraint/MinQtyLessThanOrEqualMaxQtyConstraintValidatorTest.php
View source
<?php
namespace Drupal\Tests\commerce_product_bundle\Unit\Plugin\Validation\Constraint;
use Drupal\commerce_product_bundle\Entity\BundleItemInterface;
use Drupal\commerce_product_bundle\Plugin\Validation\Constraint\MinQtyLessThanOrEqualMaxQtyConstraint;
use Drupal\commerce_product_bundle\Plugin\Validation\Constraint\MinQtyLessThanOrEqualMaxQtyConstraintValidator;
use Drupal\Tests\UnitTestCase;
use Prophecy\Argument;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
use Symfony\Component\Validator\Violation\ConstraintViolationBuilderInterface;
class MinQtyLessThanOrEqualMaxQtyConstraintValidatorTest extends UnitTestCase {
protected $constraint;
protected $validator;
public function setUp() {
$this->constraint = new MinQtyLessThanOrEqualMaxQtyConstraint();
$this->validator = new MinQtyLessThanOrEqualMaxQtyConstraintValidator();
}
public function testValidate($entity, $expected_violation) {
$context = $this
->prophesize(ExecutionContextInterface::class);
if ($expected_violation) {
$violation_builder = $this
->prophesize(ConstraintViolationBuilderInterface::class);
$violation_builder
->setParameter('%min', 20)
->willReturn($violation_builder);
$violation_builder
->setParameter('%max', 10)
->willReturn($violation_builder);
$violation_builder
->addViolation()
->willReturn($violation_builder);
$context
->buildViolation($expected_violation)
->willReturn($violation_builder
->reveal())
->shouldBeCalled();
}
else {
$context
->buildViolation(Argument::any())
->shouldNotBeCalled();
}
$this->validator
->initialize($context
->reveal());
$this->validator
->validate($entity, $this->constraint);
}
public function providerTestValidate() {
$constraint = new MinQtyLessThanOrEqualMaxQtyConstraint();
$bundleItemMock = $this
->prophesize(BundleItemInterface::class);
$bundleItemMock
->getMinimumQuantity()
->willReturn(1, 0, 1, 20);
$bundleItemMock
->getMaximumQuantity()
->willReturn(1, 1, 10, 10);
$entity = $bundleItemMock
->reveal();
$cases[] = [
$entity,
FALSE,
];
$cases[] = [
$entity,
FALSE,
];
$cases[] = [
$entity,
FALSE,
];
$cases[] = [
$entity,
$constraint->message,
];
return $cases;
}
}