|
| 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 | +} |
0 commit comments