|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace LauLamanApps\IzettleApi\Tests\Unit\Client\Purchase; |
|
6
|
|
|
|
|
7
|
|
|
use DateTime; |
|
8
|
|
|
use LauLamanApps\IzettleApi\API\Purchase\Coordinates; |
|
9
|
|
|
use LauLamanApps\IzettleApi\API\Purchase\Purchase; |
|
10
|
|
|
use LauLamanApps\IzettleApi\Client\Purchase\CoordinatesBuilderInterface; |
|
11
|
|
|
use LauLamanApps\IzettleApi\Client\Purchase\PaymentBuilderInterface; |
|
12
|
|
|
use LauLamanApps\IzettleApi\Client\Purchase\ProductBuilderInterface; |
|
13
|
|
|
use LauLamanApps\IzettleApi\Client\Purchase\PurchaseBuilder; |
|
14
|
|
|
use LauLamanApps\IzettleApi\Client\Purchase\VatBuilderInterface; |
|
15
|
|
|
use Mockery; |
|
16
|
|
|
use Money\Currency; |
|
17
|
|
|
use PHPUnit\Framework\TestCase; |
|
18
|
|
|
use Ramsey\Uuid\Uuid; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @small |
|
22
|
|
|
*/ |
|
23
|
|
|
final class PurchaseBuilderTest extends TestCase |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @test |
|
27
|
|
|
*/ |
|
28
|
|
|
public function buildFromJson(): void |
|
29
|
|
|
{ |
|
30
|
|
|
$data = $this->getDataSet1(); |
|
31
|
|
|
$coordinatesBuilderMock = Mockery::mock(CoordinatesBuilderInterface::class); |
|
32
|
|
|
$coordinatesBuilderMock->shouldReceive('buildFromArray')->once()->with($data['gpsCoordinates'])->andReturn(new Coordinates(0, 0, 0)); |
|
33
|
|
|
$productBuilderMock = Mockery::mock(ProductBuilderInterface::class); |
|
34
|
|
|
$productBuilderMock->shouldReceive('buildFromArray')->once()->with($data['products'], Mockery::type((Currency::class))); |
|
35
|
|
|
$paymentBuilderMock = Mockery::mock(PaymentBuilderInterface::class); |
|
36
|
|
|
$paymentBuilderMock->shouldReceive('buildFromArray')->once()->with($data['payments'], Mockery::type((Currency::class))); |
|
37
|
|
|
$vatBuilderMock = Mockery::mock(VatBuilderInterface::class); |
|
38
|
|
|
$vatBuilderMock->shouldReceive('buildFromArray')->once()->with($data['groupedVatAmounts'], Mockery::type((Currency::class))); |
|
39
|
|
|
|
|
40
|
|
|
$builder = new PurchaseBuilder($coordinatesBuilderMock, $productBuilderMock, $paymentBuilderMock, $vatBuilderMock); |
|
41
|
|
|
$purchase = $builder->buildFromJson(json_encode($data)); |
|
42
|
|
|
|
|
43
|
|
|
self::assertInstanceOf(Purchase::class, $purchase); |
|
44
|
|
|
self::assertSame($data['purchaseUUID'], $purchase->getUuid()); |
|
45
|
|
|
self::assertSame($data['purchaseUUID1'], (string) $purchase->getUuid1()); |
|
46
|
|
|
self::assertSame($data['amount'], (int) $purchase->getAmount()->getAmount()); |
|
47
|
|
|
self::assertSame($data['currency'], $purchase->getAmount()->getCurrency()->getCode()); |
|
48
|
|
|
self::assertSame($data['vatAmount'], (int) $purchase->getVatAmount()->getAmount()); |
|
49
|
|
|
self::assertSame($data['country'], $purchase->getCountry()); |
|
50
|
|
|
self::assertEquals(new DateTime($data['timestamp']), $purchase->getTimestamp()); |
|
51
|
|
|
self::assertInstanceOf(Coordinates::class, $purchase->getCoordinates()); |
|
52
|
|
|
self::assertSame($data['purchaseNumber'], $purchase->getPurchaseNumber()); |
|
53
|
|
|
self::assertSame($data['userDisplayName'], (string) $purchase->getUser()); |
|
54
|
|
|
self::assertSame($data['userId'], $purchase->getUser()->getId()); |
|
55
|
|
|
self::assertSame($data['organizationId'], $purchase->getOrganizationId()); |
|
56
|
|
|
self::assertSame($data['receiptCopyAllowed'], $purchase->isReceiptCopyAllowed()); |
|
57
|
|
|
self::assertSame($data['published'], $purchase->getPublished()); |
|
58
|
|
|
self::assertSame($data['refund'], $purchase->isRefund()); |
|
59
|
|
|
self::assertSame($data['refunded'], $purchase->isRefunded()); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @test |
|
64
|
|
|
*/ |
|
65
|
|
|
public function buildFromJsonArray() |
|
66
|
|
|
{ |
|
67
|
|
|
$data[] = $this->getDataSet1(); |
|
|
|
|
|
|
68
|
|
|
$data[] = $this->getDataSet2(); |
|
69
|
|
|
|
|
70
|
|
|
$count = count($data); |
|
71
|
|
|
$coordinatesBuilderMock = Mockery::mock(CoordinatesBuilderInterface::class); |
|
72
|
|
|
$coordinatesBuilderMock->shouldReceive('buildFromArray')->times($count)->andReturn(new Coordinates(0, 0, 0)); |
|
73
|
|
|
$productBuilderMock = Mockery::mock(ProductBuilderInterface::class); |
|
74
|
|
|
$productBuilderMock->shouldReceive('buildFromArray')->times($count); |
|
75
|
|
|
$paymentBuilderMock = Mockery::mock(PaymentBuilderInterface::class); |
|
76
|
|
|
$paymentBuilderMock->shouldReceive('buildFromArray')->times($count); |
|
77
|
|
|
$vatBuilderMock = Mockery::mock(VatBuilderInterface::class); |
|
78
|
|
|
$vatBuilderMock->shouldReceive('buildFromArray')->times(); |
|
79
|
|
|
|
|
80
|
|
|
$builder = new PurchaseBuilder($coordinatesBuilderMock, $productBuilderMock, $paymentBuilderMock, $vatBuilderMock); |
|
81
|
|
|
$purchases = $builder->buildFromArray($data); |
|
82
|
|
|
|
|
83
|
|
|
self::assertSame(count($data), count($purchases)); |
|
84
|
|
|
|
|
85
|
|
|
foreach ($purchases as $purchase) { |
|
86
|
|
|
self::assertInstanceOf(Purchase::class, $purchase); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function getDataSet1(): array |
|
91
|
|
|
{ |
|
92
|
|
|
return [ |
|
93
|
|
|
"purchaseUUID" => "ShNaGb-nSiMAJPESKGynSG", |
|
94
|
|
|
"purchaseUUID1" => (string) Uuid::uuid1(), |
|
95
|
|
|
"amount" => 100, |
|
96
|
|
|
"vatAmount" => 17, |
|
97
|
|
|
"country" => "NL", |
|
98
|
|
|
"currency" => "EUR", |
|
99
|
|
|
"timestamp" => "2016-05-01T14:31:29.748+0000", |
|
100
|
|
|
"gpsCoordinates" => [(string) Uuid::uuid1()], |
|
101
|
|
|
"purchaseNumber" => 1, |
|
102
|
|
|
"userDisplayName" => "John Doe", |
|
103
|
|
|
"userId" => 12766, |
|
104
|
|
|
"organizationId" => 897184, |
|
105
|
|
|
"products" => [(string) Uuid::uuid1()], |
|
106
|
|
|
"payments" => [(string) Uuid::uuid1()], |
|
107
|
|
|
"receiptCopyAllowed" => true, |
|
108
|
|
|
"published" => true, |
|
109
|
|
|
"groupedVatAmounts" => [(string) Uuid::uuid1()], |
|
110
|
|
|
"refund" => false, |
|
111
|
|
|
"refunded" => false |
|
112
|
|
|
]; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
public function getDataSet2(): array |
|
116
|
|
|
{ |
|
117
|
|
|
return [ |
|
118
|
|
|
"purchaseUUID" => "ShNaGb-nSiMAJPESKGynSG", |
|
119
|
|
|
"purchaseUUID1" => (string) Uuid::uuid1(), |
|
120
|
|
|
"amount" => 100, |
|
121
|
|
|
"vatAmount" => 17, |
|
122
|
|
|
"country" => "NL", |
|
123
|
|
|
"currency" => "EUR", |
|
124
|
|
|
"timestamp" => "2016-05-01T14:31:29.748+0000", |
|
125
|
|
|
"purchaseNumber" => 1, |
|
126
|
|
|
"userDisplayName" => "John Doe", |
|
127
|
|
|
"userId" => 12766, |
|
128
|
|
|
"organizationId" => 897184, |
|
129
|
|
|
"products" => [(string) Uuid::uuid1()], |
|
130
|
|
|
"payments" => [(string) Uuid::uuid1()], |
|
131
|
|
|
"receiptCopyAllowed" => true, |
|
132
|
|
|
"groupedVatAmounts" => [(string) Uuid::uuid1()], |
|
133
|
|
|
"refund" => false, |
|
134
|
|
|
"refunded" => false |
|
135
|
|
|
]; |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.