Completed
Push — master ( eac57c...9ff768 )
by Laurens
02:04
created

iZettleClientTest::setOrganizationUuid()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LauLamanApps\iZettleApi\Tests\Unit\Client;
6
7
use DateTime;
8
use GuzzleHttp\Client as GuzzleClient;
9
use LauLamanApps\iZettleApi\API\ImageCollection;
10
use LauLamanApps\iZettleApi\API\Product\Category;
11
use LauLamanApps\iZettleApi\API\Product\CategoryCollection;
12
use LauLamanApps\iZettleApi\API\Product\Discount;
13
use LauLamanApps\iZettleApi\API\Product\Library;
14
use LauLamanApps\iZettleApi\API\Product\Product;
15
use LauLamanApps\iZettleApi\API\Product\Variant;
16
use LauLamanApps\iZettleApi\API\Product\VariantCollection;
17
use LauLamanApps\iZettleApi\API\Purchase\PurchaseHistory;
18
use LauLamanApps\iZettleApi\Client\AccessToken;
19
use LauLamanApps\iZettleApi\iZettleClient;
20
use Mockery;
21
use Money\Money;
22
use PHPUnit\Framework\TestCase;
23
use Psr\Http\Message\ResponseInterface;
24
use Ramsey\Uuid\Uuid;
25
26
/**
27
 * @small
28
 */
29
final class iZettleClientTest extends TestCase
30
{
31
    const ACCESS_TOKEN = 'access-token';
32
    const DEFAULT_ORGANIZATION_UUID = 'self';
33
34
    /**
35
     * @test
36
     */
37
    public function setOrganizationUuid()
38
    {
39
        $newOrganizationUuid = 123456;
40
        $iZettleClient = new iZettleClient(new GuzzleClient(), $this->getAccessToken());
41
        self::assertAttributeEquals(self::DEFAULT_ORGANIZATION_UUID, 'organizationUuid', $iZettleClient);
42
43
        $iZettleClient->setOrganizationUuid($newOrganizationUuid);
44
45
        self::assertAttributeEquals($newOrganizationUuid, 'organizationUuid', $iZettleClient);
46
    }
47
48
    /**
49
     * @test
50
     */
51
    public function getCategories()
52
    {
53
        $method = 'get';
54
        $url = sprintf(iZettleClient::PRODUCT_CATEGORY_ALL, self::DEFAULT_ORGANIZATION_UUID);
55
        $options = [
56
            'headers' => [
57
                'Authorization' => sprintf('Bearer %s', self::ACCESS_TOKEN)
58
            ],
59
            'query' => null,
60
        ];
61
62
        $iZettleClient = new iZettleClient($this->getGuzzleClient($method, $url, $options), $this->getAccessToken());
63
        $categories = $iZettleClient->getCategories();
64
        self::assertTrue(is_array($categories));
65
    }
66
67
    /**
68
     * @test
69
     */
70
    public function createCategory()
71
    {
72
        $category = Category::new('name');
73
        $method = 'post';
74
        $url = sprintf(iZettleClient::PRODUCT_CATEGORY_ALL, self::DEFAULT_ORGANIZATION_UUID);
75
        $options = [
76
            'headers' => [
77
                'Authorization' => sprintf('Bearer %s', self::ACCESS_TOKEN),
78
                'content-type' => 'application/json',
79
                'Accept' => 'application/json',
80
            ],
81
            'body' => $category->getCreateData(),
82
        ];
83
84
        $iZettleClient = new iZettleClient($this->getGuzzleClient($method, $url, $options), $this->getAccessToken());
85
        $iZettleClient->createCategory($category);
86
87
        self::assertTrue(true); //-- fix till issue is solved: https://github.com/mockery/mockery/issues/376
88
    }
89
90
    /**
91
     * @test
92
     */
93
    public function getDiscounts()
94
    {
95
        $method = 'get';
96
        $url = sprintf(iZettleClient::PRODUCT_DISCOUNT_ALL, self::DEFAULT_ORGANIZATION_UUID);
97
        $options = [
98
            'headers' => [
99
                'Authorization' => sprintf('Bearer %s', self::ACCESS_TOKEN)
100
            ],
101
            'query' => null,
102
        ];
103
104
        $iZettleClient = new iZettleClient($this->getGuzzleClient($method, $url, $options), $this->getAccessToken());
105
        $discounts = $iZettleClient->getDiscounts();
106
        self::assertTrue(is_array($discounts));
107
    }
108
109
    /**
110
     * @test
111
     */
112
    public function createDiscount()
113
    {
114
        $discount = $this->getDiscount();
115
        $method = 'post';
116
        $url = sprintf(iZettleClient::PRODUCT_DISCOUNT_ALL, self::DEFAULT_ORGANIZATION_UUID);
117
        $options = [
118
            'headers' => [
119
                'Authorization' => sprintf('Bearer %s', self::ACCESS_TOKEN),
120
                'content-type' => 'application/json',
121
                'Accept' => 'application/json',
122
            ],
123
            'body' => $discount->getCreateData(),
124
        ];
125
126
        $iZettleClient = new iZettleClient($this->getGuzzleClient($method, $url, $options), $this->getAccessToken());
127
        $iZettleClient->createDiscount($discount);
128
129
        self::assertTrue(true); //-- fix till issue is solved: https://github.com/mockery/mockery/issues/376
130
    }
131
132
    /**
133
     * @test
134
     */
135
    public function deleteDiscount()
136
    {
137
        $discount = $this->getDiscount();
138
        $method = 'delete';
139
        $url = sprintf(
140
            iZettleClient::PRODUCT_DISCOUNT_SINGLE,
141
            self::DEFAULT_ORGANIZATION_UUID,
142
            (string) $discount->getUuid()
143
        );
144
        $options = [
145
            'headers' => [
146
                'Authorization' => sprintf('Bearer %s', self::ACCESS_TOKEN),
147
            ],
148
        ];
149
150
        $iZettleClient = new iZettleClient($this->getGuzzleClient($method, $url, $options), $this->getAccessToken());
151
        $iZettleClient->deleteDiscount($discount);
152
153
        self::assertTrue(true); //-- fix till issue is solved: https://github.com/mockery/mockery/issues/376
154
    }
155
156
    /**
157
     * @test
158
     */
159
    public function getLibrary()
160
    {
161
        $method = 'get';
162
        $url = sprintf(iZettleClient::PRODUCT_LIBRARY, self::DEFAULT_ORGANIZATION_UUID);
163
        $options = [
164
            'headers' => [
165
                'Authorization' => sprintf('Bearer %s', self::ACCESS_TOKEN)
166
            ],
167
            'query' => null,
168
        ];
169
170
        $return = [
171
            'fromEventLogUuid' => Uuid::uuid1(),
172
            'untilEventLogUuid' => Uuid::uuid1(),
173
            'products' => [],
174
            'discounts' => [],
175
            'deletedProducts' => [],
176
            'deletedDiscounts' => [],
177
        ];
178
179
        $iZettleClient = new iZettleClient($this->getGuzzleClient($method, $url, $options, $return), $this->getAccessToken());
180
        $library = $iZettleClient->getLibrary();
181
        self::assertInstanceOf(Library::class, $library);
182
    }
183
184
    /**
185
     * @test
186
     */
187
    public function getProducts()
188
    {
189
        $method = 'get';
190
        $url = sprintf(iZettleClient::PRODUCT_PRODUCTS_ALL, self::DEFAULT_ORGANIZATION_UUID);
191
        $options = [
192
            'headers' => [
193
                'Authorization' => sprintf('Bearer %s', self::ACCESS_TOKEN)
194
            ],
195
            'query' => null,
196
        ];
197
198
        $return = [
199
200
        ];
201
202
        $iZettleClient = new iZettleClient($this->getGuzzleClient($method, $url, $options, $return), $this->getAccessToken());
203
        $products = $iZettleClient->getProducts();
204
        self::assertTrue(is_array($products));
205
    }
206
207
    /**
208
     * @test
209
     */
210
    public function createProduct()
211
    {
212
        $product = $this->getProduct();
213
        $method = 'post';
214
        $url = sprintf(iZettleClient::PRODUCT_PRODUCTS_ALL, self::DEFAULT_ORGANIZATION_UUID);
215
        $options = [
216
            'headers' => [
217
                'Authorization' => sprintf('Bearer %s', self::ACCESS_TOKEN),
218
                'content-type' => 'application/json',
219
                'Accept' => 'application/json',
220
            ],
221
            'body' => $product->getCreateData(),
222
        ];
223
224
        $iZettleClient = new iZettleClient($this->getGuzzleClient($method, $url, $options), $this->getAccessToken());
225
        $iZettleClient->createProduct($product);
226
227
        self::assertTrue(true); //-- fix till issue is solved: https://github.com/mockery/mockery/issues/376
228
    }
229
230
    /**
231
     * @test
232
     */
233
    public function deleteProduct()
234
    {
235
        $product = $this->getProduct();
236
        $method = 'delete';
237
        $url = sprintf(
238
            iZettleClient::PRODUCT_PRODUCTS_SINGLE,
239
            self::DEFAULT_ORGANIZATION_UUID,
240
            (string) $product->getUuid()
241
        );
242
        $options = [
243
            'headers' => [
244
                'Authorization' => sprintf('Bearer %s', self::ACCESS_TOKEN),
245
            ],
246
        ];
247
248
        $iZettleClient = new iZettleClient($this->getGuzzleClient($method, $url, $options), $this->getAccessToken());
249
        $iZettleClient->deleteProduct($product);
250
251
        self::assertTrue(true); //-- fix till issue is solved: https://github.com/mockery/mockery/issues/376
252
    }
253
254
    /**
255
     * @test
256
     */
257
    public function getPurchaseHistory()
258
    {
259
        $method = 'get';
260
        $url = sprintf(iZettleClient::PURCHASE_HISTORY_URL, self::DEFAULT_ORGANIZATION_UUID);
261
        $options = [
262
            'headers' => [
263
                'Authorization' => sprintf('Bearer %s', self::ACCESS_TOKEN)
264
            ],
265
            'query' => null,
266
        ];
267
268
        $return = [
269
            'firstPurchaseHash' => Uuid::uuid1(),
270
            'lastPurchaseHash' => Uuid::uuid1(),
271
            'purchases' => [],
272
        ];
273
274
        $iZettleClient = new iZettleClient($this->getGuzzleClient($method, $url, $options, $return), $this->getAccessToken());
275
        $purchaseHistory = $iZettleClient->getPurchaseHistory();
276
        self::assertInstanceOf(PurchaseHistory::class, $purchaseHistory);
277
    }
278
279
    /**
280
     * @test
281
     * @expectedException \LauLamanApps\iZettleApi\Client\Exceptions\AccessTokenExpiredException
282
     */
283
    public function validateAccessToken()
284
    {
285
        $invalidAccessToken =  new AccessToken('', new DateTime('-1 day'), '');
286
287
        new iZettleClient(new GuzzleClient(), $invalidAccessToken);
288
    }
289
290
    private function getAccessToken() : AccessToken
291
    {
292
        return new AccessToken(self::ACCESS_TOKEN, new DateTime('+ 1 day'), '');
293
    }
294
295
    private function getGuzzleClient(
296
        string $method,
297
        string $url,
298
        array $options,
299
        ?array $return = []
300
    ): GuzzleClient {
301
        $guzzleResponseMock = Mockery::mock(ResponseInterface::class);
302
        $guzzleResponseMock->shouldReceive('getBody')->andReturnSelf();
303
        $guzzleResponseMock->shouldReceive('getContents')->andReturn(json_encode($return));
304
305
        $guzzleClientMock = Mockery::mock(GuzzleClient::class);
306
        $guzzleClientMock->shouldReceive($method)->withArgs([$url, $options])->andReturn($guzzleResponseMock);
1 ignored issue
show
Bug introduced by
The method withArgs does only exist in Mockery\HigherOrderMessage, but not in Mockery\ExpectationInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
307
308
        return $guzzleClientMock;
309
    }
310
311
    private function getDiscount(): Discount
312
    {
313
        return Discount::new('name', 'description', new ImageCollection());
314
    }
315
316
    private function getProduct(): Product
317
    {
318
        return Product::new(
319
            'name',
320
            'description',
321
            new CategoryCollection(),
322
            new ImageCollection(),
323
            new VariantCollection([ Variant::new(
324
                null,
325
                null,
326
                null,
327
                null,
328
                1,
329
                null,
330
                Money::EUR(0),
331
                null,
332
                21
333
            )])
334
        );
335
    }
336
}
337