|
6 | 6 |
|
7 | 7 | use PhpList\RestBundle\Messaging\Request\CreateBounceRegexRequest; |
8 | 8 | use PHPUnit\Framework\TestCase; |
| 9 | +use Symfony\Component\Validator\Context\ExecutionContextInterface; |
| 10 | +use Symfony\Component\Validator\Violation\ConstraintViolationBuilderInterface; |
9 | 11 |
|
10 | 12 | class CreateBounceRegexRequestTest extends TestCase |
11 | 13 | { |
@@ -43,4 +45,36 @@ public function testGetDtoWithDefaults(): void |
43 | 45 | $this->assertNull($dto['comment']); |
44 | 46 | $this->assertNull($dto['status']); |
45 | 47 | } |
| 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 | + } |
46 | 80 | } |
0 commit comments