Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
GaTest.php
Go to the documentation of this file.
1 <?php
8 
20 use PHPUnit\Framework\TestCase;
21 
25 class GaTest extends \PHPUnit\Framework\TestCase
26 {
27 
31  protected $gaBlock;
32 
36  private $cookieHelperMock;
37 
41  private $salesOrderCollectionMock;
42 
46  private $storeManagerMock;
47 
51  private $storeMock;
52 
56  private $googleAnalyticsDataMock;
57 
58  protected function setUp()
59  {
60  $objectManager = new ObjectManager($this);
61  $contextMock = $this->getMockBuilder(Context::class)->disableOriginalConstructor()->getMock();
62 
63  $contextMock->expects($this->once())
64  ->method('getEscaper')
65  ->willReturn($objectManager->getObject(Escaper::class));
66 
67  $this->storeManagerMock = $this->getMockBuilder(StoreManagerInterface::class)
68  ->disableOriginalConstructor()
69  ->getMock();
70 
71  $this->storeMock = $this->getMockBuilder(Store::class)->disableOriginalConstructor()->getMock();
72  $contextMock->expects($this->once())->method('getStoreManager')->willReturn($this->storeManagerMock);
73 
74  $this->salesOrderCollectionMock = $this->getMockBuilder(CollectionFactory::class)
75  ->disableOriginalConstructor()
76  ->getMock();
77 
78  $this->googleAnalyticsDataMock = $this->getMockBuilder(Data::class)
79  ->disableOriginalConstructor()
80  ->getMock();
81 
82  $this->cookieHelperMock = $this->getMockBuilder(\Magento\Cookie\Helper\Cookie::class)
83  ->disableOriginalConstructor()
84  ->getMock();
85 
86  $this->gaBlock = $objectManager->getObject(
87  Ga::class,
88  [
89  'context' => $contextMock,
90  'salesOrderCollection' => $this->salesOrderCollectionMock,
91  'googleAnalyticsData' => $this->googleAnalyticsDataMock,
92  'cookieHelper' => $this->cookieHelperMock
93  ]
94  );
95  }
96 
97  public function testOrderTrackingCode()
98  {
99  $this->salesOrderCollectionMock->expects($this->once())
100  ->method('create')
101  ->willReturn($this->createCollectionMock());
102  $this->storeMock->expects($this->once())->method('getFrontendName')->willReturn('test');
103  $this->storeManagerMock->expects($this->once())->method('getStore')->willReturn($this->storeMock);
104 
105  $expectedCode = "ga('require', 'ec', 'ec.js');
106  ga('set', 'currencyCode', 'USD');
107  ga('ec:addProduct', {
108  'id': 'sku0',
109  'name': 'testName0',
110  'price': '0.00',
111  'quantity': 1
112  });
113  ga('ec:setAction', 'purchase', {
114  'id': '100',
115  'affiliation': 'test',
116  'revenue': '10',
117  'tax': '2',
118  'shipping': '1'
119  });
120  ga('send', 'pageview');";
121 
122  $this->gaBlock->setOrderIds([1, 2]);
123  $this->assertEquals(
124  $this->packString($expectedCode),
125  $this->packString($this->gaBlock->getOrdersTrackingCode())
126  );
127  }
128 
130  {
131  $this->cookieHelperMock->expects($this->once())->method('isCookieRestrictionModeEnabled')->willReturn(false);
132  $this->assertFalse($this->gaBlock->isCookieRestrictionModeEnabled());
133  }
134 
135  public function testGetCurrentWebsiteId()
136  {
137  $websiteId = 100;
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());
142  }
143 
144  public function testOrderTrackingData()
145  {
146  $this->salesOrderCollectionMock->expects($this->once())
147  ->method('create')
148  ->willReturn($this->createCollectionMock());
149  $this->storeMock->expects($this->once())->method('getFrontendName')->willReturn('test');
150  $this->storeManagerMock->expects($this->once())->method('getStore')->willReturn($this->storeMock);
151 
152  $expectedResult = [
153  'orders' => [
154  [
155  'id' => 100,
156  'affiliation' => 'test',
157  'revenue' => 10,
158  'tax' => 2,
159  'shipping' => 1
160  ]
161  ],
162  'products' => [
163  [
164  'id' => 'sku0',
165  'name' => 'testName0',
166  'price' => 0.00,
167  'quantity' => 1
168  ]
169  ],
170  'currency' => 'USD'
171  ];
172 
173  $this->gaBlock->setOrderIds([1, 2]);
174  $this->assertEquals($expectedResult, $this->gaBlock->getOrdersTrackingData());
175  }
176 
177  public function testGetPageTrackingData()
178  {
179  $pageName = '/page/name';
180  $accountId = 100;
181  $expectedResult = [
182  'optPageUrl' => ", '" . $pageName . "'",
183  'isAnonymizedIpActive' => true,
184  'accountId' => $accountId
185  ];
186  $this->gaBlock->setData('page_name', $pageName);
187  $this->googleAnalyticsDataMock->expects($this->once())->method('isAnonymizedIpActive')->willReturn(true);
188 
189  $this->assertEquals($expectedResult, $this->gaBlock->getPageTrackingData($accountId));
190  }
191 
198  protected function createOrderMock($orderItemCount = 1)
199  {
200  $orderItems = [];
201  for ($i = 0; $i < $orderItemCount; $i++) {
202  $orderItemMock = $this->getMockBuilder(OrderItemInterface::class)
203  ->disableOriginalConstructor()
204  ->getMock();
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);
209  $orderItems[] = $orderItemMock;
210  }
211 
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');
219  return $orderMock;
220  }
221 
225  protected function createCollectionMock()
226  {
227  $collectionMock = $this->getMockBuilder(Collection::class)
228  ->disableOriginalConstructor()
229  ->getMock();
230 
231  $collectionMock->expects($this->any())
232  ->method('getIterator')
233  ->willReturn(new \ArrayIterator([$this->createOrderMock(1)]));
234  return $collectionMock;
235  }
236 
243  protected function packString($string)
244  {
245  return preg_replace('/\s/', '', $string);
246  }
247 }
$objectManager
Definition: bootstrap.php:17
$i
Definition: gallery.phtml:31