VisitorTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Test\Unit\Model;
  7. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
  8. /**
  9. * Class VisitorTest
  10. * @package Magento\Customer\Model
  11. */
  12. class VisitorTest extends \PHPUnit\Framework\TestCase
  13. {
  14. /**
  15. * @var \Magento\Customer\Model\Visitor
  16. */
  17. protected $visitor;
  18. /**
  19. * @var ObjectManagerHelper
  20. */
  21. protected $objectManagerHelper;
  22. /**
  23. * @var \Magento\Framework\Registry|\PHPUnit_Framework_MockObject_MockObject
  24. */
  25. protected $registry;
  26. /**
  27. * @var \Magento\Customer\Model\ResourceModel\Visitor|\PHPUnit_Framework_MockObject_MockObject
  28. */
  29. protected $resource;
  30. /**
  31. * @var \Magento\Framework\Session\SessionManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  32. */
  33. protected $session;
  34. protected function setUp()
  35. {
  36. $this->registry = $this->createMock(\Magento\Framework\Registry::class);
  37. $this->session = $this->getMockBuilder(\Magento\Customer\Model\Session::class)
  38. ->disableOriginalConstructor()
  39. ->setMethods(['getSessionId', 'getVisitorData', 'setVisitorData'])
  40. ->getMock();
  41. $this->objectManagerHelper = new ObjectManagerHelper($this);
  42. $this->resource = $this->getMockBuilder(\Magento\Customer\Model\ResourceModel\Visitor::class)
  43. ->setMethods([
  44. 'beginTransaction',
  45. '__sleep',
  46. '__wakeup',
  47. 'getIdFieldName',
  48. 'save',
  49. 'addCommitCallback',
  50. 'commit',
  51. 'clean',
  52. ])->disableOriginalConstructor()->getMock();
  53. $this->resource->expects($this->any())->method('getIdFieldName')->willReturn('visitor_id');
  54. $this->resource->expects($this->any())->method('addCommitCallback')->willReturnSelf();
  55. $arguments = $this->objectManagerHelper->getConstructArguments(
  56. \Magento\Customer\Model\Visitor::class,
  57. [
  58. 'registry' => $this->registry,
  59. 'session' => $this->session,
  60. 'resource' => $this->resource,
  61. ]
  62. );
  63. $this->visitor = $this->objectManagerHelper->getObject(\Magento\Customer\Model\Visitor::class, $arguments);
  64. }
  65. public function testInitByRequest()
  66. {
  67. $oldSessionId = 'asdfhasdfjhkj2198sadf8sdf897';
  68. $newSessionId = 'bsdfhasdfjhkj2198sadf8sdf897';
  69. $this->session->expects($this->any())->method('getSessionId')->willReturn($newSessionId);
  70. $this->session->expects($this->atLeastOnce())->method('getVisitorData')
  71. ->willReturn(['session_id' => $oldSessionId]);
  72. $this->visitor->initByRequest(null);
  73. $this->assertEquals($newSessionId, $this->visitor->getSessionId());
  74. }
  75. public function testSaveByRequest()
  76. {
  77. $this->session->expects($this->once())->method('setVisitorData')->will($this->returnSelf());
  78. $this->assertSame($this->visitor, $this->visitor->saveByRequest(null));
  79. }
  80. public function testIsModuleIgnored()
  81. {
  82. $this->visitor = $this->objectManagerHelper->getObject(
  83. \Magento\Customer\Model\Visitor::class,
  84. [
  85. 'registry' => $this->registry,
  86. 'session' => $this->session,
  87. 'resource' => $this->resource,
  88. 'ignores' => ['test_route_name' => true],
  89. ]
  90. );
  91. $request = new \Magento\Framework\DataObject(['route_name' => 'test_route_name']);
  92. $action = new \Magento\Framework\DataObject(['request' => $request]);
  93. $event = new \Magento\Framework\DataObject(['controller_action' => $action]);
  94. $observer = new \Magento\Framework\DataObject(['event' => $event]);
  95. $this->assertTrue($this->visitor->isModuleIgnored($observer));
  96. }
  97. public function testBindCustomerLogin()
  98. {
  99. $customer = new \Magento\Framework\DataObject(['id' => '1']);
  100. $observer = new \Magento\Framework\DataObject([
  101. 'event' => new \Magento\Framework\DataObject(['customer' => $customer]),
  102. ]);
  103. $this->visitor->bindCustomerLogin($observer);
  104. $this->assertTrue($this->visitor->getDoCustomerLogin());
  105. $this->assertEquals($customer->getId(), $this->visitor->getCustomerId());
  106. $this->visitor->unsetData();
  107. $this->visitor->setCustomerId('2');
  108. $this->visitor->bindCustomerLogin($observer);
  109. $this->assertNull($this->visitor->getDoCustomerLogin());
  110. $this->assertEquals('2', $this->visitor->getCustomerId());
  111. }
  112. public function testBindCustomerLogout()
  113. {
  114. $observer = new \Magento\Framework\DataObject();
  115. $this->visitor->setCustomerId('1');
  116. $this->visitor->bindCustomerLogout($observer);
  117. $this->assertTrue($this->visitor->getDoCustomerLogout());
  118. $this->visitor->unsetData();
  119. $this->visitor->bindCustomerLogout($observer);
  120. $this->assertNull($this->visitor->getDoCustomerLogout());
  121. }
  122. public function testBindQuoteCreate()
  123. {
  124. $quote = new \Magento\Framework\DataObject(['id' => '1', 'is_checkout_cart' => true]);
  125. $observer = new \Magento\Framework\DataObject([
  126. 'event' => new \Magento\Framework\DataObject(['quote' => $quote]),
  127. ]);
  128. $this->visitor->bindQuoteCreate($observer);
  129. $this->assertTrue($this->visitor->getDoQuoteCreate());
  130. }
  131. public function testBindQuoteDestroy()
  132. {
  133. $quote = new \Magento\Framework\DataObject(['id' => '1']);
  134. $observer = new \Magento\Framework\DataObject([
  135. 'event' => new \Magento\Framework\DataObject(['quote' => $quote]),
  136. ]);
  137. $this->visitor->bindQuoteDestroy($observer);
  138. $this->assertTrue($this->visitor->getDoQuoteDestroy());
  139. }
  140. public function testClean()
  141. {
  142. $this->resource->expects($this->once())->method('clean')->with($this->visitor)->willReturnSelf();
  143. $this->visitor->clean();
  144. }
  145. }