1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
/** |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace CommerceLeague\ActiveCampaign\Test\Unit\MessageQueue\Newsletter; |
7
|
|
|
|
8
|
|
|
use CommerceLeague\ActiveCampaign\Api\ContactRepositoryInterface; |
9
|
|
|
use CommerceLeague\ActiveCampaign\Api\Data\ContactInterface; |
10
|
|
|
use CommerceLeague\ActiveCampaign\Gateway\Client; |
11
|
|
|
use CommerceLeague\ActiveCampaign\Gateway\Request\ContactBuilder as ContactRequestBuilder; |
12
|
|
|
use CommerceLeague\ActiveCampaign\Logger\Logger; |
13
|
|
|
use CommerceLeague\ActiveCampaign\MessageQueue\Newsletter\ExportContactConsumer; |
14
|
|
|
use CommerceLeague\ActiveCampaignApi\Api\ContactApiResourceInterface; |
15
|
|
|
use CommerceLeague\ActiveCampaignApi\Exception\HttpException; |
16
|
|
|
use CommerceLeague\ActiveCampaignApi\Exception\UnprocessableEntityHttpException; |
17
|
|
|
use Magento\Framework\Phrase; |
18
|
|
|
use Magento\Newsletter\Model\Subscriber; |
19
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
20
|
|
|
use PHPUnit\Framework\TestCase; |
21
|
|
|
use Magento\Newsletter\Model\SubscriberFactory; |
|
|
|
|
22
|
|
|
|
23
|
|
|
class ExportContactConsumerTest extends TestCase |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var MockObject|SubscriberFactory |
27
|
|
|
*/ |
28
|
|
|
protected $subscriberFactory; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var MockObject|Subscriber |
32
|
|
|
*/ |
33
|
|
|
protected $subscriber; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var MockObject|Logger |
37
|
|
|
*/ |
38
|
|
|
protected $logger; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var MockObject|ContactRepositoryInterface |
42
|
|
|
*/ |
43
|
|
|
protected $contactRepository; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var MockObject|ContactRequestBuilder |
47
|
|
|
*/ |
48
|
|
|
protected $contactRequestBuilder; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var MockObject|Client |
52
|
|
|
*/ |
53
|
|
|
protected $client; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var MockObject|ContactInterface |
57
|
|
|
*/ |
58
|
|
|
protected $contact; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @var MockObject|ContactApiResourceInterface |
62
|
|
|
*/ |
63
|
|
|
protected $contactApi; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @var ExportContactConsumer |
67
|
|
|
*/ |
68
|
|
|
protected $exportContactConsumer; |
69
|
|
|
|
70
|
|
|
protected function setUp() |
71
|
|
|
{ |
72
|
|
|
$this->subscriberFactory = $this->getMockBuilder(SubscriberFactory::class) |
73
|
|
|
->disableOriginalConstructor() |
74
|
|
|
->setMethods(['create']) |
75
|
|
|
->getMock(); |
76
|
|
|
|
77
|
|
|
$this->subscriber = $this->createMock(Subscriber::class); |
78
|
|
|
|
79
|
|
|
$this->subscriberFactory->expects($this->any()) |
80
|
|
|
->method('create') |
81
|
|
|
->willReturn($this->subscriber); |
82
|
|
|
|
83
|
|
|
$this->logger = $this->createMock(Logger::class); |
84
|
|
|
$this->contactRepository = $this->createMock(ContactRepositoryInterface::class); |
85
|
|
|
$this->contactRequestBuilder = $this->createMock(ContactRequestBuilder::class); |
86
|
|
|
$this->client = $this->createMock(Client::class); |
87
|
|
|
$this->contact = $this->createMock(ContactInterface::class); |
88
|
|
|
$this->contactApi = $this->createMock(ContactApiResourceInterface::class); |
89
|
|
|
|
90
|
|
|
$this->exportContactConsumer = new ExportContactConsumer( |
91
|
|
|
$this->subscriberFactory, |
92
|
|
|
$this->logger, |
93
|
|
|
$this->contactRepository, |
94
|
|
|
$this->contactRequestBuilder, |
95
|
|
|
$this->client |
96
|
|
|
); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function testConsumeWithAbsentSubscriber() |
100
|
|
|
{ |
101
|
|
|
$email = '[email protected]'; |
102
|
|
|
|
103
|
|
|
$this->subscriber->expects($this->once()) |
104
|
|
|
->method('loadByEmail') |
|
|
|
|
105
|
|
|
->with($email) |
|
|
|
|
106
|
|
|
->willReturnSelf(); |
|
|
|
|
107
|
|
|
|
108
|
|
|
$this->subscriber->expects($this->once()) |
109
|
|
|
->method('getId') |
110
|
|
|
->willReturn(null); |
|
|
|
|
111
|
|
|
|
112
|
|
|
$this->logger->expects($this->once()) |
|
|
|
|
113
|
|
|
->method('error') |
114
|
|
|
->with(new Phrase('The Subscriber with the "%1" email doesn\'t exist', [$email])); |
115
|
|
|
|
116
|
|
|
$this->contactRepository->expects($this->never()) |
|
|
|
|
117
|
|
|
->method('getOrCreateByEmail'); |
118
|
|
|
|
119
|
|
|
$this->exportContactConsumer->consume(json_encode(['email' => $email])); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function testConsumeApiHttpException() |
123
|
|
|
{ |
124
|
|
|
$email = '[email protected]'; |
125
|
|
|
$request = ['request']; |
126
|
|
|
|
127
|
|
|
$this->subscriber->expects($this->once()) |
128
|
|
|
->method('loadByEmail') |
129
|
|
|
->with($email) |
130
|
|
|
->willReturnSelf(); |
131
|
|
|
|
132
|
|
|
$this->subscriber->expects($this->once()) |
133
|
|
|
->method('getId') |
134
|
|
|
->willReturn(123); |
135
|
|
|
|
136
|
|
|
$this->subscriber->expects($this->once()) |
137
|
|
|
->method('getEmail') |
138
|
|
|
->willReturn($email); |
139
|
|
|
|
140
|
|
|
$this->contactRepository->expects($this->once()) |
141
|
|
|
->method('getOrCreateByEmail') |
142
|
|
|
->with($email) |
143
|
|
|
->willReturn($this->contact); |
144
|
|
|
|
145
|
|
|
$this->contactRequestBuilder->expects($this->once()) |
|
|
|
|
146
|
|
|
->method('buildWithSubscriber') |
147
|
|
|
->willReturn($request); |
148
|
|
|
|
149
|
|
|
$this->client->expects($this->once()) |
|
|
|
|
150
|
|
|
->method('getContactApi') |
151
|
|
|
->willReturn($this->contactApi); |
152
|
|
|
|
153
|
|
|
/** @var MockObject|HttpException $httpException */ |
154
|
|
|
$httpException = $this->createMock(HttpException::class); |
155
|
|
|
|
156
|
|
|
$this->contactApi->expects($this->once()) |
|
|
|
|
157
|
|
|
->method('upsert') |
158
|
|
|
->with(['contact' => $request]) |
159
|
|
|
->willThrowException($httpException); |
160
|
|
|
|
161
|
|
|
$this->logger->expects($this->once()) |
162
|
|
|
->method('error'); |
163
|
|
|
|
164
|
|
|
$this->contact->expects($this->never()) |
|
|
|
|
165
|
|
|
->method('setActiveCampaignId'); |
166
|
|
|
|
167
|
|
|
$this->exportContactConsumer->consume(json_encode(['email' => $email])); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
public function testConsumeApiUnprocessableEntityHttpExceptionException() |
171
|
|
|
{ |
172
|
|
|
$email = '[email protected]'; |
173
|
|
|
$request = ['request']; |
174
|
|
|
$responseErrors = ['first error', 'second error']; |
175
|
|
|
|
176
|
|
|
$this->subscriber->expects($this->once()) |
177
|
|
|
->method('loadByEmail') |
178
|
|
|
->with($email) |
179
|
|
|
->willReturnSelf(); |
180
|
|
|
|
181
|
|
|
$this->subscriber->expects($this->once()) |
182
|
|
|
->method('getId') |
183
|
|
|
->willReturn(123); |
184
|
|
|
|
185
|
|
|
$this->subscriber->expects($this->once()) |
186
|
|
|
->method('getEmail') |
187
|
|
|
->willReturn($email); |
188
|
|
|
|
189
|
|
|
$this->contactRepository->expects($this->once()) |
190
|
|
|
->method('getOrCreateByEmail') |
191
|
|
|
->with($email) |
192
|
|
|
->willReturn($this->contact); |
193
|
|
|
|
194
|
|
|
$this->contactRequestBuilder->expects($this->once()) |
195
|
|
|
->method('buildWithSubscriber') |
196
|
|
|
->willReturn($request); |
197
|
|
|
|
198
|
|
|
$this->client->expects($this->once()) |
199
|
|
|
->method('getContactApi') |
200
|
|
|
->willReturn($this->contactApi); |
201
|
|
|
|
202
|
|
|
/** @var MockObject|UnprocessableEntityHttpException $unprocessableEntityHttpException */ |
203
|
|
|
$unprocessableEntityHttpException = $this->createMock(UnprocessableEntityHttpException::class); |
204
|
|
|
|
205
|
|
|
$this->contactApi->expects($this->once()) |
206
|
|
|
->method('upsert') |
207
|
|
|
->with(['contact' => $request]) |
208
|
|
|
->willThrowException($unprocessableEntityHttpException); |
209
|
|
|
|
210
|
|
|
$this->logger->expects($this->exactly(2)) |
211
|
|
|
->method('error'); |
212
|
|
|
|
213
|
|
|
$unprocessableEntityHttpException->expects($this->once()) |
214
|
|
|
->method('getResponseErrors') |
215
|
|
|
->willReturn($responseErrors); |
216
|
|
|
|
217
|
|
|
$this->logger->expects($this->at(1)) |
218
|
|
|
->method('error') |
219
|
|
|
->with(print_r($responseErrors, true)); |
220
|
|
|
|
221
|
|
|
$this->contact->expects($this->never()) |
222
|
|
|
->method('setActiveCampaignId'); |
223
|
|
|
|
224
|
|
|
$this->exportContactConsumer->consume(json_encode(['email' => $email])); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
public function testConsume() |
228
|
|
|
{ |
229
|
|
|
$email = '[email protected]'; |
230
|
|
|
$request = ['request']; |
231
|
|
|
$activeCampaignId = 456; |
232
|
|
|
$response = ['contact' => ['id' => $activeCampaignId]]; |
233
|
|
|
|
234
|
|
|
$this->subscriber->expects($this->once()) |
235
|
|
|
->method('loadByEmail') |
236
|
|
|
->with($email) |
237
|
|
|
->willReturnSelf(); |
238
|
|
|
|
239
|
|
|
$this->subscriber->expects($this->once()) |
240
|
|
|
->method('getId') |
241
|
|
|
->willReturn(123); |
242
|
|
|
|
243
|
|
|
$this->subscriber->expects($this->once()) |
244
|
|
|
->method('getEmail') |
245
|
|
|
->willReturn($email); |
246
|
|
|
|
247
|
|
|
$this->contactRepository->expects($this->once()) |
248
|
|
|
->method('getOrCreateByEmail') |
249
|
|
|
->with($email) |
250
|
|
|
->willReturn($this->contact); |
251
|
|
|
|
252
|
|
|
$this->contactRequestBuilder->expects($this->once()) |
253
|
|
|
->method('buildWithSubscriber') |
254
|
|
|
->willReturn($request); |
255
|
|
|
|
256
|
|
|
$this->client->expects($this->once()) |
257
|
|
|
->method('getContactApi') |
258
|
|
|
->willReturn($this->contactApi); |
259
|
|
|
|
260
|
|
|
|
261
|
|
|
$this->contactApi->expects($this->once()) |
262
|
|
|
->method('upsert') |
263
|
|
|
->with(['contact' => $request]) |
264
|
|
|
->willReturn($response); |
265
|
|
|
|
266
|
|
|
$this->contact->expects($this->once()) |
267
|
|
|
->method('setActiveCampaignId') |
268
|
|
|
->with($activeCampaignId) |
269
|
|
|
->willReturnSelf(); |
270
|
|
|
|
271
|
|
|
$this->contactRepository->expects($this->once()) |
272
|
|
|
->method('save') |
273
|
|
|
->with($this->contact); |
274
|
|
|
|
275
|
|
|
$this->logger->expects($this->never()) |
276
|
|
|
->method('error'); |
277
|
|
|
|
278
|
|
|
$this->exportContactConsumer->consume(json_encode(['email' => $email])); |
279
|
|
|
} |
280
|
|
|
} |
281
|
|
|
|
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:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths