|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace ZipCodeValidator\Tests\Constraints; |
| 4 | + |
| 5 | +use PHPUnit\Framework\MockObject\MockObject; |
| 6 | +use PHPUnit\Framework\TestCase; |
| 7 | +use Symfony\Component\Validator\Context\ExecutionContext; |
| 8 | +use Symfony\Component\Validator\Violation\ConstraintViolationBuilderInterface; |
| 9 | +use ZipCodeValidator\Constraints\ZipCode; |
| 10 | +use ZipCodeValidator\Constraints\ZipCodeValidator; |
| 11 | + |
| 12 | +class MdZipCodeValidatorTest extends TestCase |
| 13 | +{ |
| 14 | + protected ZipCodeValidator $validator; |
| 15 | + |
| 16 | + public function setUp(): void |
| 17 | + { |
| 18 | + $this->validator = new ZipCodeValidator; |
| 19 | + } |
| 20 | + |
| 21 | + /** |
| 22 | + * @dataProvider getValidMoldovaZipCodes |
| 23 | + */ |
| 24 | + public function testValidZipcodes(string $zipCode): void |
| 25 | + { |
| 26 | + $constraint = new ZipCode('MD'); |
| 27 | + |
| 28 | + /** @var ExecutionContext|MockObject $contextMock */ |
| 29 | + $contextMock = $this->getMockBuilder(ExecutionContext::class) |
| 30 | + ->disableOriginalConstructor() |
| 31 | + ->getMock(); |
| 32 | + |
| 33 | + # be sure that buildViolation never gets called |
| 34 | + $contextMock->expects($this->never())->method('buildViolation'); |
| 35 | + $contextMock->setConstraint($constraint); |
| 36 | + |
| 37 | + $this->validator->initialize($contextMock); |
| 38 | + $this->validator->validate($zipCode, $constraint); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Valid Moldova postal codes are four-digit numbers, optionally prefixed with "MD-". |
| 43 | + * @see https://en.wikipedia.org/wiki/Postal_codes_in_Moldova |
| 44 | + */ |
| 45 | + public static function getValidMoldovaZipCodes(): array |
| 46 | + { |
| 47 | + return [ |
| 48 | + ['1234'], |
| 49 | + ['0123'], |
| 50 | + ['9876'], |
| 51 | + ['MD-1234'], |
| 52 | + ['MD-0123'], |
| 53 | + ['MD-9876'], |
| 54 | + ]; |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * @dataProvider getInvalidMoldovaZipCodes |
| 59 | + */ |
| 60 | + public function testInvalidZipcodes(string $zipCode): void |
| 61 | + { |
| 62 | + $constraint = new ZipCode('MD'); |
| 63 | + |
| 64 | + $violation = $this->createMock(ConstraintViolationBuilderInterface::class); |
| 65 | + $violation->expects($this->once())->method('setParameter')->willReturnSelf(); |
| 66 | + |
| 67 | + /** @var ExecutionContext|MockObject $contextMock */ |
| 68 | + $contextMock = $this->getMockBuilder(ExecutionContext::class) |
| 69 | + ->disableOriginalConstructor() |
| 70 | + ->getMock(); |
| 71 | + |
| 72 | + # be sure that buildViolation never gets called |
| 73 | + $contextMock->expects($this->once())->method('buildViolation')->willReturn($violation); |
| 74 | + $contextMock->setConstraint($constraint); |
| 75 | + |
| 76 | + $this->validator->initialize($contextMock); |
| 77 | + $this->validator->validate($zipCode, $constraint); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Valid Moldova postal codes are four-digit numbers, optionally prefixed with "MD-". |
| 82 | + * @see https://en.wikipedia.org/wiki/Postal_codes_in_Moldova |
| 83 | + */ |
| 84 | + public static function getInvalidMoldovaZipCodes(): array |
| 85 | + { |
| 86 | + return [ |
| 87 | + ['123'], |
| 88 | + ['12345'], |
| 89 | + ['MD12345'], |
| 90 | + ['MO-12345'], |
| 91 | + ['MD-12345'], |
| 92 | + ]; |
| 93 | + } |
| 94 | +} |
0 commit comments