|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Sylius package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Paweł Jędrzejewski |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace spec\Sylius\Bundle\PromotionBundle\Validator; |
|
15
|
|
|
|
|
16
|
|
|
use PhpSpec\ObjectBehavior; |
|
17
|
|
|
use Prophecy\Argument; |
|
18
|
|
|
use Sylius\Bundle\PromotionBundle\Validator\Constraints\CouponPossibleGenerationAmount; |
|
19
|
|
|
use Sylius\Bundle\PromotionBundle\Validator\CouponGenerationAmountValidator; |
|
20
|
|
|
use Sylius\Component\Promotion\Generator\GenerationPolicyInterface; |
|
21
|
|
|
use Sylius\Component\Promotion\Generator\PromotionCouponGeneratorInstructionInterface; |
|
22
|
|
|
use Symfony\Component\Validator\ConstraintValidator; |
|
23
|
|
|
use Symfony\Component\Validator\Context\ExecutionContextInterface; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @author Arkadiusz Krakowiak <[email protected]> |
|
27
|
|
|
*/ |
|
28
|
|
|
final class CouponGenerationAmountValidatorSpec extends ObjectBehavior |
|
29
|
|
|
{ |
|
30
|
|
|
function let(GenerationPolicyInterface $generationPolicy, ExecutionContextInterface $context): void |
|
31
|
|
|
{ |
|
32
|
|
|
$this->beConstructedWith($generationPolicy); |
|
33
|
|
|
$this->initialize($context); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
function it_is_initializable(): void |
|
37
|
|
|
{ |
|
38
|
|
|
$this->shouldHaveType(CouponGenerationAmountValidator::class); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
function it_is_a_constraint_validator(): void |
|
42
|
|
|
{ |
|
43
|
|
|
$this->shouldHaveType(ConstraintValidator::class); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
function it_adds_violation( |
|
47
|
|
|
ExecutionContextInterface $context, |
|
48
|
|
|
PromotionCouponGeneratorInstructionInterface $instruction, |
|
49
|
|
|
GenerationPolicyInterface $generationPolicy |
|
50
|
|
|
): void { |
|
51
|
|
|
$constraint = new CouponPossibleGenerationAmount(); |
|
52
|
|
|
|
|
53
|
|
|
$instruction->getAmount()->willReturn(17); |
|
54
|
|
|
$instruction->getCodeLength()->willReturn(1); |
|
55
|
|
|
$generationPolicy->isGenerationPossible($instruction)->willReturn(false); |
|
56
|
|
|
$generationPolicy->getPossibleGenerationAmount($instruction)->shouldBeCalled(); |
|
57
|
|
|
$context->addViolation($constraint->message, Argument::any())->shouldBeCalled(); |
|
58
|
|
|
|
|
59
|
|
|
$this->validate($instruction, $constraint); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
function it_does_not_add_violation( |
|
63
|
|
|
ExecutionContextInterface $context, |
|
64
|
|
|
PromotionCouponGeneratorInstructionInterface $instruction, |
|
65
|
|
|
GenerationPolicyInterface $generationPolicy |
|
66
|
|
|
): void { |
|
67
|
|
|
$constraint = new CouponPossibleGenerationAmount(); |
|
68
|
|
|
|
|
69
|
|
|
$instruction->getAmount()->willReturn(5); |
|
70
|
|
|
$instruction->getCodeLength()->willReturn(1); |
|
71
|
|
|
$generationPolicy->isGenerationPossible($instruction)->willReturn(true); |
|
72
|
|
|
$generationPolicy->getPossibleGenerationAmount($instruction)->shouldNotBeCalled(); |
|
73
|
|
|
$context->addViolation($constraint->message, Argument::any())->shouldNotBeCalled(); |
|
74
|
|
|
|
|
75
|
|
|
$this->validate($instruction, $constraint); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|