Skip to content

Commit

Permalink
Fix OrderAwareValidTpayChannelListProvider in case of an invalid cons…
Browse files Browse the repository at this point in the history
…traint
  • Loading branch information
coldic3 committed Dec 11, 2024
1 parent 8ad8c23 commit 8d06567
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/Tpay/Provider/OrderAwareValidTpayChannelListProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 [
Expand Down

0 comments on commit 8d06567

Please sign in to comment.