SetConversionValueObserverTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\GoogleAdwords\Test\Unit\Observer;
  7. use Magento\GoogleAdwords\Helper\Data;
  8. class SetConversionValueObserverTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /**
  11. * @var \PHPUnit_Framework_MockObject_MockObject
  12. */
  13. protected $_helperMock;
  14. /**
  15. * @var \PHPUnit_Framework_MockObject_MockObject
  16. */
  17. protected $_collectionMock;
  18. /**
  19. * @var \PHPUnit_Framework_MockObject_MockObject
  20. */
  21. protected $_registryMock;
  22. /**
  23. * @var \PHPUnit_Framework_MockObject_MockObject
  24. */
  25. protected $_eventObserverMock;
  26. /**
  27. * @var \PHPUnit_Framework_MockObject_MockObject
  28. */
  29. protected $_eventMock;
  30. /**
  31. * @var \Magento\GoogleAdwords\Observer\SetConversionValueObserver
  32. */
  33. protected $_model;
  34. protected function setUp()
  35. {
  36. $this->_helperMock = $this->createMock(\Magento\GoogleAdwords\Helper\Data::class);
  37. $this->_registryMock = $this->createMock(\Magento\Framework\Registry::class);
  38. $this->_collectionMock = $this->createMock(\Magento\Sales\Model\ResourceModel\Order\Collection::class);
  39. $this->_eventObserverMock = $this->createMock(\Magento\Framework\Event\Observer::class);
  40. $this->_eventMock = $this->createPartialMock(\Magento\Framework\Event::class, ['getOrderIds']);
  41. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  42. $this->_model = $objectManager->getObject(
  43. \Magento\GoogleAdwords\Observer\SetConversionValueObserver::class,
  44. [
  45. 'helper' => $this->_helperMock,
  46. 'collection' => $this->_collectionMock,
  47. 'registry' => $this->_registryMock
  48. ]
  49. );
  50. }
  51. /**
  52. * @return array
  53. */
  54. public function dataProviderForDisabled()
  55. {
  56. return [[false, false], [false, true], [true, false]];
  57. }
  58. /**
  59. * @param bool $isActive
  60. * @param bool $isDynamic
  61. * @dataProvider dataProviderForDisabled
  62. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  63. */
  64. public function testSetConversionValueWhenAdwordsDisabled($isActive, $isDynamic)
  65. {
  66. $this->_helperMock->expects(
  67. $this->once()
  68. )->method(
  69. 'isGoogleAdwordsActive'
  70. )->will(
  71. $this->returnValue($isActive)
  72. );
  73. $this->_helperMock->expects($this->any())->method('isDynamicConversionValue')->will(
  74. $this->returnCallback(
  75. function () use ($isDynamic) {
  76. return $isDynamic;
  77. }
  78. )
  79. );
  80. $this->_eventMock->expects($this->never())->method('getOrderIds');
  81. $this->assertSame($this->_model, $this->_model->execute($this->_eventObserverMock));
  82. }
  83. /**
  84. * @return array
  85. */
  86. public function dataProviderForOrdersIds()
  87. {
  88. return [[[]], ['']];
  89. }
  90. /**
  91. * @param $ordersIds
  92. * @dataProvider dataProviderForOrdersIds
  93. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  94. */
  95. public function testSetConversionValueWhenAdwordsActiveWithoutOrdersIds($ordersIds)
  96. {
  97. $this->_helperMock->expects($this->once())->method('isGoogleAdwordsActive')->will($this->returnValue(true));
  98. $this->_helperMock->expects($this->once())->method('isDynamicConversionValue')->will($this->returnValue(true));
  99. $this->_eventMock->expects($this->once())->method('getOrderIds')->will($this->returnValue($ordersIds));
  100. $this->_eventObserverMock->expects(
  101. $this->once()
  102. )->method(
  103. 'getEvent'
  104. )->will(
  105. $this->returnValue($this->_eventMock)
  106. );
  107. $this->_collectionMock->expects($this->never())->method('addFieldToFilter');
  108. $this->assertSame($this->_model, $this->_model->execute($this->_eventObserverMock));
  109. }
  110. /**
  111. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  112. */
  113. public function testSetConversionValueWhenAdwordsActiveWithOrdersIds()
  114. {
  115. $ordersIds = [1, 2, 3];
  116. $conversionValue = 0;
  117. $conversionCurrency = 'USD';
  118. $this->_helperMock->expects($this->once())->method('isGoogleAdwordsActive')->will($this->returnValue(true));
  119. $this->_helperMock->expects($this->once())->method('isDynamicConversionValue')->will($this->returnValue(true));
  120. $this->_helperMock->expects($this->once())->method('hasSendConversionValueCurrency')
  121. ->will($this->returnValue(true));
  122. $this->_eventMock->expects($this->once())->method('getOrderIds')->will($this->returnValue($ordersIds));
  123. $this->_eventObserverMock->expects(
  124. $this->once()
  125. )->method(
  126. 'getEvent'
  127. )->will(
  128. $this->returnValue($this->_eventMock)
  129. );
  130. $orderMock = $this->createMock(\Magento\Sales\Api\Data\OrderInterface::class);
  131. $orderMock->expects($this->once())->method('getOrderCurrencyCode')->willReturn($conversionCurrency);
  132. $iteratorMock = new \ArrayIterator([$orderMock]);
  133. $this->_collectionMock->expects($this->any())->method('getIterator')->will($this->returnValue($iteratorMock));
  134. $this->_collectionMock->expects(
  135. $this->once()
  136. )->method(
  137. 'addFieldToFilter'
  138. )->with(
  139. 'entity_id',
  140. ['in' => $ordersIds]
  141. );
  142. $this->_registryMock->expects(
  143. $this->atLeastOnce()
  144. )->method(
  145. 'register'
  146. )->withConsecutive(
  147. [
  148. Data::CONVERSION_VALUE_CURRENCY_REGISTRY_NAME,
  149. $conversionCurrency
  150. ],
  151. [
  152. Data::CONVERSION_VALUE_REGISTRY_NAME,
  153. $conversionValue,
  154. ]
  155. );
  156. $this->assertSame($this->_model, $this->_model->execute($this->_eventObserverMock));
  157. }
  158. }