Skip to content

Commit ebc2ff8

Browse files
authored
fix: prevent adding array to int calculating total value (#492)
* fix: prevent adding array to int calculating total value * test: add * style: improve var name * amount means quantity i think * implement feedback
1 parent 00bdf7e commit ebc2ff8

File tree

2 files changed

+99
-1
lines changed

2 files changed

+99
-1
lines changed

src/Rule/Consignment/MinimumItemValueRule.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,13 @@ public function validate($validationSubject): void
2020
$totalValue = 0;
2121

2222
foreach ($validationSubject->items as $item) {
23-
$totalValue += $item->getItemValue();
23+
$itemValue = $item->getItemValue();
24+
25+
if (! isset($itemValue['amount'])) {
26+
continue;
27+
}
28+
29+
$totalValue += (int) $itemValue['amount'];
2430
}
2531

2632
if ($totalValue < 100) {
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace MyParcelNL\Sdk\Test\Rule;
6+
7+
use MyParcelNL\Sdk\src\Model\Consignment\PostNLConsignment;
8+
use MyParcelNL\Sdk\src\Model\MyParcelCustomsItem;
9+
use MyParcelNL\Sdk\src\Rule\Consignment\MinimumItemValueRule;
10+
use MyParcelNL\Sdk\Test\Bootstrap\TestCase;
11+
12+
class MinimumItemValueRuleTest extends TestCase
13+
{
14+
/**
15+
* @return array
16+
*/
17+
public function provideMinimumItemValueData(): array
18+
{
19+
return [
20+
[
21+
[
22+
'items' => [
23+
[
24+
'value' => 50,
25+
],
26+
[
27+
'value' => 50,
28+
],
29+
],
30+
],
31+
'expected'=>true,
32+
],
33+
[
34+
[
35+
'items' => [
36+
[
37+
'value' => 5000,
38+
],
39+
[
40+
'value' => 9995,
41+
],
42+
],
43+
],
44+
'expected'=>true,
45+
],
46+
[
47+
[
48+
'items' => [
49+
[
50+
'value' => 50,
51+
],
52+
],
53+
],
54+
'expected'=>false,
55+
],
56+
[
57+
[
58+
'items' => [],
59+
],
60+
'expected'=>true,
61+
],
62+
];
63+
}
64+
65+
/**
66+
* @param array $data
67+
* @param bool $expected
68+
*
69+
* @dataProvider provideMinimumItemValueData
70+
*/
71+
public function testMinimumItemValueRule(array $data, bool $expected): void
72+
{
73+
$rule = new MinimumItemValueRule();
74+
$consignment = new PostNLConsignment();
75+
76+
foreach ($data['items'] as $item) {
77+
$consignment->addItem(
78+
(new MyParcelCustomsItem())
79+
->setItemValue($item['value'])
80+
->setAmount(1)
81+
->setWeight(1)
82+
->setClassification(123456)
83+
->setCountry('NL')
84+
->setDescription('test')
85+
);
86+
}
87+
88+
$rule->validate($consignment);
89+
90+
self::assertEquals($expected, 0 === count($rule->getErrors()));
91+
}
92+
}

0 commit comments

Comments
 (0)