Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CurrentCustomerTest.php
Go to the documentation of this file.
1 <?php
8 
9 class CurrentCustomerTest extends \PHPUnit\Framework\TestCase
10 {
14  protected $currentCustomer;
15 
20 
24  protected $layoutMock;
25 
30 
34  protected $customerDataMock;
35 
40 
44  protected $requestMock;
45 
49  protected $moduleManagerMock;
50 
54  protected $viewMock;
55 
59  protected $customerId = 100;
60 
64  protected $customerGroupId = 500;
65 
69  protected function setUp()
70  {
71  $this->customerSessionMock = $this->createMock(\Magento\Customer\Model\Session::class);
72  $this->layoutMock = $this->createMock(\Magento\Framework\View\Layout::class);
73  $this->customerInterfaceFactoryMock = $this->createPartialMock(
74  \Magento\Customer\Api\Data\CustomerInterfaceFactory::class,
75  ['create', 'setGroupId']
76  );
77  $this->customerDataMock = $this->createMock(\Magento\Customer\Api\Data\CustomerInterface::class);
78  $this->customerRepositoryMock = $this->createMock(\Magento\Customer\Api\CustomerRepositoryInterface::class);
79  $this->requestMock = $this->createMock(\Magento\Framework\App\Request\Http::class);
80  $this->moduleManagerMock = $this->createMock(\Magento\Framework\Module\Manager::class);
81  $this->viewMock = $this->createMock(\Magento\Framework\App\View::class);
82 
83  $this->currentCustomer = new \Magento\Customer\Helper\Session\CurrentCustomer(
84  $this->customerSessionMock,
85  $this->layoutMock,
86  $this->customerInterfaceFactoryMock,
87  $this->customerRepositoryMock,
88  $this->requestMock,
89  $this->moduleManagerMock,
90  $this->viewMock
91  );
92  }
93 
98  {
99  $this->requestMock->expects($this->once())->method('isAjax')->will($this->returnValue(false));
100  $this->layoutMock->expects($this->once())->method('isCacheable')->will($this->returnValue(true));
101  $this->viewMock->expects($this->once())->method('isLayoutLoaded')->will($this->returnValue(true));
102  $this->moduleManagerMock->expects($this->once())
103  ->method('isEnabled')
104  ->with($this->equalTo('Magento_PageCache'))
105  ->will($this->returnValue(true));
106  $this->customerSessionMock->expects($this->once())
107  ->method('getCustomerGroupId')
108  ->will($this->returnValue($this->customerGroupId));
109  $this->customerInterfaceFactoryMock->expects($this->once())
110  ->method('create')
111  ->will($this->returnValue($this->customerDataMock));
112  $this->customerDataMock->expects($this->once())
113  ->method('setGroupId')
114  ->with($this->equalTo($this->customerGroupId))
115  ->will($this->returnSelf());
116  $this->assertEquals($this->customerDataMock, $this->currentCustomer->getCustomer());
117  }
118 
123  {
124  $this->moduleManagerMock->expects($this->once())
125  ->method('isEnabled')
126  ->with($this->equalTo('Magento_PageCache'))
127  ->will($this->returnValue(false));
128  $this->customerSessionMock->expects($this->once())
129  ->method('getId')
130  ->will($this->returnValue($this->customerId));
131  $this->customerRepositoryMock->expects($this->once())
132  ->method('getById')
133  ->with($this->equalTo($this->customerId))
134  ->will($this->returnValue($this->customerDataMock));
135  $this->assertEquals($this->customerDataMock, $this->currentCustomer->getCustomer());
136  }
137 }