Skip to content

Commit 3556af3

Browse files
authored
ENGCOM-6649: magento/magento2#: Remove a redundant call to DB for guest session #26476
2 parents e6941d3 + 5f84b35 commit 3556af3

File tree

2 files changed

+114
-27
lines changed

2 files changed

+114
-27
lines changed

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,11 @@ class Session extends \Magento\Framework\Session\SessionManager
9999
*/
100100
protected $_httpContext;
101101

102+
/**
103+
* @var AccountConfirmation
104+
*/
105+
protected $accountConfirmation;
106+
102107
/**
103108
* @var GroupManagementInterface
104109
*/
@@ -310,7 +315,11 @@ public function setCustomer(Customer $customerModel)
310315
public function getCustomer()
311316
{
312317
if ($this->_customerModel === null) {
313-
$this->_customerModel = $this->_customerFactory->create()->load($this->getCustomerId());
318+
$this->_customerModel = $this->_customerFactory->create();
319+
320+
if ($this->getCustomerId()) {
321+
$this->_customerResource->load($this->_customerModel, $this->getCustomerId());
322+
}
314323
}
315324

316325
return $this->_customerModel;

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

Lines changed: 104 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,48 +7,69 @@
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\Url;
22+
use Magento\Framework\UrlFactory;
23+
use PHPUnit\Framework\MockObject\MockObject;
24+
use PHPUnit\Framework\TestCase;
25+
1026
/**
1127
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1228
*/
13-
class SessionTest extends \PHPUnit\Framework\TestCase
29+
class SessionTest extends TestCase
1430
{
1531
/**
16-
* @var \PHPUnit_Framework_MockObject_MockObject
32+
* @var ResourceCustomer|MockObject
33+
*/
34+
protected $_customerResourceMock;
35+
36+
/**
37+
* @var Storage|MockObject
1738
*/
1839
protected $_storageMock;
1940

2041
/**
21-
* @var \PHPUnit_Framework_MockObject_MockObject
42+
* @var ManagerInterface|MockObject
2243
*/
2344
protected $_eventManagerMock;
2445

2546
/**
26-
* @var \PHPUnit_Framework_MockObject_MockObject
47+
* @var Context|MockObject
2748
*/
2849
protected $_httpContextMock;
2950

3051
/**
31-
* @var \Magento\Framework\UrlFactory|\PHPUnit_Framework_MockObject_MockObject
52+
* @var UrlFactory|MockObject
3253
*/
3354
protected $urlFactoryMock;
3455

3556
/**
36-
* @var \Magento\Customer\Model\CustomerFactory|\PHPUnit_Framework_MockObject_MockObject
57+
* @var CustomerFactory|MockObject
3758
*/
3859
protected $customerFactoryMock;
3960

4061
/**
41-
* @var \Magento\Customer\Api\CustomerRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
62+
* @var CustomerRepositoryInterface|MockObject
4263
*/
4364
protected $customerRepositoryMock;
4465

4566
/**
46-
* @var \Magento\Framework\App\Response\Http|\PHPUnit_Framework_MockObject_MockObject
67+
* @var Http|MockObject
4768
*/
4869
protected $responseMock;
4970

5071
/**
51-
* @var \Magento\Customer\Model\Session
72+
* @var Session
5273
*/
5374
protected $_model;
5475

@@ -58,21 +79,25 @@ class SessionTest extends \PHPUnit\Framework\TestCase
5879
protected function setUp()
5980
{
6081
$this->_storageMock = $this->createPartialMock(
61-
\Magento\Customer\Model\Session\Storage::class,
82+
Storage::class,
6283
['getIsCustomerEmulated', 'getData', 'unsIsCustomerEmulated', '__sleep', '__wakeup']
6384
);
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)
85+
$this->_eventManagerMock = $this->createMock(ManagerInterface::class);
86+
$this->_httpContextMock = $this->createMock(Context::class);
87+
$this->urlFactoryMock = $this->createMock(UrlFactory::class);
88+
$this->customerFactoryMock = $this->getMockBuilder(CustomerFactory::class)
6889
->disableOriginalConstructor()
6990
->setMethods(['create', 'save'])
7091
->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);
92+
$this->_customerResourceMock = $this->getMockBuilder(ResourceCustomer::class)
93+
->disableOriginalConstructor()
94+
->setMethods(['load', 'save'])
95+
->getMock();
96+
$this->customerRepositoryMock = $this->createMock(CustomerRepositoryInterface::class);
97+
$helper = new ObjectManagerHelper($this);
98+
$this->responseMock = $this->createMock(Http::class);
7499
$this->_model = $helper->getObject(
75-
\Magento\Customer\Model\Session::class,
100+
Session::class,
76101
[
77102
'customerFactory' => $this->customerFactoryMock,
78103
'storage' => $this->_storageMock,
@@ -81,6 +106,7 @@ protected function setUp()
81106
'urlFactory' => $this->urlFactoryMock,
82107
'customerRepository' => $this->customerRepositoryMock,
83108
'response' => $this->responseMock,
109+
'_customerResource' => $this->_customerResourceMock,
84110
]
85111
);
86112
}
@@ -90,8 +116,8 @@ protected function setUp()
90116
*/
91117
public function testSetCustomerAsLoggedIn()
92118
{
93-
$customer = $this->createMock(\Magento\Customer\Model\Customer::class);
94-
$customerDto = $this->createMock(\Magento\Customer\Api\Data\CustomerInterface::class);
119+
$customer = $this->createMock(Customer::class);
120+
$customerDto = $this->createMock(CustomerInterface::class);
95121
$customer->expects($this->any())
96122
->method('getDataModel')
97123
->will($this->returnValue($customerDto));
@@ -113,8 +139,8 @@ public function testSetCustomerAsLoggedIn()
113139
*/
114140
public function testSetCustomerDataAsLoggedIn()
115141
{
116-
$customer = $this->createMock(\Magento\Customer\Model\Customer::class);
117-
$customerDto = $this->createMock(\Magento\Customer\Api\Data\CustomerInterface::class);
142+
$customer = $this->createMock(Customer::class);
143+
$customerDto = $this->createMock(CustomerInterface::class);
118144

119145
$this->customerFactoryMock->expects($this->once())
120146
->method('create')
@@ -140,7 +166,7 @@ public function testSetCustomerDataAsLoggedIn()
140166
*/
141167
public function testAuthenticate()
142168
{
143-
$urlMock = $this->createMock(\Magento\Framework\Url::class);
169+
$urlMock = $this->createMock(Url::class);
144170
$urlMock->expects($this->exactly(2))
145171
->method('getUrl')
146172
->willReturn('');
@@ -185,13 +211,13 @@ public function testLoginById()
185211
*/
186212
protected function prepareLoginDataMock($customerId)
187213
{
188-
$customerDataMock = $this->createMock(\Magento\Customer\Api\Data\CustomerInterface::class);
214+
$customerDataMock = $this->createMock(CustomerInterface::class);
189215
$customerDataMock->expects($this->once())
190216
->method('getId')
191217
->will($this->returnValue($customerId));
192218

193219
$customerMock = $this->createPartialMock(
194-
\Magento\Customer\Model\Customer::class,
220+
Customer::class,
195221
['getId', 'getConfirmation', 'updateData', 'getGroupId']
196222
);
197223
$customerMock->expects($this->exactly(3))
@@ -259,8 +285,60 @@ public function getIsLoggedInDataProvider()
259285
*/
260286
public function testSetCustomerRemovesFlagThatShowsIfCustomerIsEmulated()
261287
{
262-
$customerMock = $this->createMock(\Magento\Customer\Model\Customer::class);
288+
$customerMock = $this->createMock(Customer::class);
263289
$this->_storageMock->expects($this->once())->method('unsIsCustomerEmulated');
264290
$this->_model->setCustomer($customerMock);
265291
}
292+
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)