Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
SetConversionValueObserverTest.php
Go to the documentation of this file.
1 <?php
7 
9 
10 class SetConversionValueObserverTest extends \PHPUnit\Framework\TestCase
11 {
15  protected $_helperMock;
16 
20  protected $_collectionMock;
21 
25  protected $_registryMock;
26 
31 
35  protected $_eventMock;
36 
40  protected $_model;
41 
42  protected function setUp()
43  {
44  $this->_helperMock = $this->createMock(\Magento\GoogleAdwords\Helper\Data::class);
45  $this->_registryMock = $this->createMock(\Magento\Framework\Registry::class);
46  $this->_collectionMock = $this->createMock(\Magento\Sales\Model\ResourceModel\Order\Collection::class);
47  $this->_eventObserverMock = $this->createMock(\Magento\Framework\Event\Observer::class);
48  $this->_eventMock = $this->createPartialMock(\Magento\Framework\Event::class, ['getOrderIds']);
49 
50  $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
51  $this->_model = $objectManager->getObject(
52  \Magento\GoogleAdwords\Observer\SetConversionValueObserver::class,
53  [
54  'helper' => $this->_helperMock,
55  'collection' => $this->_collectionMock,
56  'registry' => $this->_registryMock
57  ]
58  );
59  }
60 
64  public function dataProviderForDisabled()
65  {
66  return [[false, false], [false, true], [true, false]];
67  }
68 
75  public function testSetConversionValueWhenAdwordsDisabled($isActive, $isDynamic)
76  {
77  $this->_helperMock->expects(
78  $this->once()
79  )->method(
80  'isGoogleAdwordsActive'
81  )->will(
82  $this->returnValue($isActive)
83  );
84  $this->_helperMock->expects($this->any())->method('isDynamicConversionValue')->will(
85  $this->returnCallback(
86  function () use ($isDynamic) {
87  return $isDynamic;
88  }
89  )
90  );
91 
92  $this->_eventMock->expects($this->never())->method('getOrderIds');
93  $this->assertSame($this->_model, $this->_model->execute($this->_eventObserverMock));
94  }
95 
99  public function dataProviderForOrdersIds()
100  {
101  return [[[]], ['']];
102  }
103 
110  {
111  $this->_helperMock->expects($this->once())->method('isGoogleAdwordsActive')->will($this->returnValue(true));
112  $this->_helperMock->expects($this->once())->method('isDynamicConversionValue')->will($this->returnValue(true));
113  $this->_eventMock->expects($this->once())->method('getOrderIds')->will($this->returnValue($ordersIds));
114  $this->_eventObserverMock->expects(
115  $this->once()
116  )->method(
117  'getEvent'
118  )->will(
119  $this->returnValue($this->_eventMock)
120  );
121  $this->_collectionMock->expects($this->never())->method('addFieldToFilter');
122 
123  $this->assertSame($this->_model, $this->_model->execute($this->_eventObserverMock));
124  }
125 
130  {
131  $ordersIds = [1, 2, 3];
132  $conversionValue = 0;
133  $conversionCurrency = 'USD';
134  $this->_helperMock->expects($this->once())->method('isGoogleAdwordsActive')->will($this->returnValue(true));
135  $this->_helperMock->expects($this->once())->method('isDynamicConversionValue')->will($this->returnValue(true));
136  $this->_helperMock->expects($this->once())->method('hasSendConversionValueCurrency')
137  ->will($this->returnValue(true));
138  $this->_eventMock->expects($this->once())->method('getOrderIds')->will($this->returnValue($ordersIds));
139  $this->_eventObserverMock->expects(
140  $this->once()
141  )->method(
142  'getEvent'
143  )->will(
144  $this->returnValue($this->_eventMock)
145  );
146 
147  $orderMock = $this->createMock(\Magento\Sales\Api\Data\OrderInterface::class);
148  $orderMock->expects($this->once())->method('getOrderCurrencyCode')->willReturn($conversionCurrency);
149 
150  $iteratorMock = new \ArrayIterator([$orderMock]);
151  $this->_collectionMock->expects($this->any())->method('getIterator')->will($this->returnValue($iteratorMock));
152  $this->_collectionMock->expects(
153  $this->once()
154  )->method(
155  'addFieldToFilter'
156  )->with(
157  'entity_id',
158  ['in' => $ordersIds]
159  );
160  $this->_registryMock->expects(
161  $this->atLeastOnce()
162  )->method(
163  'register'
164  )->withConsecutive(
165  [
167  $conversionCurrency
168  ],
169  [
171  $conversionValue,
172  ]
173  );
174 
175  $this->assertSame($this->_model, $this->_model->execute($this->_eventObserverMock));
176  }
177 }
$objectManager
Definition: bootstrap.php:17
const CONVERSION_VALUE_CURRENCY_REGISTRY_NAME
Definition: Data.php:42