|
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\ProductParser; |
|
13
|
|
|
use Mockery; |
|
14
|
|
|
use PHPUnit\Framework\TestCase; |
|
15
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
16
|
|
|
use Ramsey\Uuid\Uuid; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @small |
|
20
|
|
|
*/ |
|
21
|
|
|
final class ProductParserTest extends TestCase |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @test |
|
25
|
|
|
* @dataProvider getProductResponseData |
|
26
|
|
|
*/ |
|
27
|
|
|
public function createFromResponse(ResponseInterface $response, $data) |
|
28
|
|
|
{ |
|
29
|
|
|
/** @var Product[] $products */ |
|
30
|
|
|
$products = ProductParser::createFromResponse($response); |
|
31
|
|
|
|
|
32
|
|
|
foreach ($products as $index => $product) { |
|
33
|
|
|
self::assertInstanceOf(Product::class, $product); |
|
34
|
|
|
self::assertSame($data[$index]['uuid'], (string) $product->getUuid()); |
|
35
|
|
|
self::assertInstanceOf(CategoryCollection::class, $product->getCategories()); |
|
36
|
|
|
self::assertSame($data[$index]['name'], $product->getName()); |
|
37
|
|
|
self::assertSame($data[$index]['description'], $product->getDescription()); |
|
38
|
|
|
self::assertInstanceOf(ImageCollection::class, $product->getImageLookupKeys()); |
|
39
|
|
|
self::assertInstanceOf(VariantCollection::class, $product->getVariants()); |
|
40
|
|
|
self::assertSame($data[$index]['variants'], $product->getVariants()->getAll()); |
|
41
|
|
|
self::assertSame($data[$index]['externalReference'], $product->getExternalReference()); |
|
42
|
|
|
self::assertSame($data[$index]['etag'], $product->getEtag()); |
|
43
|
|
|
self::assertEquals(new DateTime($data[$index]['updated']), $product->getUpdatedAt()); |
|
44
|
|
|
self::assertSame($data[$index]['updatedBy'], (string) $product->getUpdatedBy()); |
|
45
|
|
|
self::assertEquals(new DateTime($data[$index]['created']), $product->getCreatedAt()); |
|
46
|
|
|
self::assertSame((float)$data[$index]['vatPercentage'], $product->getVatPercentage()); |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function getProductResponseData(): array |
|
51
|
|
|
{ |
|
52
|
|
|
$data1 = [[ |
|
53
|
|
|
'uuid' => (string) Uuid::uuid1(), |
|
54
|
|
|
'categories' => [], |
|
55
|
|
|
'name' => 'Some Product', |
|
56
|
|
|
'description' => '', |
|
57
|
|
|
'imageLookupKeys' => [ |
|
58
|
|
|
'nice-image.jpeg', |
|
59
|
|
|
], |
|
60
|
|
|
'variants' => [], |
|
61
|
|
|
'externalReference' => '', |
|
62
|
|
|
'etag' => 'B1C54B44DB967F4240B59AFA30B1AC5E', |
|
63
|
|
|
'updated' => '2017-12-06T13:21:59.722+0000', |
|
64
|
|
|
'updatedBy' => (string) Uuid::uuid1(), |
|
65
|
|
|
'created' => '2017-12-21T13:12:49.272+0000', |
|
66
|
|
|
'vatPercentage' => '21.0' |
|
67
|
|
|
]]; |
|
68
|
|
|
$mock1 = Mockery::mock(ResponseInterface::class); |
|
69
|
|
|
$mock1->shouldReceive('getBody')->andReturnSelf(); |
|
70
|
|
|
$mock1->shouldReceive('getContents')->andReturn(json_encode( |
|
71
|
|
|
$data1 |
|
72
|
|
|
)); |
|
73
|
|
|
|
|
74
|
|
|
return [ |
|
75
|
|
|
'single product' => [ $mock1, $data1 ] |
|
76
|
|
|
]; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|