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

CategoryBuilderTest::buildFromArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 13
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LauLamanApps\IzettleApi\Tests\Unit\Client\Product;
6
7
use DateTime;
8
use LauLamanApps\IzettleApi\API\Product\Category;
9
use LauLamanApps\IzettleApi\API\Product\CategoryCollection;
10
use LauLamanApps\IzettleApi\Client\Product\CategoryBuilder;
11
use PHPUnit\Framework\TestCase;
12
use Ramsey\Uuid\Uuid;
13
14
/**
15
 * @small
16
 */
17
final class CategoryBuilderTest extends TestCase
18
{
19
    /**
20
     * @test
21
     * @dataProvider getProductJson
22
     */
23
    public function buildFromJson(string $json): void
24
    {
25
        $builder = new CategoryBuilder();
26
        $categories = $builder->buildFromJson($json);
27
        $data =  json_decode($json, true);
28
29
        foreach ($categories as $index => $category) {
30
            self::assertInstanceOf(Category::class, $category);
31
            self::assertSame($data[$index]['uuid'], (string) $category->getUuid());
32
            self::assertSame($data[$index]['name'], $category->getName());
33
            self::assertSame($data[$index]['etag'], $category->getEtag());
34
            self::assertEquals(new DateTime($data[$index]['updated']), $category->getUpdatedAt());
35
            self::assertSame($data[$index]['updatedBy'], (string) $category->getUpdatedBy());
36
            self::assertEquals(new DateTime($data[$index]['created']), $category->getCreatedAt());
37
        }
38
    }
39
40
    /**
41
     * @test
42
     * @dataProvider getCategoryArrayData
43
     */
44
    public function buildFromArray(array $data): void
45
    {
46
        $builder = new CategoryBuilder();
47
        $categoryCollection = $builder->buildFromArray($data);
48
49
        self::assertInstanceOf(CategoryCollection::class, $categoryCollection);
50
51
        $i = 0;// we cannot use the array key here
52
        foreach ($categoryCollection->getAll() as $category) {
53
            self::assertSame($data[$i]['uuid'], (string) $category->getUuid());
54
            self::assertSame($data[$i]['name'], $category->getName());
55
            self::assertSame($data[$i]['etag'], $category->getEtag());
56
            self::assertEquals(new DateTime($data[$i]['updated']), $category->getUpdatedAt());
57
            self::assertSame($data[$i]['updatedBy'], (string) $category->getUpdatedBy());
58
            self::assertEquals(new DateTime($data[$i]['created']), $category->getCreatedAt());
59
60
            $i++;
61
        }
62
    }
63
64
    public function getProductJson(): array
65
    {
66
        $data = [[
67
            'uuid' => (string) Uuid::uuid1(),
68
            'name' => 'category1',
69
            'etag' => 'B1C54B44DB967F4240B59AFA30B1AC5E',
70
            'updated' => '2017-12-06T13:21:59.722+0000',
71
            'updatedBy' => (string) Uuid::uuid1(),
72
            'created' => '2017-12-21T13:12:49.272+0000',
73
        ]];
74
75
        return [
76
            'Single Category' => [ json_encode($data) ],
77
        ];
78
    }
79
80
    public function getCategoryArrayData(): array
81
    {
82
        return [
83
            'Single Category' => [
84
                [
85
                    [
86
                        'uuid' => (string) Uuid::uuid1(),
87
                        'name' => 'category2',
88
                        'etag' => 'B1C54B44DB967F4240B59AFA30B1AC5E',
89
                        'updated' => '2017-12-16T13:21:59.722+0000',
90
                        'updatedBy' => (string) Uuid::uuid1(),
91
                        'created' => '2019-12-28T14:12:49.272+0000',
92
                    ],
93
                ],
94
            ],
95
            'Multiple Category' =>[
96
                [
97
                    [
98
                        'uuid' => (string) Uuid::uuid1(),
99
                        'name' => 'category3',
100
                        'etag' => 'B1C54B44DB967F4240B59AFA30B1AC5E',
101
                        'updated' => '2017-10-18T13:21:59.722+0000',
102
                        'updatedBy' => (string) Uuid::uuid1(),
103
                        'created' => '2018-06-21T13:12:49.272+0000',
104
                    ],
105
                ],
106
                [
107
                    [
108
                        'uuid' => (string) Uuid::uuid1(),
109
                        'name' => 'category4',
110
                        'etag' => 'B1C54B44DB967F4240B59AFA30B1AC5E',
111
                        'updated' => '2017-05-02T13:21:59.722+0000',
112
                        'updatedBy' => (string) Uuid::uuid1(),
113
                        'created' => '2014-08-05T13:12:49.272+0000',
114
                    ],
115
                ],
116
            ],
117
        ];
118
    }
119
}
120