Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
All Data Structures Namespaces Files Functions Variables Pages
GoogleOptimizerTest.php
Go to the documentation of this file.
1 <?php
7 
16 use PHPUnit_Framework_MockObject_MockObject as MockObject;
17 
23 class GoogleOptimizerTest extends \PHPUnit\Framework\TestCase
24 {
29 
33  protected $locatorMock;
34 
38  protected $dataHelperMock;
39 
43  protected $codeHelperMock;
44 
48  protected $productMock;
49 
53  protected $googleOptimizer;
54 
58  protected function setUp()
59  {
60  $this->objectManagerHelper = new ObjectManagerHelper($this);
61 
62  $this->productMock = $this->createMock(\Magento\Catalog\Model\Product::class);
63  $this->locatorMock = $this->createMock(\Magento\Catalog\Model\Locator\LocatorInterface::class);
64  $this->locatorMock->expects($this->any())
65  ->method('getProduct')
66  ->willReturn($this->productMock);
67  $this->dataHelperMock = $this->createMock(\Magento\GoogleOptimizer\Helper\Data::class);
68  $this->codeHelperMock = $this->createMock(\Magento\GoogleOptimizer\Helper\Code::class);
69 
70  $this->googleOptimizer = $this->objectManagerHelper->getObject(
71  GoogleOptimizer::class,
72  [
73  'locator' => $this->locatorMock,
74  'dataHelper' => $this->dataHelperMock,
75  'codeHelper' => $this->codeHelperMock
76  ]
77  );
78  }
79 
84  protected function canShowPanel($flag)
85  {
86  $storeId = 1;
87  $this->productMock->expects($this->any())
88  ->method('getStoreId')
89  ->willReturn($storeId);
90  $this->dataHelperMock->expects($this->once())
91  ->method('isGoogleExperimentActive')
92  ->with($storeId)
93  ->willReturn($flag);
94  }
95 
100  {
101  $this->canShowPanel(false);
102  $this->assertEquals([], $this->googleOptimizer->modifyData([]));
103  }
104 
113  public function testGetDataGoogleExperimentEnabled($productId, $experimentScript, $codeId, $expectedCalls)
114  {
115  $expectedResult[$productId]['google_experiment'] = [
116  'experiment_script' => $experimentScript,
117  'code_id' => $codeId,
118  ];
119 
120  $this->canShowPanel(true);
121 
123  $codeModelMock = $this->createPartialMock(
124  \Magento\GoogleOptimizer\Model\Code::class,
125  ['getExperimentScript', 'getCodeId']
126  );
127  $codeModelMock->expects($this->exactly($expectedCalls))
128  ->method('getExperimentScript')
129  ->willReturn($experimentScript);
130  $codeModelMock->expects($this->exactly($expectedCalls))
131  ->method('getCodeId')
132  ->willReturn($codeId);
133 
134  $this->codeHelperMock->expects($this->exactly($expectedCalls))
135  ->method('getCodeObjectByEntity')
136  ->with($this->productMock)
137  ->willReturn($codeModelMock);
138  $this->productMock->expects($this->atLeastOnce())
139  ->method('getId')
140  ->willReturn($productId);
141 
142  $this->assertEquals($expectedResult, $this->googleOptimizer->modifyData([]));
143  }
144 
149  {
150  return [
151  ['productId' => 2, 'experimentScript' => 'some script', 'codeId' => '3', 'expectedCalls' => 1],
152  ['productId' => null, 'experimentScript' => '', 'codeId' => '', 'expectedCalls' => 0],
153  ];
154  }
155 
160  {
161  $this->canShowPanel(false);
162  $this->assertEquals([], $this->googleOptimizer->modifyMeta([]));
163  }
164 
169  {
171  'arguments' => [
172  'data' => [
173  'config' => [
174  'componentType' => Fieldset::NAME,
175  'label' => __('Product View Optimization'),
176  'collapsible' => true,
177  'opened' => false,
178  'sortOrder' => 100,
179  'dataScope' => 'data.google_experiment',
180  ],
181  ],
182  ],
183  'children' => [
184  'experiment_script' => [
185  'arguments' => [
186  'data' => [
187  'config' => [
188  'componentType' => Field::NAME,
189  'formElement' => Textarea::NAME,
190  'dataType' => Text::NAME,
191  'label' => __('Experiment Code'),
192  'notice' => __('Experiment code should be added to the original page only.'),
193  'dataScope' => 'experiment_script',
194  'sortOrder' => 10,
195  ],
196  ],
197  ],
198  ],
199  'code_id' => [
200  'arguments' => [
201  'data' => [
202  'config' => [
203  'componentType' => Field::NAME,
204  'formElement' => Input::NAME,
205  'dataType' => Text::NAME,
206  'visible' => false,
207  'label' => '',
208  'dataScope' => 'code_id',
209  'sortOrder' => 20,
210  ],
211  ],
212  ],
213  ],
214  ],
215  ];
216 
217  $this->canShowPanel(true);
218  $this->assertEquals($expectedResult, $this->googleOptimizer->modifyMeta([]));
219  }
220 }
__()
Definition: __.php:13