class MinQtyLessThanOrEqualMaxQtyConstraintValidatorTest in Commerce Product Bundle 8
Test the MinQtyLessThanOrEqualMaxQty constraint validator.
@coversDefaultClass \Drupal\commerce_product_bundle\Plugin\Validation\Constraint\MinQtyLessThanOrEqualMaxQtyConstraintValidator @group commerce_product_bundle
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\commerce_product_bundle\Unit\Plugin\Validation\Constraint\MinQtyLessThanOrEqualMaxQtyConstraintValidatorTest
Expanded class hierarchy of MinQtyLessThanOrEqualMaxQtyConstraintValidatorTest
File
- tests/
src/ Unit/ Plugin/ Validation/ Constraint/ MinQtyLessThanOrEqualMaxQtyConstraintValidatorTest.php, line 19
Namespace
Drupal\Tests\commerce_product_bundle\Unit\Plugin\Validation\ConstraintView source
class MinQtyLessThanOrEqualMaxQtyConstraintValidatorTest extends UnitTestCase {
/**
* The constraint.
*
* @var \Drupal\commerce_product_bundle\Plugin\Validation\Constraint\MinQtyLessThanOrEqualMaxQtyConstraint
*/
protected $constraint;
/**
* The validator.
*
* @var \Drupal\commerce_product_bundle\Plugin\Validation\Constraint\MinQtyLessThanOrEqualMaxQtyConstraintValidator
*/
protected $validator;
/**
* {@inheritdoc}
*/
public function setUp() {
$this->constraint = new MinQtyLessThanOrEqualMaxQtyConstraint();
$this->validator = new MinQtyLessThanOrEqualMaxQtyConstraintValidator();
}
/**
* @covers ::validate
*
* @dataProvider providerTestValidate
*/
public function testValidate($entity, $expected_violation) {
// If a violation is expected, then the context's buildViolation method
// will be called, otherwise it should not be called.
$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);
}
/**
* Data provider for ::testValidate().
*/
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();
// Case 1: Default values, Min = Max.
$cases[] = [
$entity,
FALSE,
];
// Case 2: Min < Max, Min = 0.
$cases[] = [
$entity,
FALSE,
];
// Case 3: Min < Max, Min > 0.
$cases[] = [
$entity,
FALSE,
];
// Case 4: Min > Max.
$cases[] = [
$entity,
$constraint->message,
];
return $cases;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
MinQtyLessThanOrEqualMaxQtyConstraintValidatorTest:: |
protected | property | The constraint. | |
MinQtyLessThanOrEqualMaxQtyConstraintValidatorTest:: |
protected | property | The validator. | |
MinQtyLessThanOrEqualMaxQtyConstraintValidatorTest:: |
public | function | Data provider for ::testValidate(). | |
MinQtyLessThanOrEqualMaxQtyConstraintValidatorTest:: |
public | function |
Overrides UnitTestCase:: |
|
MinQtyLessThanOrEqualMaxQtyConstraintValidatorTest:: |
public | function | @covers ::validate | |
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. |