Completed
Pull Request — master (#11)
by Laurens
02:02
created

VariantsBuilderTest::buildFromArray()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 33
Code Lines 25

Duplication

Lines 7
Ratio 21.21 %

Importance

Changes 0
Metric Value
dl 7
loc 33
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 25
nc 3
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LauLamanApps\IzettleApi\Tests\Unit\Client\Product;
6
7
use LauLamanApps\IzettleApi\API\Product\Variant;
8
use LauLamanApps\IzettleApi\API\Product\VariantCollection;
9
use LauLamanApps\IzettleApi\Client\Product\VariantBuilder;
10
use Money\Money;
11
use PHPUnit\Framework\TestCase;
12
use Ramsey\Uuid\Uuid;
13
14
/**
15
 * @small
16
 */
17
final class VariantsBuilderTest extends TestCase
18
{
19
    /**
20
     * @test
21
     * @dataProvider getVariantArrayData
22
     */
23
    public function buildFromArray(array $data)
24
    {
25
        $builder = new VariantBuilder();
26
        $variantCollection = $builder->buildFromArray($data);
27
28
        self::assertInstanceOf(VariantCollection::class, $variantCollection);
29
30
        $i = 0;// we cannot use the array key here
31
        foreach ($variantCollection->getAll() as $variant) {
32
            self::assertInstanceOf(Variant::class, $variant);
33
            self::assertInstanceOf(Money::class, $variant->getPrice());
34
            self::assertSame($data[$i]['uuid'], (string) $variant->getUuid());
35
            self::assertSame($data[$i]['name'], $variant->getName());
36
            self::assertSame($data[$i]['description'], $variant->getDescription());
37
            self::assertSame($data[$i]['sku'], $variant->getSku());
38
            self::assertSame($data[$i]['barcode'], $variant->getBarcode());
39
            self::assertSame((int) $data[$i]['defaultQuantity'], $variant->getDefaultQuantity());
40
            self::assertSame($data[$i]['unitName'], $variant->getUnitName());
41
            self::assertSame((string) $data[$i]['price']['amount'], $variant->getPrice()->getAmount());
42
            self::assertSame($data[$i]['price']['currencyId'], $variant->getPrice()->getCurrency()->getCode());
43
            self::assertSame((float) $data[$i]['vatPercentage'], $variant->getVatPercentage());
44
45 View Code Duplication
            if (is_null($data[$i]['costPrice'])) {
46
                self::assertSame($data[$i]['costPrice'], $variant->getCostPrice());
47
            } else {
48
                self::assertInstanceOf(Money::class, $variant->getCostPrice());
49
                self::assertSame((string) $data[$i]['costPrice']['amount'], $variant->getCostPrice()->getAmount());
50
                self::assertSame($data[$i]['costPrice']['currencyId'], $variant->getCostPrice()->getCurrency()->getCode());
51
            }
52
53
            $i++;
54
        }
55
    }
56
57
    public function getVariantArrayData(): array
58
    {
59
        return [
60
            'single variant' => [
61
                [
62
                    [
63
                        "uuid" => (string) Uuid::uuid1(),
64
                        "name" => "a variant name",
65
                        "description" => 'the variant description',
66
                        "sku" => null,
67
                        "barcode" => null,
68
                        "defaultQuantity" => "1",
69
                        "unitName" => null,
70
                        "price" => [
71
                            "amount" => 1000,
72
                            "currencyId" => "EUR",
73
                        ],
74
                        "costPrice" => null,
75
                        "vatPercentage" => "6.0",
76
                    ],
77
                ],
78
            ],
79
            'multiple variant' =>[
80
                [
81
                    [
82
                        "uuid" => (string) Uuid::uuid1(),
83
                        "name" => "Variant A",
84
                        "description" => null,
85
                        "sku" => 'Prod-VarA',
86
                        "barcode" => '9782123456803',
87
                        "defaultQuantity" => "1",
88
                        "unitName" => null,
89
                        "price" => [
90
                            "amount" => 1200,
91
                            "currencyId" => "EUR",
92
                        ],
93
                        "costPrice" => [
94
                            "amount" => 700,
95
                            "currencyId" => "EUR",
96
                        ],
97
                        "vatPercentage" => "6.0",
98
                    ],
99
                ],
100
                [
101
                    [
102
                        "uuid" => (string) Uuid::uuid1(),
103
                        "name" => "Variant B",
104
                        "description" => null,
105
                        "sku" => 'Prod-VarB',
106
                        "barcode" => null,
107
                        "defaultQuantity" => "1",
108
                        "unitName" => null,
109
                        "price" => [
110
                            "amount" => 1500,
111
                            "currencyId" => "EUR",
112
                        ],
113
                        "costPrice" => [
114
                            "amount" => 900,
115
                            "currencyId" => "EUR",
116
                        ],
117
                        "vatPercentage" => "6.0",
118
                    ],
119
                ],
120
            ],
121
        ];
122
    }
123
}
124