Completed
Pull Request — master (#11)
by Laurens
02:02
created

ProductClientTest   B

Complexity

Total Complexity 15

Size/Duplication

Total Lines 308
Duplicated Lines 64.94 %

Coupling/Cohesion

Components 1
Dependencies 16

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 16
dl 200
loc 308
rs 8.4614
c 0
b 0
f 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
A getCategories() 22 22 1
A createCategory() 20 20 1
A getDiscounts() 21 21 1
A createDiscount() 20 20 1
A deleteDiscount() 20 20 1
B getLibrary() 0 29 1
A getProducts() 21 21 1
A createProduct() 20 20 1
A deleteProduct() 20 20 1
A getDiscount() 0 4 1
A getProduct() 0 20 1
A getIzettleGetMock() 14 14 1
A getIzettlePostMock() 11 11 1
A getIzettleDeleteMock() 11 11 1
A getMocks() 0 9 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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