Completed
Pull Request — master (#6)
by Laurens
03:26
created

PurchaseParserTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 7
dl 0
loc 72
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B parse() 0 24 1
A parseArray() 0 13 2
B getData() 0 24 1
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();
0 ignored issues
show
Coding Style Comprehensibility introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.

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:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key 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.

Loading history...
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