11<?php
22
3- use AlexWestergaard \PhpGa4 \Analytics ;
4- use AlexWestergaard \PhpGa4 \Event \Refund ;
53use AlexWestergaard \PhpGa4 \Item ;
6- use AlexWestergaard \PhpGa4 \UserProperty ;
4+ use AlexWestergaard \PhpGa4 \Event ;
5+ use AlexWestergaard \PhpGa4 \Analytics ;
76use AlexWestergaard \PhpGa4 \GA4Exception ;
7+ use AlexWestergaard \PhpGa4 \UserProperty ;
88
99class AnalyticsTest extends \PHPUnit \Framework \TestCase
1010{
@@ -83,6 +83,29 @@ public function testItem()
8383 $ this ->assertArrayHasKey ('quantity ' , $ arr );
8484 }
8585
86+ public function testItemFromArray ()
87+ {
88+ $ this ->prepareSituation ();
89+
90+ $ item = Item::fromArray ([
91+ 'item_id ' => '2 ' ,
92+ 'item_name ' => 'Second Product ' ,
93+ 'currency ' => $ this ->prefill ['currency ' ],
94+ 'price ' => 9.99 ,
95+ 'quantity ' => 4 ,
96+ ]);
97+
98+ $ this ->assertInstanceOf (Item::class, $ item );
99+
100+ $ arr = $ item ->toArray ();
101+ $ this ->assertTrue (is_array ($ arr ));
102+ $ this ->assertArrayHasKey ('item_id ' , $ arr );
103+ $ this ->assertArrayHasKey ('item_name ' , $ arr );
104+ $ this ->assertArrayHasKey ('currency ' , $ arr );
105+ $ this ->assertArrayHasKey ('price ' , $ arr );
106+ $ this ->assertArrayHasKey ('quantity ' , $ arr );
107+ }
108+
86109 public function testUserProperty ()
87110 {
88111 $ this ->prepareSituation ();
@@ -109,7 +132,7 @@ public function testFullRefundNoItems()
109132 {
110133 $ this ->prepareSituation ();
111134
112- $ refund = Refund::new ()->setTransactionId (1 )->isFullRefund (true );
135+ $ refund = Event \ Refund::new ()->setTransactionId (1 )->isFullRefund (true );
113136
114137 $ this ->analytics ->addEvent ($ refund );
115138
@@ -120,7 +143,7 @@ public function testPartialRefundWithItems()
120143 {
121144 $ this ->prepareSituation ();
122145
123- $ refund = Refund::new ()->setTransactionId (1 )->addItem ($ this ->item );
146+ $ refund = Event \ Refund::new ()->setTransactionId (1 )->addItem ($ this ->item );
124147
125148 $ this ->analytics ->addEvent ($ refund );
126149
@@ -137,7 +160,7 @@ public function testPartialRefundNoItemsThrows()
137160 {
138161 $ this ->prepareSituation ();
139162
140- $ refund = Refund::new ()->setTransactionId (1 );
163+ $ refund = Event \ Refund::new ()->setTransactionId (1 );
141164
142165 $ this ->expectException (GA4Exception::class);
143166
@@ -243,4 +266,20 @@ public function testPrebuildEvents()
243266
244267 $ this ->assertTrue ($ this ->analytics ->post ());
245268 }
269+
270+ public function testBuildEventFromArray ()
271+ {
272+ $ this ->prepareSituation ();
273+
274+ $ event = Event \AddToCart::fromArray ([
275+ 'currency ' => $ this ->prefill ['currency ' ],
276+ 'value ' => rand (1000 , 10000 ) / 100 ,
277+ 'items ' => [$ this ->item ],
278+ ]);
279+
280+ $ this ->assertTrue (is_array ($ event ->toArray ()), get_class ($ event ));
281+
282+ $ this ->analytics ->addEvent ($ event );
283+ $ this ->assertTrue ($ this ->analytics ->post ());
284+ }
246285}
0 commit comments