Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
SaveGoogleExperimentScriptObserverTest.php
Go to the documentation of this file.
1 <?php
7 
8 class SaveGoogleExperimentScriptObserverTest extends \PHPUnit\Framework\TestCase
9 {
13  protected $_helperMock;
14 
19 
23  protected $_pageMock;
24 
28  protected $_codeMock;
29 
33  protected $_requestMock;
34 
38  protected $_modelObserver;
39 
43  protected $_storeId;
44 
45  protected function setUp()
46  {
47  $this->_helperMock = $this->createMock(\Magento\GoogleOptimizer\Helper\Data::class);
48  $this->_codeMock = $this->createMock(\Magento\GoogleOptimizer\Model\Code::class);
49  $this->_requestMock = $this->createMock(\Magento\Framework\App\RequestInterface::class);
50 
51  $this->_pageMock = $this->createMock(\Magento\Cms\Model\Page::class);
52  $event = $this->createPartialMock(\Magento\Framework\Event::class, ['getObject']);
53  $event->expects($this->once())->method('getObject')->will($this->returnValue($this->_pageMock));
54  $this->_eventObserverMock = $this->createMock(\Magento\Framework\Event\Observer::class);
55  $this->_eventObserverMock->expects($this->once())->method('getEvent')->will($this->returnValue($event));
56 
57  $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
58  $this->_modelObserver = $objectManagerHelper->getObject(
59  \Magento\GoogleOptimizer\Observer\CmsPage\SaveGoogleExperimentScriptObserver::class,
60  ['helper' => $this->_helperMock, 'modelCode' => $this->_codeMock, 'request' => $this->_requestMock]
61  );
62  }
63 
65  {
66  $pageId = 3;
67  $experimentScript = 'some string';
68 
69  $this->_pageMock->expects($this->once())->method('getId')->will($this->returnValue($pageId));
70  $this->_helperMock->expects($this->once())->method('isGoogleExperimentActive')->will($this->returnValue(true));
71 
72  $this->_requestMock->expects(
73  $this->exactly(3)
74  )->method(
75  'getParam'
76  )->with(
77  'google_experiment'
78  )->will(
79  $this->returnValue(['code_id' => '', 'experiment_script' => $experimentScript])
80  );
81 
82  $this->_codeMock->expects(
83  $this->once()
84  )->method(
85  'addData'
86  )->with(
87  [
88  'entity_type' => \Magento\GoogleOptimizer\Model\Code::ENTITY_TYPE_PAGE,
89  'entity_id' => $pageId,
90  'store_id' => 0,
91  'experiment_script' => $experimentScript,
92  ]
93  );
94  $this->_codeMock->expects($this->once())->method('save');
95 
96  $this->_modelObserver->execute($this->_eventObserverMock);
97  }
98 
104  {
105  $this->_helperMock->expects($this->once())->method('isGoogleExperimentActive')->will($this->returnValue(true));
106 
107  $this->_requestMock->expects(
108  $this->once()
109  )->method(
110  'getParam'
111  )->with(
112  'google_experiment'
113  )->will(
114  $this->returnValue($params)
115  );
116 
117  $this->_modelObserver->execute($this->_eventObserverMock);
118  }
119 
124  {
125  return [
126  // if param 'google_experiment' is not array
127  ['wrong type'],
128  // if param 'experiment_script' is missed
129  [['code_id' => '']],
130  // if param 'code_id' is missed
131  [['experiment_script' => '']]];
132  }
133 
135  {
136  $pageId = 3;
137  $experimentScript = 'some string';
138  $codeId = 5;
139 
140  $this->_pageMock->expects($this->once())->method('getId')->will($this->returnValue($pageId));
141  $this->_helperMock->expects($this->once())->method('isGoogleExperimentActive')->will($this->returnValue(true));
142 
143  $this->_requestMock->expects(
144  $this->exactly(3)
145  )->method(
146  'getParam'
147  )->with(
148  'google_experiment'
149  )->will(
150  $this->returnValue(['code_id' => $codeId, 'experiment_script' => $experimentScript])
151  );
152 
153  $this->_codeMock->expects($this->once())->method('load')->with($codeId);
154  $this->_codeMock->expects($this->once())->method('getId')->will($this->returnValue($codeId));
155 
156  $this->_codeMock->expects(
157  $this->once()
158  )->method(
159  'addData'
160  )->with(
161  [
162  'entity_type' => \Magento\GoogleOptimizer\Model\Code::ENTITY_TYPE_PAGE,
163  'entity_id' => $pageId,
164  'store_id' => $this->_storeId,
165  'experiment_script' => $experimentScript,
166  ]
167  );
168  $this->_codeMock->expects($this->once())->method('save');
169 
170  $this->_modelObserver->execute($this->_eventObserverMock);
171  }
172 
178  {
179  $experimentScript = 'some string';
180  $codeId = 5;
181 
182  $this->_helperMock->expects($this->once())->method('isGoogleExperimentActive')->will($this->returnValue(true));
183 
184  $this->_requestMock->expects(
185  $this->exactly(3)
186  )->method(
187  'getParam'
188  )->with(
189  'google_experiment'
190  )->will(
191  $this->returnValue(['code_id' => $codeId, 'experiment_script' => $experimentScript])
192  );
193 
194  $this->_codeMock->expects($this->once())->method('load')->with($codeId);
195  $this->_codeMock->expects($this->atLeastOnce())->method('getId')->will($this->returnValue(false));
196  $this->_codeMock->expects($this->never())->method('save');
197 
198  $this->_modelObserver->execute($this->_eventObserverMock);
199  }
200 
201  public function testRemovingCode()
202  {
203  $codeId = 5;
204 
205  $this->_helperMock->expects(
206  $this->once()
207  )->method(
208  'isGoogleExperimentActive'
209  )->with(
210  $this->_storeId
211  )->will(
212  $this->returnValue(true)
213  );
214 
215  $this->_requestMock->expects(
216  $this->exactly(3)
217  )->method(
218  'getParam'
219  )->with(
220  'google_experiment'
221  )->will(
222  $this->returnValue(['code_id' => $codeId, 'experiment_script' => ''])
223  );
224 
225  $this->_codeMock->expects($this->once())->method('load')->with($codeId);
226  $this->_codeMock->expects($this->once())->method('getId')->will($this->returnValue($codeId));
227 
228  $this->_codeMock->expects($this->never())->method('save');
229  $this->_codeMock->expects($this->once())->method('delete');
230 
231  $this->_modelObserver->execute($this->_eventObserverMock);
232  }
233 
235  {
236  $this->_helperMock->expects(
237  $this->once()
238  )->method(
239  'isGoogleExperimentActive'
240  )->with(
241  $this->_storeId
242  )->will(
243  $this->returnValue(false)
244  );
245  $this->_codeMock->expects($this->never())->method('load');
246  $this->_codeMock->expects($this->never())->method('save');
247  $this->_codeMock->expects($this->never())->method('delete');
248 
249  $this->_modelObserver->execute($this->_eventObserverMock);
250  }
251 }
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18