Cancelled
Pull Request — master (#11)
by Laurens
23:42
created

ProductClientTest::getLibrary()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 28
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 22
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LauLamanApps\IzettleApi\Tests\Unit\Client;
6
7
use LauLamanApps\IzettleApi\API\ImageCollection;
8
use LauLamanApps\IzettleApi\API\Product\Category;
9
use LauLamanApps\IzettleApi\API\Product\CategoryCollection;
10
use LauLamanApps\IzettleApi\API\Product\Discount;
11
use LauLamanApps\IzettleApi\API\Product\DiscountCollection;
12
use LauLamanApps\IzettleApi\API\Product\Library;
13
use LauLamanApps\IzettleApi\API\Product\Product;
14
use LauLamanApps\IzettleApi\API\Product\ProductCollection;
15
use LauLamanApps\IzettleApi\API\Product\Variant;
16
use LauLamanApps\IzettleApi\API\Product\VariantCollection;
17
use LauLamanApps\IzettleApi\Client\Product\CategoryBuilderInterface;
18
use LauLamanApps\IzettleApi\Client\Product\DiscountBuilderInterface;
19
use LauLamanApps\IzettleApi\Client\Product\LibraryBuilderInterface;
20
use LauLamanApps\IzettleApi\Client\Product\ProductBuilderInterface;
21
use LauLamanApps\IzettleApi\Client\ProductClient;
22
use LauLamanApps\IzettleApi\IzettleClientInterface;
23
use LauLamanApps\IzettleApi\Tests\Unit\MockeryAssertionTrait;
24
use Mockery;
25
use Money\Money;
26
use PHPUnit\Framework\TestCase;
27
use Psr\Http\Message\ResponseInterface;
28
use Ramsey\Uuid\Uuid;
29
30
/**
31
 * @small
32
 */
33
final class ProductClientTest extends TestCase
34
{
35
    use MockeryAssertionTrait;
36
37
    /**
38
     * @test
39
     */
40 View Code Duplication
    public function getCategories()
41
    {
42
        $organizationUuid = Uuid::uuid1();
43
        $url = sprintf(ProductClient::GET_CATEGORIES, (string) $organizationUuid);
44
        $data = ['getCategoriesTest'];
45
46
        $izettleClientMock = $this->getIzettleGetMock($url, $data);
47
48
        list($categoryBuilderMock, $discountBuilderMock, $libraryBuilderMock, $productBuilderMock) = $this->getMocks();
49
50
        $categoryBuilderMock->shouldReceive('buildFromJson')->with(json_encode($data))->once();
51
52
        $productClient = new ProductClient(
53
            $izettleClientMock,
54
            $organizationUuid,
55
            $categoryBuilderMock,
56
            $discountBuilderMock,
57
            $libraryBuilderMock,
58
            $productBuilderMock
59
        );
60
        $productClient->getCategories();
61
    }
62
63
    /**
64
     * @test
65
     */
66 View Code Duplication
    public function createCategory()
67
    {
68
        $organizationUuid = Uuid::uuid1();
69
        $category = Category::new('name');
70
        $url = sprintf(ProductClient::POST_CATEGORY, (string) $organizationUuid);
71
72
        $izettleClientMock = $this->getIzettlePostMock($url, $category->getCreateData());
73
        list($categoryBuilderMock, $discountBuilderMock, $libraryBuilderMock, $productBuilderMock) = $this->getMocks();
74
75
        $productClient = new ProductClient(
76
            $izettleClientMock,
77
            $organizationUuid,
78
            $categoryBuilderMock,
79
            $discountBuilderMock,
80
            $libraryBuilderMock,
81
            $productBuilderMock
82
        );
83
84
        $productClient->createCategory($category);
85
    }
86
87
    /**
88
     * @test
89
     */
90 View Code Duplication
    public function getDiscounts()
91
    {
92
        $organizationUuid = Uuid::uuid1();
93
        $data = ['getDiscountsTest'];
94
        $url = sprintf(ProductClient::GET_DISCOUNTS, $organizationUuid);
95
96
        $izettleClientMock = $this->getIzettleGetMock($url, $data);
97
        list($categoryBuilderMock, $discountBuilderMock, $libraryBuilderMock, $productBuilderMock) = $this->getMocks();
98
        $discountBuilderMock->shouldReceive('buildFromJson')->with(json_encode($data))->once();
99
100
        $productClient = new ProductClient(
101
            $izettleClientMock,
102
            $organizationUuid,
103
            $categoryBuilderMock,
104
            $discountBuilderMock,
105
            $libraryBuilderMock,
106
            $productBuilderMock
107
        );
108
109
        $productClient->getDiscounts();
110
    }
111
112
    /**
113
     * @test
114
     */
115 View Code Duplication
    public function createDiscount()
116
    {
117
        $discount = $this->getDiscount();
118
        $organizationUuid = Uuid::uuid1();
119
        $url = sprintf(ProductClient::POST_DISCOUNT, (string) $organizationUuid);
120
121
        $izettleClientMock = $this->getIzettlePostMock($url, $discount->getCreateData());
122
        list($categoryBuilderMock, $discountBuilderMock, $libraryBuilderMock, $productBuilderMock) = $this->getMocks();
123
124
        $productClient = new ProductClient(
125
            $izettleClientMock,
126
            $organizationUuid,
127
            $categoryBuilderMock,
128
            $discountBuilderMock,
129
            $libraryBuilderMock,
130
            $productBuilderMock
131
        );
132
133
        $productClient->createDiscount($discount);
134
    }
135
136
    /**
137
     * @test
138
     */
139 View Code Duplication
    public function deleteDiscount()
140
    {
141
        $discount = $this->getDiscount();
142
        $organizationUuid = Uuid::uuid1();
143
        $url = sprintf(ProductClient::DELETE_DISCOUNT, (string) $organizationUuid, (string) $discount->getUuid());
144
145
        $izettleClientMock = $this->getIzettleDeleteMock($url);
146
        list($categoryBuilderMock, $discountBuilderMock, $libraryBuilderMock, $productBuilderMock) = $this->getMocks();
147
148
        $productClient = new ProductClient(
149
            $izettleClientMock,
150
            $organizationUuid,
151
            $categoryBuilderMock,
152
            $discountBuilderMock,
153
            $libraryBuilderMock,
154
            $productBuilderMock
155
        );
156
157
        $productClient->deleteDiscount($discount);
158
    }
159
160
    /**
161
     * @test
162
     */
163
    public function getLibrary()
164
    {
165
        $organizationUuid = Uuid::uuid1();
166
        $data = ['getLibraryTest'];
167
        $url = sprintf(ProductClient::GET_LIBRARY, $organizationUuid);
168
169
        $izettleClientMock = $this->getIzettleGetMock($url, $data);
170
        list($categoryBuilderMock, $discountBuilderMock, $libraryBuilderMock, $productBuilderMock) = $this->getMocks();
171
        $libraryBuilderMock->shouldReceive('buildFromJson')->with(json_encode($data))->once()
172
            ->andReturn(new Library(
173
                Uuid::uuid1(),
174
                Uuid::uuid1(),
175
                new ProductCollection(),
176
                new DiscountCollection(),
177
                new ProductCollection(),
178
                new DiscountCollection()
179
            ));
180
181
        $productClient = new ProductClient(
182
            $izettleClientMock,
183
            $organizationUuid,
184
            $categoryBuilderMock,
185
            $discountBuilderMock,
186
            $libraryBuilderMock,
187
            $productBuilderMock
188
        );
189
        $library = $productClient->getLibrary();
0 ignored issues
show
Unused Code introduced by
$library is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
190
    }
191
192
    /**
193
     * @test
194
     */
195 View Code Duplication
    public function getProducts()
196
    {
197
        $organizationUuid = Uuid::uuid1();
198
        $data = ['getProductsTest'];
199
        $url = sprintf(ProductClient::GET_PRODUCTS, $organizationUuid);
200
201
        $izettleClientMock = $this->getIzettleGetMock($url, $data);
202
        list($categoryBuilderMock, $discountBuilderMock, $libraryBuilderMock, $productBuilderMock) = $this->getMocks();
203
        $productBuilderMock->shouldReceive('buildFromJson')->with(json_encode($data))->once();
204
205
        $productClient = new ProductClient(
206
            $izettleClientMock,
207
            $organizationUuid,
208
            $categoryBuilderMock,
209
            $discountBuilderMock,
210
            $libraryBuilderMock,
211
            $productBuilderMock
212
        );
213
214
        $productClient->getProducts();
215
    }
216
217
    /**
218
     * @test
219
     */
220 View Code Duplication
    public function createProduct()
221
    {
222
        $product = $this->getProduct();
223
        $organizationUuid = Uuid::uuid1();
224
        $url = sprintf(ProductClient::POST_PRODUCT, (string) $organizationUuid);
225
226
        $izettleClientMock = $this->getIzettlePostMock($url, $product->getCreateData());
227
        list($categoryBuilderMock, $discountBuilderMock, $libraryBuilderMock, $productBuilderMock) = $this->getMocks();
228
229
        $productClient = new ProductClient(
230
            $izettleClientMock,
231
            $organizationUuid,
232
            $categoryBuilderMock,
233
            $discountBuilderMock,
234
            $libraryBuilderMock,
235
            $productBuilderMock
236
        );
237
238
        $productClient->createProduct($product);
239
    }
240
241
    /**
242
     * @test
243
     */
244 View Code Duplication
    public function deleteProduct()
245
    {
246
        $product = $this->getProduct();
247
        $organizationUuid = Uuid::uuid1();
248
        $url = sprintf(ProductClient::DELETE_PRODUCT, (string) $organizationUuid, (string) $product->getUuid());
249
250
        $izettleClientMock = $this->getIzettleDeleteMock($url);
251
        list($categoryBuilderMock, $discountBuilderMock, $libraryBuilderMock, $productBuilderMock) = $this->getMocks();
252
253
        $productClient = new ProductClient(
254
            $izettleClientMock,
255
            $organizationUuid,
256
            $categoryBuilderMock,
257
            $discountBuilderMock,
258
            $libraryBuilderMock,
259
            $productBuilderMock
260
        );
261
262
        $productClient->deleteProduct($product);
263
    }
264
265
    private function getDiscount(): Discount
266
    {
267
        return Discount::new('name', 'description', new ImageCollection());
268
    }
269
270
    private function getProduct(): Product
271
    {
272
        return Product::new(
273
            'name',
274
            'description',
275
            new CategoryCollection(),
276
            new ImageCollection(),
277
            new VariantCollection([ Variant::new(
278
                null,
279
                null,
280
                null,
281
                null,
282
                1,
283
                null,
284
                Money::EUR(0),
285
                null,
286
                21
287
            )])
288
        );
289
    }
290
291 View Code Duplication
    private function getIzettleGetMock($url, $data): IzettleClientInterface
292
    {
293
        $responseMock = Mockery::mock(ResponseInterface::class);
294
295
        $izettleClientMock = Mockery::mock(IzettleClientInterface::class);
296
        $izettleClientMock
297
            ->shouldReceive('get')
298
            ->once()
299
            ->with($url)
300
            ->andReturn($responseMock);
301
        $izettleClientMock->shouldReceive('getJson')->once()->andReturn(json_encode($data));
302
303
        return $izettleClientMock;
304
    }
305
306 View Code Duplication
    private function getIzettlePostMock($url, $postData): IzettleClientInterface
307
    {
308
        $izettleClientMock = Mockery::mock(IzettleClientInterface::class);
309
        $izettleClientMock
310
            ->shouldReceive('post')
311
            ->once()
312
            ->with($url, $postData)
313
            ->andReturnNull();
314
315
        return $izettleClientMock;
316
    }
317
318 View Code Duplication
    private function getIzettleDeleteMock($url): IzettleClientInterface
319
    {
320
        $izettleClientMock = Mockery::mock(IzettleClientInterface::class);
321
        $izettleClientMock
322
            ->shouldReceive('delete')
323
            ->once()
324
            ->with($url)
325
            ->andReturnNull();
326
327
        return $izettleClientMock;
328
    }
329
330
    private function getMocks(): array
331
    {
332
        return [
333
            Mockery::mock(CategoryBuilderInterface::class),
334
            Mockery::mock(DiscountBuilderInterface::class),
335
            Mockery::mock(LibraryBuilderInterface::class),
336
            Mockery::mock(ProductBuilderInterface::class),
337
        ];
338
    }
339
}
340