GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 0f220b...47b153 )
by Andreas
07:24
created

testConsumeApiHttpException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 48
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 34
nc 1
nop 0
dl 0
loc 48
rs 9.376
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
/**
4
 */
5
6
namespace CommerceLeague\ActiveCampaign\Test\Unit\MessageQueue\Sales;
7
8
use CommerceLeague\ActiveCampaign\Api\Data\OrderInterface;
9
use CommerceLeague\ActiveCampaign\Api\OrderRepositoryInterface;
10
use CommerceLeague\ActiveCampaign\Gateway\Client;
11
use CommerceLeague\ActiveCampaign\Gateway\Request\OrderBuilder as OrderRequestBuilder;
12
use CommerceLeague\ActiveCampaign\Logger\Logger;
13
use CommerceLeague\ActiveCampaign\MessageQueue\Sales\ExportOrderConsumer;
14
use CommerceLeague\ActiveCampaignApi\Api\OrderApiResourceInterface;
15
use CommerceLeague\ActiveCampaignApi\Exception\HttpException;
16
use CommerceLeague\ActiveCampaignApi\Exception\UnprocessableEntityHttpException;
17
use Magento\Framework\Exception\NoSuchEntityException;
18
use Magento\Framework\Phrase;
19
use Magento\Sales\Api\OrderRepositoryInterface as MagentoOrderRepositoryInterface;
20
use Magento\Sales\Model\Order as MagentoOrder;
21
use PHPUnit\Framework\MockObject\MockObject;
22
use PHPUnit\Framework\TestCase;
23
24
class ExportOrderConsumerTest extends TestCase
25
{
26
    /**
27
     * @var MockObject|MagentoOrderRepositoryInterface
28
     */
29
    protected $magentoOrderRepository;
30
31
    /**
32
     * @var MockObject|Logger
33
     */
34
    protected $logger;
35
36
    /**
37
     * @var MockObject|OrderRepositoryInterface
38
     */
39
    protected $orderRepository;
40
41
    /**
42
     * @var MockObject|OrderRequestBuilder
43
     */
44
    protected $orderRequestBuilder;
45
46
    /**
47
     * @var MockObject|Client
48
     */
49
    protected $client;
50
51
    /**
52
     * @var MockObject|OrderApiResourceInterface
53
     */
54
    protected $orderApi;
55
56
    /**
57
     * @var MockObject|MagentoOrder
58
     */
59
    protected $magentoOrder;
60
61
    /**
62
     * @var MockObject|OrderInterface
63
     */
64
    protected $order;
65
66
    /**
67
     * @var ExportOrderConsumer
68
     */
69
    protected $exportOrderConsumer;
70
71
    protected function setUp()
72
    {
73
        $this->magentoOrderRepository = $this->createMock(MagentoOrderRepositoryInterface::class);
74
        $this->logger = $this->createMock(Logger::class);
75
        $this->orderRepository = $this->createMock(OrderRepositoryInterface::class);
76
        $this->orderRequestBuilder = $this->createMock(OrderRequestBuilder::class);
77
        $this->client = $this->createMock(Client::class);
78
        $this->orderApi = $this->createMock(OrderApiResourceInterface::class);
79
        $this->magentoOrder = $this->createMock(MagentoOrder::class);
80
        $this->order = $this->createMock(OrderInterface::class);
81
82
        $this->exportOrderConsumer = new ExportOrderConsumer(
83
            $this->magentoOrderRepository,
84
            $this->logger,
85
            $this->orderRepository,
86
            $this->orderRequestBuilder,
87
            $this->client
88
        );
89
    }
90
91
    public function testConsumeWithAbsentMagentoOrder()
92
    {
93
        $magentoOrderId = 123;
94
95
        $exceptionMessage = 'an exception message';
96
        $exception = new NoSuchEntityException(new Phrase($exceptionMessage));
97
98
        $this->magentoOrderRepository->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not exist on Magento\Sales\Api\OrderRepositoryInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

98
        $this->magentoOrderRepository->/** @scrutinizer ignore-call */ 
99
                                       expects($this->once())

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
99
            ->method('get')
100
            ->with($magentoOrderId)
101
            ->willThrowException($exception);
102
103
        $this->logger->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not exist on CommerceLeague\ActiveCampaign\Logger\Logger. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

103
        $this->logger->/** @scrutinizer ignore-call */ 
104
                       expects($this->once())

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
104
            ->method('error')
105
            ->with($exceptionMessage);
106
107
        $this->orderRepository->expects($this->never())
0 ignored issues
show
Bug introduced by
The method expects() does not exist on CommerceLeague\ActiveCam...rderRepositoryInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

107
        $this->orderRepository->/** @scrutinizer ignore-call */ 
108
                                expects($this->never())

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
108
            ->method('getOrCreateByMagentoQuoteId');
109
110
       $this->exportOrderConsumer->consume(json_encode(['magento_order_id' => $magentoOrderId]));
111
    }
112
113
    public function testConsumeApiHttpException()
114
    {
115
        $magentoOrderId = 123;
116
        $magentoQuoteId = 456;
117
        $request = ['request'];
118
119
        $this->magentoOrderRepository->expects($this->once())
120
            ->method('get')
121
            ->with($magentoOrderId)
122
            ->willReturn($this->magentoOrder);
123
124
        $this->magentoOrder->expects($this->once())
125
            ->method('getQuoteId')
0 ignored issues
show
Bug introduced by
The method method() does not exist on PHPUnit\Framework\MockObject\MockObject. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

125
            ->/** @scrutinizer ignore-call */ method('getQuoteId')

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
126
            ->willReturn($magentoQuoteId);
0 ignored issues
show
Bug introduced by
The method willReturn() does not exist on PHPUnit\Framework\MockObject\MockObject. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

126
            ->/** @scrutinizer ignore-call */ willReturn($magentoQuoteId);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
127
128
        $this->orderRepository->expects($this->once())
129
            ->method('getOrCreateByMagentoQuoteId')
130
            ->with($magentoQuoteId)
131
            ->willReturn($this->order);
132
133
        $this->orderRequestBuilder->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not exist on CommerceLeague\ActiveCam...ay\Request\OrderBuilder. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

133
        $this->orderRequestBuilder->/** @scrutinizer ignore-call */ 
134
                                    expects($this->once())

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
134
            ->method('build')
135
            ->with($this->magentoOrder)
136
            ->willReturn($request);
137
138
        $this->order->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not exist on CommerceLeague\ActiveCam...Api\Data\OrderInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to CommerceLeague\ActiveCam...Api\Data\OrderInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

138
        $this->order->/** @scrutinizer ignore-call */ 
139
                      expects($this->once())
Loading history...
139
            ->method('getActiveCampaignId')
140
            ->willReturn(null);
141
142
        $this->client->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not exist on CommerceLeague\ActiveCampaign\Gateway\Client. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

142
        $this->client->/** @scrutinizer ignore-call */ 
143
                       expects($this->once())

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
143
            ->method('getOrderApi')
144
            ->willReturn($this->orderApi);
145
146
        /** @var MockObject|HttpException $httpException */
147
        $httpException = $this->createMock(HttpException::class);
148
149
        $this->orderApi->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not exist on CommerceLeague\ActiveCam...derApiResourceInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

149
        $this->orderApi->/** @scrutinizer ignore-call */ 
150
                         expects($this->once())

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
150
            ->method('create')
151
            ->with(['ecomOrder' => $request])
152
            ->willThrowException($httpException);
153
154
        $this->logger->expects($this->once())
155
            ->method('error');
156
157
        $this->order->expects($this->never())
158
            ->method('setActiveCampaignId');
159
160
        $this->exportOrderConsumer->consume(json_encode(['magento_order_id' => $magentoOrderId]));
161
    }
162
163
    public function testConsumeApiUnprocessableEntityHttpExceptionException()
164
    {
165
        $magentoOrderId = 123;
166
        $magentoQuoteId = 456;
167
        $request = ['request'];
168
        $responseErrors = ['first error', 'second error'];
169
170
        $this->magentoOrderRepository->expects($this->once())
171
            ->method('get')
172
            ->with($magentoOrderId)
173
            ->willReturn($this->magentoOrder);
174
175
        $this->magentoOrder->expects($this->once())
176
            ->method('getQuoteId')
177
            ->willReturn($magentoQuoteId);
178
179
        $this->orderRepository->expects($this->once())
180
            ->method('getOrCreateByMagentoQuoteId')
181
            ->with($magentoQuoteId)
182
            ->willReturn($this->order);
183
184
        $this->orderRequestBuilder->expects($this->once())
185
            ->method('build')
186
            ->with($this->magentoOrder)
187
            ->willReturn($request);
188
189
        $this->order->expects($this->once())
190
            ->method('getActiveCampaignId')
191
            ->willReturn(null);
192
193
        $this->client->expects($this->once())
194
            ->method('getOrderApi')
195
            ->willReturn($this->orderApi);
196
197
        /** @var MockObject|UnprocessableEntityHttpException $unprocessableEntityHttpException */
198
        $unprocessableEntityHttpException = $this->createMock(UnprocessableEntityHttpException::class);
199
200
        $this->orderApi->expects($this->once())
201
            ->method('create')
202
            ->with(['ecomOrder' => $request])
203
            ->willThrowException($unprocessableEntityHttpException);
204
205
        $this->logger->expects($this->exactly(2))
206
            ->method('error');
207
208
        $unprocessableEntityHttpException->expects($this->once())
209
            ->method('getResponseErrors')
210
            ->willReturn($responseErrors);
211
212
        $this->logger->expects($this->at(1))
213
            ->method('error')
214
            ->with(print_r($responseErrors, true));
215
216
        $this->order->expects($this->never())
217
            ->method('setActiveCampaignId');
218
219
        $this->exportOrderConsumer->consume(json_encode(['magento_order_id' => $magentoOrderId]));
220
    }
221
222
    public function testConsumeUpdate()
223
    {
224
        $magentoOrderId = 123;
225
        $magentoQuoteId = 456;
226
        $request = ['request'];
227
        $activeCampaignId = 789;
228
        $response = ['ecomOrder' => ['id' => $activeCampaignId]];
229
230
        $this->magentoOrderRepository->expects($this->once())
231
            ->method('get')
232
            ->with($magentoOrderId)
233
            ->willReturn($this->magentoOrder);
234
235
        $this->magentoOrder->expects($this->once())
236
            ->method('getQuoteId')
237
            ->willReturn($magentoQuoteId);
238
239
        $this->orderRepository->expects($this->once())
240
            ->method('getOrCreateByMagentoQuoteId')
241
            ->with($magentoQuoteId)
242
            ->willReturn($this->order);
243
244
        $this->orderRequestBuilder->expects($this->once())
245
            ->method('build')
246
            ->with($this->magentoOrder)
247
            ->willReturn($request);
248
249
        $this->order->expects($this->once())
250
            ->method('getActiveCampaignId')
251
            ->willReturn($activeCampaignId);
252
253
        $this->client->expects($this->once())
254
            ->method('getOrderApi')
255
            ->willReturn($this->orderApi);
256
257
        $this->orderApi->expects($this->once())
258
            ->method('update')
259
            ->with($activeCampaignId, ['ecomOrder' => $request])
260
            ->willReturn($response);
261
262
        $this->order->expects($this->once())
263
            ->method('setActiveCampaignId')
264
            ->with($activeCampaignId)
265
            ->willReturnSelf();
266
267
        $this->orderRepository->expects($this->once())
268
            ->method('save')
269
            ->with($this->order);
270
271
        $this->exportOrderConsumer->consume(json_encode(['magento_order_id' => $magentoOrderId]));
272
    }
273
274
    public function testConsumeCreate()
275
    {
276
        $magentoOrderId = 123;
277
        $magentoQuoteId = 456;
278
        $request = ['request'];
279
        $activeCampaignId = 789;
280
        $response = ['ecomOrder' => ['id' => $activeCampaignId]];
281
282
        $this->magentoOrderRepository->expects($this->once())
283
            ->method('get')
284
            ->with($magentoOrderId)
285
            ->willReturn($this->magentoOrder);
286
287
        $this->magentoOrder->expects($this->once())
288
            ->method('getQuoteId')
289
            ->willReturn($magentoQuoteId);
290
291
        $this->orderRepository->expects($this->once())
292
            ->method('getOrCreateByMagentoQuoteId')
293
            ->with($magentoQuoteId)
294
            ->willReturn($this->order);
295
296
        $this->orderRequestBuilder->expects($this->once())
297
            ->method('build')
298
            ->with($this->magentoOrder)
299
            ->willReturn($request);
300
301
        $this->order->expects($this->once())
302
            ->method('getActiveCampaignId')
303
            ->willReturn(null);
304
305
        $this->client->expects($this->once())
306
            ->method('getOrderApi')
307
            ->willReturn($this->orderApi);
308
309
        $this->orderApi->expects($this->once())
310
            ->method('create')
311
            ->with(['ecomOrder' => $request])
312
            ->willReturn($response);
313
314
        $this->order->expects($this->once())
315
            ->method('setActiveCampaignId')
316
            ->with($activeCampaignId)
317
            ->willReturnSelf();
318
319
        $this->orderRepository->expects($this->once())
320
            ->method('save')
321
            ->with($this->order);
322
323
        $this->exportOrderConsumer->consume(json_encode(['magento_order_id' => $magentoOrderId]));
324
    }
325
326
}
327