|
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\ImageCollection; |
|
9
|
|
|
use LauLamanApps\IzettleApi\API\Product\CategoryCollection; |
|
10
|
|
|
use LauLamanApps\IzettleApi\API\Product\Product; |
|
11
|
|
|
use LauLamanApps\IzettleApi\API\Product\VariantCollection; |
|
12
|
|
|
use LauLamanApps\IzettleApi\Client\Product\CategoryBuilderInterface; |
|
13
|
|
|
use LauLamanApps\IzettleApi\Client\Product\ProductBuilder; |
|
14
|
|
|
use LauLamanApps\IzettleApi\Client\Product\VariantBuilderInterface; |
|
15
|
|
|
use LauLamanApps\IzettleApi\Client\Universal\ImageBuilderInterface; |
|
16
|
|
|
use Mockery; |
|
17
|
|
|
use PHPUnit\Framework\TestCase; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @small |
|
21
|
|
|
*/ |
|
22
|
|
|
final class ProductBuilderTest extends TestCase |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @test |
|
26
|
|
|
* @dataProvider getProductJsonData |
|
27
|
|
|
*/ |
|
28
|
|
View Code Duplication |
public function buildFromJson($json, $data) |
|
29
|
|
|
{ |
|
30
|
|
|
$categoryBuilderMock = Mockery::mock(CategoryBuilderInterface::class); |
|
31
|
|
|
$imageBuilderMock = Mockery::mock(ImageBuilderInterface::class); |
|
32
|
|
|
$variantBuilderMock = Mockery::mock(VariantBuilderInterface::class); |
|
33
|
|
|
|
|
34
|
|
|
foreach ($data as $product) { |
|
35
|
|
|
$categoryBuilderMock->shouldReceive('buildFromArray') |
|
36
|
|
|
->with(($product['categories']))->once()->andReturn(new CategoryCollection()); |
|
37
|
|
|
$imageBuilderMock->shouldReceive('buildFromArray') |
|
38
|
|
|
->with(($product['imageLookupKeys']))->once()->andReturn(new ImageCollection()); |
|
39
|
|
|
$variantBuilderMock->shouldReceive('buildFromArray') |
|
40
|
|
|
->with(($product['variants']))->once()->andReturn(new VariantCollection()); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
$builder = new ProductBuilder($categoryBuilderMock, $imageBuilderMock, $variantBuilderMock); |
|
44
|
|
|
$products = $builder->buildFromJson($json); |
|
45
|
|
|
|
|
46
|
|
|
foreach ($products as $index => $product) { |
|
47
|
|
|
self::assertInstanceOf(Product::class, $product); |
|
48
|
|
|
self::assertSame($data[$index]['uuid'], (string) $product->getUuid()); |
|
49
|
|
|
self::assertInstanceOf(CategoryCollection::class, $product->getCategories()); |
|
50
|
|
|
self::assertSame($data[$index]['name'], $product->getName()); |
|
51
|
|
|
self::assertSame($data[$index]['description'], $product->getDescription()); |
|
52
|
|
|
self::assertInstanceOf(ImageCollection::class, $product->getImageLookupKeys()); |
|
53
|
|
|
self::assertInstanceOf(VariantCollection::class, $product->getVariants()); |
|
54
|
|
|
self::assertSame($data[$index]['externalReference'], $product->getExternalReference()); |
|
55
|
|
|
self::assertSame($data[$index]['etag'], $product->getEtag()); |
|
56
|
|
|
self::assertEquals(new DateTime($data[$index]['updated']), $product->getUpdatedAt()); |
|
57
|
|
|
self::assertSame($data[$index]['updatedBy'], (string) $product->getUpdatedBy()); |
|
58
|
|
|
self::assertEquals(new DateTime($data[$index]['created']), $product->getCreatedAt()); |
|
59
|
|
|
self::assertSame((float)$data[$index]['vatPercentage'], $product->getVatPercentage()); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @test |
|
65
|
|
|
* @dataProvider getProductArrayData |
|
66
|
|
|
*/ |
|
67
|
|
View Code Duplication |
public function buildFromArray($data) |
|
68
|
|
|
{ |
|
69
|
|
|
$categoryBuilderMock = Mockery::mock(CategoryBuilderInterface::class); |
|
70
|
|
|
$imageBuilderMock = Mockery::mock(ImageBuilderInterface::class); |
|
71
|
|
|
$variantBuilderMock = Mockery::mock(VariantBuilderInterface::class); |
|
72
|
|
|
|
|
73
|
|
|
foreach ($data as $product) { |
|
74
|
|
|
$categoryBuilderMock->shouldReceive('buildFromArray') |
|
75
|
|
|
->with(($product['categories']))->once()->andReturn(new CategoryCollection()); |
|
76
|
|
|
$imageBuilderMock->shouldReceive('buildFromArray') |
|
77
|
|
|
->with(($product['imageLookupKeys']))->once()->andReturn(new ImageCollection()); |
|
78
|
|
|
$variantBuilderMock->shouldReceive('buildFromArray') |
|
79
|
|
|
->with(($product['variants']))->once()->andReturn(new VariantCollection()); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
$builder = new ProductBuilder($categoryBuilderMock, $imageBuilderMock, $variantBuilderMock); |
|
83
|
|
|
$products = $builder->buildFromArray($data); |
|
84
|
|
|
|
|
85
|
|
|
$index = 0; |
|
86
|
|
|
foreach ($products->getAll() as $product) { |
|
87
|
|
|
self::assertInstanceOf(Product::class, $product); |
|
88
|
|
|
self::assertSame($data[$index]['uuid'], (string) $product->getUuid()); |
|
89
|
|
|
self::assertInstanceOf(CategoryCollection::class, $product->getCategories()); |
|
90
|
|
|
self::assertSame($data[$index]['name'], $product->getName()); |
|
91
|
|
|
self::assertSame($data[$index]['description'], $product->getDescription()); |
|
92
|
|
|
self::assertInstanceOf(ImageCollection::class, $product->getImageLookupKeys()); |
|
93
|
|
|
self::assertInstanceOf(VariantCollection::class, $product->getVariants()); |
|
94
|
|
|
self::assertSame($data[$index]['externalReference'], $product->getExternalReference()); |
|
95
|
|
|
self::assertSame($data[$index]['etag'], $product->getEtag()); |
|
96
|
|
|
self::assertEquals(new DateTime($data[$index]['updated']), $product->getUpdatedAt()); |
|
97
|
|
|
self::assertSame($data[$index]['updatedBy'], (string) $product->getUpdatedBy()); |
|
98
|
|
|
self::assertEquals(new DateTime($data[$index]['created']), $product->getCreatedAt()); |
|
99
|
|
|
self::assertSame((float)$data[$index]['vatPercentage'], $product->getVatPercentage()); |
|
100
|
|
|
$index++; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public function getProductJsonData(): array |
|
105
|
|
|
{ |
|
106
|
|
|
return [ |
|
107
|
|
|
'single' => $this->getDataFromFile('single-product.json'), |
|
108
|
|
|
'multiple' => $this->getDataFromFile('multiple-product.json'), |
|
109
|
|
|
]; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
public function getProductArrayData(): array |
|
113
|
|
|
{ |
|
114
|
|
|
return [ |
|
115
|
|
|
'single' => [$this->getDataFromFile('single-product.json')[1]], |
|
116
|
|
|
'multiple' => [$this->getDataFromFile('multiple-product.json')[1]], |
|
117
|
|
|
]; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
private function getDataFromFile($filename): array |
|
121
|
|
|
{ |
|
122
|
|
|
$singleProductJson = file_get_contents(dirname(__FILE__) . '/json-files/' . $filename); |
|
123
|
|
|
$singleProductArray = json_decode($singleProductJson, true); |
|
124
|
|
|
|
|
125
|
|
|
return [$singleProductJson, $singleProductArray]; |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|