|
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\PurchaseParser; |
|
11
|
|
|
use PHPUnit\Framework\TestCase; |
|
12
|
|
|
use Ramsey\Uuid\Uuid; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @small |
|
16
|
|
|
*/ |
|
17
|
|
|
final class PurchaseParserTest extends TestCase |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @test |
|
21
|
|
|
*/ |
|
22
|
|
|
public function parse(): void |
|
23
|
|
|
{ |
|
24
|
|
|
$data = $this->getData(); |
|
25
|
|
|
|
|
26
|
|
|
$purchase = PurchaseParser::parse($data); |
|
27
|
|
|
|
|
28
|
|
|
self::assertInstanceOf(Purchase::class, $purchase); |
|
29
|
|
|
self::assertSame($data['purchaseUUID'], $purchase->getUuid()); |
|
30
|
|
|
self::assertSame($data['purchaseUUID1'], (string) $purchase->getUuid1()); |
|
31
|
|
|
self::assertSame($data['amount'], (int) $purchase->getAmount()->getAmount()); |
|
32
|
|
|
self::assertSame($data['currency'], $purchase->getAmount()->getCurrency()->getCode()); |
|
33
|
|
|
self::assertSame($data['vatAmount'], (int) $purchase->getVatAmount()->getAmount()); |
|
34
|
|
|
self::assertInstanceOf(Coordinates::class, $purchase->getCoordinates()); |
|
35
|
|
|
self::assertSame($data['country'], $purchase->getCountry()); |
|
36
|
|
|
self::assertEquals(new DateTime($data['timestamp']), $purchase->getTimestamp()); |
|
37
|
|
|
self::assertSame($data['purchaseNumber'], $purchase->getPurchaseNumber()); |
|
38
|
|
|
self::assertSame($data['userDisplayName'], (string) $purchase->getUser()); |
|
39
|
|
|
self::assertSame($data['userId'], $purchase->getUser()->getId()); |
|
40
|
|
|
self::assertSame($data['organizationId'], $purchase->getOrganizationId()); |
|
41
|
|
|
self::assertSame($data['receiptCopyAllowed'], $purchase->isReceiptCopyAllowed()); |
|
42
|
|
|
self::assertSame($data['published'], $purchase->getPublished()); |
|
43
|
|
|
self::assertSame($data['refund'], $purchase->isRefund()); |
|
44
|
|
|
self::assertSame($data['refunded'], $purchase->isRefunded()); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @test |
|
49
|
|
|
*/ |
|
50
|
|
|
public function parseArray() |
|
51
|
|
|
{ |
|
52
|
|
|
$data[] = $this->getData(); |
|
|
|
|
|
|
53
|
|
|
$data[] = $this->getData(); |
|
54
|
|
|
|
|
55
|
|
|
$purchases = PurchaseParser::parseArray($data); |
|
56
|
|
|
|
|
57
|
|
|
self::assertSame(count($data), count($purchases)); |
|
58
|
|
|
|
|
59
|
|
|
foreach ($purchases as $purchase) { |
|
60
|
|
|
self::assertInstanceOf(Purchase::class, $purchase); |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function getData(): array |
|
65
|
|
|
{ |
|
66
|
|
|
return [ |
|
67
|
|
|
"purchaseUUID" => "ShNaGb-nSiMAJPESKGynSG", |
|
68
|
|
|
"purchaseUUID1" => (string) Uuid::uuid1(), |
|
69
|
|
|
"amount" => 100, |
|
70
|
|
|
"vatAmount" => 17, |
|
71
|
|
|
"country" => "NL", |
|
72
|
|
|
"currency" => "EUR", |
|
73
|
|
|
"timestamp" => "2016-05-01T14:31:29.748+0000", |
|
74
|
|
|
"gpsCoordinates" => ['latitude' => 0, 'longitude' => 0, 'accuracyMeters' => 0], |
|
75
|
|
|
"purchaseNumber" => 1, |
|
76
|
|
|
"userDisplayName" => "John Doe", |
|
77
|
|
|
"userId" => 12766, |
|
78
|
|
|
"organizationId" => 897184, |
|
79
|
|
|
"products" => [], |
|
80
|
|
|
"payments" => [], |
|
81
|
|
|
"receiptCopyAllowed" => true, |
|
82
|
|
|
"published" => true, |
|
83
|
|
|
"groupedVatAmounts" => [], |
|
84
|
|
|
"refund" => false, |
|
85
|
|
|
"refunded" => false |
|
86
|
|
|
]; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
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.