Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
VisitorTest.php
Go to the documentation of this file.
1 <?php
8 
10 
15 class VisitorTest extends \PHPUnit\Framework\TestCase
16 {
20  protected $visitor;
21 
26 
30  protected $registry;
31 
35  protected $resource;
36 
40  protected $session;
41 
42  protected function setUp()
43  {
44  $this->registry = $this->createMock(\Magento\Framework\Registry::class);
45  $this->session = $this->getMockBuilder(\Magento\Customer\Model\Session::class)
46  ->disableOriginalConstructor()
47  ->setMethods(['getSessionId', 'getVisitorData', 'setVisitorData'])
48  ->getMock();
49 
50  $this->objectManagerHelper = new ObjectManagerHelper($this);
51 
52  $this->resource = $this->getMockBuilder(\Magento\Customer\Model\ResourceModel\Visitor::class)
53  ->setMethods([
54  'beginTransaction',
55  '__sleep',
56  '__wakeup',
57  'getIdFieldName',
58  'save',
59  'addCommitCallback',
60  'commit',
61  'clean',
62  ])->disableOriginalConstructor()->getMock();
63  $this->resource->expects($this->any())->method('getIdFieldName')->willReturn('visitor_id');
64  $this->resource->expects($this->any())->method('addCommitCallback')->willReturnSelf();
65 
66  $arguments = $this->objectManagerHelper->getConstructArguments(
67  \Magento\Customer\Model\Visitor::class,
68  [
69  'registry' => $this->registry,
70  'session' => $this->session,
71  'resource' => $this->resource,
72  ]
73  );
74 
75  $this->visitor = $this->objectManagerHelper->getObject(\Magento\Customer\Model\Visitor::class, $arguments);
76  }
77 
78  public function testInitByRequest()
79  {
80  $oldSessionId = 'asdfhasdfjhkj2198sadf8sdf897';
81  $newSessionId = 'bsdfhasdfjhkj2198sadf8sdf897';
82  $this->session->expects($this->any())->method('getSessionId')->willReturn($newSessionId);
83  $this->session->expects($this->atLeastOnce())->method('getVisitorData')
84  ->willReturn(['session_id' => $oldSessionId]);
85  $this->visitor->initByRequest(null);
86  $this->assertEquals($newSessionId, $this->visitor->getSessionId());
87  }
88 
89  public function testSaveByRequest()
90  {
91  $this->session->expects($this->once())->method('setVisitorData')->will($this->returnSelf());
92  $this->assertSame($this->visitor, $this->visitor->saveByRequest(null));
93  }
94 
95  public function testIsModuleIgnored()
96  {
97  $this->visitor = $this->objectManagerHelper->getObject(
98  \Magento\Customer\Model\Visitor::class,
99  [
100  'registry' => $this->registry,
101  'session' => $this->session,
102  'resource' => $this->resource,
103  'ignores' => ['test_route_name' => true],
104  ]
105  );
106  $request = new \Magento\Framework\DataObject(['route_name' => 'test_route_name']);
107  $action = new \Magento\Framework\DataObject(['request' => $request]);
108  $event = new \Magento\Framework\DataObject(['controller_action' => $action]);
109  $observer = new \Magento\Framework\DataObject(['event' => $event]);
110  $this->assertTrue($this->visitor->isModuleIgnored($observer));
111  }
112 
113  public function testBindCustomerLogin()
114  {
115  $customer = new \Magento\Framework\DataObject(['id' => '1']);
116  $observer = new \Magento\Framework\DataObject([
117  'event' => new \Magento\Framework\DataObject(['customer' => $customer]),
118  ]);
119 
120  $this->visitor->bindCustomerLogin($observer);
121  $this->assertTrue($this->visitor->getDoCustomerLogin());
122  $this->assertEquals($customer->getId(), $this->visitor->getCustomerId());
123 
124  $this->visitor->unsetData();
125  $this->visitor->setCustomerId('2');
126  $this->visitor->bindCustomerLogin($observer);
127  $this->assertNull($this->visitor->getDoCustomerLogin());
128  $this->assertEquals('2', $this->visitor->getCustomerId());
129  }
130 
131  public function testBindCustomerLogout()
132  {
133  $observer = new \Magento\Framework\DataObject();
134 
135  $this->visitor->setCustomerId('1');
136  $this->visitor->bindCustomerLogout($observer);
137  $this->assertTrue($this->visitor->getDoCustomerLogout());
138 
139  $this->visitor->unsetData();
140  $this->visitor->bindCustomerLogout($observer);
141  $this->assertNull($this->visitor->getDoCustomerLogout());
142  }
143 
144  public function testBindQuoteCreate()
145  {
146  $quote = new \Magento\Framework\DataObject(['id' => '1', 'is_checkout_cart' => true]);
147  $observer = new \Magento\Framework\DataObject([
148  'event' => new \Magento\Framework\DataObject(['quote' => $quote]),
149  ]);
150  $this->visitor->bindQuoteCreate($observer);
151  $this->assertTrue($this->visitor->getDoQuoteCreate());
152  }
153 
154  public function testBindQuoteDestroy()
155  {
156  $quote = new \Magento\Framework\DataObject(['id' => '1']);
157  $observer = new \Magento\Framework\DataObject([
158  'event' => new \Magento\Framework\DataObject(['quote' => $quote]),
159  ]);
160  $this->visitor->bindQuoteDestroy($observer);
161  $this->assertTrue($this->visitor->getDoQuoteDestroy());
162  }
163 
164  public function testClean()
165  {
166  $this->resource->expects($this->once())->method('clean')->with($this->visitor)->willReturnSelf();
167  $this->visitor->clean();
168  }
169 }
$customer
Definition: customers.php:11
$quote
$arguments