1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
*/ |
4
|
|
|
|
5
|
|
|
namespace CommerceLeague\ActiveCampaign\Test\Unit\Model; |
6
|
|
|
|
7
|
|
|
use CommerceLeague\ActiveCampaign\Model\Contact; |
8
|
|
|
use CommerceLeague\ActiveCampaign\Model\ContactRepository; |
9
|
|
|
use Magento\Customer\Model\Customer; |
10
|
|
|
use Magento\Framework\Exception\CouldNotDeleteException; |
11
|
|
|
use Magento\Framework\Exception\CouldNotSaveException; |
12
|
|
|
use Magento\Framework\Exception\NoSuchEntityException; |
13
|
|
|
use Magento\Newsletter\Model\Subscriber; |
14
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
15
|
|
|
use PHPUnit\Framework\TestCase; |
16
|
|
|
use CommerceLeague\ActiveCampaign\Model\ResourceModel\Contact as ContactResource; |
17
|
|
|
use CommerceLeague\ActiveCampaign\Model\ContactFactory; |
|
|
|
|
18
|
|
|
|
19
|
|
|
class ContactRepositoryTest extends TestCase |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var MockObject|ContactResource |
23
|
|
|
*/ |
24
|
|
|
protected $contactResource; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var MockObject|Contact |
28
|
|
|
*/ |
29
|
|
|
protected $contact; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var MockObject|ContactFactory |
33
|
|
|
*/ |
34
|
|
|
protected $contactFactory; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var MockObject|Customer |
38
|
|
|
*/ |
39
|
|
|
protected $customer; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var MockObject|Subscriber |
43
|
|
|
*/ |
44
|
|
|
protected $subscriber; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var ContactRepository |
48
|
|
|
*/ |
49
|
|
|
protected $contactRepository; |
50
|
|
|
|
51
|
|
|
protected function setUp() |
52
|
|
|
{ |
53
|
|
|
$this->contactResource = $this->getMockBuilder(ContactResource::class) |
54
|
|
|
->disableOriginalConstructor() |
55
|
|
|
->getMock(); |
56
|
|
|
|
57
|
|
|
$this->contactFactory = $this->getMockBuilder(ContactFactory::class) |
58
|
|
|
->disableOriginalConstructor() |
59
|
|
|
->setMethods(['create']) |
60
|
|
|
->getMock(); |
61
|
|
|
|
62
|
|
|
$this->contact = $this->getMockBuilder(Contact::class) |
63
|
|
|
->disableOriginalConstructor() |
64
|
|
|
->getMock(); |
65
|
|
|
|
66
|
|
|
$this->contactFactory->expects($this->any()) |
67
|
|
|
->method('create') |
68
|
|
|
->willReturn($this->contact); |
69
|
|
|
|
70
|
|
|
$this->customer = $this->getMockBuilder(Customer::class) |
71
|
|
|
->disableOriginalConstructor() |
72
|
|
|
->getMock(); |
73
|
|
|
|
74
|
|
|
$this->subscriber = $this->getMockBuilder(Subscriber::class) |
75
|
|
|
->disableOriginalConstructor() |
76
|
|
|
->getMock(); |
77
|
|
|
|
78
|
|
|
$this->contactRepository = new ContactRepository( |
79
|
|
|
$this->contactResource, |
80
|
|
|
$this->contactFactory |
81
|
|
|
); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function testSaveThrowsException() |
85
|
|
|
{ |
86
|
|
|
$this->contactResource->expects($this->once()) |
|
|
|
|
87
|
|
|
->method('save') |
88
|
|
|
->with($this->contact) |
89
|
|
|
->willThrowException(new \Exception('an exception message')); |
90
|
|
|
|
91
|
|
|
$this->expectException(CouldNotSaveException::class); |
92
|
|
|
$this->expectExceptionMessage('an exception message'); |
93
|
|
|
|
94
|
|
|
$this->contactRepository->save($this->contact); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function testSave() |
98
|
|
|
{ |
99
|
|
|
$this->contactResource->expects($this->once()) |
100
|
|
|
->method('save') |
101
|
|
|
->with($this->contact) |
102
|
|
|
->willReturnSelf(); |
103
|
|
|
|
104
|
|
|
$this->assertEquals($this->contact, $this->contactRepository->save($this->contact)); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function testGetById() |
108
|
|
|
{ |
109
|
|
|
$contactId = 123; |
110
|
|
|
$this->assertEquals($this->contact, $this->contactRepository->getById($contactId)); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function testGetOrCreateByCustomerCreatesContact() |
114
|
|
|
{ |
115
|
|
|
$email = '[email protected]'; |
116
|
|
|
|
117
|
|
|
$this->customer->expects($this->any()) |
118
|
|
|
->method('getData') |
|
|
|
|
119
|
|
|
->with('email') |
|
|
|
|
120
|
|
|
->willReturn($email); |
|
|
|
|
121
|
|
|
|
122
|
|
|
$this->contactResource->expects($this->once()) |
123
|
|
|
->method('load') |
124
|
|
|
->with($this->contact, $email, 'email') |
125
|
|
|
->willReturn($this->contact); |
126
|
|
|
|
127
|
|
|
$this->contact->expects($this->once()) |
|
|
|
|
128
|
|
|
->method('getId') |
|
|
|
|
129
|
|
|
->willReturn(null); |
|
|
|
|
130
|
|
|
|
131
|
|
|
$this->contact->expects($this->once()) |
132
|
|
|
->method('setEmail') |
133
|
|
|
->with($email) |
|
|
|
|
134
|
|
|
->willReturnSelf(); |
|
|
|
|
135
|
|
|
|
136
|
|
|
$this->contactResource->expects($this->once()) |
137
|
|
|
->method('save') |
138
|
|
|
->with($this->contact) |
139
|
|
|
->willReturnSelf(); |
140
|
|
|
|
141
|
|
|
$this->assertSame($this->contact, $this->contactRepository->getOrCreateByCustomer($this->customer)); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
public function testGetOrCreateByCustomerLoadsContact() |
145
|
|
|
{ |
146
|
|
|
$contactId = 678; |
147
|
|
|
$email = '[email protected]'; |
148
|
|
|
|
149
|
|
|
$this->customer->expects($this->any()) |
150
|
|
|
->method('getData') |
151
|
|
|
->with('email') |
152
|
|
|
->willReturn($email); |
153
|
|
|
|
154
|
|
|
$this->contactResource->expects($this->once()) |
155
|
|
|
->method('load') |
156
|
|
|
->with($this->contact, $email, 'email') |
157
|
|
|
->willReturn($this->contact); |
158
|
|
|
|
159
|
|
|
$this->contact->expects($this->once()) |
160
|
|
|
->method('getId') |
161
|
|
|
->willReturn($contactId); |
162
|
|
|
|
163
|
|
|
$this->contact->expects($this->never()) |
164
|
|
|
->method('setEmail'); |
165
|
|
|
|
166
|
|
|
$this->contactResource->expects($this->never()) |
167
|
|
|
->method('save'); |
168
|
|
|
|
169
|
|
|
$this->assertSame($this->contact, $this->contactRepository->getOrCreateByCustomer($this->customer)); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
public function testGetOrCreateBySubscriberCreatesContact() |
173
|
|
|
{ |
174
|
|
|
$email = '[email protected]'; |
175
|
|
|
|
176
|
|
|
$this->subscriber->expects($this->any()) |
177
|
|
|
->method('getEmail') |
178
|
|
|
->willReturn($email); |
179
|
|
|
|
180
|
|
|
$this->contactResource->expects($this->once()) |
181
|
|
|
->method('load') |
182
|
|
|
->with($this->contact, $email, 'email') |
183
|
|
|
->willReturn($this->contact); |
184
|
|
|
|
185
|
|
|
$this->contact->expects($this->once()) |
186
|
|
|
->method('getId') |
187
|
|
|
->willReturn(null); |
188
|
|
|
|
189
|
|
|
$this->contact->expects($this->once()) |
190
|
|
|
->method('setEmail') |
191
|
|
|
->with($email) |
192
|
|
|
->willReturnSelf(); |
193
|
|
|
|
194
|
|
|
$this->contactResource->expects($this->once()) |
195
|
|
|
->method('save') |
196
|
|
|
->with($this->contact) |
197
|
|
|
->willReturnSelf(); |
198
|
|
|
|
199
|
|
|
$this->assertSame($this->contact, $this->contactRepository->getOrCreateBySubscriber($this->subscriber)); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
|
203
|
|
|
public function testGetOrCreateBySubscriberLoadsContact() |
204
|
|
|
{ |
205
|
|
|
$contactId = 678; |
206
|
|
|
$email = '[email protected]'; |
207
|
|
|
|
208
|
|
|
$this->subscriber->expects($this->any()) |
209
|
|
|
->method('getEmail') |
210
|
|
|
->willReturn($email); |
211
|
|
|
|
212
|
|
|
$this->contactResource->expects($this->once()) |
213
|
|
|
->method('load') |
214
|
|
|
->with($this->contact, $email, 'email') |
215
|
|
|
->willReturn($this->contact); |
216
|
|
|
|
217
|
|
|
$this->contact->expects($this->once()) |
218
|
|
|
->method('getId') |
219
|
|
|
->willReturn($contactId); |
220
|
|
|
|
221
|
|
|
$this->contact->expects($this->never()) |
222
|
|
|
->method('setEmail'); |
223
|
|
|
|
224
|
|
|
$this->contactResource->expects($this->never()) |
225
|
|
|
->method('save'); |
226
|
|
|
|
227
|
|
|
$this->assertSame($this->contact, $this->contactRepository->getOrCreateBySubscriber($this->subscriber)); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
|
231
|
|
|
public function testDeleteThrowsException() |
232
|
|
|
{ |
233
|
|
|
$this->contactResource->expects($this->once()) |
234
|
|
|
->method('delete') |
235
|
|
|
->with($this->contact) |
236
|
|
|
->willThrowException(new \Exception('an exception message')); |
237
|
|
|
|
238
|
|
|
$this->expectException(CouldNotDeleteException::class); |
239
|
|
|
$this->expectExceptionMessage('an exception message'); |
240
|
|
|
|
241
|
|
|
$this->contactRepository->delete($this->contact); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
public function testDelete() |
245
|
|
|
{ |
246
|
|
|
$this->contactResource->expects($this->once()) |
247
|
|
|
->method('delete') |
248
|
|
|
->with($this->contact) |
249
|
|
|
->willReturnSelf(); |
250
|
|
|
|
251
|
|
|
$this->assertTrue($this->contactRepository->delete($this->contact)); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
public function testDeleteByIdThrowsException() |
255
|
|
|
{ |
256
|
|
|
$contactId = 123; |
257
|
|
|
|
258
|
|
|
$this->contact->expects($this->once()) |
259
|
|
|
->method('getId') |
260
|
|
|
->willReturn(null); |
261
|
|
|
|
262
|
|
|
$this->contactResource->expects($this->once()) |
263
|
|
|
->method('load') |
264
|
|
|
->with($this->contact, $contactId) |
265
|
|
|
->willReturn($this->contact); |
266
|
|
|
|
267
|
|
|
$this->contactResource->expects($this->never()) |
268
|
|
|
->method('delete'); |
269
|
|
|
|
270
|
|
|
$this->expectException(NoSuchEntityException::class); |
271
|
|
|
$this->expectExceptionMessage('The Contact with the "123" ID doesn\'t exist'); |
272
|
|
|
|
273
|
|
|
$this->contactRepository->deleteById($contactId); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
public function testDeleteById() |
277
|
|
|
{ |
278
|
|
|
$contactId = 123; |
279
|
|
|
|
280
|
|
|
$this->contact->expects($this->once()) |
281
|
|
|
->method('getId') |
282
|
|
|
->willReturn($contactId); |
283
|
|
|
|
284
|
|
|
$this->contactResource->expects($this->once()) |
285
|
|
|
->method('load') |
286
|
|
|
->with($this->contact, $contactId) |
287
|
|
|
->willReturn($this->contact); |
288
|
|
|
|
289
|
|
|
$this->contactResource->expects($this->once()) |
290
|
|
|
->method('delete') |
291
|
|
|
->with($this->contact) |
292
|
|
|
->willReturnSelf(); |
293
|
|
|
|
294
|
|
|
$this->assertTrue($this->contactRepository->deleteById($contactId)); |
295
|
|
|
} |
296
|
|
|
} |
297
|
|
|
|
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