11<?php
22
33use AlexWestergaard \PhpGa4 \Analytics ;
4+ use AlexWestergaard \PhpGa4 \GA4Exception ;
45use AlexWestergaard \PhpGa4 \Item ;
56use 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