Skip to content

Commit 8c7e1ad

Browse files
author
Alex Westergaard
committed
Add timestamp validation and test
1 parent 1172846 commit 8c7e1ad

File tree

2 files changed

+34
-17
lines changed

2 files changed

+34
-17
lines changed

src/Analytics.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,19 @@ public function setUserId(string $id)
8484
*/
8585
public function setTimestamp($microOrUnix)
8686
{
87+
$secondInMicro = intval('1_000_000');
88+
$offsetLimit = strtotime('-3 days') * $secondInMicro;
89+
8790
if (!is_numeric($microOrUnix)) {
8891
throw new GA4Exception("setTimestamp value must be numeric");
8992
}
9093

91-
$this->timestamp_micros = floor($microOrUnix * intval('1_000_000'));
94+
$formattedTime = floor($microOrUnix * $secondInMicro);
95+
if ($formattedTime < $offsetLimit) {
96+
throw new GA4Exception("Timestamp can not be older than 3 days");
97+
}
98+
99+
$this->timestamp_micros = $formattedTime;
92100
return $this;
93101
}
94102

test/AnalyticsTest.php

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
use AlexWestergaard\PhpGa4\Analytics;
4+
use AlexWestergaard\PhpGa4\GA4Exception;
45
use AlexWestergaard\PhpGa4\Item;
56
use AlexWestergaard\PhpGa4\UserProperty;
67

@@ -10,9 +11,6 @@ class AnalyticsTest extends \PHPUnit\Framework\TestCase
1011
protected $analytics;
1112
protected $item;
1213

13-
/**
14-
* Setting up each test enviroment variables
15-
*/
1614
protected function prepareSituation()
1715
{
1816
$this->prefill = [
@@ -38,23 +36,37 @@ protected function prepareSituation()
3836
->setQuantity(2);
3937
}
4038

41-
/**
42-
* Testing that we can send request to Google Analytics with 200 response
43-
*/
4439
public function testAnalytics()
4540
{
4641
$this->prepareSituation();
47-
42+
4843
$this->assertTrue($this->analytics->post());
4944
}
5045

51-
/**
52-
* Testing that out item is properly build
53-
*/
46+
public function testTimeIsMicrotime()
47+
{
48+
$this->prepareSituation();
49+
50+
$this->analytics->setTimestamp(microtime(true));
51+
52+
$arr = $this->analytics->toArray();
53+
54+
$this->assertTrue($arr['timestamp_micros'] > intval('1_000_000'));
55+
}
56+
57+
public function testExceptionIfTimeOlderThanOffsetLimit()
58+
{
59+
$this->prepareSituation();
60+
61+
$this->expectException(GA4Exception::class);
62+
63+
$this->analytics->setTimestamp(strtotime('-1 week'));
64+
}
65+
5466
public function testItem()
5567
{
5668
$this->prepareSituation();
57-
69+
5870
$this->assertInstanceOf(Item::class, $this->item);
5971

6072
$arr = $this->item->toArray();
@@ -66,13 +78,10 @@ public function testItem()
6678
$this->assertArrayHasKey('quantity', $arr);
6779
}
6880

69-
/**
70-
* Testing that we can send a User Property
71-
*/
7281
public function testUserProperty()
7382
{
7483
$this->prepareSituation();
75-
84+
7685
$userProperty = UserProperty::new()
7786
->setName('customer_tier')
7887
->setValue('premium');
@@ -94,7 +103,7 @@ public function testUserProperty()
94103
public function testPrebuildEvents()
95104
{
96105
$this->prepareSituation();
97-
106+
98107
$getDefaultEventsByFile = glob(__DIR__ . '/../src/Event/*.php');
99108

100109
foreach (array_chunk($getDefaultEventsByFile, 25) as $chunk) {

0 commit comments

Comments
 (0)