|
14 | 14 | namespace Tests\Sylius\AdyenPlugin\Unit\Validator; |
15 | 15 |
|
16 | 16 | use PHPUnit\Framework\Attributes\DataProvider; |
| 17 | +use Sylius\AdyenPlugin\Repository\Query\AdyenPaymentMethodQueryInterface; |
17 | 18 | use Sylius\AdyenPlugin\Validator\Constraint\ProvinceAddressConstraintValidatorDecorator; |
18 | 19 | use Sylius\Bundle\AddressingBundle\Validator\Constraints\ProvinceAddressConstraint; |
19 | 20 | use Sylius\Bundle\AddressingBundle\Validator\Constraints\ProvinceAddressConstraintValidator; |
| 21 | +use Sylius\Component\Channel\Context\ChannelContextInterface; |
| 22 | +use Sylius\Component\Channel\Model\ChannelInterface; |
| 23 | +use Sylius\Component\Core\Model\PaymentMethodInterface; |
20 | 24 | use Symfony\Component\Validator\ConstraintValidatorInterface; |
21 | 25 | use Symfony\Component\Validator\Test\ConstraintValidatorTestCase; |
22 | 26 | use Tests\Sylius\AdyenPlugin\Unit\AddressMother; |
@@ -80,4 +84,138 @@ public function testRelatedCountryAndEmptyProvinceWithAlreadyViolatedConstraint( |
80 | 84 | $this->validator->validate($address, $constraint); |
81 | 85 | $this->assertCount($expectedCount, $this->context->getViolations()); |
82 | 86 | } |
| 87 | + |
| 88 | + public function testRelatedCountryAndEmptyProvinceWithProvidedDependenciesWhenNoAdyenMethodIsEnabled(): void |
| 89 | + { |
| 90 | + $constraint = new ProvinceAddressConstraint(); |
| 91 | + $address = AddressMother::createAddressWithSpecifiedCountryAndEmptyProvince('US'); |
| 92 | + |
| 93 | + $channel = $this->createMock(ChannelInterface::class); |
| 94 | + $channelContext = $this->createMock(ChannelContextInterface::class); |
| 95 | + $channelContext->method('getChannel')->willReturn($channel); |
| 96 | + |
| 97 | + $adyenPaymentMethodQuery = $this->createMock(AdyenPaymentMethodQueryInterface::class); |
| 98 | + $adyenPaymentMethodQuery |
| 99 | + ->method('findAllAdyenByChannel') |
| 100 | + ->with($channel) |
| 101 | + ->willReturn([]) |
| 102 | + ; |
| 103 | + |
| 104 | + $validator = $this->createValidatorWithDependencies($channelContext, $adyenPaymentMethodQuery); |
| 105 | + $validator->validate($address, $constraint); |
| 106 | + |
| 107 | + $this->assertNoViolation(); |
| 108 | + } |
| 109 | + |
| 110 | + public function testRelatedCountryAndEmptyProvinceWithProvidedDependenciesWhenAdyenMethodIsEnabled(): void |
| 111 | + { |
| 112 | + $constraint = new ProvinceAddressConstraint(); |
| 113 | + $address = AddressMother::createAddressWithSpecifiedCountryAndEmptyProvince('US'); |
| 114 | + |
| 115 | + $channel = $this->createMock(ChannelInterface::class); |
| 116 | + $channelContext = $this->createMock(ChannelContextInterface::class); |
| 117 | + $channelContext->method('getChannel')->willReturn($channel); |
| 118 | + |
| 119 | + $enabledPaymentMethod = $this->createMock(PaymentMethodInterface::class); |
| 120 | + $enabledPaymentMethod->method('isEnabled')->willReturn(true); |
| 121 | + |
| 122 | + $adyenPaymentMethodQuery = $this->createMock(AdyenPaymentMethodQueryInterface::class); |
| 123 | + $adyenPaymentMethodQuery |
| 124 | + ->method('findAllAdyenByChannel') |
| 125 | + ->with($channel) |
| 126 | + ->willReturn([$enabledPaymentMethod]) |
| 127 | + ; |
| 128 | + |
| 129 | + $validator = $this->createValidatorWithDependencies($channelContext, $adyenPaymentMethodQuery); |
| 130 | + $validator->validate($address, $constraint); |
| 131 | + |
| 132 | + $this->buildViolation($constraint->message) |
| 133 | + ->assertRaised() |
| 134 | + ; |
| 135 | + } |
| 136 | + |
| 137 | + public function testRelatedCountryAndEmptyProvinceWithProvidedDependenciesWhenAllMethodsDisabled(): void |
| 138 | + { |
| 139 | + $constraint = new ProvinceAddressConstraint(); |
| 140 | + $address = AddressMother::createAddressWithSpecifiedCountryAndEmptyProvince('US'); |
| 141 | + |
| 142 | + $channel = $this->createMock(ChannelInterface::class); |
| 143 | + $channelContext = $this->createMock(ChannelContextInterface::class); |
| 144 | + $channelContext->method('getChannel')->willReturn($channel); |
| 145 | + |
| 146 | + $disabledPaymentMethod = $this->createMock(PaymentMethodInterface::class); |
| 147 | + $disabledPaymentMethod->method('isEnabled')->willReturn(false); |
| 148 | + |
| 149 | + $adyenPaymentMethodQuery = $this->createMock(AdyenPaymentMethodQueryInterface::class); |
| 150 | + $adyenPaymentMethodQuery |
| 151 | + ->method('findAllAdyenByChannel') |
| 152 | + ->with($channel) |
| 153 | + ->willReturn([$disabledPaymentMethod]) |
| 154 | + ; |
| 155 | + |
| 156 | + $validator = $this->createValidatorWithDependencies($channelContext, $adyenPaymentMethodQuery); |
| 157 | + $validator->validate($address, $constraint); |
| 158 | + |
| 159 | + $this->assertNoViolation(); |
| 160 | + } |
| 161 | + |
| 162 | + public function testRelatedCountryWithProvinceWhenAdyenMethodIsEnabled(): void |
| 163 | + { |
| 164 | + $constraint = new ProvinceAddressConstraint(); |
| 165 | + $address = AddressMother::createAddressWithSpecifiedCountryAndEmptyProvince('US'); |
| 166 | + $address->setProvinceCode('US-TX'); |
| 167 | + |
| 168 | + $channel = $this->createMock(ChannelInterface::class); |
| 169 | + $channelContext = $this->createMock(ChannelContextInterface::class); |
| 170 | + $channelContext->method('getChannel')->willReturn($channel); |
| 171 | + |
| 172 | + $enabledPaymentMethod = $this->createMock(PaymentMethodInterface::class); |
| 173 | + $enabledPaymentMethod->method('isEnabled')->willReturn(true); |
| 174 | + |
| 175 | + $adyenPaymentMethodQuery = $this->createMock(AdyenPaymentMethodQueryInterface::class); |
| 176 | + $adyenPaymentMethodQuery |
| 177 | + ->method('findAllAdyenByChannel') |
| 178 | + ->with($channel) |
| 179 | + ->willReturn([$enabledPaymentMethod]) |
| 180 | + ; |
| 181 | + |
| 182 | + $validator = $this->createValidatorWithDependencies($channelContext, $adyenPaymentMethodQuery); |
| 183 | + $validator->validate($address, $constraint); |
| 184 | + |
| 185 | + $this->assertNoViolation(); |
| 186 | + } |
| 187 | + |
| 188 | + public function testCustomRequiredCountryListAddsViolation(): void |
| 189 | + { |
| 190 | + $constraint = new ProvinceAddressConstraint(); |
| 191 | + $address = AddressMother::createAddressWithSpecifiedCountryAndEmptyProvince('PL'); |
| 192 | + |
| 193 | + $validator = $this->createValidatorWithDependencies( |
| 194 | + null, |
| 195 | + null, |
| 196 | + ['PL'], |
| 197 | + ); |
| 198 | + $validator->validate($address, $constraint); |
| 199 | + |
| 200 | + $this->buildViolation($constraint->message) |
| 201 | + ->assertRaised() |
| 202 | + ; |
| 203 | + } |
| 204 | + |
| 205 | + private function createValidatorWithDependencies( |
| 206 | + ?ChannelContextInterface $channelContext, |
| 207 | + ?AdyenPaymentMethodQueryInterface $adyenPaymentMethodQuery, |
| 208 | + ?array $countryList = null, |
| 209 | + ): ProvinceAddressConstraintValidatorDecorator { |
| 210 | + $this->validator = new ProvinceAddressConstraintValidatorDecorator( |
| 211 | + $this->decorated, |
| 212 | + $countryList ?? ProvinceAddressConstraintValidatorDecorator::PROVINCE_REQUIRED_COUNTRIES_DEFAULT_LIST, |
| 213 | + $channelContext, |
| 214 | + $adyenPaymentMethodQuery, |
| 215 | + ); |
| 216 | + |
| 217 | + $this->validator->initialize($this->context); |
| 218 | + |
| 219 | + return $this->validator; |
| 220 | + } |
83 | 221 | } |
0 commit comments