1 | <?php |
||
31 | final class ProvinceAddressConstraintValidatorSpec extends ObjectBehavior |
||
32 | { |
||
33 | function let(RepositoryInterface $countryRepository, RepositoryInterface $provinceRepository): void |
||
37 | |||
38 | function it_is_initializable(): void |
||
42 | |||
43 | function it_throws_exception_if_the_value_is_not_an_address(Constraint $constraint): void |
||
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) |
||
161 | } |
||
162 |