|
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
|
|
|
namespace spec\Sylius\Bundle\ProductBundle\Validator; |
|
13
|
|
|
|
|
14
|
|
|
use PhpSpec\ObjectBehavior; |
|
15
|
|
|
use Prophecy\Argument; |
|
16
|
|
|
use Sylius\Bundle\ProductBundle\Validator\Constraint\UniqueSimpleProductCode; |
|
17
|
|
|
use Sylius\Bundle\ProductBundle\Validator\UniqueSimpleProductCodeValidator; |
|
18
|
|
|
use Sylius\Component\Product\Model\ProductInterface; |
|
19
|
|
|
use Sylius\Component\Product\Model\ProductVariantInterface; |
|
20
|
|
|
use Sylius\Component\Product\Repository\ProductVariantRepositoryInterface; |
|
21
|
|
|
use Symfony\Component\Validator\ConstraintValidator; |
|
22
|
|
|
use Symfony\Component\Validator\Context\ExecutionContextInterface; |
|
23
|
|
|
use Symfony\Component\Validator\Violation\ConstraintViolationBuilderInterface; |
|
24
|
|
|
|
|
25
|
|
|
final class UniqueSimpleProductCodeValidatorSpec extends ObjectBehavior |
|
26
|
|
|
{ |
|
27
|
|
|
function let(ExecutionContextInterface $context, ProductVariantRepositoryInterface $productVariantRepository) { |
|
|
|
|
|
|
28
|
|
|
$this->beConstructedWith($productVariantRepository); |
|
29
|
|
|
$this->initialize($context); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
function it_is_initializable() |
|
33
|
|
|
{ |
|
34
|
|
|
$this->shouldHaveType(UniqueSimpleProductCodeValidator::class); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
function it_is_a_constraint_validator() |
|
38
|
|
|
{ |
|
39
|
|
|
$this->shouldImplement(ConstraintValidator::class); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
function it_does_not_add_violation_if_product_is_configurable( |
|
43
|
|
|
ExecutionContextInterface $context, |
|
44
|
|
|
ProductInterface $product |
|
45
|
|
|
) { |
|
46
|
|
|
$constraint = new UniqueSimpleProductCode([ |
|
47
|
|
|
'message' => 'Simple product code has to be unique', |
|
48
|
|
|
]); |
|
49
|
|
|
|
|
50
|
|
|
$product->isSimple()->willReturn(false); |
|
51
|
|
|
|
|
52
|
|
|
$context->buildViolation(Argument::any())->shouldNotBeCalled(); |
|
53
|
|
|
|
|
54
|
|
|
$this->validate($product, $constraint); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
function it_does_not_add_violation_if_product_is_simple_but_code_has_not_been_used_among_neither_producs_nor_product_variants( |
|
58
|
|
|
ExecutionContextInterface $context, |
|
59
|
|
|
ProductInterface $product, |
|
60
|
|
|
ProductVariantRepositoryInterface $productVariantRepository |
|
|
|
|
|
|
61
|
|
|
) { |
|
62
|
|
|
$constraint = new UniqueSimpleProductCode([ |
|
63
|
|
|
'message' => 'Simple product code has to be unique', |
|
64
|
|
|
]); |
|
65
|
|
|
|
|
66
|
|
|
$product->isSimple()->willReturn(true); |
|
67
|
|
|
$product->getCode()->willReturn('AWESOME_PRODUCT'); |
|
68
|
|
|
|
|
69
|
|
|
$context->buildViolation(Argument::any())->shouldNotBeCalled(); |
|
70
|
|
|
|
|
71
|
|
|
$productVariantRepository->findOneBy(['code' => 'AWESOME_PRODUCT'])->willReturn(null); |
|
72
|
|
|
|
|
73
|
|
|
$this->validate($product, $constraint); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
function it_add_violation_if_product_is_simple_and_code_has_been_used_in_other_product_variant( |
|
77
|
|
|
ExecutionContextInterface $context, |
|
78
|
|
|
ProductInterface $product, |
|
79
|
|
|
ProductVariantInterface $existingProductVariant, |
|
|
|
|
|
|
80
|
|
|
ProductVariantRepositoryInterface $productVariantRepository, |
|
|
|
|
|
|
81
|
|
|
ConstraintViolationBuilderInterface $constraintViolationBuilder |
|
|
|
|
|
|
82
|
|
|
) { |
|
83
|
|
|
$constraint = new UniqueSimpleProductCode([ |
|
84
|
|
|
'message' => 'Simple product code has to be unique', |
|
85
|
|
|
]); |
|
86
|
|
|
|
|
87
|
|
|
$product->isSimple()->willReturn(true); |
|
88
|
|
|
$product->getCode()->willReturn('AWESOME_PRODUCT'); |
|
89
|
|
|
|
|
90
|
|
|
$context->buildViolation('Simple product code has to be unique', Argument::cetera())->willReturn($constraintViolationBuilder); |
|
91
|
|
|
|
|
92
|
|
|
$constraintViolationBuilder->atPath('code')->shouldBeCalled()->willReturn($constraintViolationBuilder); |
|
93
|
|
|
$constraintViolationBuilder->addViolation()->shouldBeCalled()->willReturn($constraintViolationBuilder); |
|
94
|
|
|
|
|
95
|
|
|
$productVariantRepository->findOneBy(['code' => 'AWESOME_PRODUCT'])->willReturn($existingProductVariant); |
|
96
|
|
|
|
|
97
|
|
|
$this->validate($product, $constraint); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.