123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- <?php
- /**
- * Unit test for session \Magento\Customer\Model\Session
- *
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Customer\Test\Unit\Model;
- /**
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class SessionTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $_storageMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $_eventManagerMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $_httpContextMock;
- /**
- * @var \Magento\Framework\UrlFactory|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $urlFactoryMock;
- /**
- * @var \Magento\Customer\Model\CustomerFactory|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $customerFactoryMock;
- /**
- * @var \Magento\Customer\Api\CustomerRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $customerRepositoryMock;
- /**
- * @var \Magento\Framework\App\Response\Http|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $responseMock;
- /**
- * @var \Magento\Customer\Model\Session
- */
- protected $_model;
- /**
- * @return void
- */
- protected function setUp()
- {
- $this->_storageMock = $this->createPartialMock(
- \Magento\Customer\Model\Session\Storage::class,
- ['getIsCustomerEmulated', 'getData', 'unsIsCustomerEmulated', '__sleep', '__wakeup']
- );
- $this->_eventManagerMock = $this->createMock(\Magento\Framework\Event\ManagerInterface::class);
- $this->_httpContextMock = $this->createMock(\Magento\Framework\App\Http\Context::class);
- $this->urlFactoryMock = $this->createMock(\Magento\Framework\UrlFactory::class);
- $this->customerFactoryMock = $this->getMockBuilder(\Magento\Customer\Model\CustomerFactory::class)
- ->disableOriginalConstructor()
- ->setMethods(['create'])
- ->getMock();
- $this->customerRepositoryMock = $this->createMock(\Magento\Customer\Api\CustomerRepositoryInterface::class);
- $helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
- $this->responseMock = $this->createMock(\Magento\Framework\App\Response\Http::class);
- $this->_model = $helper->getObject(
- \Magento\Customer\Model\Session::class,
- [
- 'customerFactory' => $this->customerFactoryMock,
- 'storage' => $this->_storageMock,
- 'eventManager' => $this->_eventManagerMock,
- 'httpContext' => $this->_httpContextMock,
- 'urlFactory' => $this->urlFactoryMock,
- 'customerRepository' => $this->customerRepositoryMock,
- 'response' => $this->responseMock,
- ]
- );
- }
- /**
- * @return void
- */
- public function testSetCustomerAsLoggedIn()
- {
- $customer = $this->createMock(\Magento\Customer\Model\Customer::class);
- $customerDto = $this->createMock(\Magento\Customer\Api\Data\CustomerInterface::class);
- $customer->expects($this->any())
- ->method('getDataModel')
- ->will($this->returnValue($customerDto));
- $this->_eventManagerMock->expects($this->at(0))
- ->method('dispatch')
- ->with('customer_login', ['customer' => $customer]);
- $this->_eventManagerMock->expects($this->at(1))
- ->method('dispatch')
- ->with('customer_data_object_login', ['customer' => $customerDto]);
- $_SESSION = [];
- $this->_model->setCustomerAsLoggedIn($customer);
- $this->assertSame($customer, $this->_model->getCustomer());
- }
- /**
- * @return void
- */
- public function testSetCustomerDataAsLoggedIn()
- {
- $customer = $this->createMock(\Magento\Customer\Model\Customer::class);
- $customerDto = $this->createMock(\Magento\Customer\Api\Data\CustomerInterface::class);
- $this->customerFactoryMock->expects($this->once())
- ->method('create')
- ->will($this->returnValue($customer));
- $customer->expects($this->once())
- ->method('updateData')
- ->with($customerDto)
- ->will($this->returnSelf());
- $this->_eventManagerMock->expects($this->at(0))
- ->method('dispatch')
- ->with('customer_login', ['customer' => $customer]);
- $this->_eventManagerMock->expects($this->at(1))
- ->method('dispatch')
- ->with('customer_data_object_login', ['customer' => $customerDto]);
- $this->_model->setCustomerDataAsLoggedIn($customerDto);
- $this->assertSame($customer, $this->_model->getCustomer());
- }
- /**
- * @return void
- */
- public function testAuthenticate()
- {
- $urlMock = $this->createMock(\Magento\Framework\Url::class);
- $urlMock->expects($this->exactly(2))
- ->method('getUrl')
- ->willReturn('');
- $urlMock->expects($this->once())
- ->method('getRebuiltUrl')
- ->willReturn('');
- $this->urlFactoryMock->expects($this->exactly(4))
- ->method('create')
- ->willReturn($urlMock);
- $urlMock->expects($this->once())
- ->method('getUseSession')
- ->willReturn(false);
- $this->responseMock->expects($this->once())
- ->method('setRedirect')
- ->with('')
- ->willReturn('');
- $this->assertFalse($this->_model->authenticate());
- }
- /**
- * @return void
- */
- public function testLoginById()
- {
- $customerId = 1;
- $customerDataMock = $this->prepareLoginDataMock($customerId);
- $this->customerRepositoryMock->expects($this->once())
- ->method('getById')
- ->with($this->equalTo($customerId))
- ->will($this->returnValue($customerDataMock));
- $this->assertTrue($this->_model->loginById($customerId));
- }
- /**
- * @param int $customerId
- * @return \PHPUnit_Framework_MockObject_MockObject
- */
- protected function prepareLoginDataMock($customerId)
- {
- $customerDataMock = $this->createMock(\Magento\Customer\Api\Data\CustomerInterface::class);
- $customerDataMock->expects($this->once())
- ->method('getId')
- ->will($this->returnValue($customerId));
- $customerMock = $this->createPartialMock(
- \Magento\Customer\Model\Customer::class,
- ['getId', 'isConfirmationRequired', 'getConfirmation', 'updateData', 'getGroupId']
- );
- $customerMock->expects($this->once())
- ->method('getId')
- ->will($this->returnValue($customerId));
- $customerMock->expects($this->once())
- ->method('isConfirmationRequired')
- ->will($this->returnValue(true));
- $customerMock->expects($this->never())
- ->method('getConfirmation')
- ->will($this->returnValue($customerId));
- $this->customerFactoryMock->expects($this->once())
- ->method('create')
- ->will($this->returnValue($customerMock));
- $customerMock->expects($this->once())
- ->method('updateData')
- ->with($customerDataMock)
- ->will($this->returnSelf());
- $this->_httpContextMock->expects($this->exactly(3))
- ->method('setValue');
- return $customerDataMock;
- }
- /**
- * @param bool $expectedResult
- * @param bool $isCustomerIdValid
- * @param bool $isCustomerEmulated
- * @dataProvider getIsLoggedInDataProvider
- */
- public function testIsLoggedIn($expectedResult, $isCustomerIdValid, $isCustomerEmulated)
- {
- $customerId = 1;
- $this->_storageMock->expects($this->any())->method('getData')->with('customer_id')
- ->will($this->returnValue($customerId));
- if ($isCustomerIdValid) {
- $this->customerRepositoryMock->expects($this->once())
- ->method('getById')
- ->with($customerId);
- } else {
- $this->customerRepositoryMock->expects($this->once())
- ->method('getById')
- ->with($customerId)
- ->will($this->throwException(new \Exception('Customer ID is invalid.')));
- }
- $this->_storageMock->expects($this->any())->method('getIsCustomerEmulated')
- ->will($this->returnValue($isCustomerEmulated));
- $this->assertEquals($expectedResult, $this->_model->isLoggedIn());
- }
- /**
- * @return array
- */
- public function getIsLoggedInDataProvider()
- {
- return [
- ['expectedResult' => true, 'isCustomerIdValid' => true, 'isCustomerEmulated' => false],
- ['expectedResult' => false, 'isCustomerIdValid' => true, 'isCustomerEmulated' => true],
- ['expectedResult' => false, 'isCustomerIdValid' => false, 'isCustomerEmulated' => false],
- ['expectedResult' => false, 'isCustomerIdValid' => false, 'isCustomerEmulated' => true],
- ];
- }
- /**
- * @return void
- */
- public function testSetCustomerRemovesFlagThatShowsIfCustomerIsEmulated()
- {
- $customerMock = $this->createMock(\Magento\Customer\Model\Customer::class);
- $this->_storageMock->expects($this->once())->method('unsIsCustomerEmulated');
- $this->_model->setCustomer($customerMock);
- }
- }
|