|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © 2013-2017 Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +namespace Magento\GoogleAnalytics\Test\Unit\Block; |
| 8 | + |
| 9 | +use Magento\Framework\Escaper; |
| 10 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; |
| 11 | +use Magento\Framework\View\Element\Template\Context; |
| 12 | +use Magento\GoogleAnalytics\Block\Ga; |
| 13 | +use Magento\GoogleAnalytics\Helper\Data; |
| 14 | +use Magento\Sales\Api\Data\OrderItemInterface; |
| 15 | +use Magento\Sales\Model\Order; |
| 16 | +use Magento\Sales\Model\ResourceModel\Order\Collection; |
| 17 | +use Magento\Sales\Model\ResourceModel\Order\CollectionFactory; |
| 18 | +use Magento\Store\Model\Store; |
| 19 | +use Magento\Store\Model\StoreManagerInterface; |
| 20 | +use PHPUnit_Framework_TestCase; |
| 21 | + |
| 22 | +class GaTest extends PHPUnit_Framework_TestCase |
| 23 | +{ |
| 24 | + |
| 25 | + /** |
| 26 | + * @var Ga | \PHPUnit_Framework_MockObject_MockObject |
| 27 | + */ |
| 28 | + protected $gaBlock; |
| 29 | + |
| 30 | + protected function setUp() |
| 31 | + { |
| 32 | + $objectManager = new ObjectManager($this); |
| 33 | + $contextMock = $this->getMockBuilder(Context::class) |
| 34 | + ->disableOriginalConstructor() |
| 35 | + ->getMock(); |
| 36 | + |
| 37 | + $contextMock->expects($this->once()) |
| 38 | + ->method('getEscaper') |
| 39 | + ->willReturn($objectManager->getObject(Escaper::class)); |
| 40 | + |
| 41 | + $storeManagerMock = $this->getMockBuilder(StoreManagerInterface::class) |
| 42 | + ->disableOriginalConstructor() |
| 43 | + ->getMock(); |
| 44 | + |
| 45 | + $storeMock = $this->getMockBuilder(Store::class) |
| 46 | + ->disableOriginalConstructor() |
| 47 | + ->getMock(); |
| 48 | + |
| 49 | + $storeMock->expects($this->once()) |
| 50 | + ->method('getFrontendName') |
| 51 | + ->willReturn('test'); |
| 52 | + |
| 53 | + $storeManagerMock->expects($this->once()) |
| 54 | + ->method('getStore') |
| 55 | + ->willReturn($storeMock); |
| 56 | + |
| 57 | + $contextMock->expects($this->once()) |
| 58 | + ->method('getStoreManager') |
| 59 | + ->willReturn($storeManagerMock); |
| 60 | + |
| 61 | + $salesOrderCollectionMock = $this->getMockBuilder(CollectionFactory::class) |
| 62 | + ->disableOriginalConstructor() |
| 63 | + ->getMock(); |
| 64 | + |
| 65 | + $salesOrderCollectionMock->expects($this->once()) |
| 66 | + ->method('create') |
| 67 | + ->willReturn($this->createCollectionMock()); |
| 68 | + |
| 69 | + $googleAnalyticsDataMock = $this->getMockBuilder(Data::class) |
| 70 | + ->disableOriginalConstructor() |
| 71 | + ->getMock(); |
| 72 | + |
| 73 | + $this->gaBlock = $objectManager->getObject( |
| 74 | + Ga::class, |
| 75 | + [ |
| 76 | + 'context' => $contextMock, |
| 77 | + 'salesOrderCollection' => $salesOrderCollectionMock, |
| 78 | + 'googleAnalyticsData' => $googleAnalyticsDataMock |
| 79 | + ] |
| 80 | + ); |
| 81 | + } |
| 82 | + |
| 83 | + public function testOrderTrackingCode() |
| 84 | + { |
| 85 | + $expectedCode = "ga('require', 'ec', 'ec.js'); |
| 86 | + ga('ec:addProduct', { |
| 87 | + 'id': 'sku0', |
| 88 | + 'name': 'testName0', |
| 89 | + 'price': '0.00', |
| 90 | + 'quantity': 1 |
| 91 | + }); |
| 92 | + ga('ec:setAction', 'purchase', { |
| 93 | + 'id': '', |
| 94 | + 'affiliation': 'test', |
| 95 | + 'revenue': '10', |
| 96 | + 'tax': '2', |
| 97 | + 'shipping': '1' |
| 98 | + }); |
| 99 | + ga('send', 'pageview');"; |
| 100 | + |
| 101 | + $this->gaBlock->setOrderIds([1, 2]); |
| 102 | + $this->assertEquals( |
| 103 | + $this->packString($expectedCode), |
| 104 | + $this->packString($this->gaBlock->getOrdersTrackingCode()) |
| 105 | + ); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Create Order mock with $orderItemCount items |
| 110 | + * |
| 111 | + * @param int $orderItemCount |
| 112 | + * @return Order|\PHPUnit_Framework_MockObject_MockObject |
| 113 | + */ |
| 114 | + protected function createOrderMock($orderItemCount = 1) |
| 115 | + { |
| 116 | + $orderItems = []; |
| 117 | + for ($i = 0; $i < $orderItemCount; $i++) { |
| 118 | + $orderItemMock = $this->getMockBuilder(OrderItemInterface::class) |
| 119 | + ->disableOriginalConstructor() |
| 120 | + ->getMock(); |
| 121 | + |
| 122 | + $orderItemMock->expects($this->once()) |
| 123 | + ->method('getSku') |
| 124 | + ->willReturn('sku' . $i); |
| 125 | + |
| 126 | + $orderItemMock->expects($this->once()) |
| 127 | + ->method('getName') |
| 128 | + ->willReturn('testName' . $i); |
| 129 | + |
| 130 | + $orderItemMock->expects($this->once()) |
| 131 | + ->method('getBasePrice') |
| 132 | + ->willReturn($i . '.00'); |
| 133 | + $orderItemMock->expects($this->once()) |
| 134 | + ->method('getQtyOrdered') |
| 135 | + ->willReturn($i + 1); |
| 136 | + |
| 137 | + $orderItems[] = $orderItemMock; |
| 138 | + } |
| 139 | + |
| 140 | + $orderMock = $this->getMockBuilder(Order::class) |
| 141 | + ->disableOriginalConstructor() |
| 142 | + ->getMock(); |
| 143 | + |
| 144 | + $orderMock->expects($this->once()) |
| 145 | + ->method('getAllVisibleItems') |
| 146 | + ->willReturn($orderItems); |
| 147 | + $orderMock->expects($this->once()) |
| 148 | + ->method('getBaseGrandTotal') |
| 149 | + ->willReturn(10); |
| 150 | + $orderMock->expects($this->once()) |
| 151 | + ->method('getBaseTaxAmount') |
| 152 | + ->willReturn(2); |
| 153 | + $orderMock->expects($this->once()) |
| 154 | + ->method('getBaseShippingAmount') |
| 155 | + ->willReturn($orderItemCount); |
| 156 | + return $orderMock; |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * @return Collection | \PHPUnit_Framework_MockObject_MockObject |
| 161 | + */ |
| 162 | + protected function createCollectionMock() |
| 163 | + { |
| 164 | + $collectionMock = $this->getMockBuilder(Collection::class) |
| 165 | + ->disableOriginalConstructor() |
| 166 | + ->getMock(); |
| 167 | + |
| 168 | + $collectionMock->expects($this->any()) |
| 169 | + ->method('getIterator') |
| 170 | + ->willReturn(new \ArrayIterator([$this->createOrderMock(1)])); |
| 171 | + return $collectionMock; |
| 172 | + } |
| 173 | + |
| 174 | + /** |
| 175 | + * Removes from $string whitespace characters |
| 176 | + * |
| 177 | + * @param string $string |
| 178 | + * @return string |
| 179 | + */ |
| 180 | + protected function packString($string) |
| 181 | + { |
| 182 | + return preg_replace('/\s/', '', $string); |
| 183 | + } |
| 184 | +} |
0 commit comments