1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
*/ |
4
|
|
|
|
5
|
|
|
namespace CommerceLeague\ActiveCampaign\Test\Unit\Observer\Customer; |
6
|
|
|
|
7
|
|
|
use CommerceLeague\ActiveCampaign\Api\ContactRepositoryInterface; |
8
|
|
|
use CommerceLeague\ActiveCampaign\Gateway\Request\ContactRequestBuilder; |
9
|
|
|
use CommerceLeague\ActiveCampaign\Helper\Config as ConfigHelper; |
10
|
|
|
use CommerceLeague\ActiveCampaign\Logger\Logger; |
11
|
|
|
use CommerceLeague\ActiveCampaign\MessageQueue\Contact\CreateUpdateMessage; |
12
|
|
|
use CommerceLeague\ActiveCampaign\MessageQueue\Contact\Topics; |
13
|
|
|
use CommerceLeague\ActiveCampaign\Model\Contact; |
14
|
|
|
use CommerceLeague\ActiveCampaign\Observer\Customer\CreateUpdateContactObserver; |
15
|
|
|
use Magento\Customer\Model\Customer; |
16
|
|
|
use Magento\Framework\Event; |
17
|
|
|
use Magento\Framework\Event\Observer; |
18
|
|
|
use Magento\Framework\Exception\CouldNotSaveException; |
19
|
|
|
use Magento\Framework\MessageQueue\PublisherInterface; |
20
|
|
|
use Magento\Framework\Phrase; |
21
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
22
|
|
|
use PHPUnit\Framework\TestCase; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Class CreateUpdateContactObserverTest |
26
|
|
|
*/ |
27
|
|
|
class CreateUpdateContactObserverTest extends TestCase |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var MockObject|ConfigHelper |
31
|
|
|
*/ |
32
|
|
|
protected $configHelper; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var MockObject|ContactRepositoryInterface |
36
|
|
|
*/ |
37
|
|
|
protected $contactRepository; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var MockObject|Logger |
41
|
|
|
*/ |
42
|
|
|
protected $logger; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var MockObject|ContactRequestBuilder |
46
|
|
|
*/ |
47
|
|
|
protected $contactRequestBuilder; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var MockObject|PublisherInterface |
51
|
|
|
*/ |
52
|
|
|
protected $publisher; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var MockObject|Observer |
56
|
|
|
*/ |
57
|
|
|
protected $observer; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var MockObject|Event |
61
|
|
|
*/ |
62
|
|
|
protected $event; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @var MockObject|Customer |
66
|
|
|
*/ |
67
|
|
|
protected $customer; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @var MockObject|Contact |
71
|
|
|
*/ |
72
|
|
|
protected $contact; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @var CreateUpdateContactObserver |
76
|
|
|
*/ |
77
|
|
|
protected $createUpdateContactObserver; |
78
|
|
|
|
79
|
|
|
protected function setUp() |
80
|
|
|
{ |
81
|
|
|
$this->configHelper = $this->createMock(ConfigHelper::class); |
82
|
|
|
$this->contactRepository = $this->createMock(ContactRepositoryInterface::class); |
83
|
|
|
$this->logger = $this->createMock(Logger::class); |
84
|
|
|
$this->contactRequestBuilder = $this->createMock(ContactRequestBuilder::class); |
85
|
|
|
$this->publisher = $this->createMock(PublisherInterface::class); |
86
|
|
|
$this->observer = $this->createMock(Observer::class); |
87
|
|
|
$this->event = $this->createMock(Event::class); |
88
|
|
|
$this->customer = $this->createMock(Customer::class); |
89
|
|
|
$this->contact = $this->createMock(Contact::class); |
90
|
|
|
|
91
|
|
|
$this->createUpdateContactObserver = new CreateUpdateContactObserver( |
92
|
|
|
$this->configHelper, |
93
|
|
|
$this->contactRepository, |
94
|
|
|
$this->logger, |
95
|
|
|
$this->contactRequestBuilder, |
96
|
|
|
$this->publisher |
97
|
|
|
); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function testExecuteApiNotEnabled() |
101
|
|
|
{ |
102
|
|
|
$this->configHelper->expects($this->once()) |
|
|
|
|
103
|
|
|
->method('isApiEnabled') |
104
|
|
|
->willReturn(false); |
105
|
|
|
|
106
|
|
|
$this->observer->expects($this->never()) |
107
|
|
|
->method('getEvent'); |
|
|
|
|
108
|
|
|
|
109
|
|
|
$this->createUpdateContactObserver->execute($this->observer); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function testExecuteWithUnknownContact() |
113
|
|
|
{ |
114
|
|
|
$customerId = 123; |
115
|
|
|
$contactId = 456; |
116
|
|
|
$request = []; |
117
|
|
|
|
118
|
|
|
$this->configHelper->expects($this->once()) |
119
|
|
|
->method('isApiEnabled') |
120
|
|
|
->willReturn(true); |
121
|
|
|
|
122
|
|
|
$this->observer->expects($this->once()) |
123
|
|
|
->method('getEvent') |
124
|
|
|
->willReturn($this->event); |
|
|
|
|
125
|
|
|
|
126
|
|
|
$this->event->expects($this->once()) |
127
|
|
|
->method('getData') |
128
|
|
|
->with('customer') |
|
|
|
|
129
|
|
|
->willReturn($this->customer); |
130
|
|
|
|
131
|
|
|
$this->customer->expects($this->any()) |
132
|
|
|
->method('getId') |
133
|
|
|
->willReturn($customerId); |
134
|
|
|
|
135
|
|
|
$this->contactRepository->expects($this->once()) |
|
|
|
|
136
|
|
|
->method('getByCustomerId') |
137
|
|
|
->with($customerId) |
138
|
|
|
->willReturn($this->contact); |
139
|
|
|
|
140
|
|
|
$this->contact->expects($this->at(0)) |
|
|
|
|
141
|
|
|
->method('getId') |
|
|
|
|
142
|
|
|
->willReturn(null); |
|
|
|
|
143
|
|
|
|
144
|
|
|
$this->contact->expects($this->once()) |
145
|
|
|
->method('setCustomerId') |
146
|
|
|
->with($customerId) |
|
|
|
|
147
|
|
|
->willReturnSelf(); |
|
|
|
|
148
|
|
|
|
149
|
|
|
$this->contactRepository->expects($this->once()) |
150
|
|
|
->method('save') |
151
|
|
|
->with($this->contact) |
152
|
|
|
->willReturn($this->contact); |
153
|
|
|
|
154
|
|
|
$this->contactRequestBuilder->expects($this->once()) |
|
|
|
|
155
|
|
|
->method('build') |
156
|
|
|
->with($this->customer) |
157
|
|
|
->willReturn($request); |
158
|
|
|
|
159
|
|
|
$this->contact->expects($this->at(1)) |
160
|
|
|
->method('getId') |
161
|
|
|
->willReturn($contactId); |
162
|
|
|
|
163
|
|
|
$this->publisher->expects($this->once()) |
|
|
|
|
164
|
|
|
->method('publish') |
165
|
|
|
->with( |
166
|
|
|
Topics::CREATE_UPDATE, |
167
|
|
|
$this->isInstanceOf(CreateUpdateMessage::class) |
168
|
|
|
); |
169
|
|
|
|
170
|
|
|
$this->createUpdateContactObserver->execute($this->observer); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
public function testExecuteWithExistingContact() |
174
|
|
|
{ |
175
|
|
|
$customerId = 123; |
176
|
|
|
$contactId = 456; |
177
|
|
|
$request = []; |
178
|
|
|
|
179
|
|
|
$this->configHelper->expects($this->once()) |
180
|
|
|
->method('isApiEnabled') |
181
|
|
|
->willReturn(true); |
182
|
|
|
|
183
|
|
|
$this->observer->expects($this->once()) |
184
|
|
|
->method('getEvent') |
185
|
|
|
->willReturn($this->event); |
186
|
|
|
|
187
|
|
|
$this->event->expects($this->once()) |
188
|
|
|
->method('getData') |
189
|
|
|
->with('customer') |
190
|
|
|
->willReturn($this->customer); |
191
|
|
|
|
192
|
|
|
$this->customer->expects($this->any()) |
193
|
|
|
->method('getId') |
194
|
|
|
->willReturn($customerId); |
195
|
|
|
|
196
|
|
|
$this->contactRepository->expects($this->once()) |
197
|
|
|
->method('getByCustomerId') |
198
|
|
|
->with($customerId) |
199
|
|
|
->willReturn($this->contact); |
200
|
|
|
|
201
|
|
|
$this->contact->expects($this->any()) |
202
|
|
|
->method('getId') |
203
|
|
|
->willReturn($contactId); |
204
|
|
|
|
205
|
|
|
$this->contact->expects($this->never()) |
206
|
|
|
->method('setCustomerId'); |
207
|
|
|
|
208
|
|
|
$this->contactRepository->expects($this->never()) |
209
|
|
|
->method('save'); |
210
|
|
|
|
211
|
|
|
$this->contactRequestBuilder->expects($this->once()) |
212
|
|
|
->method('build') |
213
|
|
|
->with($this->customer) |
214
|
|
|
->willReturn($request); |
215
|
|
|
|
216
|
|
|
$this->contact->expects($this->at(1)) |
217
|
|
|
->method('getId') |
218
|
|
|
->willReturn($contactId); |
219
|
|
|
|
220
|
|
|
$this->publisher->expects($this->once()) |
221
|
|
|
->method('publish') |
222
|
|
|
->with( |
223
|
|
|
Topics::CREATE_UPDATE, |
224
|
|
|
$this->isInstanceOf(CreateUpdateMessage::class) |
225
|
|
|
); |
226
|
|
|
|
227
|
|
|
$this->createUpdateContactObserver->execute($this->observer); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
public function testExecuteContactCouldNotSave() |
231
|
|
|
{ |
232
|
|
|
$customerId = 123; |
233
|
|
|
$exception = new CouldNotSaveException(new Phrase('an exception')); |
234
|
|
|
|
235
|
|
|
$this->configHelper->expects($this->once()) |
236
|
|
|
->method('isApiEnabled') |
237
|
|
|
->willReturn(true); |
238
|
|
|
|
239
|
|
|
$this->observer->expects($this->once()) |
240
|
|
|
->method('getEvent') |
241
|
|
|
->willReturn($this->event); |
242
|
|
|
|
243
|
|
|
$this->event->expects($this->once()) |
244
|
|
|
->method('getData') |
245
|
|
|
->with('customer') |
246
|
|
|
->willReturn($this->customer); |
247
|
|
|
|
248
|
|
|
$this->customer->expects($this->any()) |
249
|
|
|
->method('getId') |
250
|
|
|
->willReturn($customerId); |
251
|
|
|
|
252
|
|
|
$this->contactRepository->expects($this->once()) |
253
|
|
|
->method('getByCustomerId') |
254
|
|
|
->with($customerId) |
255
|
|
|
->willReturn($this->contact); |
256
|
|
|
|
257
|
|
|
$this->contact->expects($this->at(0)) |
258
|
|
|
->method('getId') |
259
|
|
|
->willReturn(null); |
260
|
|
|
|
261
|
|
|
$this->contact->expects($this->once()) |
262
|
|
|
->method('setCustomerId') |
263
|
|
|
->with($customerId) |
264
|
|
|
->willReturnSelf(); |
265
|
|
|
|
266
|
|
|
$this->contactRepository->expects($this->once()) |
267
|
|
|
->method('save') |
268
|
|
|
->willThrowException($exception); |
269
|
|
|
|
270
|
|
|
$this->contactRequestBuilder->expects($this->never()) |
271
|
|
|
->method('build'); |
272
|
|
|
|
273
|
|
|
$this->publisher->expects($this->never()) |
274
|
|
|
->method('publish'); |
275
|
|
|
|
276
|
|
|
$this->logger->expects($this->once()) |
|
|
|
|
277
|
|
|
->method('critical') |
278
|
|
|
->with($exception); |
279
|
|
|
|
280
|
|
|
$this->createUpdateContactObserver->execute($this->observer); |
281
|
|
|
} |
282
|
|
|
} |
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.