|
392 | 392 | expect(preg_match($regex, ' '))->toBe(0); // Only one invisible character
|
393 | 393 | expect(preg_match($regex, ' '))->toBe(0); // More than 2 invisible characters
|
394 | 394 | });
|
| 395 | + |
| 396 | + |
| 397 | +test('group method creates capturing groups correctly', function () { |
| 398 | + $builder = new BuilderPattern(); |
| 399 | + $builder->group(function($group) { |
| 400 | + $group->digits(3); // Matches exactly 3 digits |
| 401 | + })->character("-")->digits(2); // Followed by a hyphen and 2 digits |
| 402 | + |
| 403 | + $regex = $builder->getMatchesValidationPattern(); |
| 404 | + expect(preg_match($regex, '123-45'))->toBe(1); |
| 405 | + expect(preg_match($regex, '12345'))->toBe(0); // No hyphen |
| 406 | + expect(preg_match($regex, '12-34'))->toBe(0); // First group doesn't have 3 digits |
| 407 | +}); |
| 408 | + |
| 409 | +test('nonCapturingGroup method creates non-capturing groups correctly', function () { |
| 410 | + $builder = new BuilderPattern(); |
| 411 | + $builder->nonCapturingGroup(function($group) { |
| 412 | + $group->digits(3); // Matches exactly 3 digits |
| 413 | + })->character("-")->digits(2); // Followed by a hyphen and 2 digits |
| 414 | + |
| 415 | + $regex = $builder->getMatchesValidationPattern(); |
| 416 | + expect(preg_match($regex, '123-45'))->toBe(1); |
| 417 | + expect(preg_match($regex, '12345'))->toBe(0); // No hyphen |
| 418 | + expect(preg_match($regex, '12-34'))->toBe(0); // First group doesn't have 3 digits |
| 419 | +}); |
| 420 | + |
| 421 | +test('capturing group captures content correctly', function () { |
| 422 | + $builder = new BuilderPattern(); |
| 423 | + $builder->group(function($group) { |
| 424 | + $group->exact('abc'); |
| 425 | + }); |
| 426 | + |
| 427 | + $regex = $builder->getMatchesValidationPattern(); |
| 428 | + $matches = []; |
| 429 | + preg_match($regex, 'abcdef', $matches); |
| 430 | + expect($matches[1])->toBe('abc'); // Check the captured content |
| 431 | +}); |
| 432 | + |
| 433 | +test('non-capturing group does not capture content', function () { |
| 434 | + $builder = new BuilderPattern(); |
| 435 | + $builder->nonCapturingGroup(function($group) { |
| 436 | + $group->exact('abc'); |
| 437 | + }); |
| 438 | + |
| 439 | + $regex = $builder->getInputValidationPattern(); |
| 440 | + $matches = []; |
| 441 | + preg_match($regex, 'abcdef', $matches); |
| 442 | + expect(isset($matches[1]))->toBeFalse(); // No captured content |
| 443 | +}); |
0 commit comments