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\AddressingBundle\Validator\Constraints; |
15
|
|
|
|
16
|
|
|
use PhpSpec\ObjectBehavior; |
17
|
|
|
use Prophecy\Argument; |
18
|
|
|
use Sylius\Bundle\AddressingBundle\Validator\Constraints\ProvinceAddressConstraint; |
19
|
|
|
use Sylius\Bundle\AddressingBundle\Validator\Constraints\ProvinceAddressConstraintValidator; |
20
|
|
|
use Sylius\Component\Addressing\Model\AddressInterface; |
21
|
|
|
use Sylius\Component\Addressing\Model\Country; |
22
|
|
|
use Sylius\Component\Addressing\Model\Province; |
23
|
|
|
use Sylius\Component\Resource\Repository\RepositoryInterface; |
24
|
|
|
use Symfony\Component\Validator\Constraint; |
25
|
|
|
use Symfony\Component\Validator\ConstraintViolation; |
26
|
|
|
use Symfony\Component\Validator\Context\ExecutionContextInterface; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @author Arnaud Langlade <[email protected]> |
30
|
|
|
*/ |
31
|
|
|
final class ProvinceAddressConstraintValidatorSpec extends ObjectBehavior |
32
|
|
|
{ |
33
|
|
|
function let(RepositoryInterface $countryRepository, RepositoryInterface $provinceRepository): void |
34
|
|
|
{ |
35
|
|
|
$this->beConstructedWith($countryRepository, $provinceRepository); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
function it_is_initializable(): void |
39
|
|
|
{ |
40
|
|
|
$this->shouldHaveType(ProvinceAddressConstraintValidator::class); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
function it_throws_exception_if_the_value_is_not_an_address(Constraint $constraint): void |
44
|
|
|
{ |
45
|
|
|
$this->shouldThrow(\InvalidArgumentException::class)->during('validate', [ |
46
|
|
|
'', |
47
|
|
|
$constraint, |
48
|
|
|
]); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
function it_does_not_add_violation_because_a_violation_exists( |
52
|
|
|
AddressInterface $address, |
53
|
|
|
ProvinceAddressConstraint $constraint, |
54
|
|
|
ExecutionContextInterface $context |
55
|
|
|
): void { |
56
|
|
|
$this->initialize($context); |
57
|
|
|
|
58
|
|
|
$context->getPropertyPath()->willReturn('property_path'); |
59
|
|
|
$context->getViolations()->willReturn(new \ArrayIterator([ |
60
|
|
|
$this->createViolation('property_path'), |
61
|
|
|
])); |
62
|
|
|
|
63
|
|
|
$context->addViolation(Argument::any())->shouldNotBeCalled(); |
64
|
|
|
|
65
|
|
|
$this->validate($address, $constraint); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
function it_adds_violation_because_address_has_no_province( |
69
|
|
|
AddressInterface $address, |
70
|
|
|
Country $country, |
71
|
|
|
ProvinceAddressConstraint $constraint, |
72
|
|
|
ExecutionContextInterface $context, |
73
|
|
|
RepositoryInterface $countryRepository |
74
|
|
|
): void { |
75
|
|
|
$country->getCode()->willReturn('IE'); |
76
|
|
|
$address->getCountryCode()->willReturn('IE'); |
77
|
|
|
$countryRepository->findOneBy(['code' => 'IE'])->willReturn($country); |
78
|
|
|
|
79
|
|
|
$country->hasProvinces()->willReturn(true); |
80
|
|
|
$address->getProvinceCode()->willReturn(null); |
81
|
|
|
$this->initialize($context); |
82
|
|
|
|
83
|
|
|
$context->getPropertyPath()->willReturn('property_path'); |
84
|
|
|
$context->getViolations()->willReturn(new \ArrayIterator([ |
85
|
|
|
$this->createViolation('other_property_path'), |
86
|
|
|
])); |
87
|
|
|
|
88
|
|
|
$context->addViolation(Argument::any())->shouldBeCalled(); |
89
|
|
|
|
90
|
|
|
$this->validate($address, $constraint); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
function it_adds_violation_because_address_province_does_not_belong_to_country( |
94
|
|
|
AddressInterface $address, |
95
|
|
|
Country $country, |
96
|
|
|
Province $province, |
97
|
|
|
ProvinceAddressConstraint $constraint, |
98
|
|
|
ExecutionContextInterface $context, |
99
|
|
|
RepositoryInterface $countryRepository, |
100
|
|
|
RepositoryInterface $provinceRepository |
101
|
|
|
): void { |
102
|
|
|
$country->getCode()->willReturn('US'); |
103
|
|
|
$address->getCountryCode()->willReturn('US'); |
104
|
|
|
$countryRepository->findOneBy(['code' => 'US'])->willReturn($country); |
105
|
|
|
|
106
|
|
|
$country->hasProvinces()->willReturn(true); |
107
|
|
|
$address->getProvinceCode()->willReturn('US-AK'); |
108
|
|
|
|
109
|
|
|
$province->getCode()->willReturn('US-AK'); |
110
|
|
|
$provinceRepository->findOneBy(['code' => 'US-AK'])->willReturn($province); |
111
|
|
|
$country->hasProvince($province)->willReturn(false); |
112
|
|
|
|
113
|
|
|
$this->initialize($context); |
114
|
|
|
|
115
|
|
|
$context->getPropertyPath()->willReturn('property_path'); |
116
|
|
|
$context->getViolations()->willReturn(new \ArrayIterator([ |
117
|
|
|
$this->createViolation('other_property_path'), |
118
|
|
|
])); |
119
|
|
|
|
120
|
|
|
$context->addViolation(Argument::any())->shouldBeCalled(); |
121
|
|
|
|
122
|
|
|
$this->validate($address, $constraint); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
function it_does_not_add_a_violation_if_province_is_valid( |
126
|
|
|
AddressInterface $address, |
127
|
|
|
Country $country, |
128
|
|
|
Province $province, |
129
|
|
|
ProvinceAddressConstraint $constraint, |
130
|
|
|
ExecutionContextInterface $context, |
131
|
|
|
RepositoryInterface $countryRepository, |
132
|
|
|
RepositoryInterface $provinceRepository |
133
|
|
|
): void { |
134
|
|
|
$country->getCode()->willReturn('US'); |
135
|
|
|
$address->getCountryCode()->willReturn('US'); |
136
|
|
|
$countryRepository->findOneBy(['code' => 'US'])->willReturn($country); |
137
|
|
|
|
138
|
|
|
$country->hasProvinces()->willReturn(true); |
139
|
|
|
$address->getProvinceCode()->willReturn('US-AK'); |
140
|
|
|
|
141
|
|
|
$province->getCode()->willReturn('US-AK'); |
142
|
|
|
$provinceRepository->findOneBy(['code' => 'US-AK'])->willReturn($province); |
143
|
|
|
$country->hasProvince($province)->willReturn(true); |
144
|
|
|
|
145
|
|
|
$this->initialize($context); |
146
|
|
|
|
147
|
|
|
$context->getPropertyPath()->willReturn('property_path'); |
148
|
|
|
$context->getViolations()->willReturn(new \ArrayIterator([ |
149
|
|
|
$this->createViolation('other_property_path'), |
150
|
|
|
])); |
151
|
|
|
|
152
|
|
|
$context->addViolation(Argument::any())->shouldNotBeCalled(); |
153
|
|
|
|
154
|
|
|
$this->validate($address, $constraint); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
private function createViolation($propertyPath) |
158
|
|
|
{ |
159
|
|
|
return new ConstraintViolation('message', 'template', [], 'root', $propertyPath, 'invalidValue'); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|