Skip to content

Commit 838f535

Browse files
author
Alex Westergaard
committed
Fix legacy analytics test for deprecated stuff
1 parent 757b877 commit 838f535

File tree

2 files changed

+288
-1
lines changed

2 files changed

+288
-1
lines changed

src/Helper/AbstractEvent.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,15 @@ public function toArray(): array
3333

3434
return $return;
3535
}
36-
36+
3737
public static function new(): static
3838
{
3939
return new static();
4040
}
41+
42+
/** @deprecated 1.1.1 */
43+
public function debug()
44+
{
45+
return $this;
46+
}
4147
}

test/Unit/LegacyAnalyticsTest.php

Lines changed: 281 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,281 @@
1+
<?php
2+
3+
namespace AlexWestergaard\PhpGa4Test\Unit;
4+
5+
use AlexWestergaard\PhpGa4\UserProperty;
6+
use AlexWestergaard\PhpGa4\Item;
7+
use AlexWestergaard\PhpGa4\Exception\Ga4IOException;
8+
use AlexWestergaard\PhpGa4\Exception\Ga4Exception;
9+
use AlexWestergaard\PhpGa4\Event;
10+
use AlexWestergaard\PhpGa4\Analytics;
11+
12+
class LegacyAnalyticsTest extends \PHPUnit\Framework\TestCase
13+
{
14+
protected $prefill;
15+
protected $analytics;
16+
protected $item;
17+
18+
protected function setUp(): void
19+
{
20+
$this->prefill = [
21+
// Analytics
22+
'measurement_id' => 'G-XXXXXXXX',
23+
'api_secret' => 'gDS1gs423dDSH34sdfa',
24+
'client_id' => 'GA0.43535.234234',
25+
'user_id' => 'm6435',
26+
// Default Vars
27+
'currency' => 'EUR',
28+
'currency_virtual' => 'GA4Coins',
29+
];
30+
31+
$this->analytics = Analytics::new($this->prefill['measurement_id'], $this->prefill['api_secret'], /* DEBUG */ true)
32+
->setClientId($this->prefill['client_id'])
33+
->setUserId($this->prefill['user_id']);
34+
35+
$this->item = Item::new()
36+
->setItemId('1')
37+
->setItemName('First Product')
38+
->setCurrency($this->prefill['currency'])
39+
->setPrice(7.39)
40+
->setQuantity(2);
41+
}
42+
43+
public function testAnalytics()
44+
{
45+
$this->assertNull($this->analytics->post());
46+
}
47+
48+
public function testTimeIsMicrotime()
49+
{
50+
$this->analytics->setTimestamp(microtime(true));
51+
52+
$arr = $this->analytics->toArray();
53+
54+
$this->assertTrue($arr['timestamp_micros'] > intval(strtr('1_000_000', ['_' => ''])));
55+
}
56+
57+
public function testExceptionIfTimeOlderThanOffsetLimit()
58+
{
59+
try {
60+
$this->analytics->setTimestamp(strtotime('-1 week'));
61+
} catch (Ga4Exception $e) {
62+
$this->assertTrue(true);
63+
} catch (\Exception $e) {
64+
$this->assertTrue(false, "Did not receive correct Exception");
65+
}
66+
}
67+
68+
public function testItem()
69+
{
70+
$this->assertInstanceOf(Item::class, $this->item);
71+
72+
$arr = $this->item->toArray();
73+
$this->assertIsArray($arr);
74+
$this->assertArrayHasKey('item_id', $arr);
75+
$this->assertArrayHasKey('item_name', $arr);
76+
$this->assertArrayHasKey('currency', $arr);
77+
$this->assertArrayHasKey('price', $arr);
78+
$this->assertArrayHasKey('quantity', $arr);
79+
}
80+
81+
public function testItemFromArray()
82+
{
83+
$item = Item::fromArray([
84+
'item_id' => '2',
85+
'item_name' => 'Second Product',
86+
'currency' => $this->prefill['currency'],
87+
'price' => 9.99,
88+
'quantity' => 4,
89+
]);
90+
91+
$this->assertInstanceOf(Item::class, $item);
92+
93+
$arr = $item->toArray();
94+
$this->assertIsArray($arr);
95+
$this->assertArrayHasKey('item_id', $arr);
96+
$this->assertArrayHasKey('item_name', $arr);
97+
$this->assertArrayHasKey('currency', $arr);
98+
$this->assertArrayHasKey('price', $arr);
99+
$this->assertArrayHasKey('quantity', $arr);
100+
}
101+
102+
public function testUserProperty()
103+
{
104+
$userProperty = UserProperty::new()
105+
->setName('customer_tier')
106+
->setValue('premium');
107+
108+
$this->assertInstanceOf(UserProperty::class, $userProperty);
109+
$this->assertIsArray($userProperty->toArray());
110+
111+
$this->analytics->addUserProperty($userProperty);
112+
113+
$arr = $this->analytics->toArray();
114+
$this->assertArrayHasKey('user_properties', $arr);
115+
116+
$arr = $arr['user_properties'];
117+
$this->assertArrayHasKey('customer_tier', $arr);
118+
119+
$this->assertNull($this->analytics->post());
120+
}
121+
122+
public function testFullRefundNoItems()
123+
{
124+
$refund = Event\Refund::new()->setTransactionId(1)->isFullRefund(true);
125+
126+
$this->analytics->addEvent($refund);
127+
128+
$this->assertNull($this->analytics->post());
129+
}
130+
131+
public function testPartialRefundWithItems()
132+
{
133+
$refund = Event\Refund::new()->setTransactionId(1)->addItem($this->item);
134+
135+
$this->analytics->addEvent($refund);
136+
137+
$arr = $this->analytics->toArray();
138+
$this->assertIsArray($arr);
139+
140+
$arr = $refund->toArray();
141+
$this->assertArrayHasKey('params', $arr);
142+
$arr = $arr['params'];
143+
$this->assertArrayHasKey('items', $arr);
144+
}
145+
146+
public function testPartialRefundNoItemsThrows()
147+
{
148+
$refund = Event\Refund::new()->setTransactionId(1);
149+
150+
$this->expectException(Ga4IOException::class);
151+
152+
$this->analytics->addEvent($refund);
153+
}
154+
155+
public function testPrebuildEvents()
156+
{
157+
$getDefaultEventsByFile = glob(__DIR__ . '/../src/Event/*.php');
158+
159+
foreach ($getDefaultEventsByFile as $file) {
160+
$eventName = 'AlexWestergaard\\PhpGa4\\Event\\' . basename($file, '.php');
161+
162+
$this->assertTrue(class_exists($eventName), $eventName);
163+
164+
$event = new $eventName;
165+
$required = $event->getRequiredParams();
166+
$params = array_unique(array_merge($event->getParams(), $required));
167+
168+
$this->assertEquals(
169+
strtolower(basename($file, '.php')),
170+
strtolower(strtr($event->getName(), ['_' => ''])),
171+
strtolower(basename($file, '.php')) . ' is not equal to ' . strtolower(strtr($event->getName(), ['_' => '']))
172+
);
173+
174+
if (in_array('currency', $params)) {
175+
$event->setCurrency($this->prefill['currency']);
176+
if (in_array('value', $params)) {
177+
$event->setValue(9.99);
178+
}
179+
}
180+
181+
if (in_array('price', $params)) {
182+
$event->setPrice(9.99);
183+
}
184+
185+
if (in_array('quantity', $params)) {
186+
$event->setQuantity(9.99);
187+
}
188+
189+
if (in_array('payment_type', $params)) {
190+
$event->setPaymentType('credit card');
191+
}
192+
193+
if (in_array('shipping_tier', $params)) {
194+
$event->setShippingTier('ground');
195+
}
196+
197+
if (in_array('items', $params)) {
198+
if (method_exists($event, 'addItem')) {
199+
$event->addItem($this->item);
200+
} elseif (method_exists($event, 'setItem')) {
201+
$event->setItem($this->item);
202+
}
203+
}
204+
205+
if (in_array('virtual_currency_name', $params)) {
206+
$event->setVirtualCurrencyName($this->prefill['currency_virtual']);
207+
208+
if (in_array('value', $params)) {
209+
$event->setValue(9.99);
210+
}
211+
212+
if (in_array('item_name', $params)) {
213+
$event->setItemName('CookieBite');
214+
}
215+
}
216+
217+
if (in_array('character', $params)) {
218+
$event->setCharacter('AlexWestergaard');
219+
220+
if (in_array('level', $params)) {
221+
$event->setLEvel(3);
222+
}
223+
224+
if (in_array('score', $params)) {
225+
$event->setScore(500);
226+
}
227+
}
228+
229+
if (in_array('location_id', $params)) {
230+
$event->setLocationId('ChIJeRpOeF67j4AR9ydy_PIzPuM');
231+
}
232+
233+
if (in_array('transaction_id', $params)) {
234+
$event->setTransactionId('O6435DK');
235+
}
236+
237+
if (in_array('achievement_id', $params)) {
238+
$event->setAchievementId('achievement_buy_5_items');
239+
}
240+
241+
if (in_array('group_id', $params)) {
242+
$event->setGroupId('999');
243+
}
244+
245+
$this->assertIsArray($event->toArray(), $eventName);
246+
247+
$this->analytics->addEvent($event);
248+
}
249+
250+
$this->assertNull($this->analytics->post());
251+
}
252+
253+
public function testBuildEventFromArray()
254+
{
255+
$event = Event\AddToCart::fromArray([
256+
'currency' => $this->prefill['currency'],
257+
'value' => rand(1000, 10000) / 100,
258+
'items' => [$this->item],
259+
]);
260+
261+
$this->assertIsArray($event->toArray(), get_class($event));
262+
263+
$this->analytics->addEvent($event);
264+
$this->assertNull($this->analytics->post());
265+
}
266+
267+
public function testEventArrayable()
268+
{
269+
$event = new Event\AddToCart();
270+
$event['currency'] = $this->prefill['currency'];
271+
$event['value'] = ($value = rand(1000, 10000) / 100);
272+
273+
$this->assertArrayHasKey('currency', $event);
274+
$this->assertArrayHasKey('value', $event);
275+
$this->assertArrayHasKey('items', $event);
276+
277+
$this->assertEquals($this->prefill['currency'], $event['currency']);
278+
$this->assertEquals($value, $event['value']);
279+
$this->assertIsArray($event['items']);
280+
}
281+
}

0 commit comments

Comments
 (0)