Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ApplyBlockPersistentDataObserverTest.php
Go to the documentation of this file.
1 <?php
9 
10 class ApplyBlockPersistentDataObserverTest extends \PHPUnit\Framework\TestCase
11 {
15  protected $model;
16 
20  protected $sessionMock;
21 
26 
31 
35  protected $observerMock;
36 
40  protected $configMock;
41 
45  protected $eventMock;
46 
50  protected $blockMock;
51 
56 
57  protected function setUp()
58  {
59  $eventMethods = ['getConfigFilePath', 'getBlock', '__wakeUp'];
60  $this->sessionMock = $this->createMock(\Magento\Persistent\Helper\Session::class);
61  $this->customerSessionMock = $this->createMock(\Magento\Customer\Model\Session::class);
62  $this->persistentHelperMock = $this->createMock(\Magento\Persistent\Helper\Data::class);
63  $this->configMock =
64  $this->createPartialMock(\Magento\Persistent\Model\Persistent\ConfigFactory::class, ['create']);
65  $this->observerMock = $this->createMock(\Magento\Framework\Event\Observer::class);
66  $this->eventMock = $this->createPartialMock(\Magento\Framework\Event::class, $eventMethods);
67  $this->blockMock = $this->createMock(\Magento\Framework\View\Element\AbstractBlock::class);
68  $this->persistentConfigMock = $this->createMock(\Magento\Persistent\Model\Persistent\Config::class);
69  $this->model = new \Magento\Persistent\Observer\ApplyBlockPersistentDataObserver(
70  $this->sessionMock,
71  $this->persistentHelperMock,
72  $this->customerSessionMock,
73  $this->configMock
74  );
75  }
76 
78  {
79  $this->sessionMock->expects($this->once())->method('isPersistent')->will($this->returnValue(false));
80  $this->observerMock->expects($this->never())->method('getEvent');
81  $this->model->execute($this->observerMock);
82  }
83 
85  {
86  $this->sessionMock->expects($this->once())->method('isPersistent')->will($this->returnValue(true));
87  $this->customerSessionMock->expects($this->once())->method('isLoggedIn')->will($this->returnValue(true));
88  $this->observerMock->expects($this->never())->method('getEvent');
89  $this->model->execute($this->observerMock);
90  }
91 
93  {
94  $this->sessionMock->expects($this->once())->method('isPersistent')->will($this->returnValue(true));
95  $this->customerSessionMock->expects($this->once())->method('isLoggedIn')->will($this->returnValue(false));
96  $this->observerMock->expects($this->once())->method('getEvent')->will($this->returnValue($this->eventMock));
97  $this->eventMock->expects($this->once())->method('getBlock')->will($this->returnValue(null));
98  $this->eventMock->expects($this->never())->method('getConfigFilePath');
99  $this->model->execute($this->observerMock);
100  }
101 
103  {
104  $this->sessionMock->expects($this->once())->method('isPersistent')->will($this->returnValue(true));
105  $this->customerSessionMock->expects($this->once())->method('isLoggedIn')->will($this->returnValue(false));
106  $this->observerMock
107  ->expects($this->any())
108  ->method('getEvent')
109  ->will($this->returnValue($this->eventMock));
110  $this->eventMock->expects($this->once())->method('getBlock')->will($this->returnValue($this->blockMock));
111  $this->eventMock->expects($this->once())->method('getConfigFilePath')->will($this->returnValue(false));
112  $this->persistentHelperMock
113  ->expects($this->once())
114  ->method('getPersistentConfigFilePath')
115  ->will($this->returnValue('path1/path2'));
116  $this->configMock
117  ->expects($this->once())
118  ->method('create')
119  ->will($this->returnValue($this->persistentConfigMock));
120  $this->persistentConfigMock->expects($this->once())->method('setConfigFilePath')->with('path1/path2');
121  $this->persistentConfigMock
122  ->expects($this->once())
123  ->method('getBlockConfigInfo')
124  ->with(get_class($this->blockMock))
125  ->will($this->returnValue(['persistentConfigInfo']));
126  $this->persistentConfigMock
127  ->expects($this->once())
128  ->method('fireOne')
129  ->with('persistentConfigInfo', $this->blockMock);
130  $this->model->execute($this->observerMock);
131  }
132 
133  public function testExecute()
134  {
135  $this->sessionMock->expects($this->once())->method('isPersistent')->will($this->returnValue(true));
136  $this->customerSessionMock->expects($this->once())->method('isLoggedIn')->will($this->returnValue(false));
137  $this->observerMock
138  ->expects($this->any())
139  ->method('getEvent')
140  ->will($this->returnValue($this->eventMock));
141  $this->eventMock->expects($this->once())->method('getBlock')->will($this->returnValue($this->blockMock));
142  $this->eventMock->expects($this->once())->method('getConfigFilePath')->will($this->returnValue('path1/path2'));
143  $this->persistentHelperMock
144  ->expects($this->never())
145  ->method('getPersistentConfigFilePath');
146  $this->configMock
147  ->expects($this->once())
148  ->method('create')
149  ->will($this->returnValue($this->persistentConfigMock));
150  $this->persistentConfigMock->expects($this->once())->method('setConfigFilePath')->with('path1/path2');
151  $this->persistentConfigMock
152  ->expects($this->once())
153  ->method('getBlockConfigInfo')
154  ->with(get_class($this->blockMock))
155  ->will($this->returnValue(['persistentConfigInfo']));
156  $this->persistentConfigMock
157  ->expects($this->once())
158  ->method('fireOne')
159  ->with('persistentConfigInfo', $this->blockMock);
160  $this->model->execute($this->observerMock);
161  }
162 }