Skip to content

Commit a62f72b

Browse files
committed
Unit test for UpgradeQuoteCustomerEmailObserver
1 parent 9177050 commit a62f72b

File tree

2 files changed

+106
-14
lines changed

2 files changed

+106
-14
lines changed

app/code/Magento/Customer/Observer/UpgradeQuoteCustomerEmailObserver.php

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,40 +14,37 @@ class UpgradeQuoteCustomerEmailObserver implements ObserverInterface
1414
/**
1515
* @var CartRepositoryInterface
1616
*/
17-
private $cartRepository;
17+
private $quoteRepository;
1818

1919
/**
20-
* @param CartRepositoryInterface $cartRepository
20+
* @param CartRepositoryInterface $quoteRepository
2121
*/
2222
public function __construct(
23-
CartRepositoryInterface $cartRepository
23+
CartRepositoryInterface $quoteRepository
2424
) {
25-
$this->cartRepository = $cartRepository;
25+
$this->quoteRepository = $quoteRepository;
2626
}
2727

2828
/**
2929
* Upgrade quote customer email when customer has changed email
30+
*
3031
* @param Observer $observer
3132
* @return void
3233
*/
3334
public function execute(Observer $observer)
3435
{
35-
/**
36-
* @var \Magento\Customer\Model\Data\Customer $customer
37-
*/
38-
$customer = $observer->getEvent()->getData('customer_data_object');
36+
/** @var \Magento\Customer\Model\Data\Customer $customer */
37+
$customer = $observer->getEvent()->getCustomerDataObject();
3938
$email = $customer->getEmail();
4039

41-
/**
42-
* @var \Magento\Customer\Model\Data\Customer $customer
43-
*/
44-
$customerOrig = $observer->getEvent()->getData('orig_customer_data_object');
40+
/** @var \Magento\Customer\Model\Data\Customer $customerOrig */
41+
$customerOrig = $observer->getEvent()->getOrigCustomerDataObject();
4542
$emailOrig = $customerOrig->getEmail();
4643

4744
if($email != $emailOrig){
48-
$quote = $this->cartRepository->getForCustomer($customer->getId());
45+
$quote = $this->quoteRepository->getForCustomer($customer->getId());
4946
$quote->setCustomerEmail($email);
50-
$this->cartRepository->save($quote);
47+
$this->quoteRepository->save($quote);
5148
}
5249
}
5350
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
3+
namespace Magento\Customer\Test\Unit\Observer;
4+
5+
use Magento\Customer\Observer\UpgradeQuoteCustomerEmailObserver;
6+
7+
/**
8+
* Class UpgradeQuoteCustomerEmailObserverTest for testing upgrade quote customer email
9+
*/
10+
class UpgradeQuoteCustomerEmailObserverTest extends \PHPUnit\Framework\TestCase
11+
{
12+
/**
13+
* @var UpgradeQuoteCustomerEmailObserver
14+
*/
15+
protected $model;
16+
17+
/**
18+
* @var \Magento\Quote\Api\CartRepositoryInterface
19+
*/
20+
protected $quoteRepositoryMock;
21+
22+
protected $observerMock;
23+
24+
protected $eventMock;
25+
26+
/**
27+
* @inheritdoc
28+
*/
29+
protected function setUp()
30+
{
31+
$this->observerMock = $this->getMockBuilder(\Magento\Framework\Event\Observer::class)
32+
->disableOriginalConstructor()
33+
->getMock();
34+
35+
$this->eventMock = $this->getMockBuilder(\Magento\Framework\Event::class)
36+
->disableOriginalConstructor()
37+
->setMethods(['getCustomerDataObject', 'getOrigCustomerDataObject'])
38+
->getMock();
39+
40+
$this->observerMock->expects($this->any())->method('getEvent')->will($this->returnValue($this->eventMock));
41+
42+
$this->quoteRepositoryMock = $this
43+
->getMockBuilder(\Magento\Quote\Api\CartRepositoryInterface::class)
44+
->getMockForAbstractClass();
45+
$this->model = new UpgradeQuoteCustomerEmailObserver($this->quoteRepositoryMock);
46+
}
47+
48+
/**
49+
*
50+
*/
51+
public function testUpgradeQuoteCustomerEmail()
52+
{
53+
$email = "[email protected]";
54+
$origEmail = "[email protected]";
55+
56+
$customer = $this->getMockBuilder(\Magento\Customer\Api\Data\CustomerInterface::class)
57+
->disableOriginalConstructor()
58+
->getMock();
59+
$customerOrig = $this->getMockBuilder(\Magento\Customer\Api\Data\CustomerInterface::class)
60+
->disableOriginalConstructor()
61+
->getMock();
62+
63+
$quoteMock = $this->getMockBuilder(\Magento\Quote\Model\Quote::class)
64+
->setMethods(['setCustomerEmail'])
65+
->disableOriginalConstructor()
66+
->getMock();
67+
68+
$this->eventMock->expects($this->any())
69+
->method('getCustomerDataObject')
70+
->will($this->returnValue($customer));
71+
$this->eventMock->expects($this->any())
72+
->method('getOrigCustomerDataObject')
73+
->will($this->returnValue($customerOrig));
74+
75+
$customer->expects($this->any())
76+
->method('getEmail')
77+
->willReturn($this->returnValue($email));
78+
$customerOrig->expects($this->any())
79+
->method('getEmail')
80+
->willReturn($this->returnValue($origEmail));
81+
82+
$this->quoteRepositoryMock->expects($this->once())
83+
->method('getForCustomer')
84+
->willReturn($quoteMock);
85+
86+
$quoteMock->expects($this->once())
87+
->method('setCustomerEmail');
88+
89+
$this->quoteRepositoryMock->expects($this->once())
90+
->method('save')
91+
->with($quoteMock);
92+
93+
$this->model->execute($this->observerMock);
94+
}
95+
}

0 commit comments

Comments
 (0)