1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
*/ |
4
|
|
|
|
5
|
|
|
namespace CommerceLeague\ActiveCampaign\Test\Unit\MessageQueue\Contact; |
6
|
|
|
|
7
|
|
|
use CommerceLeague\ActiveCampaign\Api\ContactRepositoryInterface; |
8
|
|
|
use CommerceLeague\ActiveCampaign\Api\Data\ContactInterface; |
9
|
|
|
use CommerceLeague\ActiveCampaign\Logger\Logger; |
10
|
|
|
use CommerceLeague\ActiveCampaign\MessageQueue\Contact\CreateUpdateConsumer; |
11
|
|
|
use CommerceLeague\ActiveCampaign\MessageQueue\Contact\CreateUpdateMessage; |
12
|
|
|
use CommerceLeague\ActiveCampaignApi\Api\ContactApiResourceInterface; |
13
|
|
|
use CommerceLeague\ActiveCampaignApi\Exception\HttpException; |
14
|
|
|
use Magento\Framework\Exception\CouldNotSaveException; |
15
|
|
|
use Magento\Framework\Phrase; |
16
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
17
|
|
|
use PHPUnit\Framework\TestCase; |
18
|
|
|
use CommerceLeague\ActiveCampaign\Gateway\Client; |
19
|
|
|
|
20
|
|
|
class CreateUpdateConsumerTest extends TestCase |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var MockObject|Client |
24
|
|
|
*/ |
25
|
|
|
protected $client; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var MockObject|Logger |
29
|
|
|
*/ |
30
|
|
|
protected $logger; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var MockObject|ContactRepositoryInterface |
34
|
|
|
*/ |
35
|
|
|
protected $contactRepository; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var MockObject|CreateUpdateMessage |
39
|
|
|
*/ |
40
|
|
|
protected $createUpdateMessage; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var MockObject|ContactInterface |
44
|
|
|
*/ |
45
|
|
|
protected $contact; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var MockObject|ContactApiResourceInterface |
49
|
|
|
*/ |
50
|
|
|
protected $contactApi; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var CreateUpdateConsumer |
54
|
|
|
*/ |
55
|
|
|
protected $createUpdateConsumer; |
56
|
|
|
|
57
|
|
|
protected function setUp() |
58
|
|
|
{ |
59
|
|
|
$this->client = $this->createMock(Client::class); |
60
|
|
|
$this->logger = $this->createMock(Logger::class); |
61
|
|
|
$this->contactRepository = $this->createMock(ContactRepositoryInterface::class); |
62
|
|
|
$this->createUpdateMessage = $this->createMock(CreateUpdateMessage::class); |
63
|
|
|
$this->contact = $this->createMock(ContactInterface::class); |
64
|
|
|
$this->contactApi = $this->createMock(ContactApiResourceInterface::class); |
65
|
|
|
|
66
|
|
|
$this->createUpdateConsumer = new CreateUpdateConsumer( |
67
|
|
|
$this->client, |
68
|
|
|
$this->logger, |
69
|
|
|
$this->contactRepository |
70
|
|
|
); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function testConsumeWithUnknownCustomer() |
74
|
|
|
{ |
75
|
|
|
$contactId = 123; |
76
|
|
|
|
77
|
|
|
$this->contactRepository->expects($this->once()) |
|
|
|
|
78
|
|
|
->method('getById') |
79
|
|
|
->with($contactId) |
80
|
|
|
->willReturn($this->contact); |
81
|
|
|
|
82
|
|
|
$this->createUpdateMessage->expects($this->any()) |
|
|
|
|
83
|
|
|
->method('getContactId') |
84
|
|
|
->willReturn($contactId); |
85
|
|
|
|
86
|
|
|
$this->contact->expects($this->once()) |
|
|
|
|
87
|
|
|
->method('getId') |
88
|
|
|
->willReturn(null); |
89
|
|
|
|
90
|
|
|
$this->logger->expects($this->once()) |
|
|
|
|
91
|
|
|
->method('error') |
92
|
|
|
->with(__('Unable to find contact with id "%1".', $contactId)); |
93
|
|
|
|
94
|
|
|
$this->createUpdateMessage->expects($this->never()) |
95
|
|
|
->method('getSerializedRequest'); |
96
|
|
|
|
97
|
|
|
$this->createUpdateConsumer->consume($this->createUpdateMessage); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function testConsumeApiCallFails() |
101
|
|
|
{ |
102
|
|
|
$contactId = 123; |
103
|
|
|
$request = ['request']; |
104
|
|
|
/** @var MockObject|HttpException $exception */ |
105
|
|
|
$exception = $this->createMock(HttpException::class); |
106
|
|
|
|
107
|
|
|
$this->contactRepository->expects($this->once()) |
108
|
|
|
->method('getById') |
109
|
|
|
->with($contactId) |
110
|
|
|
->willReturn($this->contact); |
111
|
|
|
|
112
|
|
|
$this->createUpdateMessage->expects($this->any()) |
113
|
|
|
->method('getContactId') |
114
|
|
|
->willReturn($contactId); |
115
|
|
|
|
116
|
|
|
$this->contact->expects($this->once()) |
117
|
|
|
->method('getId') |
118
|
|
|
->willReturn($contactId); |
119
|
|
|
|
120
|
|
|
$this->createUpdateMessage->expects($this->once()) |
121
|
|
|
->method('getSerializedRequest') |
122
|
|
|
->willReturn(json_encode($request)); |
123
|
|
|
|
124
|
|
|
$this->client->expects($this->once()) |
|
|
|
|
125
|
|
|
->method('getContactApi') |
126
|
|
|
->willReturn($this->contactApi); |
127
|
|
|
|
128
|
|
|
$this->contactApi->expects($this->once()) |
|
|
|
|
129
|
|
|
->method('upsert') |
130
|
|
|
->with(['contact' => $request]) |
131
|
|
|
->willThrowException($exception); |
132
|
|
|
|
133
|
|
|
$this->logger->expects($this->once()) |
134
|
|
|
->method('error'); |
135
|
|
|
|
136
|
|
|
$this->contact->expects($this->never()) |
137
|
|
|
->method('setActiveCampaignId'); |
138
|
|
|
|
139
|
|
|
$this->createUpdateConsumer->consume($this->createUpdateMessage); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function testConsumeContactCouldNotSave() |
143
|
|
|
{ |
144
|
|
|
$contactId = 123; |
145
|
|
|
$activeCampaignId = 456; |
146
|
|
|
$request = ['request']; |
147
|
|
|
|
148
|
|
|
$this->contactRepository->expects($this->once()) |
149
|
|
|
->method('getById') |
150
|
|
|
->with($contactId) |
151
|
|
|
->willReturn($this->contact); |
152
|
|
|
|
153
|
|
|
$this->createUpdateMessage->expects($this->any()) |
154
|
|
|
->method('getContactId') |
155
|
|
|
->willReturn($contactId); |
156
|
|
|
|
157
|
|
|
$this->contact->expects($this->once()) |
158
|
|
|
->method('getId') |
159
|
|
|
->willReturn($contactId); |
160
|
|
|
|
161
|
|
|
$this->createUpdateMessage->expects($this->once()) |
162
|
|
|
->method('getSerializedRequest') |
163
|
|
|
->willReturn(json_encode($request)); |
164
|
|
|
|
165
|
|
|
$this->client->expects($this->once()) |
166
|
|
|
->method('getContactApi') |
167
|
|
|
->willReturn($this->contactApi); |
168
|
|
|
|
169
|
|
|
$this->contactApi->expects($this->once()) |
170
|
|
|
->method('upsert') |
171
|
|
|
->with(['contact' => $request]) |
172
|
|
|
->willReturn(['contact' => ['id' => $activeCampaignId]]); |
173
|
|
|
|
174
|
|
|
$this->contact->expects($this->once()) |
175
|
|
|
->method('setActiveCampaignId') |
176
|
|
|
->with($activeCampaignId); |
177
|
|
|
|
178
|
|
|
$this->contactRepository->expects($this->once()) |
179
|
|
|
->method('save') |
180
|
|
|
->with($this->contact) |
181
|
|
|
->willThrowException(new CouldNotSaveException(new Phrase('an exception'))); |
182
|
|
|
|
183
|
|
|
$this->logger->expects($this->once()) |
184
|
|
|
->method('error') |
185
|
|
|
->with('an exception'); |
186
|
|
|
|
187
|
|
|
$this->createUpdateConsumer->consume($this->createUpdateMessage); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
public function testConsume() |
191
|
|
|
{ |
192
|
|
|
$contactId = 123; |
193
|
|
|
$activeCampaignId = 456; |
194
|
|
|
$request = ['request']; |
195
|
|
|
|
196
|
|
|
$this->contactRepository->expects($this->once()) |
197
|
|
|
->method('getById') |
198
|
|
|
->with($contactId) |
199
|
|
|
->willReturn($this->contact); |
200
|
|
|
|
201
|
|
|
$this->createUpdateMessage->expects($this->any()) |
202
|
|
|
->method('getContactId') |
203
|
|
|
->willReturn($contactId); |
204
|
|
|
|
205
|
|
|
$this->contact->expects($this->once()) |
206
|
|
|
->method('getId') |
207
|
|
|
->willReturn($contactId); |
208
|
|
|
|
209
|
|
|
$this->createUpdateMessage->expects($this->once()) |
210
|
|
|
->method('getSerializedRequest') |
211
|
|
|
->willReturn(json_encode($request)); |
212
|
|
|
|
213
|
|
|
$this->client->expects($this->once()) |
214
|
|
|
->method('getContactApi') |
215
|
|
|
->willReturn($this->contactApi); |
216
|
|
|
|
217
|
|
|
$this->contactApi->expects($this->once()) |
218
|
|
|
->method('upsert') |
219
|
|
|
->with(['contact' => $request]) |
220
|
|
|
->willReturn(['contact' => ['id' => $activeCampaignId]]); |
221
|
|
|
|
222
|
|
|
$this->contact->expects($this->once()) |
223
|
|
|
->method('setActiveCampaignId') |
224
|
|
|
->with($activeCampaignId); |
225
|
|
|
|
226
|
|
|
$this->contactRepository->expects($this->once()) |
227
|
|
|
->method('save') |
228
|
|
|
->with($this->contact); |
229
|
|
|
|
230
|
|
|
$this->logger->expects($this->never()) |
231
|
|
|
->method('error'); |
232
|
|
|
|
233
|
|
|
$this->createUpdateConsumer->consume($this->createUpdateMessage); |
234
|
|
|
} |
235
|
|
|
} |
236
|
|
|
|
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.