20 use PHPUnit\Framework\TestCase;
25 class GaTest extends \PHPUnit\Framework\TestCase
36 private $cookieHelperMock;
41 private $salesOrderCollectionMock;
46 private $storeManagerMock;
56 private $googleAnalyticsDataMock;
61 $contextMock = $this->getMockBuilder(Context::class)->disableOriginalConstructor()->getMock();
63 $contextMock->expects($this->once())
64 ->method(
'getEscaper')
67 $this->storeManagerMock = $this->getMockBuilder(StoreManagerInterface::class)
68 ->disableOriginalConstructor()
71 $this->storeMock = $this->getMockBuilder(Store::class)->disableOriginalConstructor()->getMock();
72 $contextMock->expects($this->once())->method(
'getStoreManager')->willReturn($this->storeManagerMock);
74 $this->salesOrderCollectionMock = $this->getMockBuilder(CollectionFactory::class)
75 ->disableOriginalConstructor()
78 $this->googleAnalyticsDataMock = $this->getMockBuilder(Data::class)
79 ->disableOriginalConstructor()
82 $this->cookieHelperMock = $this->getMockBuilder(\
Magento\Cookie\Helper\Cookie::class)
83 ->disableOriginalConstructor()
89 'context' => $contextMock,
90 'salesOrderCollection' => $this->salesOrderCollectionMock,
91 'googleAnalyticsData' => $this->googleAnalyticsDataMock,
92 'cookieHelper' => $this->cookieHelperMock
99 $this->salesOrderCollectionMock->expects($this->once())
102 $this->storeMock->expects($this->once())->method(
'getFrontendName')->willReturn(
'test');
103 $this->storeManagerMock->expects($this->once())->method(
'getStore')->willReturn($this->storeMock);
105 $expectedCode =
"ga('require', 'ec', 'ec.js'); 106 ga('set', 'currencyCode', 'USD'); 107 ga('ec:addProduct', { 113 ga('ec:setAction', 'purchase', { 115 'affiliation': 'test', 120 ga('send', 'pageview');";
122 $this->gaBlock->setOrderIds([1, 2]);
125 $this->
packString($this->gaBlock->getOrdersTrackingCode())
131 $this->cookieHelperMock->expects($this->once())->method(
'isCookieRestrictionModeEnabled')->willReturn(
false);
132 $this->assertFalse($this->gaBlock->isCookieRestrictionModeEnabled());
138 $websiteMock = $this->getMockBuilder(\
Magento\
Store\Api\
Data\WebsiteInterface::class)->getMock();
139 $websiteMock->expects($this->once())->method(
'getId')->willReturn(
$websiteId);
140 $this->storeManagerMock->expects($this->once())->method(
'getWebsite')->willReturn($websiteMock);
141 $this->assertEquals(
$websiteId, $this->gaBlock->getCurrentWebsiteId());
146 $this->salesOrderCollectionMock->expects($this->once())
149 $this->storeMock->expects($this->once())->method(
'getFrontendName')->willReturn(
'test');
150 $this->storeManagerMock->expects($this->once())->method(
'getStore')->willReturn($this->storeMock);
156 'affiliation' =>
'test',
165 'name' =>
'testName0',
173 $this->gaBlock->setOrderIds([1, 2]);
174 $this->assertEquals($expectedResult, $this->gaBlock->getOrdersTrackingData());
179 $pageName =
'/page/name';
182 'optPageUrl' =>
", '" . $pageName .
"'",
183 'isAnonymizedIpActive' =>
true,
184 'accountId' => $accountId
186 $this->gaBlock->setData(
'page_name', $pageName);
187 $this->googleAnalyticsDataMock->expects($this->once())->method(
'isAnonymizedIpActive')->willReturn(
true);
189 $this->assertEquals($expectedResult, $this->gaBlock->getPageTrackingData($accountId));
201 for (
$i = 0;
$i < $orderItemCount;
$i++) {
202 $orderItemMock = $this->getMockBuilder(OrderItemInterface::class)
203 ->disableOriginalConstructor()
205 $orderItemMock->expects($this->once())->method(
'getSku')->willReturn(
'sku' .
$i);
206 $orderItemMock->expects($this->once())->method(
'getName')->willReturn(
'testName' .
$i);
207 $orderItemMock->expects($this->once())->method(
'getPrice')->willReturn(
$i .
'.00');
208 $orderItemMock->expects($this->once())->method(
'getQtyOrdered')->willReturn(
$i + 1);
212 $orderMock = $this->getMockBuilder(Order::class)->disableOriginalConstructor()->getMock();
213 $orderMock->expects($this->once())->method(
'getIncrementId')->willReturn(100);
214 $orderMock->expects($this->once())->method(
'getAllVisibleItems')->willReturn(
$orderItems);
215 $orderMock->expects($this->once())->method(
'getGrandTotal')->willReturn(10);
216 $orderMock->expects($this->once())->method(
'getTaxAmount')->willReturn(2);
217 $orderMock->expects($this->once())->method(
'getShippingAmount')->willReturn($orderItemCount);
218 $orderMock->expects($this->once())->method(
'getOrderCurrencyCode')->willReturn(
'USD');
227 $collectionMock = $this->getMockBuilder(Collection::class)
228 ->disableOriginalConstructor()
231 $collectionMock->expects($this->any())
232 ->method(
'getIterator')
234 return $collectionMock;
245 return preg_replace(
'/\s/',
'', $string);
testGetCurrentWebsiteId()
createOrderMock($orderItemCount=1)
testGetPageTrackingData()
testIsCookieRestrictionModeEnabled()