Skip to content

Commit d07b84d

Browse files
committed
MAGETWO-38011: [Nord] Unit test Coverage - Sprint 22
1 parent 8ff2e8a commit d07b84d

File tree

1 file changed

+189
-16
lines changed

1 file changed

+189
-16
lines changed

app/code/Magento/Newsletter/Test/Unit/Model/SubscriberTest.php

Lines changed: 189 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,9 @@ public function setUp()
100100
[
101101
'loadByEmail',
102102
'getIdFieldName',
103-
'save'
103+
'save',
104+
'loadByCustomerData',
105+
'received'
104106
],
105107
[],
106108
'',
@@ -134,7 +136,6 @@ public function testSubscribe()
134136
'name' => 'subscriber_name'
135137
]
136138
);
137-
$this->resource->expects($this->any())->method('getIdFieldName')->willReturn('id_field');
138139
$this->scopeConfig->expects($this->any())->method('getValue')->willReturn(true);
139140
$this->customerSession->expects($this->any())->method('isLoggedIn')->willReturn(true);
140141
$customerDataModel = $this->getMock('\Magento\Customer\Api\Data\CustomerInterface');
@@ -144,21 +145,193 @@ public function testSubscribe()
144145
$this->customerRepository->expects($this->any())->method('getById')->willReturn($customerDataModel);
145146
$customerDataModel->expects($this->any())->method('getStoreId')->willReturn(1);
146147
$customerDataModel->expects($this->any())->method('getId')->willReturn(1);
147-
$this->transportBuilder->expects($this->any())->method('setTemplateIdentifier')->willReturnSelf();
148-
$this->transportBuilder->expects($this->any())->method('setTemplateOptions')->willReturnSelf();
149-
$this->transportBuilder->expects($this->any())->method('setTemplateVars')->willReturnSelf();
150-
$this->transportBuilder->expects($this->any())->method('setFrom')->willReturnSelf();
151-
$this->transportBuilder->expects($this->any())->method('addTo')->willReturnSelf();
152-
$storeModel = $this->getMock('\Magento\Store\Model\Store', ['getId'], [], '', false);
153-
$this->scopeConfig->expects($this->any())->method('getValue')->willReturn('owner_email@magento.com');
154-
$this->storeManager->expects($this->any())->method('getStore')->willReturn($storeModel);
155-
$storeModel->expects($this->any())->method('getId')->willReturn(1);
156-
$transport = $this->getMock('\Magento\Framework\Mail\TransportInterface');
157-
$this->transportBuilder->expects($this->any())->method('getTransport')->willReturn($transport);
158-
$transport->expects($this->any())->method('sendMessage')->willReturnSelf();
159-
$inlineTranslation = $this->getMock('Magento\Framework\Translate\Inline\StateInterface');
160-
$inlineTranslation->expects($this->any())->method('resume')->willReturnSelf();
148+
$this->sendEmailCheck();
161149
$this->resource->expects($this->atLeastOnce())->method('save')->willReturnSelf();
150+
162151
$this->assertEquals(1, $this->subscriber->subscribe($email));
163152
}
153+
154+
public function testSubscribeNotLoggedIn()
155+
{
156+
$email = 'subscriber_email@magento.com';
157+
$this->resource->expects($this->any())->method('loadByEmail')->willReturn(
158+
[
159+
'subscriber_status' => 3,
160+
'subscriber_email' => $email,
161+
'name' => 'subscriber_name'
162+
]
163+
);
164+
$this->scopeConfig->expects($this->any())->method('getValue')->willReturn(true);
165+
$this->customerSession->expects($this->any())->method('isLoggedIn')->willReturn(false);
166+
$customerDataModel = $this->getMock('\Magento\Customer\Api\Data\CustomerInterface');
167+
$this->customerSession->expects($this->any())->method('getCustomerDataObject')->willReturn($customerDataModel);
168+
$this->customerSession->expects($this->any())->method('getCustomerId')->willReturn(1);
169+
$customerDataModel->expects($this->any())->method('getEmail')->willReturn($email);
170+
$this->customerRepository->expects($this->any())->method('getById')->willReturn($customerDataModel);
171+
$customerDataModel->expects($this->any())->method('getStoreId')->willReturn(1);
172+
$customerDataModel->expects($this->any())->method('getId')->willReturn(1);
173+
$this->sendEmailCheck();
174+
$this->resource->expects($this->atLeastOnce())->method('save')->willReturnSelf();
175+
176+
$this->assertEquals(2, $this->subscriber->subscribe($email));
177+
}
178+
179+
public function testUpdateSubscription()
180+
{
181+
$customerId = 1;
182+
$customerDataMock = $this->getMockBuilder('\Magento\Customer\Api\Data\CustomerInterface')
183+
->getMock();
184+
$this->customerRepository->expects($this->atLeastOnce())
185+
->method('getById')
186+
->with($customerId)->willReturn($customerDataMock);
187+
$this->resource->expects($this->atLeastOnce())
188+
->method('loadByCustomerData')
189+
->with($customerDataMock)
190+
->willReturn(
191+
[
192+
'subscriber_id' => 1,
193+
'subscriber_status' => 1
194+
]
195+
);
196+
$customerDataMock->expects($this->atLeastOnce())->method('getId')->willReturn('id');
197+
$this->resource->expects($this->atLeastOnce())->method('save')->willReturnSelf();
198+
$this->customerAccountManagement->expects($this->once())
199+
->method('getConfirmationStatus')
200+
->with($customerId)
201+
->willReturn('account_confirmation_required');
202+
$customerDataMock->expects($this->once())->method('getStoreId')->willReturn('store_id');
203+
$customerDataMock->expects($this->once())->method('getEmail')->willReturn('email');
204+
205+
$this->assertEquals($this->subscriber, $this->subscriber->updateSubscription($customerId));
206+
}
207+
208+
public function testUnsubscribeCustomerById()
209+
{
210+
$customerId = 1;
211+
$customerDataMock = $this->getMockBuilder('\Magento\Customer\Api\Data\CustomerInterface')
212+
->getMock();
213+
$this->customerRepository->expects($this->atLeastOnce())
214+
->method('getById')
215+
->with($customerId)->willReturn($customerDataMock);
216+
$this->resource->expects($this->atLeastOnce())
217+
->method('loadByCustomerData')
218+
->with($customerDataMock)
219+
->willReturn(
220+
[
221+
'subscriber_id' => 1,
222+
'subscriber_status' => 1
223+
]
224+
);
225+
$customerDataMock->expects($this->atLeastOnce())->method('getId')->willReturn('id');
226+
$this->resource->expects($this->atLeastOnce())->method('save')->willReturnSelf();
227+
$customerDataMock->expects($this->once())->method('getStoreId')->willReturn('store_id');
228+
$customerDataMock->expects($this->once())->method('getEmail')->willReturn('email');
229+
$this->sendEmailCheck();
230+
231+
$this->subscriber->unsubscribeCustomerById($customerId);
232+
}
233+
234+
public function testSubscribeCustomerById()
235+
{
236+
$customerId = 1;
237+
$customerDataMock = $this->getMockBuilder('\Magento\Customer\Api\Data\CustomerInterface')
238+
->getMock();
239+
$this->customerRepository->expects($this->atLeastOnce())
240+
->method('getById')
241+
->with($customerId)->willReturn($customerDataMock);
242+
$this->resource->expects($this->atLeastOnce())
243+
->method('loadByCustomerData')
244+
->with($customerDataMock)
245+
->willReturn(
246+
[
247+
'subscriber_id' => 1,
248+
'subscriber_status' => 3
249+
]
250+
);
251+
$customerDataMock->expects($this->atLeastOnce())->method('getId')->willReturn('id');
252+
$this->resource->expects($this->atLeastOnce())->method('save')->willReturnSelf();
253+
$customerDataMock->expects($this->once())->method('getStoreId')->willReturn('store_id');
254+
$customerDataMock->expects($this->once())->method('getEmail')->willReturn('email');
255+
$this->sendEmailCheck();
256+
257+
$this->subscriber->subscribeCustomerById($customerId);
258+
}
259+
260+
public function testUnsubscribe()
261+
{
262+
$this->resource->expects($this->once())->method('save')->willReturnSelf();
263+
$this->sendEmailCheck();
264+
265+
$this->assertEquals($this->subscriber, $this->subscriber->unsubscribe());
266+
}
267+
268+
/**
269+
* @expectedException \Magento\Framework\Exception\LocalizedException
270+
* @expectedExceptionMessage This is an invalid subscription confirmation code.
271+
*/
272+
public function testUnsubscribeException()
273+
{
274+
$this->subscriber->setCode(111);
275+
$this->subscriber->setCheckCode(222);
276+
277+
$this->subscriber->unsubscribe();
278+
}
279+
280+
public function testGetSubscriberFullName()
281+
{
282+
$this->subscriber->setCustomerFirstname('John');
283+
$this->subscriber->setCustomerLastname('Doe');
284+
285+
$this->assertEquals('John Doe', $this->subscriber->getSubscriberFullName());
286+
}
287+
288+
public function testConfirm()
289+
{
290+
$code = 111;
291+
$this->subscriber->setCode($code);
292+
$this->resource->expects($this->once())->method('save')->willReturnSelf();
293+
294+
$this->assertTrue($this->subscriber->confirm($code));
295+
}
296+
297+
public function testConfirmWrongCode()
298+
{
299+
$code = 111;
300+
$this->subscriber->setCode(222);
301+
302+
$this->assertFalse($this->subscriber->confirm($code));
303+
}
304+
305+
public function testReceived()
306+
{
307+
$queue = $this->getMockBuilder('\Magento\Newsletter\Model\Queue')
308+
->disableOriginalConstructor()
309+
->getMock();
310+
$this->resource->expects($this->once())->method('received')->with($this->subscriber, $queue)->willReturnSelf();
311+
312+
$this->assertEquals($this->subscriber, $this->subscriber->received($queue));
313+
}
314+
315+
protected function sendEmailCheck()
316+
{
317+
$storeModel = $this->getMockBuilder('\Magento\Store\Model\Store')
318+
->disableOriginalConstructor()
319+
->setMethods(['getId'])
320+
->getMock();
321+
$transport = $this->getMock('\Magento\Framework\Mail\TransportInterface');
322+
$this->scopeConfig->expects($this->any())->method('getValue')->willReturn(true);
323+
$this->transportBuilder->expects($this->once())->method('setTemplateIdentifier')->willReturnSelf();
324+
$this->transportBuilder->expects($this->once())->method('setTemplateOptions')->willReturnSelf();
325+
$this->transportBuilder->expects($this->once())->method('setTemplateVars')->willReturnSelf();
326+
$this->transportBuilder->expects($this->once())->method('setFrom')->willReturnSelf();
327+
$this->transportBuilder->expects($this->once())->method('addTo')->willReturnSelf();
328+
$this->storeManager->expects($this->any())->method('getStore')->willReturn($storeModel);
329+
$storeModel->expects($this->any())->method('getId')->willReturn(1);
330+
$this->transportBuilder->expects($this->once())->method('getTransport')->willReturn($transport);
331+
$transport->expects($this->once())->method('sendMessage')->willReturnSelf();
332+
$this->inlineTranslation->expects($this->once())->method('suspend')->willReturnSelf();
333+
$this->inlineTranslation->expects($this->once())->method('resume')->willReturnSelf();
334+
335+
return $this;
336+
}
164337
}

0 commit comments

Comments
 (0)