Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ApplyPersistentDataObserverTest.php
Go to the documentation of this file.
1 <?php
9 
10 class ApplyPersistentDataObserverTest extends \PHPUnit\Framework\TestCase
11 {
15  protected $model;
16 
20  protected $sessionMock;
21 
26 
31 
36 
40  protected $observerMock;
41 
45  protected $configMock;
46 
47  protected function setUp()
48  {
49  $this->sessionMock = $this->createMock(\Magento\Persistent\Helper\Session::class);
50  $this->customerSessionMock = $this->createMock(\Magento\Customer\Model\Session::class);
51  $this->persistentHelperMock = $this->createMock(\Magento\Persistent\Helper\Data::class);
52  $this->observerMock = $this->createMock(\Magento\Framework\Event\Observer::class);
53  $this->persistentConfigMock = $this->createMock(\Magento\Persistent\Model\Persistent\Config::class);
54  $this->configMock =
55  $this->createPartialMock(\Magento\Persistent\Model\Persistent\ConfigFactory::class, ['create']);
56  $this->model = new \Magento\Persistent\Observer\ApplyPersistentDataObserver(
57  $this->sessionMock,
58  $this->persistentHelperMock,
59  $this->customerSessionMock,
60  $this->configMock
61  );
62  }
63 
65  {
66  $this->persistentHelperMock
67  ->expects($this->once())
68  ->method('canProcess')
69  ->with($this->observerMock)
70  ->will($this->returnValue(false));
71  $this->configMock->expects($this->never())->method('create');
72  $this->model->execute($this->observerMock);
73  }
74 
76  {
77  $this->persistentHelperMock
78  ->expects($this->once())
79  ->method('canProcess')
80  ->with($this->observerMock)
81  ->will($this->returnValue(true));
82  $this->sessionMock->expects($this->once())->method('isPersistent')->will($this->returnValue(false));
83  $this->configMock->expects($this->never())->method('create');
84  $this->model->execute($this->observerMock);
85  }
86 
88  {
89  $this->persistentHelperMock
90  ->expects($this->once())
91  ->method('canProcess')
92  ->with($this->observerMock)
93  ->will($this->returnValue(true));
94  $this->sessionMock->expects($this->once())->method('isPersistent')->will($this->returnValue(true));
95  $this->customerSessionMock->expects($this->once())->method('isLoggedIn')->will($this->returnValue(true));
96  $this->configMock->expects($this->never())->method('create');
97  $this->model->execute($this->observerMock);
98  }
99 
100  public function testExecute()
101  {
102  $this->persistentHelperMock
103  ->expects($this->once())
104  ->method('canProcess')
105  ->with($this->observerMock)
106  ->will($this->returnValue(true));
107  $this->sessionMock->expects($this->once())->method('isPersistent')->will($this->returnValue(true));
108  $this->customerSessionMock->expects($this->once())->method('isLoggedIn')->will($this->returnValue(false));
109  $this->configMock
110  ->expects($this->once())
111  ->method('create')
112  ->will($this->returnValue($this->persistentConfigMock));
113  $this->persistentHelperMock
114  ->expects($this->once())
115  ->method('getPersistentConfigFilePath')
116  ->will($this->returnValue('path/path1'));
117  $this->persistentConfigMock
118  ->expects($this->once())
119  ->method('setConfigFilePath')
120  ->with('path/path1')
121  ->will($this->returnSelf());
122  $this->persistentConfigMock->expects($this->once())->method('fire');
123  $this->model->execute($this->observerMock);
124  }
125 }