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 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
|
|
|
|
23
|
|
|
class ExportCustomerConsumerTest extends TestCase |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var MockObject|MagentoCustomerRepositoryInterface |
27
|
|
|
*/ |
28
|
|
|
protected $magentoCustomerRepository; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var MockObject|Logger |
32
|
|
|
*/ |
33
|
|
|
protected $logger; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var MockObject|CustomerRepositoryInterface |
37
|
|
|
*/ |
38
|
|
|
protected $customerRepository; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var MockObject|CustomerRequestBuilder |
42
|
|
|
*/ |
43
|
|
|
protected $customerRequestBuilder; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var MockObject|Client |
47
|
|
|
*/ |
48
|
|
|
protected $client; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var MockObject|MagentoCustomerInterface |
52
|
|
|
*/ |
53
|
|
|
protected $magentoCustomer; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var MockObject|CustomerInterface |
57
|
|
|
*/ |
58
|
|
|
protected $customer; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @var MockObject|CustomerApiResourceInterface |
62
|
|
|
*/ |
63
|
|
|
protected $customerApi; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @var ExportCustomerConsumer |
67
|
|
|
*/ |
68
|
|
|
protected $exportCustomerConsumer; |
69
|
|
|
|
70
|
|
|
protected function setUp() |
71
|
|
|
{ |
72
|
|
|
$this->magentoCustomerRepository = $this->createMock(MagentoCustomerRepositoryInterface::class); |
73
|
|
|
$this->logger = $this->createMock(Logger::class); |
74
|
|
|
$this->customerRepository = $this->createMock(CustomerRepositoryInterface::class); |
75
|
|
|
$this->customerRequestBuilder = $this->createMock(CustomerRequestBuilder::class); |
76
|
|
|
$this->client = $this->createMock(Client::class); |
77
|
|
|
$this->customer = $this->createMock(CustomerInterface::class); |
78
|
|
|
$this->customerApi = $this->createMock(CustomerApiResourceInterface::class); |
79
|
|
|
$this->magentoCustomer = $this->createMock(MagentoCustomerInterface::class); |
80
|
|
|
|
81
|
|
|
$this->exportCustomerConsumer = new ExportCustomerConsumer( |
82
|
|
|
$this->magentoCustomerRepository, |
83
|
|
|
$this->logger, |
84
|
|
|
$this->customerRepository, |
85
|
|
|
$this->customerRequestBuilder, |
86
|
|
|
$this->client |
87
|
|
|
); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function testConsumeWithAbsentMagentoCustomer() |
91
|
|
|
{ |
92
|
|
|
$magentoCustomerId = 123; |
93
|
|
|
|
94
|
|
|
$exceptionMessage = 'an exception message'; |
95
|
|
|
$exception = new NoSuchEntityException(new Phrase($exceptionMessage)); |
96
|
|
|
|
97
|
|
|
$this->magentoCustomerRepository->expects($this->once()) |
|
|
|
|
98
|
|
|
->method('getById') |
99
|
|
|
->with($magentoCustomerId) |
100
|
|
|
->willThrowException($exception); |
101
|
|
|
|
102
|
|
|
$this->logger->expects($this->once()) |
|
|
|
|
103
|
|
|
->method('error') |
104
|
|
|
->with($exceptionMessage); |
105
|
|
|
|
106
|
|
|
$this->customerRepository->expects($this->never()) |
|
|
|
|
107
|
|
|
->method('getOrCreateByMagentoCustomerId'); |
108
|
|
|
|
109
|
|
|
$this->exportCustomerConsumer->consume(json_encode(['magento_customer_id' => $magentoCustomerId])); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function testConsumeApiRequestException() |
113
|
|
|
{ |
114
|
|
|
$magentoCustomerId = 123; |
115
|
|
|
$request = ['request']; |
116
|
|
|
|
117
|
|
|
$this->magentoCustomerRepository->expects($this->once()) |
118
|
|
|
->method('getById') |
119
|
|
|
->with($magentoCustomerId) |
120
|
|
|
->willReturn($this->magentoCustomer); |
121
|
|
|
|
122
|
|
|
$this->magentoCustomer->expects($this->once()) |
|
|
|
|
123
|
|
|
->method('getId') |
124
|
|
|
->willReturn($magentoCustomerId); |
125
|
|
|
|
126
|
|
|
$this->customerRequestBuilder->expects($this->once()) |
|
|
|
|
127
|
|
|
->method('build') |
128
|
|
|
->with($this->magentoCustomer) |
129
|
|
|
->willReturn($request); |
130
|
|
|
|
131
|
|
|
$this->client->expects($this->once()) |
|
|
|
|
132
|
|
|
->method('getCustomerApi') |
133
|
|
|
->willReturn($this->customerApi); |
134
|
|
|
|
135
|
|
|
/** @var MockObject|HttpException $httpException */ |
136
|
|
|
$httpException = $this->createMock(HttpException::class); |
137
|
|
|
|
138
|
|
|
$this->customerApi->expects($this->once()) |
|
|
|
|
139
|
|
|
->method('create') |
140
|
|
|
->with(['ecomCustomer' => $request]) |
141
|
|
|
->willThrowException($httpException); |
142
|
|
|
|
143
|
|
|
$this->logger->expects($this->once()) |
144
|
|
|
->method('error'); |
145
|
|
|
|
146
|
|
|
$this->customer->expects($this->never()) |
|
|
|
|
147
|
|
|
->method('setActiveCampaignId'); |
148
|
|
|
|
149
|
|
|
$this->exportCustomerConsumer->consume(json_encode(['magento_customer_id' => $magentoCustomerId])); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
public function testConsumeUpdate() |
153
|
|
|
{ |
154
|
|
|
$magentoCustomerId = 123; |
155
|
|
|
$request = ['request']; |
156
|
|
|
$activeCampaignId = 456; |
157
|
|
|
$response = ['ecomCustomer' => ['id' => $activeCampaignId]]; |
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->customerRepository->expects($this->once()) |
169
|
|
|
->method('getOrCreateByMagentoCustomerId') |
170
|
|
|
->willReturn($this->customer); |
171
|
|
|
|
172
|
|
|
$this->customerRequestBuilder->expects($this->once()) |
173
|
|
|
->method('build') |
174
|
|
|
->with($this->magentoCustomer) |
175
|
|
|
->willReturn($request); |
176
|
|
|
|
177
|
|
|
$this->customer->expects($this->once()) |
178
|
|
|
->method('getActiveCampaignId') |
179
|
|
|
->willReturn($activeCampaignId); |
180
|
|
|
|
181
|
|
|
$this->client->expects($this->once()) |
182
|
|
|
->method('getCustomerApi') |
183
|
|
|
->willReturn($this->customerApi); |
184
|
|
|
|
185
|
|
|
$this->customerApi->expects($this->once()) |
186
|
|
|
->method('update') |
187
|
|
|
->with($activeCampaignId, ['ecomCustomer' => $request]) |
188
|
|
|
->willReturn($response); |
189
|
|
|
|
190
|
|
|
$this->customer->expects($this->once()) |
191
|
|
|
->method('setActiveCampaignId') |
192
|
|
|
->with($activeCampaignId) |
193
|
|
|
->willReturnSelf(); |
194
|
|
|
|
195
|
|
|
$this->customerRepository->expects($this->once()) |
196
|
|
|
->method('save') |
197
|
|
|
->with($this->customer); |
198
|
|
|
|
199
|
|
|
$this->exportCustomerConsumer->consume(json_encode(['magento_customer_id' => $magentoCustomerId])); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
public function testConsumeCreate() |
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(null); |
230
|
|
|
|
231
|
|
|
$this->client->expects($this->once()) |
232
|
|
|
->method('getCustomerApi') |
233
|
|
|
->willReturn($this->customerApi); |
234
|
|
|
|
235
|
|
|
$this->customerApi->expects($this->once()) |
236
|
|
|
->method('create') |
237
|
|
|
->with(['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
|
|
|
|
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.