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 $_productMock;
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->_productMock = $this->createMock(\Magento\Catalog\Model\Product::class);
49  $this->_storeId = 0;
50  $this->_productMock->expects(
51  $this->atLeastOnce()
52  )->method(
53  'getStoreId'
54  )->will(
55  $this->returnValue($this->_storeId)
56  );
57  $event = $this->createPartialMock(\Magento\Framework\Event::class, ['getProduct']);
58  $event->expects($this->once())->method('getProduct')->will($this->returnValue($this->_productMock));
59  $this->_eventObserverMock = $this->createMock(\Magento\Framework\Event\Observer::class);
60  $this->_eventObserverMock->expects($this->once())->method('getEvent')->will($this->returnValue($event));
61  $this->_codeMock = $this->createMock(\Magento\GoogleOptimizer\Model\Code::class);
62  $this->_requestMock = $this->createMock(\Magento\Framework\App\RequestInterface::class);
63 
64  $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
65  $this->_modelObserver = $objectManagerHelper->getObject(
66  \Magento\GoogleOptimizer\Observer\Product\SaveGoogleExperimentScriptObserver::class,
67  ['helper' => $this->_helperMock, 'modelCode' => $this->_codeMock, 'request' => $this->_requestMock]
68  );
69  }
70 
72  {
73  $productId = 3;
74  $experimentScript = 'some string';
75 
76  $this->_productMock->expects($this->once())->method('getId')->will($this->returnValue($productId));
77  $this->_helperMock->expects(
78  $this->once()
79  )->method(
80  'isGoogleExperimentActive'
81  )->with(
82  $this->_storeId
83  )->will(
84  $this->returnValue(true)
85  );
86 
87  $this->_requestMock->expects(
88  $this->exactly(3)
89  )->method(
90  'getParam'
91  )->with(
92  'google_experiment'
93  )->will(
94  $this->returnValue(['code_id' => '', 'experiment_script' => $experimentScript])
95  );
96 
97  $this->_codeMock->expects(
98  $this->once()
99  )->method(
100  'addData'
101  )->with(
102  [
103  'entity_type' => \Magento\GoogleOptimizer\Model\Code::ENTITY_TYPE_PRODUCT,
104  'entity_id' => $productId,
105  'store_id' => $this->_storeId,
106  'experiment_script' => $experimentScript,
107  ]
108  );
109  $this->_codeMock->expects($this->once())->method('save');
110 
111  $this->_modelObserver->execute($this->_eventObserverMock);
112  }
113 
119  {
120  $this->_helperMock->expects(
121  $this->once()
122  )->method(
123  'isGoogleExperimentActive'
124  )->with(
125  $this->_storeId
126  )->will(
127  $this->returnValue(true)
128  );
129 
130  $this->_requestMock->expects(
131  $this->once()
132  )->method(
133  'getParam'
134  )->with(
135  'google_experiment'
136  )->will(
137  $this->returnValue($params)
138  );
139 
140  $this->_modelObserver->execute($this->_eventObserverMock);
141  }
142 
147  {
148  return [
149  // if param 'google_experiment' is not array
150  ['wrong type'],
151  // if param 'experiment_script' is missed
152  [['code_id' => '']],
153  // if param 'code_id' is missed
154  [['experiment_script' => '']]];
155  }
156 
158  {
159  $productId = 3;
160  $experimentScript = 'some string';
161  $codeId = 5;
162 
163  $this->_productMock->expects($this->once())->method('getId')->will($this->returnValue($productId));
164  $this->_helperMock->expects(
165  $this->once()
166  )->method(
167  'isGoogleExperimentActive'
168  )->with(
169  $this->_storeId
170  )->will(
171  $this->returnValue(true)
172  );
173 
174  $this->_requestMock->expects(
175  $this->exactly(3)
176  )->method(
177  'getParam'
178  )->with(
179  'google_experiment'
180  )->will(
181  $this->returnValue(['code_id' => $codeId, 'experiment_script' => $experimentScript])
182  );
183 
184  $this->_codeMock->expects($this->once())->method('load')->with($codeId);
185  $this->_codeMock->expects($this->once())->method('getId')->will($this->returnValue($codeId));
186 
187  $this->_codeMock->expects(
188  $this->once()
189  )->method(
190  'addData'
191  )->with(
192  [
193  'entity_type' => \Magento\GoogleOptimizer\Model\Code::ENTITY_TYPE_PRODUCT,
194  'entity_id' => $productId,
195  'store_id' => $this->_storeId,
196  'experiment_script' => $experimentScript,
197  ]
198  );
199  $this->_codeMock->expects($this->once())->method('save');
200 
201  $this->_modelObserver->execute($this->_eventObserverMock);
202  }
203 
209  {
210  $experimentScript = 'some string';
211  $codeId = 5;
212 
213  $this->_helperMock->expects(
214  $this->once()
215  )->method(
216  'isGoogleExperimentActive'
217  )->with(
218  $this->_storeId
219  )->will(
220  $this->returnValue(true)
221  );
222 
223  $this->_requestMock->expects(
224  $this->exactly(3)
225  )->method(
226  'getParam'
227  )->with(
228  'google_experiment'
229  )->will(
230  $this->returnValue(['code_id' => $codeId, 'experiment_script' => $experimentScript])
231  );
232 
233  $this->_codeMock->expects($this->once())->method('load')->with($codeId);
234  $this->_codeMock->expects($this->atLeastOnce())->method('getId')->will($this->returnValue(false));
235  $this->_codeMock->expects($this->never())->method('save');
236 
237  $this->_modelObserver->execute($this->_eventObserverMock);
238  }
239 
240  public function testRemovingCode()
241  {
242  $codeId = 5;
243 
244  $this->_helperMock->expects(
245  $this->once()
246  )->method(
247  'isGoogleExperimentActive'
248  )->with(
249  $this->_storeId
250  )->will(
251  $this->returnValue(true)
252  );
253 
254  $this->_requestMock->expects(
255  $this->exactly(3)
256  )->method(
257  'getParam'
258  )->with(
259  'google_experiment'
260  )->will(
261  $this->returnValue(['code_id' => $codeId, 'experiment_script' => ''])
262  );
263 
264  $this->_codeMock->expects($this->once())->method('load')->with($codeId);
265  $this->_codeMock->expects($this->once())->method('getId')->will($this->returnValue($codeId));
266 
267  $this->_codeMock->expects($this->never())->method('save');
268  $this->_codeMock->expects($this->once())->method('delete');
269 
270  $this->_modelObserver->execute($this->_eventObserverMock);
271  }
272 
274  {
275  $this->_helperMock->expects(
276  $this->once()
277  )->method(
278  'isGoogleExperimentActive'
279  )->with(
280  $this->_storeId
281  )->will(
282  $this->returnValue(false)
283  );
284  $this->_codeMock->expects($this->never())->method('load');
285  $this->_codeMock->expects($this->never())->method('save');
286  $this->_codeMock->expects($this->never())->method('delete');
287 
288  $this->_modelObserver->execute($this->_eventObserverMock);
289  }
290 }
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18