|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
*/ |
|
4
|
|
|
|
|
5
|
|
|
namespace CommerceLeague\ActiveCampaign\Test\Unit\Observer\Customer; |
|
6
|
|
|
|
|
7
|
|
|
use CommerceLeague\ActiveCampaign\Api\ContactRepositoryInterface; |
|
8
|
|
|
use CommerceLeague\ActiveCampaign\Api\Data\ContactInterface; |
|
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\CreateUpdateMessageBuilder; |
|
13
|
|
|
use CommerceLeague\ActiveCampaign\MessageQueue\Contact\CreateUpdatePublisher; |
|
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\Phrase; |
|
20
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
|
21
|
|
|
use PHPUnit\Framework\TestCase; |
|
22
|
|
|
|
|
23
|
|
|
class CreateUpdateContactObserverTest extends TestCase |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @var MockObject|ConfigHelper |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $configHelper; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var MockObject|ContactRepositoryInterface |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $contactRepository; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var MockObject|Logger |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $logger; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var MockObject|CreateUpdateMessageBuilder |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $createUpdateMessageBuilder; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var MockObject|CreateUpdatePublisher |
|
47
|
|
|
*/ |
|
48
|
|
|
protected $createUpdatePublisher; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @var MockObject|Observer |
|
52
|
|
|
*/ |
|
53
|
|
|
protected $observer; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @var MockObject|Event |
|
57
|
|
|
*/ |
|
58
|
|
|
protected $event; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @var MockObject|Customer |
|
62
|
|
|
*/ |
|
63
|
|
|
protected $customer; |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @var MockObject|ContactInterface |
|
67
|
|
|
*/ |
|
68
|
|
|
protected $contact; |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @var MockObject|CreateUpdateMessage |
|
72
|
|
|
*/ |
|
73
|
|
|
protected $createUpdateMessage; |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @var CreateUpdateContactObserver |
|
77
|
|
|
*/ |
|
78
|
|
|
protected $customerSaveAfterObserver; |
|
79
|
|
|
|
|
80
|
|
|
protected function setUp() |
|
81
|
|
|
{ |
|
82
|
|
|
$this->configHelper = $this->createMock(ConfigHelper::class); |
|
83
|
|
|
$this->contactRepository = $this->createMock(ContactRepositoryInterface::class); |
|
84
|
|
|
$this->logger = $this->createMock(Logger::class); |
|
85
|
|
|
$this->createUpdateMessageBuilder = $this->createMock(CreateUpdateMessageBuilder::class); |
|
86
|
|
|
$this->createUpdatePublisher = $this->createMock(CreateUpdatePublisher::class); |
|
87
|
|
|
$this->observer = $this->createMock(Observer::class); |
|
88
|
|
|
$this->event = $this->createMock(Event::class); |
|
89
|
|
|
$this->customer = $this->createMock(Customer::class); |
|
90
|
|
|
$this->contact = $this->createMock(ContactInterface::class); |
|
91
|
|
|
$this->createUpdateMessage = $this->createMock(CreateUpdateMessage::class); |
|
92
|
|
|
|
|
93
|
|
|
$this->customerSaveAfterObserver = new CreateUpdateContactObserver( |
|
94
|
|
|
$this->configHelper, |
|
95
|
|
|
$this->contactRepository, |
|
96
|
|
|
$this->logger, |
|
97
|
|
|
$this->createUpdateMessageBuilder, |
|
98
|
|
|
$this->createUpdatePublisher |
|
99
|
|
|
); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
public function testExecuteApiNotEnabled() |
|
103
|
|
|
{ |
|
104
|
|
|
$this->configHelper->expects($this->once()) |
|
|
|
|
|
|
105
|
|
|
->method('isApiEnabled') |
|
106
|
|
|
->willReturn(false); |
|
107
|
|
|
|
|
108
|
|
|
$this->observer->expects($this->never()) |
|
109
|
|
|
->method('getEvent'); |
|
|
|
|
|
|
110
|
|
|
|
|
111
|
|
|
$this->customerSaveAfterObserver->execute($this->observer); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
public function testExecuteWithException() |
|
115
|
|
|
{ |
|
116
|
|
|
$this->configHelper->expects($this->once()) |
|
117
|
|
|
->method('isApiEnabled') |
|
118
|
|
|
->willReturn(true); |
|
119
|
|
|
|
|
120
|
|
|
$this->observer->expects($this->once()) |
|
121
|
|
|
->method('getEvent') |
|
122
|
|
|
->willReturn($this->event); |
|
|
|
|
|
|
123
|
|
|
|
|
124
|
|
|
$this->event->expects($this->once()) |
|
125
|
|
|
->method('getData') |
|
126
|
|
|
->with('customer') |
|
|
|
|
|
|
127
|
|
|
->willReturn($this->customer); |
|
128
|
|
|
|
|
129
|
|
|
$exception = new CouldNotSaveException(new Phrase('')); |
|
130
|
|
|
|
|
131
|
|
|
$this->contactRepository->expects($this->once()) |
|
|
|
|
|
|
132
|
|
|
->method('getOrCreateByCustomer') |
|
133
|
|
|
->with($this->customer) |
|
134
|
|
|
->willThrowException($exception); |
|
135
|
|
|
|
|
136
|
|
|
$this->logger->expects($this->once()) |
|
|
|
|
|
|
137
|
|
|
->method('critical') |
|
138
|
|
|
->with($exception); |
|
139
|
|
|
|
|
140
|
|
|
$this->createUpdatePublisher->expects($this->never()) |
|
|
|
|
|
|
141
|
|
|
->method('publish'); |
|
142
|
|
|
|
|
143
|
|
|
$this->customerSaveAfterObserver->execute($this->observer); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
public function testExecute() |
|
147
|
|
|
{ |
|
148
|
|
|
$this->configHelper->expects($this->once()) |
|
149
|
|
|
->method('isApiEnabled') |
|
150
|
|
|
->willReturn(true); |
|
151
|
|
|
|
|
152
|
|
|
$this->observer->expects($this->once()) |
|
153
|
|
|
->method('getEvent') |
|
154
|
|
|
->willReturn($this->event); |
|
155
|
|
|
|
|
156
|
|
|
$this->event->expects($this->once()) |
|
157
|
|
|
->method('getData') |
|
158
|
|
|
->with('customer') |
|
159
|
|
|
->willReturn($this->customer); |
|
160
|
|
|
|
|
161
|
|
|
$this->contactRepository->expects($this->once()) |
|
162
|
|
|
->method('getOrCreateByCustomer') |
|
163
|
|
|
->with($this->customer) |
|
164
|
|
|
->willReturn($this->contact); |
|
165
|
|
|
|
|
166
|
|
|
$this->createUpdateMessageBuilder->expects($this->once()) |
|
|
|
|
|
|
167
|
|
|
->method('buildWithCustomer') |
|
168
|
|
|
->with($this->contact, $this->customer) |
|
169
|
|
|
->willReturn($this->createUpdateMessage); |
|
170
|
|
|
|
|
171
|
|
|
$this->createUpdatePublisher->expects($this->once()) |
|
172
|
|
|
->method('publish') |
|
173
|
|
|
->with($this->createUpdateMessage); |
|
174
|
|
|
|
|
175
|
|
|
$this->customerSaveAfterObserver->execute($this->observer); |
|
176
|
|
|
} |
|
177
|
|
|
} |
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.