123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\GoogleAdwords\Test\Unit\Observer;
- use Magento\GoogleAdwords\Helper\Data;
- class SetConversionValueObserverTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $_helperMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $_collectionMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $_registryMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $_eventObserverMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $_eventMock;
- /**
- * @var \Magento\GoogleAdwords\Observer\SetConversionValueObserver
- */
- protected $_model;
- protected function setUp()
- {
- $this->_helperMock = $this->createMock(\Magento\GoogleAdwords\Helper\Data::class);
- $this->_registryMock = $this->createMock(\Magento\Framework\Registry::class);
- $this->_collectionMock = $this->createMock(\Magento\Sales\Model\ResourceModel\Order\Collection::class);
- $this->_eventObserverMock = $this->createMock(\Magento\Framework\Event\Observer::class);
- $this->_eventMock = $this->createPartialMock(\Magento\Framework\Event::class, ['getOrderIds']);
- $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
- $this->_model = $objectManager->getObject(
- \Magento\GoogleAdwords\Observer\SetConversionValueObserver::class,
- [
- 'helper' => $this->_helperMock,
- 'collection' => $this->_collectionMock,
- 'registry' => $this->_registryMock
- ]
- );
- }
- /**
- * @return array
- */
- public function dataProviderForDisabled()
- {
- return [[false, false], [false, true], [true, false]];
- }
- /**
- * @param bool $isActive
- * @param bool $isDynamic
- * @dataProvider dataProviderForDisabled
- * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
- */
- public function testSetConversionValueWhenAdwordsDisabled($isActive, $isDynamic)
- {
- $this->_helperMock->expects(
- $this->once()
- )->method(
- 'isGoogleAdwordsActive'
- )->will(
- $this->returnValue($isActive)
- );
- $this->_helperMock->expects($this->any())->method('isDynamicConversionValue')->will(
- $this->returnCallback(
- function () use ($isDynamic) {
- return $isDynamic;
- }
- )
- );
- $this->_eventMock->expects($this->never())->method('getOrderIds');
- $this->assertSame($this->_model, $this->_model->execute($this->_eventObserverMock));
- }
- /**
- * @return array
- */
- public function dataProviderForOrdersIds()
- {
- return [[[]], ['']];
- }
- /**
- * @param $ordersIds
- * @dataProvider dataProviderForOrdersIds
- * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
- */
- public function testSetConversionValueWhenAdwordsActiveWithoutOrdersIds($ordersIds)
- {
- $this->_helperMock->expects($this->once())->method('isGoogleAdwordsActive')->will($this->returnValue(true));
- $this->_helperMock->expects($this->once())->method('isDynamicConversionValue')->will($this->returnValue(true));
- $this->_eventMock->expects($this->once())->method('getOrderIds')->will($this->returnValue($ordersIds));
- $this->_eventObserverMock->expects(
- $this->once()
- )->method(
- 'getEvent'
- )->will(
- $this->returnValue($this->_eventMock)
- );
- $this->_collectionMock->expects($this->never())->method('addFieldToFilter');
- $this->assertSame($this->_model, $this->_model->execute($this->_eventObserverMock));
- }
- /**
- * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
- */
- public function testSetConversionValueWhenAdwordsActiveWithOrdersIds()
- {
- $ordersIds = [1, 2, 3];
- $conversionValue = 0;
- $conversionCurrency = 'USD';
- $this->_helperMock->expects($this->once())->method('isGoogleAdwordsActive')->will($this->returnValue(true));
- $this->_helperMock->expects($this->once())->method('isDynamicConversionValue')->will($this->returnValue(true));
- $this->_helperMock->expects($this->once())->method('hasSendConversionValueCurrency')
- ->will($this->returnValue(true));
- $this->_eventMock->expects($this->once())->method('getOrderIds')->will($this->returnValue($ordersIds));
- $this->_eventObserverMock->expects(
- $this->once()
- )->method(
- 'getEvent'
- )->will(
- $this->returnValue($this->_eventMock)
- );
- $orderMock = $this->createMock(\Magento\Sales\Api\Data\OrderInterface::class);
- $orderMock->expects($this->once())->method('getOrderCurrencyCode')->willReturn($conversionCurrency);
- $iteratorMock = new \ArrayIterator([$orderMock]);
- $this->_collectionMock->expects($this->any())->method('getIterator')->will($this->returnValue($iteratorMock));
- $this->_collectionMock->expects(
- $this->once()
- )->method(
- 'addFieldToFilter'
- )->with(
- 'entity_id',
- ['in' => $ordersIds]
- );
- $this->_registryMock->expects(
- $this->atLeastOnce()
- )->method(
- 'register'
- )->withConsecutive(
- [
- Data::CONVERSION_VALUE_CURRENCY_REGISTRY_NAME,
- $conversionCurrency
- ],
- [
- Data::CONVERSION_VALUE_REGISTRY_NAME,
- $conversionValue,
- ]
- );
- $this->assertSame($this->_model, $this->_model->execute($this->_eventObserverMock));
- }
- }
|