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 43
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 30
nc 1
nop 0
dl 0
loc 43
rs 9.44
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\ContactRepositoryInterface;
9
use CommerceLeague\ActiveCampaign\Api\Data\ContactInterface;
10
use CommerceLeague\ActiveCampaign\Gateway\Request\ContactBuilder as ContactRequestBuilder;
11
use CommerceLeague\ActiveCampaign\Logger\Logger;
12
use CommerceLeague\ActiveCampaign\MessageQueue\Customer\ExportContactConsumer;
13
use CommerceLeague\ActiveCampaignApi\Api\ContactApiResourceInterface;
14
use CommerceLeague\ActiveCampaignApi\Exception\HttpException;
15
use CommerceLeague\ActiveCampaignApi\Exception\UnprocessableEntityHttpException;
16
use Magento\Customer\Api\CustomerRepositoryInterface as MagentoCustomerRepositoryInterface;
17
use Magento\Customer\Api\Data\CustomerInterface as MagentoCustomerInterface;
18
use Magento\Framework\Exception\NoSuchEntityException;
19
use Magento\Framework\Phrase;
20
use PHPUnit\Framework\MockObject\MockObject;
21
use PHPUnit\Framework\TestCase;
22
use CommerceLeague\ActiveCampaign\Gateway\Client;
23
24
class ExportContactConsumerTest 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|ContactRepositoryInterface
38
     */
39
    protected $contactRepository;
40
41
    /**
42
     * @var MockObject|ContactRequestBuilder
43
     */
44
    protected $contactRequestBuilder;
45
46
    /**
47
     * @var MockObject|Client
48
     */
49
    protected $client;
50
51
    /**
52
     * @var MockObject|ContactInterface
53
     */
54
    protected $contact;
55
56
    /**
57
     * @var MockObject|ContactApiResourceInterface
58
     */
59
    protected $contactApi;
60
61
    /**
62
     * @var MockObject|MagentoCustomerInterface
63
     */
64
    protected $magentoCustomer;
65
66
    /**
67
     * @var ExportContactConsumer
68
     */
69
    protected $exportContactConsumer;
70
71
    protected function setUp()
72
    {
73
        $this->magentoCustomerRepository = $this->createMock(MagentoCustomerRepositoryInterface::class);
74
        $this->logger = $this->createMock(Logger::class);
75
        $this->contactRepository = $this->createMock(ContactRepositoryInterface::class);
76
        $this->contactRequestBuilder = $this->createMock(ContactRequestBuilder::class);
77
        $this->client = $this->createMock(Client::class);
78
        $this->contact = $this->createMock(ContactInterface::class);
79
        $this->contactApi = $this->createMock(ContactApiResourceInterface::class);
80
        $this->magentoCustomer = $this->createMock(MagentoCustomerInterface::class);
81
82
        $this->exportContactConsumer = new ExportContactConsumer(
83
            $this->magentoCustomerRepository,
84
            $this->logger,
85
            $this->contactRepository,
86
            $this->contactRequestBuilder,
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->contactRepository->expects($this->never())
0 ignored issues
show
Bug introduced by
The method expects() does not exist on CommerceLeague\ActiveCam...tactRepositoryInterface. ( 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->contactRepository->/** @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('getOrCreateByEmail');
109
110
        $this->exportContactConsumer->consume(json_encode(['magento_customer_id' => $magentoCustomerId]));
111
    }
112
113
    public function testConsumeApiHttpException()
114
    {
115
        $magentoCustomerId = 123;
116
        $email = '[email protected]';
117
        $request = ['request'];
118
119
        $this->magentoCustomerRepository->expects($this->once())
120
            ->method('getById')
121
            ->with($magentoCustomerId)
122
            ->willReturn($this->magentoCustomer);
123
124
        $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

124
        $this->magentoCustomer->/** @scrutinizer ignore-call */ 
125
                                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...
125
            ->method('getEmail')
126
            ->willReturn($email);
127
128
        $this->contactRepository->expects($this->once())
129
            ->method('getOrCreateByEmail')
130
            ->with($email)
131
            ->willReturn($this->contact);
132
133
        $this->contactRequestBuilder->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not exist on CommerceLeague\ActiveCam...\Request\ContactBuilder. ( 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->contactRequestBuilder->/** @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('buildWithMagentoCustomer')
135
            ->willReturn($request);
136
137
        $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

137
        $this->client->/** @scrutinizer ignore-call */ 
138
                       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...
138
            ->method('getContactApi')
139
            ->willReturn($this->contactApi);
140
141
        /** @var MockObject|HttpException $httpException */
142
        $httpException = $this->createMock(HttpException::class);
143
144
        $this->contactApi->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not exist on CommerceLeague\ActiveCam...actApiResourceInterface. ( Ignorable by Annotation )

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

144
        $this->contactApi->/** @scrutinizer ignore-call */ 
145
                           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...
145
            ->method('upsert')
146
            ->with(['contact' => $request])
147
            ->willThrowException($httpException);
148
149
        $this->logger->expects($this->once())
150
            ->method('error');
151
152
        $this->contact->expects($this->never())
0 ignored issues
show
Bug introduced by
The method expects() does not exist on CommerceLeague\ActiveCam...i\Data\ContactInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to CommerceLeague\ActiveCam...i\Data\ContactInterface. ( Ignorable by Annotation )

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

152
        $this->contact->/** @scrutinizer ignore-call */ 
153
                        expects($this->never())
Loading history...
153
            ->method('setActiveCampaignId');
154
155
        $this->exportContactConsumer->consume(json_encode(['magento_customer_id' => $magentoCustomerId]));
156
    }
157
158
    public function testConsumeApiUnprocessableEntityHttpExceptionException()
159
    {
160
        $magentoCustomerId = 123;
161
        $email = '[email protected]';
162
        $request = ['request'];
163
        $responseErrors = ['first error', 'second error'];
164
165
        $this->magentoCustomerRepository->expects($this->once())
166
            ->method('getById')
167
            ->with($magentoCustomerId)
168
            ->willReturn($this->magentoCustomer);
169
170
        $this->magentoCustomer->expects($this->once())
171
            ->method('getEmail')
172
            ->willReturn($email);
173
174
        $this->contactRepository->expects($this->once())
175
            ->method('getOrCreateByEmail')
176
            ->with($email)
177
            ->willReturn($this->contact);
178
179
        $this->contactRequestBuilder->expects($this->once())
180
            ->method('buildWithMagentoCustomer')
181
            ->willReturn($request);
182
183
        $this->client->expects($this->once())
184
            ->method('getContactApi')
185
            ->willReturn($this->contactApi);
186
187
        /** @var MockObject|UnprocessableEntityHttpException $unprocessableEntityHttpException */
188
        $unprocessableEntityHttpException = $this->createMock(UnprocessableEntityHttpException::class);
189
190
        $this->contactApi->expects($this->once())
191
            ->method('upsert')
192
            ->with(['contact' => $request])
193
            ->willThrowException($unprocessableEntityHttpException);
194
195
        $this->logger->expects($this->exactly(2))
196
            ->method('error');
197
198
        $unprocessableEntityHttpException->expects($this->once())
199
            ->method('getResponseErrors')
200
            ->willReturn($responseErrors);
201
202
        $this->logger->expects($this->at(1))
203
            ->method('error')
204
            ->with(print_r($responseErrors, true));
205
206
        $this->contact->expects($this->never())
207
            ->method('setActiveCampaignId');
208
209
        $this->exportContactConsumer->consume(json_encode(['magento_customer_id' => $magentoCustomerId]));
210
    }
211
212
    public function testConsume()
213
    {
214
        $magentoCustomerId = 123;
215
        $email = '[email protected]';
216
        $request = ['request'];
217
        $activeCampaignId = 456;
218
        $response = ['contact' => ['id' => $activeCampaignId]];
219
220
        $this->magentoCustomerRepository->expects($this->once())
221
            ->method('getById')
222
            ->with($magentoCustomerId)
223
            ->willReturn($this->magentoCustomer);
224
225
        $this->magentoCustomer->expects($this->once())
226
            ->method('getEmail')
227
            ->willReturn($email);
228
229
        $this->contactRepository->expects($this->once())
230
            ->method('getOrCreateByEmail')
231
            ->with($email)
232
            ->willReturn($this->contact);
233
234
        $this->contactRequestBuilder->expects($this->once())
235
            ->method('buildWithMagentoCustomer')
236
            ->with($this->magentoCustomer)
237
            ->willReturn($request);
238
239
        $this->client->expects($this->once())
240
            ->method('getContactApi')
241
            ->willReturn($this->contactApi);
242
243
        $this->contactApi->expects($this->once())
244
            ->method('upsert')
245
            ->with(['contact' => $request])
246
            ->willReturn($response);
247
248
        $this->contact->expects($this->once())
249
            ->method('setActiveCampaignId')
250
            ->with($activeCampaignId)
251
            ->willReturnSelf();
252
253
        $this->contactRepository->expects($this->once())
254
            ->method('save')
255
            ->with($this->contact);
256
257
        $this->logger->expects($this->never())
258
            ->method('error');
259
260
        $this->exportContactConsumer->consume(json_encode(['magento_customer_id' => $magentoCustomerId]));
261
    }
262
}
263