1 | <?php |
||
28 | final class ProvinceAddressConstraintValidatorSpec extends ObjectBehavior |
||
29 | { |
||
30 | function let(RepositoryInterface $countryRepository, RepositoryInterface $provinceRepository): void |
||
34 | |||
35 | function it_is_initializable(): void |
||
39 | |||
40 | function it_throws_exception_if_the_value_is_not_an_address(Constraint $constraint): void |
||
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) |
||
158 | } |
||
159 |