Skip to content

Commit 7bfc990

Browse files
committed
magento/magento2#: Remove a redundant call to DB for guest session
1 parent c2e2646 commit 7bfc990

File tree

2 files changed

+116
-28
lines changed

2 files changed

+116
-28
lines changed

app/code/Magento/Customer/Model/Session.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
* @method string getNoReferer()
2020
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2121
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
22+
* @SuppressWarnings(PHPMD.TooManyFields)
2223
* @since 100.0.2
2324
*/
2425
class Session extends \Magento\Framework\Session\SessionManager
@@ -98,6 +99,11 @@ class Session extends \Magento\Framework\Session\SessionManager
9899
*/
99100
protected $_httpContext;
100101

102+
/**
103+
* @var AccountConfirmation
104+
*/
105+
protected $accountConfirmation;
106+
101107
/**
102108
* @var GroupManagementInterface
103109
*/
@@ -304,7 +310,11 @@ public function setCustomer(Customer $customerModel)
304310
public function getCustomer()
305311
{
306312
if ($this->_customerModel === null) {
307-
$this->_customerModel = $this->_customerFactory->create()->load($this->getCustomerId());
313+
$this->_customerModel = $this->_customerFactory->create();
314+
315+
if ($this->getCustomerId()) {
316+
$this->_customerResource->load($this->_customerModel, $this->getCustomerId());
317+
}
308318
}
309319

310320
return $this->_customerModel;

app/code/Magento/Customer/Test/Unit/Model/SessionTest.php

Lines changed: 105 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,48 +7,67 @@
77
*/
88
namespace Magento\Customer\Test\Unit\Model;
99

10+
use Magento\Customer\Api\CustomerRepositoryInterface;
11+
use Magento\Customer\Api\Data\CustomerInterface;
12+
use Magento\Customer\Model\Customer;
13+
use Magento\Customer\Model\CustomerFactory;
14+
use Magento\Customer\Model\ResourceModel\Customer as ResourceCustomer;
15+
use Magento\Customer\Model\Session;
16+
use Magento\Customer\Model\Session\Storage;
17+
use Magento\Framework\App\Http\Context;
18+
use Magento\Framework\App\Response\Http;
19+
use Magento\Framework\Event\ManagerInterface;
20+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
21+
use Magento\Framework\UrlFactory;
22+
use PHPUnit\Framework\MockObject\MockObject;
23+
1024
/**
1125
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1226
*/
1327
class SessionTest extends \PHPUnit\Framework\TestCase
1428
{
1529
/**
16-
* @var \PHPUnit_Framework_MockObject_MockObject
30+
* @var ResourceCustomer|MockObject
31+
*/
32+
protected $_customerResourceMock;
33+
34+
/**
35+
* @var Storage|MockObject
1736
*/
1837
protected $_storageMock;
1938

2039
/**
21-
* @var \PHPUnit_Framework_MockObject_MockObject
40+
* @var ManagerInterface|MockObject
2241
*/
2342
protected $_eventManagerMock;
2443

2544
/**
26-
* @var \PHPUnit_Framework_MockObject_MockObject
45+
* @var Context|MockObject
2746
*/
2847
protected $_httpContextMock;
2948

3049
/**
31-
* @var \Magento\Framework\UrlFactory|\PHPUnit_Framework_MockObject_MockObject
50+
* @var UrlFactory|MockObject
3251
*/
3352
protected $urlFactoryMock;
3453

3554
/**
36-
* @var \Magento\Customer\Model\CustomerFactory|\PHPUnit_Framework_MockObject_MockObject
55+
* @var CustomerFactory|MockObject
3756
*/
3857
protected $customerFactoryMock;
3958

4059
/**
41-
* @var \Magento\Customer\Api\CustomerRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
60+
* @var CustomerRepositoryInterface|MockObject
4261
*/
4362
protected $customerRepositoryMock;
4463

4564
/**
46-
* @var \Magento\Framework\App\Response\Http|\PHPUnit_Framework_MockObject_MockObject
65+
* @var Http|MockObject
4766
*/
4867
protected $responseMock;
4968

5069
/**
51-
* @var \Magento\Customer\Model\Session
70+
* @var Session
5271
*/
5372
protected $_model;
5473

@@ -58,21 +77,25 @@ class SessionTest extends \PHPUnit\Framework\TestCase
5877
protected function setUp()
5978
{
6079
$this->_storageMock = $this->createPartialMock(
61-
\Magento\Customer\Model\Session\Storage::class,
80+
Storage::class,
6281
['getIsCustomerEmulated', 'getData', 'unsIsCustomerEmulated', '__sleep', '__wakeup']
6382
);
64-
$this->_eventManagerMock = $this->createMock(\Magento\Framework\Event\ManagerInterface::class);
65-
$this->_httpContextMock = $this->createMock(\Magento\Framework\App\Http\Context::class);
66-
$this->urlFactoryMock = $this->createMock(\Magento\Framework\UrlFactory::class);
67-
$this->customerFactoryMock = $this->getMockBuilder(\Magento\Customer\Model\CustomerFactory::class)
83+
$this->_eventManagerMock = $this->createMock(ManagerInterface::class);
84+
$this->_httpContextMock = $this->createMock(Context::class);
85+
$this->urlFactoryMock = $this->createMock(UrlFactory::class);
86+
$this->customerFactoryMock = $this->getMockBuilder(CustomerFactory::class)
6887
->disableOriginalConstructor()
69-
->setMethods(['create', 'save'])
88+
->setMethods(['create'])
7089
->getMock();
71-
$this->customerRepositoryMock = $this->createMock(\Magento\Customer\Api\CustomerRepositoryInterface::class);
72-
$helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
73-
$this->responseMock = $this->createMock(\Magento\Framework\App\Response\Http::class);
90+
$this->_customerResourceMock = $this->getMockBuilder(ResourceCustomer::class)
91+
->disableOriginalConstructor()
92+
->setMethods(['load'])
93+
->getMock();
94+
$this->customerRepositoryMock = $this->createMock(CustomerRepositoryInterface::class);
95+
$helper = new ObjectManagerHelper($this);
96+
$this->responseMock = $this->createMock(Http::class);
7497
$this->_model = $helper->getObject(
75-
\Magento\Customer\Model\Session::class,
98+
Session::class,
7699
[
77100
'customerFactory' => $this->customerFactoryMock,
78101
'storage' => $this->_storageMock,
@@ -81,6 +104,7 @@ protected function setUp()
81104
'urlFactory' => $this->urlFactoryMock,
82105
'customerRepository' => $this->customerRepositoryMock,
83106
'response' => $this->responseMock,
107+
'_customerResource' => $this->_customerResourceMock,
84108
]
85109
);
86110
}
@@ -90,8 +114,8 @@ protected function setUp()
90114
*/
91115
public function testSetCustomerAsLoggedIn()
92116
{
93-
$customer = $this->createMock(\Magento\Customer\Model\Customer::class);
94-
$customerDto = $this->createMock(\Magento\Customer\Api\Data\CustomerInterface::class);
117+
$customer = $this->createMock(Customer::class);
118+
$customerDto = $this->createMock(CustomerInterface::class);
95119
$customer->expects($this->any())
96120
->method('getDataModel')
97121
->will($this->returnValue($customerDto));
@@ -113,8 +137,8 @@ public function testSetCustomerAsLoggedIn()
113137
*/
114138
public function testSetCustomerDataAsLoggedIn()
115139
{
116-
$customer = $this->createMock(\Magento\Customer\Model\Customer::class);
117-
$customerDto = $this->createMock(\Magento\Customer\Api\Data\CustomerInterface::class);
140+
$customer = $this->createMock(Customer::class);
141+
$customerDto = $this->createMock(CustomerInterface::class);
118142

119143
$this->customerFactoryMock->expects($this->once())
120144
->method('create')
@@ -185,19 +209,22 @@ public function testLoginById()
185209
*/
186210
protected function prepareLoginDataMock($customerId)
187211
{
188-
$customerDataMock = $this->createMock(\Magento\Customer\Api\Data\CustomerInterface::class);
212+
$customerDataMock = $this->createMock(CustomerInterface::class);
189213
$customerDataMock->expects($this->once())
190214
->method('getId')
191215
->will($this->returnValue($customerId));
192216

193217
$customerMock = $this->createPartialMock(
194-
\Magento\Customer\Model\Customer::class,
195-
['getId', 'getConfirmation', 'updateData', 'getGroupId']
218+
Customer::class,
219+
['getId', 'isConfirmationRequired', 'getConfirmation', 'updateData', 'getGroupId']
196220
);
197-
$customerMock->expects($this->exactly(3))
221+
$customerMock->expects($this->once())
198222
->method('getId')
199223
->will($this->returnValue($customerId));
200224
$customerMock->expects($this->once())
225+
->method('isConfirmationRequired')
226+
->will($this->returnValue(true));
227+
$customerMock->expects($this->never())
201228
->method('getConfirmation')
202229
->will($this->returnValue($customerId));
203230

@@ -259,8 +286,59 @@ public function getIsLoggedInDataProvider()
259286
*/
260287
public function testSetCustomerRemovesFlagThatShowsIfCustomerIsEmulated()
261288
{
262-
$customerMock = $this->createMock(\Magento\Customer\Model\Customer::class);
289+
$customerMock = $this->createMock(Customer::class);
263290
$this->_storageMock->expects($this->once())->method('unsIsCustomerEmulated');
264291
$this->_model->setCustomer($customerMock);
265292
}
293+
/**
294+
* Test "getCustomer()" for guest user
295+
*
296+
* @return void
297+
*/
298+
public function testGetCustomerForGuestUser()
299+
{
300+
$customerMock = $this->getMockBuilder(Customer::class)
301+
->disableOriginalConstructor()
302+
->getMock();
303+
304+
$this->customerFactoryMock
305+
->expects($this->once())
306+
->method('create')
307+
->will($this->returnValue($customerMock));
308+
309+
$this->assertSame($customerMock, $this->_model->getCustomer());
310+
}
311+
312+
/**
313+
* Test "getCustomer()" for registered user
314+
*
315+
* @return void
316+
*/
317+
public function testGetCustomerForRegisteredUser()
318+
{
319+
$customerId = 1;
320+
321+
$customerMock = $this->getMockBuilder(Customer::class)
322+
->disableOriginalConstructor()
323+
->getMock();
324+
325+
$this->customerFactoryMock
326+
->expects($this->once())
327+
->method('create')
328+
->will($this->returnValue($customerMock));
329+
330+
$this->_storageMock
331+
->expects($this->exactly(4))
332+
->method('getData')
333+
->with('customer_id')
334+
->willReturn($customerId);
335+
336+
$this->_customerResourceMock
337+
->expects($this->once())
338+
->method('load')
339+
->with($customerMock, $customerId)
340+
->will($this->returnValue($customerMock));
341+
342+
$this->assertSame($customerMock, $this->_model->getCustomer());
343+
}
266344
}

0 commit comments

Comments
 (0)