441
441
preg_match ($ regex , 'abcdef ' , $ matches );
442
442
expect (isset ($ matches [1 ]))->toBeFalse (); // No captured content
443
443
});
444
+
445
+ test ('orPattern method combines patterns with alternation correctly ' , function () {
446
+ $ builder = new BuilderPattern ();
447
+ $ builder ->exact ('apple ' )->orPattern (function ($ pattern ) {
448
+ $ pattern ->exact ('orange ' );
449
+ });
450
+
451
+ $ regex = $ builder ->getInputValidationPattern ();
452
+ expect (preg_match ($ regex , 'apple ' ))->toBe (1 );
453
+ expect (preg_match ($ regex , 'orange ' ))->toBe (1 );
454
+ expect (preg_match ($ regex , 'banana ' ))->toBe (0 ); // Not part of the alternation
455
+ });
456
+
457
+ test ('orPattern can be used multiple times for complex alternation ' , function () {
458
+ $ builder = new BuilderPattern ();
459
+ $ builder ->exact ('apple ' )->orPattern (function ($ pattern ) {
460
+ $ pattern ->exact ('orange ' );
461
+ })->orPattern (function ($ pattern ) {
462
+ $ pattern ->exact ('banana ' );
463
+ });
464
+
465
+ $ regex = $ builder ->getInputValidationPattern ();
466
+ expect (preg_match ($ regex , 'apple ' ))->toBe (1 );
467
+ expect (preg_match ($ regex , 'orange ' ))->toBe (1 );
468
+ expect (preg_match ($ regex , 'banana ' ))->toBe (1 );
469
+ expect (preg_match ($ regex , 'pear ' ))->toBe (0 ); // Not part of the alternation
470
+ });
471
+
472
+ test ('lookAhead method adds positive lookahead correctly ' , function () {
473
+ $ builder = new BuilderPattern ();
474
+ $ builder ->digits ()->lookAhead (function ($ pattern ) {
475
+ $ pattern ->character ('D ' );
476
+ });
477
+
478
+ $ regex = $ builder ->getMatchesValidationPattern ();
479
+ expect (preg_match ($ regex , '3D ' ))->toBe (1 ); // Digit followed by 'D'
480
+ expect (preg_match ($ regex , '3A ' ))->toBe (0 ); // Digit not followed by 'D'
481
+ });
482
+
483
+ test ('lookBehind method adds positive lookbehind correctly ' , function () {
484
+ $ builder = new BuilderPattern ();
485
+ $ builder ->lookBehind (function ($ pattern ) {
486
+ $ pattern ->character ('P ' );
487
+ })->digits ();
488
+
489
+ $ regex = $ builder ->getMatchesValidationPattern ();
490
+ expect (preg_match ($ regex , 'P3 ' ))->toBe (1 ); // Digit preceded by 'P', in case of get() ot will return only digit
491
+ expect (preg_match ($ regex , 'A3 ' ))->toBe (0 ); // Digit not preceded by 'P'
492
+ });
493
+
494
+ test ('negativeLookAhead method adds negative lookahead correctly ' , function () {
495
+ $ builder = new BuilderPattern ();
496
+ $ builder ->digits ()->negativeLookAhead (function ($ pattern ) {
497
+ $ pattern ->character ('- ' );
498
+ });
499
+
500
+ $ regex = $ builder ->getMatchesValidationPattern ();
501
+ expect (preg_match ($ regex , '3A ' ))->toBe (1 ); // Digit not followed by '-'
502
+ expect (preg_match ($ regex , '3- ' ))->toBe (0 ); // Digit followed by '-'
503
+ });
504
+
505
+ test ('negativeLookBehind method adds negative lookbehind correctly ' , function () {
506
+ $ builder = new BuilderPattern ();
507
+ $ builder ->negativeLookBehind (function ($ pattern ) {
508
+ $ pattern ->character ('- ' );
509
+ })->digits ();
510
+
511
+ $ regex = $ builder ->getMatchesValidationPattern ();
512
+ expect (preg_match ($ regex , 'A3 ' ))->toBe (1 ); // Digit not preceded by '-'
513
+ expect (preg_match ($ regex , '-3 ' ))->toBe (0 ); // Digit preceded by '-'
514
+ });
515
+
516
+ test ('addRawRegex method adds raw regex patterns correctly ' , function () {
517
+ $ builder = new BuilderPattern ();
518
+ $ builder ->addRawRegex ('\d{3}-\d{2}-\d{4} ' ); // SSN pattern
519
+
520
+ $ regex = $ builder ->getInputValidationPattern ();
521
+ expect (preg_match ($ regex , '123-45-6789 ' ))->toBe (1 );
522
+ expect (preg_match ($ regex , '123456789 ' ))->toBe (0 );
523
+ });
524
+
525
+ test ('addRawNonCapturingGroup method adds and wraps raw regex in a non-capturing group ' , function () {
526
+ $ builder = new BuilderPattern ();
527
+ $ builder ->addRawNonCapturingGroup ('\d+ ' )->exact ('A ' );
528
+
529
+ $ regex = $ builder ->getInputValidationPattern ();
530
+ expect (preg_match ($ regex , '123A ' ))->toBe (1 );
531
+ expect (preg_match ($ regex , 'A123 ' ))->toBe (0 );
532
+ });
0 commit comments