File tree Expand file tree Collapse file tree 2 files changed +37
-1
lines changed Expand file tree Collapse file tree 2 files changed +37
-1
lines changed Original file line number Diff line number Diff line change @@ -73,7 +73,7 @@ public function toArray(): array
7373 throw Ga4EventException::throwNameMissing ();
7474 } elseif (strlen ($ name ) > 40 ) {
7575 throw Ga4EventException::throwNameTooLong ();
76- } elseif (preg_match ('/[^\w\d\-]/ ' , $ name )) {
76+ } elseif (preg_match ('/[^\w\d\-]|^\-|\-$ / ' , $ name )) {
7777 throw Ga4EventException::throwNameInvalid ();
7878 } elseif (in_array ($ name , EventType::RESERVED_NAMES ) && !($ this instanceof GtmEventType)) {
7979 throw Ga4EventException::throwNameReserved ($ name );
Original file line number Diff line number Diff line change @@ -679,6 +679,42 @@ public function getName(): string
679679 $ class ->toArray ();
680680 }
681681
682+ public function test_throw_name_invalid_starting_line ()
683+ {
684+ $ mock = new class extends Event \Refund
685+ {
686+ public function getName (): string
687+ {
688+ return '-almost-valid-name ' ;
689+ }
690+ };
691+
692+ $ class = $ mock ::new ()->setTransactionId (1 );
693+
694+ $ this ->expectException (Ga4EventException::class);
695+ $ this ->expectExceptionCode (Ga4Exception::EVENT_NAME_INVALID );
696+
697+ $ class ->toArray ();
698+ }
699+
700+ public function test_throw_name_invalid_ending_line ()
701+ {
702+ $ mock = new class extends Event \Refund
703+ {
704+ public function getName (): string
705+ {
706+ return 'almost-valid-name- ' ;
707+ }
708+ };
709+
710+ $ class = $ mock ::new ()->setTransactionId (1 );
711+
712+ $ this ->expectException (Ga4EventException::class);
713+ $ this ->expectExceptionCode (Ga4Exception::EVENT_NAME_INVALID );
714+
715+ $ class ->toArray ();
716+ }
717+
682718 public function test_throw_name_reserved ()
683719 {
684720 $ mock = new class extends Event \Refund
You can’t perform that action at this time.
0 commit comments