33use AlexWestergaard \PhpGa4 \Analytics ;
44use AlexWestergaard \PhpGa4 \Item ;
55use AlexWestergaard \PhpGa4 \UserProperty ;
6+ use AlexWestergaard \PhpGa4 \GA4Exception ;
67
78class 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