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