1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
*/ |
4
|
|
|
|
5
|
|
|
namespace CommerceLeague\ActiveCampaign\Test\Unit\Service\Contact; |
6
|
|
|
|
7
|
|
|
use CommerceLeague\ActiveCampaign\Api\ContactRepositoryInterface; |
8
|
|
|
use CommerceLeague\ActiveCampaign\Api\Data\ContactInterface; |
9
|
|
|
use CommerceLeague\ActiveCampaign\Gateway\Request\ContactRequestBuilder; |
10
|
|
|
use CommerceLeague\ActiveCampaign\Logger\Logger; |
11
|
|
|
use CommerceLeague\ActiveCampaign\MessageQueue\Contact\CreateUpdateMessage; |
12
|
|
|
use CommerceLeague\ActiveCampaign\MessageQueue\Contact\CreateUpdatePublisher; |
13
|
|
|
use CommerceLeague\ActiveCampaign\Service\Contact\CreateUpdateContactService; |
14
|
|
|
use Magento\Customer\Model\Customer; |
15
|
|
|
use Magento\Framework\Exception\CouldNotSaveException; |
16
|
|
|
use Magento\Framework\Phrase; |
17
|
|
|
use Magento\Newsletter\Model\Subscriber; |
18
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
19
|
|
|
use PHPUnit\Framework\TestCase; |
20
|
|
|
|
21
|
|
|
class CreateUpdateContactServiceTest extends TestCase |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var MockObject|ContactRepositoryInterface |
25
|
|
|
*/ |
26
|
|
|
protected $contactRepository; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var MockObject|Logger |
30
|
|
|
*/ |
31
|
|
|
protected $logger; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var MockObject|ContactRequestBuilder |
35
|
|
|
*/ |
36
|
|
|
protected $contactRequestBuilder; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var MockObject|CreateUpdatePublisher |
40
|
|
|
*/ |
41
|
|
|
protected $createUpdatePublisher; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var MockObject|Customer |
45
|
|
|
*/ |
46
|
|
|
protected $customer; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var MockObject|Subscriber |
50
|
|
|
*/ |
51
|
|
|
protected $subscriber; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var MockObject|ContactInterface |
55
|
|
|
*/ |
56
|
|
|
protected $contact; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var CreateUpdateContactService |
60
|
|
|
*/ |
61
|
|
|
protected $createUpdateContactService; |
62
|
|
|
|
63
|
|
|
protected function setUp() |
64
|
|
|
{ |
65
|
|
|
$this->contactRepository = $this->createMock(ContactRepositoryInterface::class); |
66
|
|
|
$this->logger = $this->createMock(Logger::class); |
67
|
|
|
$this->contactRequestBuilder = $this->createMock(ContactRequestBuilder::class); |
68
|
|
|
$this->createUpdatePublisher = $this->createMock(CreateUpdatePublisher::class); |
69
|
|
|
$this->customer = $this->createMock(Customer::class); |
70
|
|
|
$this->subscriber = $this->createMock(Subscriber::class); |
71
|
|
|
$this->contact = $this->createMock(ContactInterface::class); |
72
|
|
|
|
73
|
|
|
$this->createUpdateContactService = new CreateUpdateContactService( |
74
|
|
|
$this->contactRepository, |
75
|
|
|
$this->logger, |
76
|
|
|
$this->contactRequestBuilder, |
77
|
|
|
$this->createUpdatePublisher |
78
|
|
|
); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function testExecuteWithCustomerContactCouldNotSave() |
82
|
|
|
{ |
83
|
|
|
$exception = new CouldNotSaveException(new Phrase('an exception')); |
84
|
|
|
|
85
|
|
|
$this->contactRepository->expects($this->once()) |
|
|
|
|
86
|
|
|
->method('getOrCreateByCustomer') |
87
|
|
|
->willThrowException($exception); |
88
|
|
|
|
89
|
|
|
$this->logger->expects($this->once()) |
|
|
|
|
90
|
|
|
->method('critical') |
91
|
|
|
->with($exception); |
92
|
|
|
|
93
|
|
|
$this->contactRequestBuilder->expects($this->never()) |
|
|
|
|
94
|
|
|
->method('buildWithCustomer'); |
95
|
|
|
|
96
|
|
|
$this->createUpdatePublisher->expects($this->never()) |
|
|
|
|
97
|
|
|
->method('publish'); |
98
|
|
|
|
99
|
|
|
$this->createUpdateContactService->executeWithCustomer($this->customer); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function testExecuteWithCustomer() |
103
|
|
|
{ |
104
|
|
|
$contactId = 123; |
105
|
|
|
|
106
|
|
|
$this->contactRepository->expects($this->once()) |
107
|
|
|
->method('getOrCreateByCustomer') |
108
|
|
|
->willReturn($this->contact); |
109
|
|
|
|
110
|
|
|
$this->contact->expects($this->once()) |
|
|
|
|
111
|
|
|
->method('getId') |
112
|
|
|
->willReturn($contactId); |
113
|
|
|
|
114
|
|
|
$this->contactRequestBuilder->expects($this->once()) |
115
|
|
|
->method('buildWithCustomer') |
116
|
|
|
->with($this->customer) |
117
|
|
|
->willReturn(['request']); |
118
|
|
|
|
119
|
|
|
$this->createUpdatePublisher->expects($this->once()) |
120
|
|
|
->method('publish') |
121
|
|
|
->with($this->isInstanceOf(CreateUpdateMessage::class)); |
122
|
|
|
|
123
|
|
|
$this->createUpdateContactService->executeWithCustomer($this->customer); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function testExecuteWithSubscriberContactCouldNotSave() |
127
|
|
|
{ |
128
|
|
|
$exception = new CouldNotSaveException(new Phrase('an exception')); |
129
|
|
|
|
130
|
|
|
$this->contactRepository->expects($this->once()) |
131
|
|
|
->method('getOrCreateBySubscriber') |
132
|
|
|
->willThrowException($exception); |
133
|
|
|
|
134
|
|
|
$this->logger->expects($this->once()) |
135
|
|
|
->method('critical') |
136
|
|
|
->with($exception); |
137
|
|
|
|
138
|
|
|
$this->contactRequestBuilder->expects($this->never()) |
139
|
|
|
->method('buildWithSubscriber'); |
140
|
|
|
|
141
|
|
|
$this->createUpdatePublisher->expects($this->never()) |
142
|
|
|
->method('publish'); |
143
|
|
|
|
144
|
|
|
$this->createUpdateContactService->executeWithSubscriber($this->subscriber); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
public function testExecuteWithSubscriber() |
148
|
|
|
{ |
149
|
|
|
$contactId = 123; |
150
|
|
|
|
151
|
|
|
$this->contactRepository->expects($this->once()) |
152
|
|
|
->method('getOrCreateBySubscriber') |
153
|
|
|
->willReturn($this->contact); |
154
|
|
|
|
155
|
|
|
$this->contact->expects($this->once()) |
156
|
|
|
->method('getId') |
157
|
|
|
->willReturn($contactId); |
158
|
|
|
|
159
|
|
|
$this->contactRequestBuilder->expects($this->once()) |
160
|
|
|
->method('buildWithSubscriber') |
161
|
|
|
->with($this->subscriber) |
162
|
|
|
->willReturn(['request']); |
163
|
|
|
|
164
|
|
|
$this->createUpdatePublisher->expects($this->once()) |
165
|
|
|
->method('publish') |
166
|
|
|
->with($this->isInstanceOf(CreateUpdateMessage::class)); |
167
|
|
|
|
168
|
|
|
$this->createUpdateContactService->executeWithSubscriber($this->subscriber); |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|
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.