Skip to content

Commit 715ee8a

Browse files
authored
V1.x: Extend Testing & Update Found Issues (#10)
- Add PHP 7.1 to testing Matrix - Add timestamp validation is within the offset limit - Update test iterating through default events with validation - Update .gitignore and composer.json - Add option to post to Google Analytics on Event limit reached (Empties Events from Class)
2 parents 8ce4cdb + b415191 commit 715ee8a

File tree

5 files changed

+71
-50
lines changed

5 files changed

+71
-50
lines changed

.github/workflows/ci.yml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ jobs:
1010
render-php:
1111
runs-on: ${{ matrix.operating-system }}
1212
strategy:
13-
max-parallel: 4
13+
max-parallel: 3
1414
fail-fast: false
1515
matrix:
1616
operating-system: ["ubuntu-latest", "windows-latest"]
17-
php-versions: ["7.0", "7.4", "8.0", "8.1"]
17+
php-versions: ["7.0", "7.1", "7.4", "8.0", "8.1"]
1818
phpunit-versions: ["latest"]
1919
steps:
2020
- uses: actions/checkout@v3
@@ -37,8 +37,9 @@ jobs:
3737
- if: ${{ matrix.php-versions >= 8.0 }}
3838
name: "PHP >8.0"
3939
run: composer require --dev --with-all-dependencies --no-install phpunit/phpunit
40-
- name: "Install & Test"
40+
- name: "Install"
4141
run: |
4242
composer update --no-install --with-all-dependencies
4343
composer install
44-
./vendor/bin/phpunit
44+
- name: "PHPUnit Tests"
45+
run: ./vendor/bin/phpunit

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44

55
# PHPUnit
66
/.phpunit.cache
7-
.phpunit.result.cache
7+
/.phpunit.result.cache

src/Analytics.php

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class Analytics extends Model\ToArray implements Facade\Analytics, Facade\Export
1919
private $debug;
2020
private $measurement_id;
2121
private $api_secret;
22+
private $postOnEventLimit = false;
2223

2324
protected $non_personalized_ads;
2425
protected $timestamp_micros;
@@ -84,11 +85,25 @@ public function setUserId(string $id)
8485
*/
8586
public function setTimestamp($microOrUnix)
8687
{
88+
$secondInMicro = intval('1_000_000');
89+
$offsetLimit = strtotime('-3 days') * $secondInMicro;
90+
8791
if (!is_numeric($microOrUnix)) {
8892
throw new GA4Exception("setTimestamp value must be numeric");
8993
}
9094

91-
$this->timestamp_micros = floor($microOrUnix * 1000000);
95+
$formattedTime = floor($microOrUnix * $secondInMicro);
96+
if ($formattedTime < $offsetLimit) {
97+
throw new GA4Exception("Timestamp can not be older than 3 days");
98+
}
99+
100+
$this->timestamp_micros = $formattedTime;
101+
return $this;
102+
}
103+
104+
public function postOnEventLimit(bool $push = true)
105+
{
106+
$this->postOnEventLimit = $push;
92107
return $this;
93108
}
94109

@@ -133,16 +148,22 @@ public function addEvent(Model\Event $event)
133148
}
134149

135150
$this->events[] = $event->toArray();
151+
152+
if (count($this->events) >= 25 && $this->postOnEventLimit === true) {
153+
$this->post();
154+
}
136155
return $this;
137156
}
138157

139158
/**
140-
* Push your current stack to Google Analytics
159+
* Push your current stack to Google Analytics \
160+
* Will reset the events list on success
141161
*
162+
* @var bool $skipReset Avoid resetting events list on success, default: false
142163
* @return bool Whether the request returned status 200
143164
* @throws AlexWestergaard\PhpGa4\GA4Exception
144165
*/
145-
public function post()
166+
public function post($skipReset = false)
146167
{
147168
$errorStack = null;
148169

@@ -187,6 +208,10 @@ public function post()
187208
throw $errorStack;
188209
}
189210

211+
if (!$skipReset) {
212+
$this->events = [];
213+
}
214+
190215
return $resCode === 200;
191216
}
192217

src/GA4Exception.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
class GA4Exception extends \Exception
66
{
7-
public function __construct(string $message = "", ?\Throwable $previous = null)
7+
public function __construct(string $message = "", $previous = null)
88
{
99
parent::__construct($message, 0, $previous);
1010
}

test/AnalyticsTest.php

Lines changed: 36 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,14 @@
33
use AlexWestergaard\PhpGa4\Analytics;
44
use AlexWestergaard\PhpGa4\Item;
55
use AlexWestergaard\PhpGa4\UserProperty;
6+
use AlexWestergaard\PhpGa4\GA4Exception;
67

78
class AnalyticsTest extends \PHPUnit\Framework\TestCase
89
{
910
protected $prefill;
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,19 +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();
4742

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+
try {
62+
$this->analytics->setTimestamp(strtotime('-1 week'));
63+
} catch (GA4Exception $e) {
64+
$this->assertTrue(true);
65+
} catch (Exception $e) {
66+
$this->assertTrue(false, "Did not receive correct Exception");
67+
}
68+
}
69+
5470
public function testItem()
5571
{
5672
$this->prepareSituation();
@@ -66,9 +82,6 @@ public function testItem()
6682
$this->assertArrayHasKey('quantity', $arr);
6783
}
6884

69-
/**
70-
* Testing that we can send a User Property
71-
*/
7285
public function testUserProperty()
7386
{
7487
$this->prepareSituation();
@@ -95,17 +108,19 @@ public function testPrebuildEvents()
95108
{
96109
$this->prepareSituation();
97110

98-
$eventCount = 0;
99-
foreach (glob(__DIR__ . '/../src/Event/*.php') as $file) {
100-
$eventName = 'AlexWestergaard\\PhpGa4\\Event\\' . basename($file, '.php');
111+
$getDefaultEventsByFile = glob(__DIR__ . '/../src/Event/*.php');
112+
113+
foreach (array_chunk($getDefaultEventsByFile, 25) as $chunk) {
114+
115+
foreach ($chunk as $file) {
116+
$eventName = 'AlexWestergaard\\PhpGa4\\Event\\' . basename($file, '.php');
101117

102-
$this->assertTrue(class_exists($eventName), $eventName);
118+
$this->assertTrue(class_exists($eventName), $eventName);
103119

104-
$event = new $eventName;
105-
$required = $event->getRequiredParams();
106-
$params = array_unique(array_merge($event->getParams(), $required));
120+
$event = new $eventName;
121+
$required = $event->getRequiredParams();
122+
$params = array_unique(array_merge($event->getParams(), $required));
107123

108-
try {
109124
$this->assertEquals(
110125
strtolower(basename($file, '.php')),
111126
strtolower(strtr($event->getName(), ['_' => ''])),
@@ -182,29 +197,9 @@ public function testPrebuildEvents()
182197
$this->assertTrue(is_array($event->toArray()), $eventName);
183198

184199
$this->analytics->addEvent($event);
185-
$eventCount += 1;
186-
} catch (throwable $t) {
187-
$this->assertTrue(false, $t->getFile() . ':' . $t->getLine() . ' > ' . $t->getMessage());
188200
}
189201

190-
if ($eventCount >= 25) {
191-
try {
192-
$this->assertTrue($this->analytics->post());
193-
} catch (throwable $t) {
194-
$this->assertTrue(false, $t->getFile() . ':' . $t->getLine() . ' > ' . $t->getMessage());
195-
} finally {
196-
$eventCount = 1;
197-
$this->prepareSituation();
198-
}
199-
}
200-
}
201-
202-
if ($eventCount > 0) {
203-
try {
204-
$this->assertTrue($this->analytics->post());
205-
} catch (throwable $t) {
206-
$this->assertTrue(false, $t->getFile() . ':' . $t->getLine() . ' > ' . $t->getMessage());
207-
}
202+
$this->assertTrue($this->analytics->post());
208203
}
209204
}
210205
}

0 commit comments

Comments
 (0)