Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
SynchronizeWebsiteAttributesOnStoreChangeTest.php
Go to the documentation of this file.
1 <?php
8 
13 
14 class SynchronizeWebsiteAttributesOnStoreChangeTest extends \PHPUnit\Framework\TestCase
15 {
20  public function testExecuteInvalidStore($invalidDataObject)
21  {
22  $eventObserver = new Observer([
23  'data_object' => $invalidDataObject,
24  ]);
25 
26  $synchronizerMock = $this->getMockBuilder(WebsiteAttributesSynchronizer::class)
27  ->disableOriginalConstructor()
28  ->setMethods([
29  'scheduleSynchronization',
30  ])
31  ->getMock();
32 
33  $synchronizerMock->expects($this->never())
34  ->method('scheduleSynchronization');
35 
36  $instance = new SynchronizeWebsiteAttributesOnStoreChange($synchronizerMock);
37  $result = $instance->execute($eventObserver);
38  $this->assertNull($result);
39  }
40 
45  {
46  return [
47  [
48  ['invalidDataObject'],
49  ],
50  ];
51  }
52 
58  {
59  $eventObserver = new Observer([
60  'data_object' => $store,
61  ]);
62 
63  $synchronizerMock = $this->getMockBuilder(WebsiteAttributesSynchronizer::class)
64  ->disableOriginalConstructor()
65  ->setMethods([
66  'scheduleSynchronization',
67  ])
68  ->getMock();
69 
70  $synchronizerMock->expects($this->never())
71  ->method('scheduleSynchronization');
72 
73  $instance = new SynchronizeWebsiteAttributesOnStoreChange($synchronizerMock);
74  $result = $instance->execute($eventObserver);
75  $this->assertNull($result);
76  }
77 
82  {
83  $store = $this->getMockBuilder(Store::class)
84  ->disableOriginalConstructor()
85  ->setMethods([
86  'hasDataChanges',
87  'getOrigData',
88  ])
89  ->getMock();
90 
91  $store->expects($this->once())
92  ->method('hasDataChanges')
93  ->will(
94  $this->returnValue(false)
95  );
96 
97  $store->expects($this->never())
98  ->method('getOrigData');
99 
100  return [
101  [
102  $store,
103  ],
104  ];
105  }
106 
112  {
113  $eventObserver = new Observer([
114  'data_object' => $store,
115  ]);
116 
117  $synchronizerMock = $this->getMockBuilder(WebsiteAttributesSynchronizer::class)
118  ->disableOriginalConstructor()
119  ->setMethods([
120  'scheduleSynchronization',
121  ])
122  ->getMock();
123 
124  $synchronizerMock->expects($this->never())
125  ->method('scheduleSynchronization');
126 
127  $instance = new SynchronizeWebsiteAttributesOnStoreChange($synchronizerMock);
128  $result = $instance->execute($eventObserver);
129  $this->assertNull($result);
130  }
131 
136  {
137  $sameWebsiteId = 1;
138  $store = $this->getMockBuilder(Store::class)
139  ->disableOriginalConstructor()
140  ->setMethods([
141  'hasDataChanges',
142  'getOrigData',
143  'getWebsiteId',
144  'isObjectNew',
145  ])
146  ->getMock();
147 
148  $store->expects($this->once())
149  ->method('hasDataChanges')
150  ->will(
151  $this->returnValue(true)
152  );
153 
154  $store->expects($this->once())
155  ->method('getOrigData')
156  ->with('website_id')
157  ->will(
158  $this->returnValue($sameWebsiteId)
159  );
160 
161  $store->expects($this->once())
162  ->method('getWebsiteId')
163  ->will(
164  $this->returnValue($sameWebsiteId)
165  );
166 
167  $store->expects($this->once())
168  ->method('isObjectNew')
169  ->will(
170  $this->returnValue(false)
171  );
172 
173  return [
174  [
175  $store,
176  ],
177  ];
178  }
179 
184  public function testExecuteSuccess(Store $store)
185  {
186  $eventObserver = new Observer([
187  'data_object' => $store,
188  ]);
189 
190  $synchronizerMock = $this->getMockBuilder(WebsiteAttributesSynchronizer::class)
191  ->disableOriginalConstructor()
192  ->setMethods([
193  'scheduleSynchronization',
194  ])
195  ->getMock();
196 
197  $synchronizerMock->expects($this->once())
198  ->method('scheduleSynchronization');
199 
200  $instance = new SynchronizeWebsiteAttributesOnStoreChange($synchronizerMock);
201  $result = $instance->execute($eventObserver);
202  $this->assertNull($result);
203  }
204 
208  public function executeSuccessDataProvider()
209  {
210  $sameWebsiteId = 1;
211  $storeNew = $this->getMockBuilder(Store::class)
212  ->disableOriginalConstructor()
213  ->setMethods([
214  'hasDataChanges',
215  'getOrigData',
216  'getWebsiteId',
217  'isObjectNew',
218  ])
219  ->getMock();
220 
221  $storeNew->expects($this->once())
222  ->method('hasDataChanges')
223  ->will(
224  $this->returnValue(true)
225  );
226 
227  $storeNew->expects($this->once())
228  ->method('getOrigData')
229  ->with('website_id')
230  ->will(
231  $this->returnValue($sameWebsiteId)
232  );
233 
234  $storeNew->expects($this->once())
235  ->method('getWebsiteId')
236  ->will(
237  $this->returnValue($sameWebsiteId)
238  );
239 
240  $storeNew->expects($this->once())
241  ->method('isObjectNew')
242  ->will(
243  $this->returnValue(true)
244  );
245 
246  $sameWebsiteId = 1;
247  $newWebsiteId = 2;
248  $storeChangedWebsite = $this->getMockBuilder(Store::class)
249  ->disableOriginalConstructor()
250  ->setMethods([
251  'hasDataChanges',
252  'getOrigData',
253  'getWebsiteId',
254  'isObjectNew',
255  ])
256  ->getMock();
257 
258  $storeChangedWebsite->expects($this->once())
259  ->method('hasDataChanges')
260  ->will(
261  $this->returnValue(true)
262  );
263 
264  $storeChangedWebsite->expects($this->once())
265  ->method('getOrigData')
266  ->with('website_id')
267  ->will(
268  $this->returnValue($sameWebsiteId)
269  );
270 
271  $storeChangedWebsite->expects($this->once())
272  ->method('getWebsiteId')
273  ->will(
274  $this->returnValue($newWebsiteId)
275  );
276 
277  $storeChangedWebsite->expects($this->once())
278  ->method('isObjectNew')
279  ->will(
280  $this->returnValue(false)
281  );
282 
283  return [
284  [
285  $storeNew,
286  ],
287  [
288  $storeChangedWebsite,
289  ],
290  ];
291  }
292 }