Passed
Push — master ( fa975d...67ba18 )
by Radosław
02:17
created

HelperTest::testFinishAuctions()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 35
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 35
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 21
nc 1
nop 2
1
<?php
2
3
namespace Radowoj\Yaah;
4
5
use PHPUnit\Framework\TestCase;
6
use Radowoj\Yaah\Field;
7
use SoapClient;
8
9
class HelperTest extends TestCase
10
{
11
    protected $config = null;
12
13
    protected $soapClient = null;
14
15
    public function setUp()
16
    {
17
        $this->config = $this->getMockBuilder(Config::class)
18
            ->setMethods(['getApiKey'])
19
            ->disableOriginalConstructor()
20
            ->getMock();
21
22
        $this->config->expects($this->any())
23
            ->method('getApiKey')
24
            ->willReturn('someApiKey');
25
26
        $this->soapClient = $this->getMockBuilder(SoapClient::class)
27
            ->disableOriginalConstructor()
28
            ->setMethods(['doNewAuctionExt', 'doVerifyItem', 'doQuerySysStatus', 'doLogin', 'doSomethingNotImplementedInHelper'])
29
            ->getMock();
30
    }
31
32
33
    public function testNewAuction()
34
    {
35
        $apiClient = $this->getMockBuilder(Client::class)
36
            ->setConstructorArgs([$this->config, $this->soapClient])
37
            ->setMethods(['doNewAuctionExt', 'doVerifyItem'])
38
            ->getMock();
39
40
        $apiClient->expects($this->once())
41
            ->method('doNewAuctionExt')
42
            ->with($this->equalTo([
43
                'fields' => [
44
                    [
45
                        'fvalueString' => 'test title',
46
                        'fid' => 1,
47
                        'fvalueInt' => 0,
48
                        'fvalueFloat' => 0,
49
                        'fvalueImage' => '',
50
                        'fvalueDate' => '',
51
                        'fvalueDatetime' => 0,
52
                        'fvalueRangeInt' => [
53
                            'fvalueRangeIntMin' => 0,
54
                            'fvalueRangeIntMax' => 0,
55
                        ],
56
                        'fvalueRangeFloat' => [
57
                            'fvalueRangeFloatMin' => 0,
58
                            'fvalueRangeFloatMax' => 0,
59
                        ],
60
                        'fvalueRangeDate' => [
61
                            'fvalueRangeDateMin' => '',
62
                            'fvalueRangeDateMax' => '',
63
                        ]
64
                    ]
65
                ],
66
                'localId' => 1
67
            ]));
68
69
        $apiClient->expects($this->once())
70
            ->method('doVerifyItem')
71
            ->willReturn((object)['itemId' => 1234]);
72
73
        $helper = new Helper($apiClient);
74
75
        $result = $helper->newAuction(new Auction([1 => 'test title']), 1);
76
77
        $this->assertSame(1234, $result);
78
    }
79
80
    /**
81
     * @expectedException Radowoj\Yaah\Exception
82
     * @expectedExceptionMessage Auction has not been created
83
     */
84
    public function testExceptionOnInvalidNewAuctionResponse()
85
    {
86
        $apiClient = $this->getMockBuilder(Client::class)
87
            ->setConstructorArgs([$this->config, $this->soapClient])
88
            ->setMethods(['doNewAuctionExt', 'doVerifyItem'])
89
            ->getMock();
90
91
        $apiClient->expects($this->once())
92
            ->method('doNewAuctionExt')
93
            ->willReturn((object)['whatever' => 1]);
94
95
        $apiClient->expects($this->once())
96
            ->method('doVerifyItem')
97
            ->willReturn((object)['definitelyNotItemId' => 1234]);
98
99
        $helper = new Helper($apiClient);
100
        $helper->newAuction(new Auction([1 => 'test title']), 1);
101
    }
102
103
104
    public function testDirectWebapiCall()
105
    {
106
        $apiClient = $this->getMockBuilder(Client::class)
107
            ->setConstructorArgs([$this->config, $this->soapClient])
108
            ->setMethods(['doSomethingNotImplementedInHelper'])
109
            ->getMock();
110
111
        $apiClient->expects($this->once())
112
            ->method('doSomethingNotImplementedInHelper')
113
            ->with($this->equalTo([
114
                'param1' => 1,
115
                'param2' => 2,
116
            ]))
117
            ->willReturn(42);
118
119
        $helper = new Helper($apiClient);
120
        $result = $helper->doSomethingNotImplementedInHelper([
121
            'param1' => 1,
122
            'param2' => 2,
123
        ]);
124
125
        $this->assertSame(42, $result);
126
    }
127
128
129
    /**
130
     * This test checks if call of a method not implemented in helper is correctly passed via WebAPI client to soapClient
131
     * (extended by WebAPI request param - just one is tested here, as all required params are tested in ClientTest)
132
     */
133
    public function testDirectWebapiCallFromSoapClient()
134
    {
135
        $this->soapClient->expects($this->once())
136
            ->method('doQuerySysStatus')
137
            ->willReturn((object)['verKey' => 'someVersionKey']);
138
139
        $this->soapClient->expects($this->once())
140
            ->method('doLogin')
141
            ->willReturn((object)['sessionHandlePart' => 'foo', 'userId' => 'bar']);
142
143
        $this->soapClient->expects($this->once())
144
            ->method('doSomethingNotImplementedInHelper')
145
            ->with(
146
                $this->callback(function($params){
147
                    return array_key_exists('param1', $params)
148
                        && array_key_exists('webapiKey', $params)
149
                        && $params['param1'] == 1337
150
                        && $params['webapiKey'] == 'someApiKey';
151
                })
152
            )
153
            ->willReturn(42);
154
155
        $apiClient = new Client($this->config, $this->soapClient);
156
157
        $helper = new Helper($apiClient);
158
        $result = $helper->doSomethingNotImplementedInHelper([
159
            'param1' => 1337
160
        ]);
161
162
        $this->assertSame(42, $result);
163
    }
164
165
166
167
    public function providerFinishAuctions()
168
    {
169
        return [
170
            'do not cancel all bids, no reason' => [
171
                0,
172
                ''
173
            ],
174
            'cancel all bids, no reason' => [
175
                1,
176
                ''
177
            ],
178
            'do not cancel all bids, some reason' => [
179
                0,
180
                'some reason'
181
            ],
182
            'cancel all bids, some reason' => [
183
                1,
184
                'some reason'
185
            ],
186
187
        ];
188
    }
189
190
    /**
191
     * @dataProvider providerFinishAuctions
192
     */
193
    public function testFinishAuctions($finishCancelAllBids, $finishCancelReason)
194
    {
195
        $auctionIds = [31337, 1337, 42];
196
197
        $apiClient = $this->getMockBuilder(Client::class)
198
            ->setConstructorArgs([$this->config, $this->soapClient])
199
            ->setMethods(['getLocalVersionKey', 'login', 'doFinishItems'])
200
            ->getMock();
201
202
        $apiClient->expects($this->once())
203
            ->method('doFinishItems')
204
            ->with($this->equalTo([
205
                'finishItemsList' => [
206
                    [
207
                        'finishItemId' => 31337,
208
                        'finishCancelAllBids' => $finishCancelAllBids,
209
                        'finishCancelReason' => $finishCancelReason
210
                    ],
211
                    [
212
                        'finishItemId' => 1337,
213
                        'finishCancelAllBids' => $finishCancelAllBids,
214
                        'finishCancelReason' => $finishCancelReason
215
                    ],
216
                    [
217
                        'finishItemId' => 42,
218
                        'finishCancelAllBids' => $finishCancelAllBids,
219
                        'finishCancelReason' => $finishCancelReason
220
                    ],
221
                ]
222
            ]));
223
224
        $helper = new Helper($apiClient);
225
226
        $helper->finishAuctions($auctionIds, $finishCancelAllBids, $finishCancelReason);
227
    }
228
229
    /**
230
     * @dataProvider providerFinishAuctions
231
     */
232
    public function testFinishAuction($finishCancelAllBids, $finishCancelReason)
233
    {
234
        $apiClient = $this->getMockBuilder(Client::class)
235
            ->setConstructorArgs([$this->config, $this->soapClient])
236
            ->setMethods(['getLocalVersionKey', 'login', 'doFinishItems'])
237
            ->getMock();
238
239
        $apiClient->expects($this->once())
240
            ->method('doFinishItems')
241
            ->with($this->equalTo([
242
                'finishItemsList' => [
243
                    [
244
                        'finishItemId' => 31337,
245
                        'finishCancelAllBids' => $finishCancelAllBids,
246
                        'finishCancelReason' => $finishCancelReason
247
                    ],
248
                ]
249
            ]));
250
251
        $helper = new Helper($apiClient);
252
253
        $helper->finishAuction(31337, $finishCancelAllBids, $finishCancelReason);
254
    }
255
256
257
    public function testChangeQuantity()
258
    {
259
        $apiClient = $this->getMockBuilder(Client::class)
260
            ->setConstructorArgs([$this->config, $this->soapClient])
261
            ->setMethods(['getLocalVersionKey', 'login', 'doChangeQuantityItem'])
262
            ->getMock();
263
264
        $apiClient->expects($this->once())
265
            ->method('doChangeQuantityItem')
266
            ->with($this->equalTo([
267
                'itemId' => 1337,
268
                'newItemQuantity' => 42
269
            ]));
270
271
        $helper = new Helper($apiClient);
272
        $helper->changeQuantity(1337, 42);
273
    }
274
275
276
    /**
277
     * @expectedException Radowoj\Yaah\Exception
278
     * @expectedExceptionMessage Method nonexistentMethodWithoutWebApiDoPrefix is not implemented
279
     */
280
    public function testExceptionOnNonexistentMethodWithoutDoPrefix()
281
    {
282
        $apiClient = $this->getMockBuilder(Client::class)
283
            ->setConstructorArgs([$this->config, $this->soapClient])
284
            ->setMethods(['getLocalVersionKey', 'login', 'doChangeQuantityItem'])
285
            ->getMock();
286
287
        $helper = new Helper($apiClient);
288
        $helper->nonexistentMethodWithoutWebApiDoPrefix();
289
    }
290
291
292
    /**
293
     * @expectedException Radowoj\Yaah\Exception
294
     * @expectedExceptionMessage Invalid WebAPI response
295
     */
296
    public function testExceptionOnInvalidApiResponseFromGetFieldsByCategory()
297
    {
298
        $apiClient = $this->getMockBuilder(Client::class)
299
            ->setConstructorArgs([$this->config, $this->soapClient])
300
            ->setMethods(['getLocalVersionKey', 'login', 'doGetSellFormFieldsForCategory'])
301
            ->getMock();
302
303
        $helper = new Helper($apiClient);
304
        $helper->getFieldsByCategory(42);
305
    }
306
307
308
    public function testGetFieldsByCategory()
309
    {
310
        $apiClient = $this->getMockBuilder(Client::class)
311
            ->setConstructorArgs([$this->config, $this->soapClient])
312
            ->setMethods(['getLocalVersionKey', 'login', 'doGetSellFormFieldsForCategory'])
313
            ->getMock();
314
315
        $apiClient->expects($this->once())
316
            ->method('doGetSellFormFieldsForCategory')
317
            ->willReturn((object)[
318
                'sellFormFieldsForCategory' => (object)[
319
                    'sellFormFieldsList' => (object)[
320
                        'item' => [
321
                            (object)[
322
                                'sellFormId' => 1,
323
                                'sellFormTitle' => 'Some field title',
324
                                'sellFormOpt' => 1,
325
                                'sellFormOptsValues' => '0|1',
326
                                'sellFormDesc' => 'Some sell form description',
327
                            ],
328
                            (object)[
329
                                'sellFormId' => 2,
330
                                'sellFormTitle' => 'Some other field title',
331
                                'sellFormOpt' => 8,
332
                                'sellFormOptsValues' => '0|1|2',
333
                                'sellFormDesc' => 'Some other sell form description',
334
                            ]
335
                        ]
336
                    ]
337
                ]
338
            ]);
339
340
        $helper = new Helper($apiClient);
341
        $result = $helper->getFieldsByCategory(42);
342
343
        $this->assertSame([
344
            [
345
                'fid' => 1,
346
                'title' => 'Some field title',
347
                'required' => true,
348
                'options' => '0|1',
349
                'optionsDesc' => 'Some sell form description',
350
            ],
351
            [
352
                'fid' => 2,
353
                'title' => 'Some other field title',
354
                'required' => false,
355
                'options' => '0|1|2',
356
                'optionsDesc' => 'Some other sell form description',
357
            ],
358
359
        ], $result);
360
    }
361
362
    /**
363
     * @expectedException Radowoj\Yaah\Exception
364
     * @expectedExceptionMessage Unable to get site journal deals
365
     */
366
    public function testGetSiteJournalDealsExceptionOnFailure()
367
    {
368
        $apiClient = $this->getMockBuilder(Client::class)
369
            ->disableOriginalConstructor()
370
            ->getMock();
371
372
        $helper = new Helper($apiClient);
373
374
        $helper->getSiteJournalDeals();
375
    }
376
377
378
    public function providerGetSiteJournalDealsStartParam()
379
    {
380
        return [
381
            [0],
382
            [71830]
383
        ];
384
    }
385
386
387
    /**
388
     * @dataProvider providerGetSiteJournalDealsStartParam
389
     */
390
    public function testGetSiteJournalDealsWithDefaultParams($journalStart)
391
    {
392
        $apiClient = $this->getMockBuilder(Client::class)
393
            ->disableOriginalConstructor()
394
            ->setMethods(['doGetSiteJournalDeals'])
395
            ->getMock();
396
397
        $apiClient->expects($this->once())
398
            ->method('doGetSiteJournalDeals')
399
            ->with($this->equalTo([
400
                'journalStart' => $journalStart,
401
            ]))
402
            ->willReturn((object)[
403
                'siteJournalDeals' => (object)[
404
                    'item' => [
405
                        (object)[
406
                            'dealEventId' => 42
407
                        ]
408
                    ]
409
                ]
410
            ]);
411
412
        $helper = new Helper($apiClient);
413
414
        if ($journalStart !== 0) {
415
            $deals = $helper->getSiteJournalDeals($journalStart);
416
        } else {
417
            //test default param, which should be 0
418
            $deals = $helper->getSiteJournalDeals();
419
        }
420
421
        $this->assertContainsOnly('Radowoj\Yaah\Journal\Deal', $deals);
422
    }
423
424
425
    /**
426
     * @expectedException Radowoj\Yaah\Exception
427
     * @expectedExceptionMessage Invalid WebAPI response: stdClass Object
428
     */
429
    public function testGetAuctionByItemIdExceptionOnInvalidResponse()
430
    {
431
        $apiClient = $this->getMockBuilder(Client::class)
432
            ->disableOriginalConstructor()
433
            ->setMethods(['doGetItemFields'])
434
            ->getMock();
435
436
        $apiClient->expects($this->once())
437
            ->method('doGetItemFields')
438
            ->with($this->equalTo([
439
                'itemId' => 4321,
440
            ]))
441
            ->willReturn((object)[]);
442
443
        $helper = new Helper($apiClient);
444
        $helper->getAuctionByItemId(4321);
445
    }
446
447
448
    public function testGetAuctionByItem()
449
    {
450
        $apiClient = $this->getMockBuilder(Client::class)
451
            ->disableOriginalConstructor()
452
            ->setMethods(['doGetItemFields'])
453
            ->getMock();
454
455
        $apiClient->expects($this->once())
456
            ->method('doGetItemFields')
457
            ->with($this->equalTo([
458
                'itemId' => 4321,
459
            ]))
460
            ->willReturn((object)[
461
                'itemFields' => (object)[
462
                    'item' => [
463
                        //no fields returned - mapping from fields to auction tested separately
464
                    ]
465
                ]
466
            ]);
467
468
        $helper = new Helper($apiClient);
469
        $auction = $helper->getAuctionByItemId(4321);
470
471
        $this->assertInstanceOf('Radowoj\Yaah\Auction', $auction);
472
    }
473
474
}
475