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 PHPUnit\Framework\MockObject\MockObject; |
14
|
|
|
use PHPUnit\Framework\TestCase; |
15
|
|
|
use CommerceLeague\ActiveCampaign\Model\ResourceModel\Contact as ContactResource; |
16
|
|
|
use CommerceLeague\ActiveCampaign\Model\ContactFactory; |
|
|
|
|
17
|
|
|
|
18
|
|
|
class ContactRepositoryTest extends TestCase |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var MockObject|ContactResource |
22
|
|
|
*/ |
23
|
|
|
protected $contactResource; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var MockObject|Contact |
27
|
|
|
*/ |
28
|
|
|
protected $contact; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var MockObject|ContactFactory |
32
|
|
|
*/ |
33
|
|
|
protected $contactFactory; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var MockObject|Customer |
37
|
|
|
*/ |
38
|
|
|
protected $customer; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var ContactRepository |
42
|
|
|
*/ |
43
|
|
|
protected $contactRepository; |
44
|
|
|
|
45
|
|
|
protected function setUp() |
46
|
|
|
{ |
47
|
|
|
$this->contactResource = $this->getMockBuilder(ContactResource::class) |
48
|
|
|
->disableOriginalConstructor() |
49
|
|
|
->getMock(); |
50
|
|
|
|
51
|
|
|
$this->contactFactory = $this->getMockBuilder(ContactFactory::class) |
52
|
|
|
->disableOriginalConstructor() |
53
|
|
|
->setMethods(['create']) |
54
|
|
|
->getMock(); |
55
|
|
|
|
56
|
|
|
$this->contact = $this->getMockBuilder(Contact::class) |
57
|
|
|
->disableOriginalConstructor() |
58
|
|
|
->getMock(); |
59
|
|
|
|
60
|
|
|
$this->contactFactory->expects($this->any()) |
61
|
|
|
->method('create') |
62
|
|
|
->willReturn($this->contact); |
63
|
|
|
|
64
|
|
|
$this->customer = $this->getMockBuilder(Customer::class) |
65
|
|
|
->disableOriginalConstructor() |
66
|
|
|
->getMock(); |
67
|
|
|
|
68
|
|
|
$this->contactRepository = new ContactRepository( |
69
|
|
|
$this->contactResource, |
70
|
|
|
$this->contactFactory |
71
|
|
|
); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function testSaveThrowsException() |
75
|
|
|
{ |
76
|
|
|
$this->contactResource->expects($this->once()) |
|
|
|
|
77
|
|
|
->method('save') |
78
|
|
|
->with($this->contact) |
79
|
|
|
->willThrowException(new \Exception('an exception message')); |
80
|
|
|
|
81
|
|
|
$this->expectException(CouldNotSaveException::class); |
82
|
|
|
$this->expectExceptionMessage('an exception message'); |
83
|
|
|
|
84
|
|
|
$this->contactRepository->save($this->contact); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function testSave() |
88
|
|
|
{ |
89
|
|
|
$this->contactResource->expects($this->once()) |
90
|
|
|
->method('save') |
91
|
|
|
->with($this->contact) |
92
|
|
|
->willReturnSelf(); |
93
|
|
|
|
94
|
|
|
$this->assertEquals($this->contact, $this->contactRepository->save($this->contact)); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function testGetById() |
98
|
|
|
{ |
99
|
|
|
$contactId = 123; |
100
|
|
|
$this->assertEquals($this->contact, $this->contactRepository->getById($contactId)); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function testGetByCustomerId() |
104
|
|
|
{ |
105
|
|
|
$customerId = 456; |
106
|
|
|
|
107
|
|
|
$this->contactResource->expects($this->once()) |
108
|
|
|
->method('load') |
109
|
|
|
->with($this->contact, $customerId, 'customer_id') |
110
|
|
|
->willReturn($this->contact); |
111
|
|
|
|
112
|
|
|
$this->assertEquals($this->contact, $this->contactRepository->getByCustomerId($customerId)); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function testGetOrCreateByCustomerCreatesContact() |
116
|
|
|
{ |
117
|
|
|
$customerId = 456; |
118
|
|
|
|
119
|
|
|
$this->customer->expects($this->any()) |
120
|
|
|
->method('getId') |
|
|
|
|
121
|
|
|
->willReturn($customerId); |
|
|
|
|
122
|
|
|
|
123
|
|
|
$this->contactResource->expects($this->once()) |
124
|
|
|
->method('load') |
125
|
|
|
->with($this->contact, $customerId, 'customer_id') |
126
|
|
|
->willReturn($this->contact); |
127
|
|
|
|
128
|
|
|
$this->contact->expects($this->once()) |
|
|
|
|
129
|
|
|
->method('getId') |
|
|
|
|
130
|
|
|
->willReturn(null); |
|
|
|
|
131
|
|
|
|
132
|
|
|
$this->contact->expects($this->once()) |
133
|
|
|
->method('setCustomerId') |
134
|
|
|
->with($customerId) |
|
|
|
|
135
|
|
|
->willReturnSelf(); |
|
|
|
|
136
|
|
|
|
137
|
|
|
$this->contactResource->expects($this->once()) |
138
|
|
|
->method('save') |
139
|
|
|
->with($this->contact) |
140
|
|
|
->willReturnSelf(); |
141
|
|
|
|
142
|
|
|
$this->assertSame($this->contact, $this->contactRepository->getOrCreateByCustomer($this->customer)); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
public function testGetOrCreateByCustomerLoadsContact() |
146
|
|
|
{ |
147
|
|
|
$customerId = 456; |
148
|
|
|
$contactId = 678; |
149
|
|
|
|
150
|
|
|
$this->customer->expects($this->any()) |
151
|
|
|
->method('getId') |
152
|
|
|
->willReturn($customerId); |
153
|
|
|
|
154
|
|
|
$this->contactResource->expects($this->once()) |
155
|
|
|
->method('load') |
156
|
|
|
->with($this->contact, $customerId, 'customer_id') |
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('setCustomerId'); |
165
|
|
|
|
166
|
|
|
$this->contactResource->expects($this->never()) |
167
|
|
|
->method('save'); |
168
|
|
|
|
169
|
|
|
$this->assertSame($this->contact, $this->contactRepository->getOrCreateByCustomer($this->customer)); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
|
173
|
|
|
public function testDeleteThrowsException() |
174
|
|
|
{ |
175
|
|
|
$this->contactResource->expects($this->once()) |
176
|
|
|
->method('delete') |
177
|
|
|
->with($this->contact) |
178
|
|
|
->willThrowException(new \Exception('an exception message')); |
179
|
|
|
|
180
|
|
|
$this->expectException(CouldNotDeleteException::class); |
181
|
|
|
$this->expectExceptionMessage('an exception message'); |
182
|
|
|
|
183
|
|
|
$this->contactRepository->delete($this->contact); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
public function testDelete() |
187
|
|
|
{ |
188
|
|
|
$this->contactResource->expects($this->once()) |
189
|
|
|
->method('delete') |
190
|
|
|
->with($this->contact) |
191
|
|
|
->willReturnSelf(); |
192
|
|
|
|
193
|
|
|
$this->assertTrue($this->contactRepository->delete($this->contact)); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
public function testDeleteByIdThrowsException() |
197
|
|
|
{ |
198
|
|
|
$contactId = 123; |
199
|
|
|
|
200
|
|
|
$this->contact->expects($this->once()) |
201
|
|
|
->method('getId') |
202
|
|
|
->willReturn(null); |
203
|
|
|
|
204
|
|
|
$this->contactResource->expects($this->once()) |
205
|
|
|
->method('load') |
206
|
|
|
->with($this->contact, $contactId) |
207
|
|
|
->willReturn($this->contact); |
208
|
|
|
|
209
|
|
|
$this->contactResource->expects($this->never()) |
210
|
|
|
->method('delete'); |
211
|
|
|
|
212
|
|
|
$this->expectException(NoSuchEntityException::class); |
213
|
|
|
$this->expectExceptionMessage('The Contact with the "123" ID doesn\'t exist'); |
214
|
|
|
|
215
|
|
|
$this->contactRepository->deleteById($contactId); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
public function testDeleteById() |
219
|
|
|
{ |
220
|
|
|
$contactId = 123; |
221
|
|
|
|
222
|
|
|
$this->contact->expects($this->once()) |
223
|
|
|
->method('getId') |
224
|
|
|
->willReturn($contactId); |
225
|
|
|
|
226
|
|
|
$this->contactResource->expects($this->once()) |
227
|
|
|
->method('load') |
228
|
|
|
->with($this->contact, $contactId) |
229
|
|
|
->willReturn($this->contact); |
230
|
|
|
|
231
|
|
|
$this->contactResource->expects($this->once()) |
232
|
|
|
->method('delete') |
233
|
|
|
->with($this->contact) |
234
|
|
|
->willReturnSelf(); |
235
|
|
|
|
236
|
|
|
$this->assertTrue($this->contactRepository->deleteById($contactId)); |
237
|
|
|
} |
238
|
|
|
} |
239
|
|
|
|
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