Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
DeleteCategoryGoogleExperimentScriptObserverTest.php
Go to the documentation of this file.
1 <?php
7 
8 class DeleteCategoryGoogleExperimentScriptObserverTest extends \PHPUnit\Framework\TestCase
9 {
13  protected $_codeMock;
14 
18  protected $_category;
19 
24 
28  protected $_model;
29 
30  protected function setUp()
31  {
32  $this->_codeMock = $this->createMock(\Magento\GoogleOptimizer\Model\Code::class);
33  $this->_category = $this->createMock(\Magento\Catalog\Model\Category::class);
34  $event = $this->createPartialMock(\Magento\Framework\Event::class, ['getCategory']);
35  $event->expects($this->once())->method('getCategory')->will($this->returnValue($this->_category));
36  $this->_eventObserverMock = $this->createMock(\Magento\Framework\Event\Observer::class);
37  $this->_eventObserverMock->expects($this->once())->method('getEvent')->will($this->returnValue($event));
38 
39  $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
40  $this->_model = $objectManagerHelper->getObject(
41  \Magento\GoogleOptimizer\Observer\Category\DeleteCategoryGoogleExperimentScriptObserver::class,
42  ['modelCode' => $this->_codeMock]
43  );
44  }
45 
47  {
48  $entityId = 3;
49  $storeId = 0;
50 
51  $this->_category->expects($this->once())->method('getId')->will($this->returnValue($entityId));
52  $this->_category->expects($this->once())->method('getStoreId')->will($this->returnValue($storeId));
53 
54  $this->_codeMock->expects(
55  $this->once()
56  )->method(
57  'loadByEntityIdAndType'
58  )->with(
59  $entityId,
60  \Magento\GoogleOptimizer\Model\Code::ENTITY_TYPE_CATEGORY,
61  $storeId
62  );
63  $this->_codeMock->expects($this->once())->method('getId')->will($this->returnValue(2));
64  $this->_codeMock->expects($this->once())->method('delete');
65 
66  $this->_model->execute($this->_eventObserverMock);
67  }
68 
70  {
71  $entityId = 3;
72  $storeId = 0;
73 
74  $this->_category->expects($this->once())->method('getId')->will($this->returnValue($entityId));
75  $this->_category->expects($this->once())->method('getStoreId')->will($this->returnValue($storeId));
76 
77  $this->_codeMock->expects(
78  $this->once()
79  )->method(
80  'loadByEntityIdAndType'
81  )->with(
82  $entityId,
83  \Magento\GoogleOptimizer\Model\Code::ENTITY_TYPE_CATEGORY,
84  $storeId
85  );
86  $this->_codeMock->expects($this->once())->method('getId')->will($this->returnValue(0));
87  $this->_codeMock->expects($this->never())->method('delete');
88 
89  $this->_model->execute($this->_eventObserverMock);
90  }
91 }