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

testConsumeApiUnprocessableEntityHttpExceptionException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 47
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 33
nc 1
nop 0
dl 0
loc 47
rs 9.392
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
/**
4
 */
5
6
namespace CommerceLeague\ActiveCampaign\Test\Unit\MessageQueue\Customer;
7
8
use CommerceLeague\ActiveCampaign\Api\CustomerRepositoryInterface;
9
use CommerceLeague\ActiveCampaign\Api\Data\CustomerInterface;
10
use CommerceLeague\ActiveCampaign\Gateway\Client;
11
use CommerceLeague\ActiveCampaign\Gateway\Request\CustomerBuilder as CustomerRequestBuilder;
12
use CommerceLeague\ActiveCampaign\Logger\Logger;
13
use CommerceLeague\ActiveCampaign\MessageQueue\Customer\ExportCustomerConsumer;
14
use CommerceLeague\ActiveCampaignApi\Api\CustomerApiResourceInterface;
15
use CommerceLeague\ActiveCampaignApi\Exception\HttpException;
16
use CommerceLeague\ActiveCampaignApi\Exception\UnprocessableEntityHttpException;
17
use Magento\Customer\Api\CustomerRepositoryInterface as MagentoCustomerRepositoryInterface;
18
use Magento\Customer\Api\Data\CustomerInterface as MagentoCustomerInterface;
19
use Magento\Framework\Exception\NoSuchEntityException;
20
use Magento\Framework\Phrase;
21
use PHPUnit\Framework\MockObject\MockObject;
22
use PHPUnit\Framework\TestCase;
23
24
class ExportCustomerConsumerTest extends TestCase
25
{
26
    /**
27
     * @var MockObject|MagentoCustomerRepositoryInterface
28
     */
29
    protected $magentoCustomerRepository;
30
31
    /**
32
     * @var MockObject|Logger
33
     */
34
    protected $logger;
35
36
    /**
37
     * @var MockObject|CustomerRepositoryInterface
38
     */
39
    protected $customerRepository;
40
41
    /**
42
     * @var MockObject|CustomerRequestBuilder
43
     */
44
    protected $customerRequestBuilder;
45
46
    /**
47
     * @var MockObject|Client
48
     */
49
    protected $client;
50
51
    /**
52
     * @var MockObject|MagentoCustomerInterface
53
     */
54
    protected $magentoCustomer;
55
56
    /**
57
     * @var MockObject|CustomerInterface
58
     */
59
    protected $customer;
60
61
    /**
62
     * @var MockObject|CustomerApiResourceInterface
63
     */
64
    protected $customerApi;
65
66
    /**
67
     * @var ExportCustomerConsumer
68
     */
69
    protected $exportCustomerConsumer;
70
71
    protected function setUp()
72
    {
73
        $this->magentoCustomerRepository = $this->createMock(MagentoCustomerRepositoryInterface::class);
74
        $this->logger = $this->createMock(Logger::class);
75
        $this->customerRepository = $this->createMock(CustomerRepositoryInterface::class);
76
        $this->customerRequestBuilder = $this->createMock(CustomerRequestBuilder::class);
77
        $this->client = $this->createMock(Client::class);
78
        $this->customer = $this->createMock(CustomerInterface::class);
79
        $this->customerApi = $this->createMock(CustomerApiResourceInterface::class);
80
        $this->magentoCustomer = $this->createMock(MagentoCustomerInterface::class);
81
82
        $this->exportCustomerConsumer = new ExportCustomerConsumer(
83
            $this->magentoCustomerRepository,
84
            $this->logger,
85
            $this->customerRepository,
86
            $this->customerRequestBuilder,
87
            $this->client
88
        );
89
    }
90
91
    public function testConsumeWithAbsentMagentoCustomer()
92
    {
93
        $magentoCustomerId = 123;
94
95
        $exceptionMessage = 'an exception message';
96
        $exception = new NoSuchEntityException(new Phrase($exceptionMessage));
97
98
        $this->magentoCustomerRepository->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not exist on Magento\Customer\Api\CustomerRepositoryInterface. ( 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->magentoCustomerRepository->/** @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('getById')
100
            ->with($magentoCustomerId)
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->customerRepository->expects($this->never())
0 ignored issues
show
Bug introduced by
The method expects() does not exist on CommerceLeague\ActiveCam...omerRepositoryInterface. ( 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->customerRepository->/** @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('getOrCreateByMagentoCustomerId');
109
110
        $this->exportCustomerConsumer->consume(json_encode(['magento_customer_id' => $magentoCustomerId]));
111
    }
112
113
    public function testConsumeApiHttpException()
114
    {
115
        $magentoCustomerId = 123;
116
        $request = ['request'];
117
118
        $this->magentoCustomerRepository->expects($this->once())
119
            ->method('getById')
120
            ->with($magentoCustomerId)
121
            ->willReturn($this->magentoCustomer);
122
123
        $this->magentoCustomer->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not exist on Magento\Customer\Api\Data\CustomerInterface. ( Ignorable by Annotation )

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

123
        $this->magentoCustomer->/** @scrutinizer ignore-call */ 
124
                                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...
124
            ->method('getId')
125
            ->willReturn($magentoCustomerId);
126
127
        $this->customerRequestBuilder->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not exist on CommerceLeague\ActiveCam...Request\CustomerBuilder. ( Ignorable by Annotation )

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

127
        $this->customerRequestBuilder->/** @scrutinizer ignore-call */ 
128
                                       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...
128
            ->method('build')
129
            ->with($this->magentoCustomer)
130
            ->willReturn($request);
131
132
        $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

132
        $this->client->/** @scrutinizer ignore-call */ 
133
                       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...
133
            ->method('getCustomerApi')
134
            ->willReturn($this->customerApi);
135
136
        /** @var MockObject|HttpException $httpException */
137
        $httpException = $this->createMock(HttpException::class);
138
139
        $this->customerApi->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not exist on CommerceLeague\ActiveCam...merApiResourceInterface. ( Ignorable by Annotation )

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

139
        $this->customerApi->/** @scrutinizer ignore-call */ 
140
                            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...
140
            ->method('create')
141
            ->with(['ecomCustomer' => $request])
142
            ->willThrowException($httpException);
143
144
        $this->logger->expects($this->once())
145
            ->method('error');
146
147
        $this->customer->expects($this->never())
0 ignored issues
show
Bug introduced by
The method expects() does not exist on CommerceLeague\ActiveCam...\Data\CustomerInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to CommerceLeague\ActiveCam...\Data\CustomerInterface. ( 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->customer->/** @scrutinizer ignore-call */ 
148
                         expects($this->never())
Loading history...
148
            ->method('setActiveCampaignId');
149
150
        $this->exportCustomerConsumer->consume(json_encode(['magento_customer_id' => $magentoCustomerId]));
151
    }
152
153
    public function testConsumeApiUnprocessableEntityHttpExceptionException()
154
    {
155
        $magentoCustomerId = 123;
156
        $request = ['request'];
157
        $responseErrors = ['first error', 'second error'];
158
159
        $this->magentoCustomerRepository->expects($this->once())
160
            ->method('getById')
161
            ->with($magentoCustomerId)
162
            ->willReturn($this->magentoCustomer);
163
164
        $this->magentoCustomer->expects($this->once())
165
            ->method('getId')
166
            ->willReturn($magentoCustomerId);
167
168
        $this->customerRequestBuilder->expects($this->once())
169
            ->method('build')
170
            ->with($this->magentoCustomer)
171
            ->willReturn($request);
172
173
        $this->client->expects($this->once())
174
            ->method('getCustomerApi')
175
            ->willReturn($this->customerApi);
176
177
        /** @var MockObject|UnprocessableEntityHttpException $unprocessableEntityHttpException */
178
        $unprocessableEntityHttpException = $this->createMock(UnprocessableEntityHttpException::class);
179
180
        $this->customerApi->expects($this->once())
181
            ->method('create')
182
            ->with(['ecomCustomer' => $request])
183
            ->willThrowException($unprocessableEntityHttpException);
184
185
        $this->logger->expects($this->exactly(2))
186
            ->method('error');
187
188
        $unprocessableEntityHttpException->expects($this->once())
189
            ->method('getResponseErrors')
190
            ->willReturn($responseErrors);
191
192
        $this->logger->expects($this->at(1))
193
            ->method('error')
194
            ->with(print_r($responseErrors, true));
195
196
        $this->customer->expects($this->never())
197
            ->method('setActiveCampaignId');
198
199
        $this->exportCustomerConsumer->consume(json_encode(['magento_customer_id' => $magentoCustomerId]));
200
    }
201
202
    public function testConsumeUpdate()
203
    {
204
        $magentoCustomerId = 123;
205
        $request = ['request'];
206
        $activeCampaignId = 456;
207
        $response = ['ecomCustomer' => ['id' => $activeCampaignId]];
208
209
        $this->magentoCustomerRepository->expects($this->once())
210
            ->method('getById')
211
            ->with($magentoCustomerId)
212
            ->willReturn($this->magentoCustomer);
213
214
        $this->magentoCustomer->expects($this->once())
215
            ->method('getId')
216
            ->willReturn($magentoCustomerId);
217
218
        $this->customerRepository->expects($this->once())
219
            ->method('getOrCreateByMagentoCustomerId')
220
            ->willReturn($this->customer);
221
222
        $this->customerRequestBuilder->expects($this->once())
223
            ->method('build')
224
            ->with($this->magentoCustomer)
225
            ->willReturn($request);
226
227
        $this->customer->expects($this->once())
228
            ->method('getActiveCampaignId')
229
            ->willReturn($activeCampaignId);
230
231
        $this->client->expects($this->once())
232
            ->method('getCustomerApi')
233
            ->willReturn($this->customerApi);
234
235
        $this->customerApi->expects($this->once())
236
            ->method('update')
237
            ->with($activeCampaignId, ['ecomCustomer' => $request])
238
            ->willReturn($response);
239
240
        $this->customer->expects($this->once())
241
            ->method('setActiveCampaignId')
242
            ->with($activeCampaignId)
243
            ->willReturnSelf();
244
245
        $this->customerRepository->expects($this->once())
246
            ->method('save')
247
            ->with($this->customer);
248
249
        $this->exportCustomerConsumer->consume(json_encode(['magento_customer_id' => $magentoCustomerId]));
250
    }
251
252
    public function testConsumeCreate()
253
    {
254
        $magentoCustomerId = 123;
255
        $request = ['request'];
256
        $activeCampaignId = 456;
257
        $response = ['ecomCustomer' => ['id' => $activeCampaignId]];
258
259
        $this->magentoCustomerRepository->expects($this->once())
260
            ->method('getById')
261
            ->with($magentoCustomerId)
262
            ->willReturn($this->magentoCustomer);
263
264
        $this->magentoCustomer->expects($this->once())
265
            ->method('getId')
266
            ->willReturn($magentoCustomerId);
267
268
        $this->customerRepository->expects($this->once())
269
            ->method('getOrCreateByMagentoCustomerId')
270
            ->willReturn($this->customer);
271
272
        $this->customerRequestBuilder->expects($this->once())
273
            ->method('build')
274
            ->with($this->magentoCustomer)
275
            ->willReturn($request);
276
277
        $this->customer->expects($this->once())
278
            ->method('getActiveCampaignId')
279
            ->willReturn(null);
280
281
        $this->client->expects($this->once())
282
            ->method('getCustomerApi')
283
            ->willReturn($this->customerApi);
284
285
        $this->customerApi->expects($this->once())
286
            ->method('create')
287
            ->with(['ecomCustomer' => $request])
288
            ->willReturn($response);
289
290
        $this->customer->expects($this->once())
291
            ->method('setActiveCampaignId')
292
            ->with($activeCampaignId)
293
            ->willReturnSelf();
294
295
        $this->customerRepository->expects($this->once())
296
            ->method('save')
297
            ->with($this->customer);
298
299
        $this->exportCustomerConsumer->consume(json_encode(['magento_customer_id' => $magentoCustomerId]));
300
    }
301
}
302