Skip to content

Commit bd5bf95

Browse files
committed
validateRegexPattern
1 parent 9ad7491 commit bd5bf95

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

src/Messaging/Request/CreateBounceRegexRequest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use PhpList\RestBundle\Common\Request\RequestInterface;
88
use Symfony\Component\Validator\Constraints as Assert;
9+
use Symfony\Component\Validator\Context\ExecutionContextInterface;
910

1011
class CreateBounceRegexRequest implements RequestInterface
1112
{
@@ -39,4 +40,24 @@ public function getDto(): array
3940
'status' => $this->status,
4041
];
4142
}
43+
44+
#[Assert\Callback('validateRegexPattern')]
45+
public function validateRegexPattern(ExecutionContextInterface $context): void
46+
{
47+
if (!isset($this->regex)) {
48+
return;
49+
}
50+
set_error_handler(static function () {
51+
return true;
52+
});
53+
// phpcs:ignore Generic.PHP.NoSilencedErrors
54+
$allGood = @preg_match($this->regex, '');
55+
restore_error_handler();
56+
57+
if ($allGood === false) {
58+
$context->buildViolation('Invalid regular expression pattern.')
59+
->atPath('regex')
60+
->addViolation();
61+
}
62+
}
4263
}

tests/Unit/Messaging/Request/CreateBounceRegexRequestTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
use PhpList\RestBundle\Messaging\Request\CreateBounceRegexRequest;
88
use PHPUnit\Framework\TestCase;
9+
use Symfony\Component\Validator\Context\ExecutionContextInterface;
10+
use Symfony\Component\Validator\Violation\ConstraintViolationBuilderInterface;
911

1012
class CreateBounceRegexRequestTest extends TestCase
1113
{
@@ -43,4 +45,36 @@ public function testGetDtoWithDefaults(): void
4345
$this->assertNull($dto['comment']);
4446
$this->assertNull($dto['status']);
4547
}
48+
49+
public function testValidateRegexPatternWithValidRegexDoesNotAddViolation(): void
50+
{
51+
$req = new CreateBounceRegexRequest();
52+
$req->regex = '/valid.*/i';
53+
54+
$context = $this->createMock(ExecutionContextInterface::class);
55+
$context->expects($this->never())->method('buildViolation');
56+
57+
$req->validateRegexPattern($context);
58+
59+
// if no exception and no violation calls, the test passes
60+
$this->assertTrue(true);
61+
}
62+
63+
public function testValidateRegexPatternWithInvalidRegexAddsViolation(): void
64+
{
65+
$req = new CreateBounceRegexRequest();
66+
$req->regex = '/[invalid';
67+
68+
$builder = $this->createMock(ConstraintViolationBuilderInterface::class);
69+
$builder->expects($this->once())->method('atPath')->with('regex')->willReturnSelf();
70+
$builder->expects($this->once())->method('addViolation');
71+
72+
$context = $this->createMock(ExecutionContextInterface::class);
73+
$context->expects($this->once())
74+
->method('buildViolation')
75+
->with('Invalid regular expression pattern.')
76+
->willReturn($builder);
77+
78+
$req->validateRegexPattern($context);
79+
}
4680
}

0 commit comments

Comments
 (0)