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

ExportAbandonedCartConsumerTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 245
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 146
dl 0
loc 245
rs 10
c 1
b 0
f 1
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 26 1
A testConsumeApiHttpException() 0 43 1
A testConsume() 0 45 1
A testConsumeWithAbsentQuote() 0 22 1
A testConsumeApiUnprocessableEntityHttpExceptionException() 0 52 1
1
<?php
2
declare(strict_types=1);
3
/**
4
 */
5
6
namespace CommerceLeague\ActiveCampaign\Test\Unit\MessageQueue\Quote;
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\AbandonedCartBuilder as AbandonedCartRequestBuilder;
12
use CommerceLeague\ActiveCampaign\Logger\Logger;
13
use CommerceLeague\ActiveCampaign\MessageQueue\Quote\ExportAbandonedCartConsumer;
14
use CommerceLeague\ActiveCampaignApi\Api\OrderApiResourceInterface;
15
use CommerceLeague\ActiveCampaignApi\Exception\HttpException;
16
use CommerceLeague\ActiveCampaignApi\Exception\UnprocessableEntityHttpException;
17
use Magento\Quote\Model\Quote;
18
use PHPUnit\Framework\MockObject\MockObject;
19
use PHPUnit\Framework\TestCase;
20
use Magento\Quote\Model\QuoteFactory;
0 ignored issues
show
Bug introduced by
The type Magento\Quote\Model\QuoteFactory was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
21
22
class ExportAbandonedCartConsumerTest extends TestCase
23
{
24
    /**
25
     * @var MockObject|QuoteFactory
26
     */
27
    protected $quoteFactory;
28
29
    /**
30
     * @var MockObject|Quote
31
     */
32
    protected $quote;
33
34
    /**
35
     * @var MockObject|Logger
36
     */
37
    protected $logger;
38
39
    /**
40
     * @var MockObject|OrderRepositoryInterface
41
     */
42
    protected $orderRepository;
43
44
    /**
45
     * @var MockObject|AbandonedCartRequestBuilder
46
     */
47
    protected $abandonedCartRequestBuilder;
48
49
    /**
50
     * @var MockObject|Client
51
     */
52
    protected $client;
53
54
    /**
55
     * @var MockObject|OrderApiResourceInterface
56
     */
57
    protected $orderApi;
58
59
    /**
60
     * @var MockObject|OrderInterface
61
     */
62
    protected $order;
63
64
    /**
65
     * @var ExportAbandonedCartConsumer
66
     */
67
    protected $exportAbandonedCartConsumer;
68
69
    protected function setUp()
70
    {
71
        $this->quoteFactory = $this->getMockBuilder(QuoteFactory::class)
72
            ->disableOriginalConstructor()
73
            ->setMethods(['create'])
74
            ->getMock();
75
76
        $this->quote = $this->createMock(Quote::class);
77
78
        $this->quoteFactory->expects($this->any())
79
            ->method('create')
80
            ->willReturn($this->quote);
81
82
        $this->logger = $this->createMock(Logger::class);
83
        $this->orderRepository = $this->createMock(OrderRepositoryInterface::class);
84
        $this->abandonedCartRequestBuilder = $this->createMock(AbandonedCartRequestBuilder::class);
85
        $this->client = $this->createMock(Client::class);
86
        $this->orderApi = $this->createMock(OrderApiResourceInterface::class);
87
        $this->order = $this->createMock(OrderInterface::class);
88
89
        $this->exportAbandonedCartConsumer = new ExportAbandonedCartConsumer(
90
            $this->quoteFactory,
91
            $this->logger,
92
            $this->orderRepository,
93
            $this->abandonedCartRequestBuilder,
94
            $this->client
95
        );
96
    }
97
98
    public function testConsumeWithAbsentQuote()
99
    {
100
        $quoteId = 123;
101
102
        $this->quote->expects($this->once())
103
            ->method('loadByIdWithoutStore')
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

103
            ->/** @scrutinizer ignore-call */ method('loadByIdWithoutStore')

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
            ->with(123)
0 ignored issues
show
Bug introduced by
The method with() 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

104
            ->/** @scrutinizer ignore-call */ with(123)

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...
105
            ->willReturn($this->quote);
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

105
            ->/** @scrutinizer ignore-call */ willReturn($this->quote);

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...
106
107
        $this->quote->expects($this->once())
108
            ->method('getId')
109
            ->willReturn(null);
110
111
        $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

111
        $this->logger->/** @scrutinizer ignore-call */ 
112
                       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...
112
            ->method('error')
113
            ->with(__('The Quote with the "%1" ID doesn\'t exist', $quoteId));
114
115
        $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

115
        $this->orderRepository->/** @scrutinizer ignore-call */ 
116
                                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...
116
            ->method('getOrCreateByMagentoQuoteId');
117
118
        $this->exportAbandonedCartConsumer->consume(
119
            json_encode(['quote_id' => $quoteId])
120
        );
121
    }
122
123
    public function testConsumeApiHttpException()
124
    {
125
        $quoteId = 123;
126
        $request = ['request'];
127
128
        $this->quote->expects($this->once())
129
            ->method('loadByIdWithoutStore')
130
            ->with(123)
131
            ->willReturn($this->quote);
132
133
        $this->quote->expects($this->any())
134
            ->method('getId')
135
            ->willReturn($quoteId);
136
137
        $this->orderRepository->expects($this->once())
138
            ->method('getOrCreateByMagentoQuoteId')
139
            ->with($quoteId)
140
            ->willReturn($this->order);
141
142
        $this->abandonedCartRequestBuilder->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not exist on CommerceLeague\ActiveCam...st\AbandonedCartBuilder. ( 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->abandonedCartRequestBuilder->/** @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('build')
144
            ->with($this->quote)
145
            ->willReturn($request);
146
147
        $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

147
        $this->client->/** @scrutinizer ignore-call */ 
148
                       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...
148
            ->method('getOrderApi')
149
            ->willReturn($this->orderApi);
150
151
        /** @var MockObject|HttpException $httpException */
152
        $httpException = $this->createMock(HttpException::class);
153
154
        $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

154
        $this->orderApi->/** @scrutinizer ignore-call */ 
155
                         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...
155
            ->method('create')
156
            ->with(['ecomOrder' => $request])
157
            ->willThrowException($httpException);
158
159
        $this->logger->expects($this->once())
160
            ->method('error');
161
162
        $this->order->expects($this->never())
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

162
        $this->order->/** @scrutinizer ignore-call */ 
163
                      expects($this->never())
Loading history...
163
            ->method('setActiveCampaignId');
164
165
        $this->exportAbandonedCartConsumer->consume(json_encode(['quote_id' => $quoteId]));
166
    }
167
168
    public function testConsumeApiUnprocessableEntityHttpExceptionException()
169
    {
170
        $quoteId = 123;
171
        $request = ['request'];
172
        $responseErrors = ['first error', 'second error'];
173
174
        $this->quote->expects($this->once())
175
            ->method('loadByIdWithoutStore')
176
            ->with(123)
177
            ->willReturn($this->quote);
178
179
        $this->quote->expects($this->any())
180
            ->method('getId')
181
            ->willReturn($quoteId);
182
183
        $this->orderRepository->expects($this->once())
184
            ->method('getOrCreateByMagentoQuoteId')
185
            ->with($quoteId)
186
            ->willReturn($this->order);
187
188
        $this->abandonedCartRequestBuilder->expects($this->once())
189
            ->method('build')
190
            ->with($this->quote)
191
            ->willReturn($request);
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->exportAbandonedCartConsumer->consume(json_encode(['quote_id' => $quoteId]));
220
    }
221
222
    public function testConsume()
223
    {
224
        $quoteId = 123;
225
        $request = ['request'];
226
        $activeCampaignId = 789;
227
        $response = ['ecomOrder' => ['id' => $activeCampaignId]];
228
229
        $this->quote->expects($this->once())
230
            ->method('loadByIdWithoutStore')
231
            ->with(123)
232
            ->willReturn($this->quote);
233
234
        $this->quote->expects($this->any())
235
            ->method('getId')
236
            ->willReturn($quoteId);
237
238
        $this->orderRepository->expects($this->once())
239
            ->method('getOrCreateByMagentoQuoteId')
240
            ->with($quoteId)
241
            ->willReturn($this->order);
242
243
        $this->abandonedCartRequestBuilder->expects($this->once())
244
            ->method('build')
245
            ->with($this->quote)
246
            ->willReturn($request);
247
248
        $this->client->expects($this->once())
249
            ->method('getOrderApi')
250
            ->willReturn($this->orderApi);
251
252
        $this->orderApi->expects($this->once())
253
            ->method('create')
254
            ->with(['ecomOrder' => $request])
255
            ->willReturn($response);
256
257
        $this->order->expects($this->once())
258
            ->method('setActiveCampaignId')
259
            ->with($activeCampaignId)
260
            ->willReturnSelf();
261
262
        $this->orderRepository->expects($this->once())
263
            ->method('save')
264
            ->with($this->order);
265
266
        $this->exportAbandonedCartConsumer->consume(json_encode(['quote_id' => $quoteId]));
267
    }
268
}
269