Skip to content

Commit 1e3fa81

Browse files
authored
Merge pull request #41 from shopware/feat/add-label-to-calculatedtax
feat: add label to CalculatedTax
2 parents 72cd219 + 85ab423 commit 1e3fa81

File tree

5 files changed

+47
-16
lines changed

5 files changed

+47
-16
lines changed

src/Context/Cart/CalculatedTax.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ public function getTax(): float
2727
return $this->data['tax'];
2828
}
2929

30+
public function getLabel(): ?string
31+
{
32+
\assert(is_string($this->data['label'] ?? null) || is_null($this->data['label'] ?? null));
33+
return $this->data['label'] ?? null;
34+
}
35+
3036
/**
3137
* @param Collection<CalculatedTax> $calculatedTaxes
3238
* @return Collection<CalculatedTax>
@@ -47,6 +53,7 @@ public static function sum(Collection $calculatedTaxes): Collection
4753
'taxRate' => $calculatedTax->getTaxRate(),
4854
'price' => $new[$calculatedTax->getTaxRate()]->getPrice() + $calculatedTax->getPrice(),
4955
'tax' => $new[$calculatedTax->getTaxRate()]->getTax() + $calculatedTax->getTax(),
56+
'label' => implode(' + ', array_filter([$new[$calculatedTax->getTaxRate()]->getLabel(), $calculatedTax->getLabel()])) ?: null,
5057
]);
5158
}
5259

src/TaxProvider/CalculatedTax.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public function __construct(
1010
public readonly float $tax,
1111
public readonly float $taxRate,
1212
public readonly float $price,
13+
public readonly ?string $label = null,
1314
) {
1415
}
1516

@@ -27,6 +28,7 @@ public function add(CalculatedTax $tax): self
2728
$this->tax + $tax->tax,
2829
$this->taxRate,
2930
$this->price + $tax->price,
31+
implode(' + ', array_filter([$this->label, $tax->label])) ?: null,
3032
);
3133
}
3234
}

tests/Context/Cart/CalculatedTaxTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@ public function testConstruct(): void
1818
'taxRate' => 19.0,
1919
'price' => 10.0,
2020
'tax' => 1.9,
21+
'label' => 'label',
2122
]);
2223

2324
static::assertSame(19.0, $calculatedTax->getTaxRate());
2425
static::assertSame(10.0, $calculatedTax->getPrice());
2526
static::assertSame(1.9, $calculatedTax->getTax());
27+
static::assertSame('label', $calculatedTax->getLabel());
2628
}
2729

2830
public function testSum(): void
@@ -32,6 +34,7 @@ public function testSum(): void
3234
'taxRate' => 19.0,
3335
'price' => 10.0,
3436
'tax' => 1.9,
37+
'label' => 'label 1',
3538
]),
3639
new CalculatedTax([
3740
'taxRate' => 19.0,
@@ -50,13 +53,17 @@ public function testSum(): void
5053
static::assertCount(2, $sum);
5154

5255
$tax19 = $sum->get('19');
56+
static::assertNotNull($tax19);
5357
static::assertSame(19.0, $tax19->getTaxRate());
5458
static::assertSame(15.0, $tax19->getPrice());
5559
static::assertSame(2.75, $tax19->getTax());
60+
static::assertSame('label 1', $tax19->getLabel());
5661

5762
$tax7 = $sum->get('7');
63+
static::assertNotNull($tax7);
5864
static::assertSame(7.0, $tax7->getTaxRate());
5965
static::assertSame(10.0, $tax7->getPrice());
6066
static::assertSame(0.7, $tax7->getTax());
67+
static::assertNull($tax7->getLabel());
6168
}
6269
}

tests/TaxProvider/CalculatedTaxTest.php

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,29 +13,44 @@ class CalculatedTaxTest extends TestCase
1313
{
1414
public function testConstruct(): void
1515
{
16-
$tax = new CalculatedTax(19.0, 100.0, 19.0);
16+
$tax = new CalculatedTax(19.0, 100.0, 19.0, 'label');
1717

18-
static::assertEquals(19.0, $tax->tax);
19-
static::assertEquals(100.0, $tax->taxRate);
20-
static::assertEquals(19.0, $tax->price);
18+
static::assertSame(19.0, $tax->tax);
19+
static::assertSame(100.0, $tax->taxRate);
20+
static::assertSame(19.0, $tax->price);
21+
static::assertSame('label', $tax->label);
2122
}
2223

2324
public function testAdd(): void
2425
{
25-
$tax1 = new CalculatedTax(19.0, 100.0, 19.0);
26+
$tax1 = new CalculatedTax(19.0, 100.0, 19.0, 'label 1');
27+
$tax2 = new CalculatedTax(19.0, 100.0, 19.0, 'label 2');
28+
29+
$tax = $tax1->add($tax2);
30+
31+
static::assertSame(38.0, $tax->tax);
32+
static::assertSame(100.0, $tax->taxRate);
33+
static::assertSame(38.0, $tax->price);
34+
static::assertSame('label 1 + label 2', $tax->label);
35+
}
36+
37+
public function testAddWithoutSomeLabels(): void
38+
{
39+
$tax1 = new CalculatedTax(19.0, 100.0, 19.0, 'label 1');
2640
$tax2 = new CalculatedTax(19.0, 100.0, 19.0);
2741

2842
$tax = $tax1->add($tax2);
2943

30-
static::assertEquals(38.0, $tax->tax);
31-
static::assertEquals(100.0, $tax->taxRate);
32-
static::assertEquals(38.0, $tax->price);
44+
static::assertSame(38.0, $tax->tax);
45+
static::assertSame(100.0, $tax->taxRate);
46+
static::assertSame(38.0, $tax->price);
47+
static::assertSame('label 1', $tax->label);
3348
}
3449

3550
public function testJsonSerialize(): void
3651
{
37-
$tax = new CalculatedTax(19.0, 100.0, 19.0);
52+
$tax = new CalculatedTax(19.0, 100.0, 19.0, 'label');
3853

39-
static::assertEquals(['tax' => 19.0, 'taxRate' => 100.0, 'price' => 19.0], $tax->jsonSerialize());
54+
static::assertSame(['tax' => 19.0, 'taxRate' => 100.0, 'price' => 19.0, 'label' => 'label'], $tax->jsonSerialize());
4055
}
4156
}

tests/TaxProvider/TaxProviderResponseBuilderTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function testGlobalTax(): void
2121

2222
static::assertSame(200, $response->getStatusCode());
2323
static::assertSame(
24-
'{"lineItemTaxes":[],"deliveryTaxes":[],"cartPriceTaxes":{"100":{"tax":19,"taxRate":100,"price":19}}}',
24+
'{"lineItemTaxes":[],"deliveryTaxes":[],"cartPriceTaxes":{"100":{"tax":19,"taxRate":100,"price":19,"label":null}}}',
2525
$response->getBody()->getContents()
2626
);
2727
}
@@ -37,7 +37,7 @@ public function testGlobalTaxIsAddedUp(): void
3737

3838
static::assertSame(200, $response->getStatusCode());
3939
static::assertSame(
40-
'{"lineItemTaxes":[],"deliveryTaxes":[],"cartPriceTaxes":{"100":{"tax":38,"taxRate":100,"price":38}}}',
40+
'{"lineItemTaxes":[],"deliveryTaxes":[],"cartPriceTaxes":{"100":{"tax":38,"taxRate":100,"price":38,"label":null}}}',
4141
$response->getBody()->getContents()
4242
);
4343
}
@@ -51,7 +51,7 @@ public function testLineItemTax(): void
5151

5252
static::assertSame(200, $response->getStatusCode());
5353
static::assertSame(
54-
'{"lineItemTaxes":{"lineItem1":{"100":{"tax":19,"taxRate":100,"price":19}}},"deliveryTaxes":[],"cartPriceTaxes":[]}',
54+
'{"lineItemTaxes":{"lineItem1":{"100":{"tax":19,"taxRate":100,"price":19,"label":null}}},"deliveryTaxes":[],"cartPriceTaxes":[]}',
5555
$response->getBody()->getContents()
5656
);
5757
}
@@ -66,7 +66,7 @@ public function testLineItemTaxIsOverridden(): void
6666

6767
static::assertSame(200, $response->getStatusCode());
6868
static::assertSame(
69-
'{"lineItemTaxes":{"lineItem1":{"100":{"tax":38,"taxRate":100,"price":19}}},"deliveryTaxes":[],"cartPriceTaxes":[]}',
69+
'{"lineItemTaxes":{"lineItem1":{"100":{"tax":38,"taxRate":100,"price":19,"label":null}}},"deliveryTaxes":[],"cartPriceTaxes":[]}',
7070
$response->getBody()->getContents()
7171
);
7272
}
@@ -80,7 +80,7 @@ public function testDeliveryTax(): void
8080

8181
static::assertSame(200, $response->getStatusCode());
8282
static::assertSame(
83-
'{"lineItemTaxes":[],"deliveryTaxes":{"delivery1":{"100":{"tax":19,"taxRate":100,"price":19}}},"cartPriceTaxes":[]}',
83+
'{"lineItemTaxes":[],"deliveryTaxes":{"delivery1":{"100":{"tax":19,"taxRate":100,"price":19,"label":null}}},"cartPriceTaxes":[]}',
8484
$response->getBody()->getContents()
8585
);
8686
}
@@ -93,7 +93,7 @@ public function testBuildPayload(): void
9393
$response = $builder->buildPayload();
9494

9595
static::assertSame(
96-
'{"lineItemTaxes":[],"deliveryTaxes":[],"cartPriceTaxes":{"100":{"tax":19,"taxRate":100,"price":19}}}',
96+
'{"lineItemTaxes":[],"deliveryTaxes":[],"cartPriceTaxes":{"100":{"tax":19,"taxRate":100,"price":19,"label":null}}}',
9797
$response
9898
);
9999
}

0 commit comments

Comments
 (0)