Skip to content

Commit

Permalink
Fix: Using user_id before initialization with tests (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
aawnu authored Jul 31, 2024
2 parents 31e37cd + 3db6adb commit 7b2be4a
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Analytics.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public function post(): void

$body = array_replace_recursive(
$this->toArray(),
["user_data" => $this->user_id != null ? $this->userdata->toArray() : []], // Only accepted if user_id is passed too
["user_data" => !empty($this->user_id) ? $this->userdata->toArray() : []], // Only accepted if user_id is passed too
["user_properties" => $this->user_properties],
["consent" => $this->consent->toArray()],
);
Expand Down
73 changes: 72 additions & 1 deletion test/Unit/AnalyticsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,64 @@

final class AnalyticsTest extends TestCase
{
public function test_can_configure_only_client_id_and_export()
{
$analytics = Analytics::new(
$this->prefill['measurement_id'],
$this->prefill['api_secret'],
$debug = true
)
->setClientId($this->prefill['client_id'])
->setTimestampMicros($time = time())
->addEvent($event = Event\JoinGroup::fromArray(['group_id' => 1]))
->addUserProperty($userProperty = UserProperty::fromArray(['name' => 'test', 'value' => 'testvalue']));

$asArray = $analytics->toArray();
$this->assertIsArray($asArray);

$this->assertArrayHasKey('timestamp_micros', $asArray);
$this->assertArrayHasKey('client_id', $asArray);
$this->assertArrayNotHasKey('user_id', $asArray);
$this->assertArrayHasKey('user_properties', $asArray);
$this->assertArrayHasKey('events', $asArray);

$timeAsMicro = $time * 1_000_000;

$this->assertEquals($timeAsMicro, $asArray['timestamp_micros']);
$this->assertEquals($this->prefill['client_id'], $asArray['client_id']);
$this->assertEquals($userProperty->toArray(), $asArray['user_properties']);
$this->assertEquals([$event->toArray()], $asArray['events']);
}

public function test_can_configure_only_user_id_and_export()
{
$analytics = Analytics::new(
$this->prefill['measurement_id'],
$this->prefill['api_secret'],
$debug = true
)
->setUserId($this->prefill['user_id'])
->setTimestampMicros($time = time())
->addEvent($event = Event\JoinGroup::fromArray(['group_id' => 1]))
->addUserProperty($userProperty = UserProperty::fromArray(['name' => 'test', 'value' => 'testvalue']));

$asArray = $analytics->toArray();
$this->assertIsArray($asArray);

$this->assertArrayHasKey('timestamp_micros', $asArray);
$this->assertArrayNotHasKey('client_id', $asArray);
$this->assertArrayHasKey('user_id', $asArray);
$this->assertArrayHasKey('user_properties', $asArray);
$this->assertArrayHasKey('events', $asArray);

$timeAsMicro = $time * 1_000_000;

$this->assertEquals($timeAsMicro, $asArray['timestamp_micros']);
$this->assertEquals($this->prefill['user_id'], $asArray['user_id']);
$this->assertEquals($userProperty->toArray(), $asArray['user_properties']);
$this->assertEquals([$event->toArray()], $asArray['events']);
}

public function test_can_configure_and_export()
{
$analytics = Analytics::new(
Expand Down Expand Up @@ -42,6 +100,19 @@ public function test_can_configure_and_export()
$this->assertEquals([$event->toArray()], $asArray['events']);
}

public function test_can_post_only_client_id_to_google()
{
$this->assertNull(
Analytics::new(
$this->prefill['measurement_id'],
$this->prefill['api_secret'],
$debug = true
)
->setClientId($this->prefill['user_id'])
->addEvent(Login::new())->post()
);
}

public function test_can_post_to_google()
{
$this->assertNull($this->analytics->addEvent(Login::new())->post());
Expand All @@ -67,7 +138,7 @@ public function test_throws_if_microtime_older_than_three_days()
public function test_exports_userproperty_to_array()
{
$this->analytics->addEvent(Login::new());

$userProperty = UserProperty::new()
->setName('customer_tier')
->setValue('premium');
Expand Down

0 comments on commit 7b2be4a

Please sign in to comment.