From 8d06567a01ffeda3801e82d885e21a844097eadc Mon Sep 17 00:00:00 2001 From: Kevin Kaniaburka Date: Wed, 11 Dec 2024 12:22:32 +0100 Subject: [PATCH] Fix OrderAwareValidTpayChannelListProvider in case of an invalid constraint --- ...OrderAwareValidTpayChannelListProvider.php | 12 ++-- ...rAwareValidTpayChannelListProviderTest.php | 57 +++++++++++++++++++ 2 files changed, 65 insertions(+), 4 deletions(-) diff --git a/src/Tpay/Provider/OrderAwareValidTpayChannelListProvider.php b/src/Tpay/Provider/OrderAwareValidTpayChannelListProvider.php index e8e529bc..1e1b8f10 100644 --- a/src/Tpay/Provider/OrderAwareValidTpayChannelListProvider.php +++ b/src/Tpay/Provider/OrderAwareValidTpayChannelListProvider.php @@ -21,15 +21,19 @@ public function provide(OrderInterface $order): array foreach ($channelList as $key => $channel) { foreach ($channel['constraints'] ?? [] as $constraint) { - if ('amount' !== $constraint['field']) { + $constraintField = $constraint['field'] ?? null; + $constraintValue = $constraint['value'] ?? null; + $constraintType = $constraint['type'] ?? null; + + if ('amount' !== $constraintField || null == $constraintValue || null === $constraintType) { continue; } - $constraintValue = (int) $constraint['value'] * self::FLOAT_AMOUNT_VALUE_TO_INT_MULTIPLIER; + $constraintIntValue = (int) ($constraintValue * self::FLOAT_AMOUNT_VALUE_TO_INT_MULTIPLIER); if ( - ('min' === $constraint['type'] && $orderTotal < $constraintValue) || - ('max' === $constraint['type'] && $orderTotal > $constraintValue) + ('min' === $constraintType && $orderTotal < $constraintIntValue) || + ('max' === $constraintType && $orderTotal > $constraintIntValue) ) { unset($channelList[$key]); diff --git a/tests/Unit/Tpay/Provider/OrderAwareValidTpayChannelListProviderTest.php b/tests/Unit/Tpay/Provider/OrderAwareValidTpayChannelListProviderTest.php index 6b458dcb..79f17f97 100644 --- a/tests/Unit/Tpay/Provider/OrderAwareValidTpayChannelListProviderTest.php +++ b/tests/Unit/Tpay/Provider/OrderAwareValidTpayChannelListProviderTest.php @@ -101,6 +101,63 @@ public function test_it_provides_valid_tpay_channels_for_order(int $orderTotal, $this->assertSame($expectedChannels, array_keys($result)); } + public function test_it_does_not_validate_tpay_channels_if_constraints_exist_but_with_missing_fields(): void + { + $validTpayChannelListProvider = $this->prophesize(ValidTpayChannelListProviderInterface::class); + $order = $this->prophesize(OrderInterface::class); + $validTpayChannelListProvider->provide()->willReturn([ + '1' => [ + 'id' => '1', + 'name' => 'I am invalid because of missing field in constraint', + 'available' => true, + 'groups' => [ + ['id' => '173'], + ], + 'constraints' => [ + [ + 'type' => 'min', + 'value' => '30.00', + ] + ], + ], + '2' => [ + 'id' => '2', + 'name' => 'I am invalid because of missing type in constraint', + 'available' => true, + 'groups' => [ + ['id' => '173'], + ], + 'constraints' => [ + [ + 'field' => 'amount', + 'value' => '30.00', + ] + ], + ], + '3' => [ + 'id' => '3', + 'name' => 'I am invalid because of missing value in constraint', + 'available' => true, + 'groups' => [ + ['id' => '173'], + ], + 'constraints' => [ + [ + 'field' => 'amount', + 'type' => 'min', + ] + ], + ], + ]); + $order->getTotal()->willReturn(2000); + + $result = (new OrderAwareValidTpayChannelListProvider( + $validTpayChannelListProvider->reveal(), + ))->provide($order->reveal()); + + $this->assertSame([1, 2, 3], array_keys($result)); + } + private function orderTotalDataProvider(): array { return [