Skip to content

Commit 72ef542

Browse files
committed
Update tests and cover firebase handler
1 parent 27ed2fe commit 72ef542

File tree

11 files changed

+278
-29
lines changed

11 files changed

+278
-29
lines changed

test/FirebaseTestCase.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace AlexWestergaard\PhpGa4Test;
4+
5+
use PHPUnit\Framework\TestCase as FrameworkTestCase;
6+
use AlexWestergaard\PhpGa4\Item;
7+
use AlexWestergaard\PhpGa4\Exception\Ga4Exception;
8+
use AlexWestergaard\PhpGa4\Firebase;
9+
10+
class FirebaseTestCase extends FrameworkTestCase
11+
{
12+
protected Item $item;
13+
protected Firebase $firebase;
14+
protected array $prefill;
15+
16+
protected function setUp(): void
17+
{
18+
Ga4Exception::resetStack();
19+
parent::setUp();
20+
21+
$this->prefill = [
22+
// Analytics
23+
'firebase_app_id' => 'sdha62HsGjk63lKe2hkyLs',
24+
'api_secret' => 'gDS1gs423dDSH34sdfa',
25+
'app_instance_id' => 'e85ab98bdbbf3713a74b8f0409363d20',
26+
'user_id' => 'm6435',
27+
// Default Vars
28+
'currency' => 'EUR',
29+
'currency_virtual' => 'GA4Coins',
30+
];
31+
32+
$this->firebase = Firebase::new($this->prefill['firebase_app_id'], $this->prefill['api_secret'], /* DEBUG */ true)
33+
->setAppInstanceId($this->prefill['app_instance_id'])
34+
->setUserId($this->prefill['user_id']);
35+
36+
$this->item = Item::new()
37+
->setItemId('1')
38+
->setItemName('First Product')
39+
->setCurrency($this->prefill['currency'])
40+
->setPrice(7.39)
41+
->setQuantity(2);
42+
}
43+
}

test/TestCase.php renamed to test/MeasurementTestCase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use AlexWestergaard\PhpGa4\Exception\Ga4Exception;
88
use AlexWestergaard\PhpGa4\Analytics;
99

10-
class TestCase extends FrameworkTestCase
10+
class MeasurementTestCase extends FrameworkTestCase
1111
{
1212
protected Item $item;
1313
protected Analytics $analytics;

test/Unit/AbstractTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
use AlexWestergaard\PhpGa4\Helper;
77
use AlexWestergaard\PhpGa4\Facade\Type;
88
use AlexWestergaard\PhpGa4\Exception;
9-
use AlexWestergaard\PhpGa4Test\TestCase;
9+
use AlexWestergaard\PhpGa4Test\MeasurementTestCase;
1010
use AlexWestergaard\PhpGa4Test\Mocks;
1111

12-
final class AbstractTest extends TestCase
12+
final class AbstractTest extends MeasurementTestCase
1313
{
1414
/******************************************************************
1515
* ABSTRACT IO | INPUT OUTPUT

test/Unit/AnalyticsTest.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
use AlexWestergaard\PhpGa4\Event;
88
use AlexWestergaard\PhpGa4\Analytics;
99
use AlexWestergaard\PhpGa4\Event\Login;
10-
use AlexWestergaard\PhpGa4Test\TestCase;
10+
use AlexWestergaard\PhpGa4Test\MeasurementTestCase;
1111

12-
final class AnalyticsTest extends TestCase
12+
final class AnalyticsTest extends MeasurementTestCase
1313
{
1414
public function test_can_configure_only_client_id_and_export()
1515
{
@@ -102,20 +102,20 @@ public function test_can_configure_and_export()
102102

103103
public function test_can_post_only_client_id_to_google()
104104
{
105-
$this->assertNull(
106-
Analytics::new(
107-
$this->prefill['measurement_id'],
108-
$this->prefill['api_secret'],
109-
$debug = true
110-
)
111-
->setClientId($this->prefill['user_id'])
112-
->addEvent(Login::new())->post()
113-
);
105+
$this->expectNotToPerformAssertions();
106+
Analytics::new(
107+
$this->prefill['measurement_id'],
108+
$this->prefill['api_secret'],
109+
$debug = true
110+
)
111+
->setClientId($this->prefill['user_id'])
112+
->addEvent(Login::new())->post();
114113
}
115114

116115
public function test_can_post_to_google()
117116
{
118-
$this->assertNull($this->analytics->addEvent(Login::new())->post());
117+
$this->expectNotToPerformAssertions();
118+
$this->analytics->addEvent(Login::new())->post();
119119
}
120120

121121
public function test_converts_to_full_microtime_stamp()
@@ -154,7 +154,7 @@ public function test_exports_userproperty_to_array()
154154
$arr = $arr['user_properties'];
155155
$this->assertArrayHasKey('customer_tier', $arr);
156156

157-
$this->assertNull($this->analytics->post());
157+
$this->analytics->post();
158158
}
159159

160160
public function test_exports_events_to_array()
@@ -171,7 +171,7 @@ public function test_exports_events_to_array()
171171
$this->assertArrayHasKey('events', $arr);
172172
$this->assertCount(1, $arr['events']);
173173

174-
$this->assertNull($this->analytics->post());
174+
$this->analytics->post();
175175
}
176176

177177
public function test_throws_missing_measurement_id()

test/Unit/ConsentTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
use AlexWestergaard\PhpGa4\Event\Login;
66
use AlexWestergaard\PhpGa4\Helper\ConsentHelper;
7-
use AlexWestergaard\PhpGa4Test\TestCase;
7+
use AlexWestergaard\PhpGa4Test\MeasurementTestCase;
88

9-
final class ConsentTest extends TestCase
9+
final class ConsentTest extends MeasurementTestCase
1010
{
1111
public function test_no_consent_is_empty()
1212
{

test/Unit/EventTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
use AlexWestergaard\PhpGa4\Exception\Ga4EventException;
1212
use AlexWestergaard\PhpGa4\Event;
1313
use AlexWestergaard\PhpGa4\Campaign;
14-
use AlexWestergaard\PhpGa4Test\TestCase;
14+
use AlexWestergaard\PhpGa4Test\MeasurementTestCase;
1515

16-
final class EventTest extends TestCase
16+
final class EventTest extends MeasurementTestCase
1717
{
1818
public function test_pageview()
1919
{

test/Unit/FirebaseTest.php

Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
<?php
2+
3+
namespace AlexWestergaard\PhpGa4Test\Unit;
4+
5+
use AlexWestergaard\PhpGa4\UserProperty;
6+
use AlexWestergaard\PhpGa4\Facade;
7+
use AlexWestergaard\PhpGa4\Event;
8+
use AlexWestergaard\PhpGa4\Firebase;
9+
use AlexWestergaard\PhpGa4\Event\Login;
10+
use AlexWestergaard\PhpGa4Test\FirebaseTestCase;
11+
12+
final class FirebaseTest extends FirebaseTestCase
13+
{
14+
public function test_can_configure_only_client_id_and_export()
15+
{
16+
$analytics = Firebase::new(
17+
$this->prefill['firebase_app_id'],
18+
$this->prefill['api_secret'],
19+
$debug = true
20+
)
21+
->setAppInstanceId($this->prefill['app_instance_id'])
22+
->setTimestampMicros($time = time())
23+
->addEvent($event = Event\JoinGroup::fromArray(['group_id' => 1]))
24+
->addUserProperty($userProperty = UserProperty::fromArray(['name' => 'test', 'value' => 'testvalue']));
25+
26+
$asArray = $analytics->toArray();
27+
$this->assertIsArray($asArray);
28+
29+
$this->assertArrayHasKey('timestamp_micros', $asArray);
30+
$this->assertArrayHasKey('app_instance_id', $asArray);
31+
$this->assertArrayNotHasKey('user_id', $asArray);
32+
$this->assertArrayHasKey('user_properties', $asArray);
33+
$this->assertArrayHasKey('events', $asArray);
34+
35+
$timeAsMicro = $time * 1_000_000;
36+
37+
$this->assertEquals($timeAsMicro, $asArray['timestamp_micros']);
38+
$this->assertEquals($this->prefill['app_instance_id'], $asArray['app_instance_id']);
39+
$this->assertEquals($userProperty->toArray(), $asArray['user_properties']);
40+
$this->assertEquals([$event->toArray()], $asArray['events']);
41+
}
42+
43+
public function test_can_configure_and_export()
44+
{
45+
$analytics = Firebase::new(
46+
$this->prefill['firebase_app_id'],
47+
$this->prefill['api_secret'],
48+
$debug = true
49+
)
50+
->setAppInstanceId($this->prefill['app_instance_id'])
51+
->setUserId($this->prefill['user_id'])
52+
->setTimestampMicros($time = time())
53+
->addEvent($event = Event\JoinGroup::fromArray(['group_id' => 1]))
54+
->addUserProperty($userProperty = UserProperty::fromArray(['name' => 'test', 'value' => 'testvalue']));
55+
56+
$asArray = $analytics->toArray();
57+
$this->assertIsArray($asArray);
58+
59+
$this->assertArrayHasKey('timestamp_micros', $asArray);
60+
$this->assertArrayHasKey('app_instance_id', $asArray);
61+
$this->assertArrayHasKey('user_id', $asArray);
62+
$this->assertArrayHasKey('user_properties', $asArray);
63+
$this->assertArrayHasKey('events', $asArray);
64+
65+
$timeAsMicro = $time * 1_000_000;
66+
67+
$this->assertEquals($timeAsMicro, $asArray['timestamp_micros']);
68+
$this->assertEquals($this->prefill['app_instance_id'], $asArray['app_instance_id']);
69+
$this->assertEquals($this->prefill['user_id'], $asArray['user_id']);
70+
$this->assertEquals($userProperty->toArray(), $asArray['user_properties']);
71+
$this->assertEquals([$event->toArray()], $asArray['events']);
72+
}
73+
74+
public function test_can_post_only_client_id_to_google()
75+
{
76+
$this->expectNotToPerformAssertions();
77+
Firebase::new(
78+
$this->prefill['firebase_app_id'],
79+
$this->prefill['api_secret'],
80+
$debug = true
81+
)
82+
->setAppInstanceId($this->prefill['app_instance_id'])
83+
->addEvent(Login::new())
84+
->post();
85+
}
86+
87+
public function test_can_post_to_google()
88+
{
89+
$this->expectNotToPerformAssertions();
90+
$this->firebase->addEvent(Login::new())->post();
91+
}
92+
93+
public function test_converts_to_full_microtime_stamp()
94+
{
95+
$this->firebase->setTimestampMicros(microtime(true));
96+
97+
$arr = $this->firebase->toArray();
98+
99+
$this->assertTrue($arr['timestamp_micros'] > 1_000_000);
100+
}
101+
102+
public function test_throws_if_microtime_older_than_three_days()
103+
{
104+
$this->expectException(Facade\Type\Ga4ExceptionType::class);
105+
$this->expectExceptionCode(Facade\Type\Ga4ExceptionType::MICROTIME_EXPIRED);
106+
107+
$this->firebase->setTimestampMicros(strtotime('-1 week'));
108+
}
109+
110+
public function test_exports_userproperty_to_array()
111+
{
112+
$this->firebase->addEvent(Login::new());
113+
114+
$userProperty = UserProperty::new()
115+
->setName('customer_tier')
116+
->setValue('premium');
117+
118+
$this->assertInstanceOf(UserProperty::class, $userProperty);
119+
$this->assertIsArray($userProperty->toArray());
120+
121+
$this->firebase->addUserProperty($userProperty);
122+
123+
$arr = $this->firebase->toArray();
124+
$this->assertArrayHasKey('user_properties', $arr);
125+
126+
$arr = $arr['user_properties'];
127+
$this->assertArrayHasKey('customer_tier', $arr);
128+
129+
$this->firebase->post();
130+
}
131+
132+
public function test_exports_events_to_array()
133+
{
134+
$event = Event\JoinGroup::new()
135+
->setGroupId('1');
136+
137+
$this->assertInstanceOf(Facade\Type\EventType::class, $event);
138+
$this->assertIsArray($event->toArray());
139+
140+
$this->firebase->addEvent($event);
141+
142+
$arr = $this->firebase->toArray();
143+
$this->assertArrayHasKey('events', $arr);
144+
$this->assertCount(1, $arr['events']);
145+
146+
$this->firebase->post();
147+
}
148+
149+
public function test_throws_missing_firebase_app_id()
150+
{
151+
$this->expectException(Facade\Type\Ga4ExceptionType::class);
152+
$this->expectExceptionCode(Facade\Type\Ga4ExceptionType::REQUEST_MISSING_FIREBASE_APP_ID);
153+
154+
Firebase::new('', $this->prefill['api_secret'], true)->post();
155+
}
156+
157+
public function test_throws_missing_apisecret()
158+
{
159+
$this->expectException(Facade\Type\Ga4ExceptionType::class);
160+
$this->expectExceptionCode(Facade\Type\Ga4ExceptionType::REQUEST_MISSING_API_SECRET);
161+
162+
Firebase::new($this->prefill['firebase_app_id'], '', true)->post();
163+
}
164+
165+
public function test_throws_on_too_large_request_package()
166+
{
167+
$kB = 1024;
168+
$preparyKB = '';
169+
while (mb_strlen($preparyKB) < $kB) {
170+
$preparyKB .= 'AAAAAAAA'; // 8 bytes
171+
}
172+
173+
$this->expectException(Facade\Type\Ga4ExceptionType::class);
174+
$this->expectExceptionCode(Facade\Type\Ga4ExceptionType::REQUEST_TOO_LARGE);
175+
176+
$userProperty = UserProperty::new()->setName('large_package');
177+
178+
$overflowValue = '';
179+
while (mb_strlen(json_encode($userProperty->toArray())) <= ($kB * 131)) {
180+
$overflowValue .= $preparyKB;
181+
$userProperty->setValue($overflowValue);
182+
}
183+
184+
$this->firebase->addEvent(Login::new())->addUserProperty($userProperty)->post();
185+
}
186+
187+
public function test_timeasmicro_throws_exceeding_max()
188+
{
189+
$time = time() + 60;
190+
191+
$this->expectException(Facade\Type\Ga4ExceptionType::class);
192+
$this->expectExceptionCode(Facade\Type\Ga4ExceptionType::MICROTIME_EXPIRED);
193+
194+
$this->firebase->setTimestampMicros($time);
195+
}
196+
197+
public function test_timeasmicro_throws_exceeding_min()
198+
{
199+
$time = strtotime('-1 month');
200+
201+
$this->expectException(Facade\Type\Ga4ExceptionType::class);
202+
$this->expectExceptionCode(Facade\Type\Ga4ExceptionType::MICROTIME_EXPIRED);
203+
204+
$this->firebase->setTimestampMicros($time);
205+
}
206+
}

test/Unit/HelperTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
use AlexWestergaard\PhpGa4\Helper;
66
use AlexWestergaard\PhpGa4\Facade;
77
use AlexWestergaard\PhpGa4\Event;
8-
use AlexWestergaard\PhpGa4Test\TestCase;
8+
use AlexWestergaard\PhpGa4Test\MeasurementTestCase;
99

10-
final class HelperTest extends TestCase
10+
final class HelperTest extends MeasurementTestCase
1111
{
1212
public function test_convert_helper_transforms_array_into_events()
1313
{

test/Unit/ItemTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
namespace AlexWestergaard\PhpGa4Test\Unit;
44

55
use AlexWestergaard\PhpGa4\Item;
6-
use AlexWestergaard\PhpGa4Test\TestCase;
6+
use AlexWestergaard\PhpGa4Test\MeasurementTestCase;
77

8-
final class ItemTest extends TestCase
8+
final class ItemTest extends MeasurementTestCase
99
{
1010
public function test_can_configure_and_export()
1111
{

test/Unit/UserDataTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
use AlexWestergaard\PhpGa4\Event\Login;
66
use AlexWestergaard\PhpGa4\Helper\UserDataHelper;
7-
use AlexWestergaard\PhpGa4Test\TestCase;
7+
use AlexWestergaard\PhpGa4Test\MeasurementTestCase;
88

9-
final class UserDataTest extends TestCase
9+
final class UserDataTest extends MeasurementTestCase
1010
{
1111
public function test_user_data_is_fillable()
1212
{

0 commit comments

Comments
 (0)